Return of SDL_SetVideoMode

Hi. I am having a problem with SDL that I can’t seem to fix. This
function runs fine and a window pops but BUT when I try to blit anything
to screen it segfaults. I used GDB too look at screen and after
SDL_SetVideoMode is ran screen looks like it hasn’t been initialized
which seems to me why SDL_Blit segfaults.

Here is my compile:
cc -O -g -d -D_REENTRANT -c main.c -o main.o
cc -O -g -d -D_REENTRANT -c console.c -o console.o
cc -O -g -d -D_REENTRANT -c graphics.c -o graphics.o
cc -O -g -d -D_REENTRANT -c game.c -o game.o
gcc -o gd main.o console.o graphics.o game.o -lpthread
-L/usr/local/lib -lSDL -ld l

gdb say it looks like so: (SDL_Surface *) 0x0 after SDL_SetVideoMode has
been called.

– cut here graphics.c
SDL_Surface *screen, *buffer;

void graphics_init() {
if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_AUDIO) < 0) {
printf(“sdl: sdl failed to initialize”);
exit(1);
}

/* 640x480, any depth(usually what the XServer is in) */
screen = SDL_SetVideoMode(640, 480, 0, SDL_FULLSCREEN);
if (screen = NULL) {
printf(“sdl: sdl failed to switch video mode”);
exit(1);
}

}
– cut again

This is a common C programming error.

This line sets screen to NULL:

if (screen = NULL) {

It should be:
if (screen == NULL) {

See ya!
-Sam Lantinga (slouken at devolution.com)–
Author of Simple DirectMedia Layer -
http://www.devolution.com/~slouken/SDL/

This is a common C programming error.

This line sets screen to NULL:

if (screen = NULL) {

It should be:
if (screen == NULL) {

D’oh. :slight_smile:

This is why I have nothing but high regards for my old Intro to C instructor.
He gave us a tip: when making a comparison to a constant, always put the
constant on the left. The compiler will squawk at you if you try

if( NULL = screen )On Wed, 11 Nov 1998, Sam Lantinga wrote:


K. Spoon <@K_Spoon>

“Ok, somebody break out a thorazine jab stick and cart this detached mental
omelette over to the belted coat crowd.” – Harold Stevens in c.o.l.m.

if (screen = NULL) {

The million dollar bug :wink:

if(screen == NULL) {
^^On Wed, Nov 11, 1998 at 03:29:57PM -0600, Mike Fletcher wrote:


– Michael Samuel

Sorry, but isn’t that unlogical?
I wouldn’t say, “Is big that house?”, either…

Yes, it is logical:

Is NULL the same as screen?
Is screen the same as NULL?

When checking to see if they’re the same, what does the computer care if it’s
counter-intuitive to put the constant first?On Sat, Nov 14, 1998 at 09:53:51PM +0100, Paulus Esterhazy wrote:


– Michael Samuel

Sorry, but isn’t that unlogical?
I wouldn’t say, “Is big that house?”, either…

Yes, it is logical:

From a mathematical point of view one would ask if the independant
variable matches a value, from a functional point of view one could
consider the NULL == to be a single function (NULL ==)(x). Neither
methods are illogical as == is symmetric.

njhOn Sun, 15 Nov 1998, Michael Samuel wrote:

On Sat, Nov 14, 1998 at 09:53:51PM +0100, Paulus Esterhazy wrote:

From a mathematical point of view one would ask if the independant
variable matches a value, from a functional point of view one could
consider the NULL == to be a single function (NULL ==)(x). Neither
methods are illogical as == is symmetric.

However, NULL == x is much harder to screw up, which I believe was the
whole point.

-ChuckOn Sun, 15 Nov 1998, Nathan Hurst wrote:

“kspoon” == K Spoon writes:

kspoon> This is why I have nothing but high regards for my old Intro
kspoon> to C instructor. He gave us a tip: when making a comparison
kspoon> to a constant, always put the constant on the left. The
kspoon> compiler will squawk at you if you try

kspoon> if( NULL = screen )

…or just use a compiler like gcc with warnings enabled:

penguin:~/tmp$ cat foo.c
void foo(int x)
{
if (x = 9)
;
}
penguin:~/tmp$ gcc -Wall -c foo.c
foo.c: In function `foo’:
foo.c:3: warning: suggest parentheses around assignment used as truth value

-Mat