Newbie SDL question (SDL_BlitSurface)

I’m trying to add some 2d effects to a GL program I’m working on,
however when anything failed to show up I wrote the following test
program, and I’m completely stumped as to why nothing shows up.--------------------------------------------------------------------
#include <SDL/SDL.h>
#include <SDL/SDL_video.h>
#include <SDL/SDL_image.h>

int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf(“Failed 1\n”);
return 0;
}

SDL_Surface* s = SDL_SetVideoMode(800, 600, 32, SDL_SWSURFACE);
if (!s) {
printf(“Failed 2\n”);
return 0;
}

SDL_Surface* i = IMG_Load(“console.png”);
if (!i) {
printf(“Failed 3\n”);
return 0;
}

SDL_Surface* img = SDL_DisplayFormat(i);
SDL_FreeSurface(i);

SDL_Rect sr = {0, 0, img->w, img->h};
SDL_Rect dr = {10, 10, 0, 0};

if (SDL_BlitSurface(s, &sr, img, &dr) < 0) {
printf(“Failed 4\n”);
return 0;
}

SDL_UpdateRects(s, 1, &dr);

while (1) {
SDL_Event e;
while (SDL_PollEvent(&e))
if (e.type == SDL_QUIT) {
SDL_FreeSurface(s);
SDL_FreeSurface(img);
SDL_Quit();
return 0;
}
}
}


None of the failed messages are printed… A window opens and sits there,
completely black… Can anyone tell me what I’m doing wrong?

Thanks in advance,
Jp Calderone

Ok… found the problem in the previous post, the surface order was wrong
in SDL_BlitSurface (oops). But now it’s behaving as my main program does,
which is crashing on the call to SDL_UpdateRects. Here is the new code:----------------------------------------------------------------------------
#include <SDL/SDL.h>
#include <SDL/SDL_video.h>
#include <SDL/SDL_image.h>

int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf(“Failed 1\n”);
return 0;
}

SDL_Surface* s = SDL_SetVideoMode(800, 600, 32, SDL_OPENGL);
if (!s) {
printf(“Failed 2\n”);
return 0;
}

SDL_Surface* img = IMG_Load(“console.png”);
if (!img) {
printf(“Failed 3\n”);
return 0;
}

SDL_Rect dr = {10, 10, img->w, img->h};
if (SDL_BlitSurface(img, NULL, s, &dr) < 0) {
printf(“Failed 4\n”);
return 0;
}
SDL_UpdateRects(s, 1, &dr); // Crash occurs here

while (1) {
SDL_Event e;
while (SDL_PollEvent(&e))
if (e.type == SDL_QUIT) {
SDL_FreeSurface(s);
SDL_FreeSurface(img);
SDL_Quit();
return 0;
}
}
}


Compiled with:
g++ main.cpp -o main sdl-config --cflags --libs -lSDL_image

Again, thanks for any help you can provide,
Jp Calderone

From Doc:
SDL_OPENGL Create an OpenGL rendering context. You should have
previously set OpenGL video attributes with SDL_GL_SetAttribute.
SDL_OPENGLBLIT Create an OpenGL rendering context, like above, but
allow normal blitting operations. The screen (2D) surface may have an alpha
channel, and SDL_UpdateRects must be used for updating changes to the screen
surface.

So you must use SDL_OPENGLBLIT

Murlock--------------------
Gloire ? mon saigneur Arioch
http://www.murlock.org

----- Original Message -----
From: kuran@ruinsmud.com (Jp Calderone)
To:
Sent: Tuesday, September 25, 2001 5:50 AM
Subject: Re: [SDL] Newbie SDL question (SDL_BlitSurface)

Ok… found the problem in the previous post, the surface order was wrong
in SDL_BlitSurface (oops). But now it’s behaving as my main program does,
which is crashing on the call to SDL_UpdateRects. Here is the new code:


#include <SDL/SDL.h>
#include <SDL/SDL_video.h>
#include <SDL/SDL_image.h>

int main() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf(“Failed 1\n”);
return 0;
}

SDL_Surface* s = SDL_SetVideoMode(800, 600, 32, SDL_OPENGL);
if (!s) {
printf(“Failed 2\n”);
return 0;
}

SDL_Surface* img = IMG_Load(“console.png”);
if (!img) {
printf(“Failed 3\n”);
return 0;
}

SDL_Rect dr = {10, 10, img->w, img->h};
if (SDL_BlitSurface(img, NULL, s, &dr) < 0) {
printf(“Failed 4\n”);
return 0;
}
SDL_UpdateRects(s, 1, &dr); // Crash occurs here

while (1) {
SDL_Event e;
while (SDL_PollEvent(&e))
if (e.type == SDL_QUIT) {
SDL_FreeSurface(s);
SDL_FreeSurface(img);
SDL_Quit();
return 0;
}
}
}


Compiled with:
g++ main.cpp -o main sdl-config --cflags --libs -lSDL_image

Again, thanks for any help you can provide,
Jp Calderone


SDL mailing list
SDL at libsdl.org
http://www.libsdl.org/mailman/listinfo/sdl

From Doc:
SDL_OPENGL Create an OpenGL rendering context. You should have
previously set OpenGL video attributes with SDL_GL_SetAttribute.
SDL_OPENGLBLIT Create an OpenGL rendering context, like above, but
allow normal blitting operations. The screen (2D) surface may have an alpha
channel, and SDL_UpdateRects must be used for updating changes to the screen
surface.

I must add a caveat:
The SDL_OPENGLBLIT code is very slow, if you can do what you want
to do with OpenGL textures directly, I highly recommend it.

BTW, one of the things I’m doing at Blizzard is writing an optimized 2D
UI that works in a 3D environment, so I know it’s possible. :slight_smile:
(No, it’s not public code, and no, I can’t say anything more about it.)

See ya,
-Sam Lantinga, Software Engineer, Blizzard Entertainment

And if someone wants an open-source (LGPL ?ed) Gui for OpenGL,
I?ll point at libUFO ;-)On Tuesday 25 September 2001 09:55, Sam Lantinga wrote:

BTW, one of the things I’m doing at Blizzard is writing an optimized 2D
UI that works in a 3D environment, so I know it’s possible. :slight_smile:
(No, it’s not public code, and no, I can’t say anything more about it.)


Johannes Schmidt

< http://libufo.sourceforge.net > Your widget set for OpenGL