WHy is this program not responding

Hi I am changing all my examples from the book programming linux games from sdl1 to sdl2

some of the programs work fine when the functions are updated but this one compiles fine but does nothing, can anyone help me get it running? thanks.

compiled on gcc with -lSDL2

#include<SDL2/SDL.h>
#include<stdio.h>
#include<stdlib.h>

int main(int argn, char **argv)
{
SDL_Event event;

/*Initialize SDL's video system and check for errors. */
if (SDL_Init(SDL_INIT_VIDEO) !=0) {
printf("Unable to initialize SDL: %s\n", SDL_GetError());
return 1;
}

/*Make sure SDL_Quit gets called when the program exits" */
atexit(SDL_Quit);

/* Attempt to set a 256x256 hicolour (16-bit) video mode */
SDL_Window * window = SDL_CreateWindow("MouseMonitoring",0,0,256,256,SDL_WINDOW_RESIZABLE);
if (window == NULL){printf("Could not create Window! %s\n", SDL_GetError());

	/* Start the event loop. Keep reading events untill there
	is an error, or the user presses a mouse button. */
	while(SDL_WaitEvent(&event) !=0) 
	{

		/* SDL_WaitEvent has filled in our event structure
		with the next event. We check its type field to
		find out what happened. */
		switch(event.type) 
		{

		/* The next two event types deal
		with mouse activity. */
		case SDL_MOUSEMOTION:
		printf("Mouse motion. ");

		/* SDL provides the current position. */
		printf("New position is (%i,%i). ",
		event.motion.x, event.motion.y);

		/*We can also get relative motion */
		printf("That is a (%i,%i) change.\n",
		event.motion.xrel, event.motion.yrel);
		break;

		case SDL_MOUSEBUTTONDOWN:
		printf("Mouse button pressed. ");

		printf("Button %i at (%i,%i)\n",
		event.button.button,
		event.button.x, event.button.y);
		break;

		/* The SDL_Quit event indicates that
		the window "Close" button has been
		pressed. We can ignore this if we
		need to, but that tends to make
		users rather impatient, */

		case SDL_QUIT:
		printf("Quit event. Bye,\n");
		exit(0);
		}
	}

return 0;
}

}

Simple. Very Simple.
What’s happening is on this line:

You forgot to close the if condition. Therefore, the rest of the codes only executes if(window == NULL).

Just replace it with:
if (window == NULL){printf(“Could not create Window! %s\n”, SDL_GetError());}
So you have to remove the last “}”, too, as it’s no longer gonna make sense to have it.

And also, you cannot create a 16-bit window in SDL2 as far as I know. Your window is probably gonna have 24 or 32 bpp.

Finally, you have created a window at position (0,0). You might prefer having a window centered, which you can do with the macro SDL_WINDOWPOS_CENTERED.

So yeah. Here is the final, corrected code:

#include<SDL2/SDL.h>
#include<stdio.h>
#include<stdlib.h>

int main(int argn, char **argv)
{
SDL_Event event;

/*Initialize SDL’s video system and check for errors. */
if (SDL_Init(SDL_INIT_VIDEO) !=0) {
printf(“Unable to initialize SDL: %s\n”, SDL_GetError());
return 1;
}

/*Make sure SDL_Quit gets called when the program exits" */
atexit(SDL_Quit);

/* Attempt to set a 256x256 truecolor (24-bit or 32-bit) video mode */
SDL_Window * window =
SDL_CreateWindow(“MouseMonitoring”,SDL_WINDOWPOS_CENTERED,SDL_WINDOWPOS_CENTERED,256,256,SDL_WINDOW_RESIZABLE);
if (window == NULL){printf(“Could not create Window! %s\n”, SDL_GetError());}

/* Start the event loop. Keep reading events untill there
is an error, or the user presses a mouse button. */
while(SDL_WaitEvent(&event) !=0)
{

	/* SDL_WaitEvent has filled in our event structure
	with the next event. We check its type field to
	find out what happened. */
	switch(event.type)
	{

	/* The next two event types deal
	with mouse activity. */
	case SDL_MOUSEMOTION:
	printf("Mouse motion. ");

	/* SDL provides the current position. */
	printf("New position is (%i,%i). ",
	event.motion.x, event.motion.y);

	/*We can also get relative motion */
	printf("That is a (%i,%i) change.\n",
	event.motion.xrel, event.motion.yrel);
	break;

	case SDL_MOUSEBUTTONDOWN:
	printf("Mouse button pressed. ");

	printf("Button %i at (%i,%i)\n",
	event.button.button,
	event.button.x, event.button.y);
	break;

	/* The SDL_Quit event indicates that
	the window "Close" button has been
	pressed. We can ignore this if we
	need to, but that tends to make
	users rather impatient, */

	case SDL_QUIT:
	printf("Quit event. Bye,\n");
	exit(0);
	}
}

return 0;
}

Cant believe I missed that thanks :slight_smile: