Problem with fill up the event queue

Hello i’m Daniel,and I’m starting to learn the SDL API but im stuck for
about 2 days.

I’m trying to make a simple game using sprites in C++ I have a main.cpp and
a class named “jugador.cpp” (or player.cpp) to handle the properties of the
player. My main looks like this:

int main ( int argc, char *argv[] )
{
SDL_Surface *screen;
SDL_Event evento;
int colorkey, gameover;
gameover = 0;

/* initialize SDL */
SDL_Init(SDL_INIT_VIDEO);

/* set the title bar */
SDL_WM_SetCaption("SDL Sprite", "SDL Sprite");

/* create window */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
colorkey = SDL_MapRGB(screen->format, 255, 0, 255);

//Create  JUGADOR CLASS
jugador jug(colorkey);

/* message pump */
while (!gameover)
{
    /* look for an event */
    if (SDL_WaitEvent(&evento))
    {
      if(evento.type == SDL_QUIT || evento.key.keysym.sym== SDLK_ESCAPE)

gameover=1;
}

 /*pass the event to jugador class to process the movement*/
    jug.rebre_moviment(&evento);

    /* draw the jugador */
    jug.dibuixar(screen);

    /* update the screen */
    SDL_UpdateRect(screen, 0, 0, 0, 0);
    SDL_Delay(20);
}

SDL_Quit();

return 0;
}

So if a event is polled i pass the EVENT to the “jug” object using
jug.rebremoviment(&evento) and it process the movement, then i paint the
player in the new position using jug.dibuixar(screen). All works very well!
BUT!!! when I move the MOUSE at the same time im pressing the WASD
direction keys to move my player and i do some crazy movement with my mouse
it seems that it fills de event queue and when I stop moving the mouse and
pressing the keys the player DONT stop! it continues moving doing the rest
events in the event queue for about a second or two…

Well im new to the game programming world and i need help on that i cant
figure myself how to not fill up the queue, and make the movement fast and
smooth.

Thank you for your help and sorry for my poor english.
If you need some code of the “jugador” class just tell me, i did not include
it because the message will be very large :).

Hello i’m Daniel,and I’m starting to learn the SDL API but im stuck for
about 2 days.

I’m trying to make a simple game using sprites in C++ I have a main.cpp and
a class named “jugador.cpp” (or player.cpp) to handle the properties of the
player. My main looks like this:

int main ( int argc, char *argv[] )
{
SDL_Surface *screen;
SDL_Event evento;
int colorkey, gameover;
gameover = 0;

/* initialize SDL */
SDL_Init(SDL_INIT_VIDEO);

/* set the title bar */
SDL_WM_SetCaption("SDL Sprite", "SDL Sprite");

/* create window */
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
colorkey = SDL_MapRGB(screen->format, 255, 0, 255);

//Create  JUGADOR CLASS
jugador jug(colorkey);

/* message pump */
while (!gameover)
{
    /* look for an event */
    if (SDL_WaitEvent(&evento))
    {
      if(evento.type == SDL_QUIT || evento.key.keysym.sym== SDLK_ESCAPE)

gameover=1;
}

 /*pass the event to jugador class to process the movement*/
    jug.rebre_moviment(&evento);

    /* draw the jugador */
    jug.dibuixar(screen);

    /* update the screen */
    SDL_UpdateRect(screen, 0, 0, 0, 0);
    SDL_Delay(20);
}

SDL_Quit();

return 0;
}

So if a event is polled i pass the EVENT to the “jug” object using
jug.rebremoviment(&evento) and it process the movement, then i paint the
player in the new position using jug.dibuixar(screen). All works very well!
BUT!!! when I move the MOUSE at the same time im pressing the WASD
direction keys to move my player and i do some crazy movement with my mouse
it seems that it fills de event queue and when I stop moving the mouse and
pressing the keys the player DONT stop! it continues moving doing the rest
events in the event queue for about a second or two…

Well im new to the game programming world and i need help on that i cant
figure myself how to not fill up the queue, and make the movement fast and
smooth.

Thank you for your help and sorry for my poor english.
If you need some code of the “jugador” class just tell me, i did not include
it because the message will be very large :).

You should try using SDL_PollEvent()…
while (!gameover)
{
/* look for an event */
while (SDL_PollEvent(&evento))
{
if(evento.type == SDL_QUIT || evento.key.keysym.sym== SDLK_ESCAPE)
{
gameover=1;
break;
}

    /*pass the event to jugador class to process the movement*/
    jug.rebre_moviment(&evento);
}

… // The rest of the stuff

}

Jonny DOn Thu, Sep 24, 2009 at 12:42 PM, Daniel Guzm?n <daniel.guzman85 at gmail.com> wrote:

Hello i’m Daniel,and I’m starting to learn the SDL API but im stuck for
about 2 days.

I’m trying to make a simple game using sprites in C++ I have a main.cpp and
a class named “jugador.cpp” (or player.cpp) to handle the properties of the
player. My main looks like this:

int main ( int argc, char *argv[] )
{
??? SDL_Surface *screen;
??? SDL_Event evento;
??? int colorkey, gameover;
??? gameover = 0;

??? /* initialize SDL */
??? SDL_Init(SDL_INIT_VIDEO);

??? /* set the title bar */
??? SDL_WM_SetCaption(“SDL Sprite”, “SDL Sprite”);

??? /* create window */
??? screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
??? colorkey = SDL_MapRGB(screen->format, 255, 0, 255);

??? //Create? JUGADOR CLASS
??? jugador jug(colorkey);

??? /* message pump /
??? while (!gameover)
??? {
??? ??? /
look for an event */
??? ??? if (SDL_WaitEvent(&evento))
??? ??? {
??? ??? ? if(evento.type == SDL_QUIT || evento.key.keysym.sym== SDLK_ESCAPE)
gameover=1;
??? ??? }

??? /pass the event to jugador class to process the movement/
??? ??? jug.rebre_moviment(&evento);

??? ??? /* draw the jugador */
??? ??? jug.dibuixar(screen);

??? ??? /* update the screen */
??? ??? SDL_UpdateRect(screen, 0, 0, 0, 0);
? ? ??? SDL_Delay(20);
??? }

? SDL_Quit();

? return 0;
}

So if a event is polled i pass the EVENT to the “jug” object using
jug.rebremoviment(&evento) and it process the movement, then i paint the
player in the new position using jug.dibuixar(screen). All works very well!
BUT!!! when I move the MOUSE? at the same time im pressing the WASD
direction keys to move my player and i do some crazy movement with my mouse
it seems that it fills de event queue and when I stop moving the mouse and
pressing the keys the player DONT stop! it continues moving doing the rest
events in the event queue for about a second or two…

Well im new to the game programming world and i need help on that i cant
figure myself how to not fill up the queue, and make the movement fast and
smooth.

Thank you for your help and sorry for my poor english.
If you need some code of the “jugador” class just tell me, i did not include
it because the message will be very large :).


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Oh thank you Jonny D that was a very noob thing… >_< now it works perfect!

Final code:

while (!gameover)
{
/* look for an event */
while(SDL_PollEvent(&evento) && !gameover)
{
if(evento.type == SDL_QUIT || evento.key.keysym.sym==
SDLK_ESCAPE) gameover=true;

		jug.rebre_moviment(&evento);
	
		
	}	
		
		background.pinta(screen);
		/* draw the jugador */
		jug.dibuixar(screen);
		/* update the screen */
		SDL_UpdateRect(screen, 0, 0, 0, 0);
		// baixem la velocitat del joc.
		SDL_Delay(20);
}

Thanks!

You should try using SDL_PollEvent()…
while (!gameover)
{

/* look for an event */

while (SDL_PollEvent(&evento))
{
if(evento.type == SDL_QUIT || evento.key.keysym.sym== SDLK_ESCAPE)
{
gameover=1;
break;
}

   /*pass the event to jugador class to process the movement*/
   jug.rebre_moviment(&evento);

}

… // The rest of the stuff

}

Jonny D

  • Hello i’m Daniel,and I’m starting to learn the SDL API but im stuck for
    > about 2 days.
    >
    > I’m trying to make a simple game using sprites in C++ I have a main.cpp and
    > a class named “jugador.cpp” (or player.cpp) to handle the properties of the
    > player. My main looks like this:
    >
    > int main ( int argc, char *argv[] )
    > {
    > SDL_Surface screen;
    > SDL_Event evento;
    > int colorkey, gameover;
    > gameover = 0;
    >
    > /
    initialize SDL /
    > SDL_Init(SDL_INIT_VIDEO);
    >
    > /
    set the title bar /
    > SDL_WM_SetCaption(“SDL Sprite”, “SDL Sprite”);
    >
    > /
    create window /
    > screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
    > colorkey = SDL_MapRGB(screen->format, 255, 0, 255);
    >
    > //Create JUGADOR CLASS
    > jugador jug(colorkey);
    >
    > /
    message pump /
    > while (!gameover)
    > {
    > /
    look for an event /
    > if (SDL_WaitEvent(&evento))
    > {
    > if(evento.type == SDL_QUIT || evento.key.keysym.sym== SDLK_ESCAPE)
    > gameover=1;
    > }
    >
    > /pass the event to jugador class to process the movement/
    > jug.rebre_moviment(&evento);
    >
    > /
    draw the jugador /
    > jug.dibuixar(screen);
    >
    > /
    update the screen */
    > SDL_UpdateRect(screen, 0, 0, 0, 0);
    > SDL_Delay(20);
    > }
    >
    > SDL_Quit();
    >
    > return 0;
    > }
    >
    > So if a event is polled i pass the EVENT to the “jug” object using
    > jug.rebremoviment(&evento) and it process the movement, then i paint the
    > player in the new position using jug.dibuixar(screen). All works very well!
    > BUT!!! when I move the MOUSE at the same time im pressing the WASD
    > direction keys to move my player and i do some crazy movement with my mouse
    > it seems that it fills de event queue and when I stop moving the mouse and
    > pressing the keys the player DONT stop! it continues moving doing the rest
    > events in the event queue for about a second or two…
    >
    > Well im new to the game programming world and i need help on that i cant
    > figure myself how to not fill up the queue, and make the movement fast and
    > smooth.
    >
    > Thank you for your help and sorry for my poor english.
    > If you need some code of the “jugador” class just tell me, i did not include
    > it because the message will be very large :).
    >
    > _______________________________________________
    > SDL mailing list
    > SDL at lists.libsdl.org
    http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
    > http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org
    >
    >>On Thu, Sep 24, 2009 at 12:42 PM, Daniel Guzm?n <@Daniel_Guzman1 http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org> wrote:

No problem. That’s what we’re here for. :slight_smile:

Jonny DOn Thu, Sep 24, 2009 at 1:38 PM, Daniel Guzm?n <daniel.guzman85 at gmail.com> wrote:

Oh thank you Jonny D that was a very noob thing… >_< now it works perfect!

Final code:

while (!gameover)
{
/* look for an event */
while(SDL_PollEvent(&evento) && !gameover)

  {
  	if(evento.type == SDL_QUIT || evento.key.keysym.sym==

SDLK_ESCAPE) gameover=true;

  	jug.rebre_moviment(&evento);
  
  	
  }	
  	
  	background.pinta(screen);
  	/* draw the jugador */

  	jug.dibuixar(screen);
  	/* update the screen */
  	SDL_UpdateRect(screen, 0, 0, 0, 0);
  	// baixem la velocitat del joc.
  	SDL_Delay(20);

}

Thanks!

You should try using SDL_PollEvent()…

while (!gameover)
{

/* look for an event */

while (SDL_PollEvent(&evento))
{
if(evento.type == SDL_QUIT || evento.key.keysym.sym== SDLK_ESCAPE)
{

       gameover=1;
       break;
   }


   /*pass the event to jugador class to process the movement*/
   jug.rebre_moviment(&evento);

}

… // The rest of the stuff

}

Jonny D

On Thu, Sep 24, 2009 at 12:42 PM, Daniel Guzm?n <daniel.guzman85 at gmail.com> wrote:

Hello i’m Daniel,and I’m starting to learn the SDL API but im stuck for
about 2 days.

I’m trying to make a simple game using sprites in C++ I have a main.cpp
and

a class named “jugador.cpp” (or player.cpp) to handle the properties of
the
player. My main looks like this:

int main ( int argc, char *argv[] )
{

??? SDL_Surface *screen;
??? SDL_Event evento;
??? int colorkey, gameover;
??? gameover = 0;

??? /* initialize SDL */
??? SDL_Init(SDL_INIT_VIDEO);

??? /* set the title bar */
??? SDL_WM_SetCaption(“SDL Sprite”, “SDL Sprite”);

??? /* create window */
??? screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);

??? colorkey = SDL_MapRGB(screen->format, 255, 0, 255);

??? //Create? JUGADOR CLASS
??? jugador jug(colorkey);

??? /* message pump */

??? while (!gameover)
??? {
??? ??? /* look for an event */
??? ??? if (SDL_WaitEvent(&evento))
??? ??? {
??? ??? ? if(evento.type == SDL_QUIT || evento.key.keysym.sym==
SDLK_ESCAPE)

gameover=1;
??? ??? }

??? /pass the event to jugador class to process the movement/
??? ??? jug.rebre_moviment(&evento);

??? ??? /* draw the jugador */

??? ??? jug.dibuixar(screen);

??? ??? /* update the screen */
??? ??? SDL_UpdateRect(screen, 0, 0, 0, 0);
? ? ??? SDL_Delay(20);
??? }

? SDL_Quit();

? return 0;
}

So if a event is polled i pass the EVENT to the “jug” object using
jug.rebremoviment(&evento) and it process the movement, then i paint the

player in the new position using jug.dibuixar(screen). All works very
well!
BUT!!! when I move the MOUSE? at the same time im pressing the WASD
direction keys to move my player and i do some crazy movement with my
mouse

it seems that it fills de event queue and when I stop moving the mouse and
pressing the keys the player DONT stop! it continues moving doing the rest
events in the event queue for about a second or two…

Well im new to the game programming world and i need help on that i cant
figure myself how to not fill up the queue, and make the movement fast and
smooth.

Thank you for your help and sorry for my poor english.
If you need some code of the “jugador” class just tell me, i did not
include
it because the message will be very large :).


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org


SDL mailing list
SDL at lists.libsdl.org
http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

2009/9/24 Daniel Guzm?n <daniel.guzman85 at gmail.com>:

int main ( int argc, char *argv[] )
{
??? SDL_Surface *screen;
??? SDL_Event evento;
??? int colorkey, gameover;

you should use unsigned, Uint32 or uint32_t for colorkey and bool for gameover.

??? gameover = 0;

??? /* initialize SDL */
??? SDL_Init(SDL_INIT_VIDEO);

You need to check for errors here. It really does fail occasionally.

??? /* set the title bar */
??? SDL_WM_SetCaption(“SDL Sprite”, “SDL Sprite”);

There is no window yet, so you can’t set the title.
Move this below SDL_SetVideoMode().

??? /* create window */
??? screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 0, 0);
??? colorkey = SDL_MapRGB(screen->format, 255, 0, 255);

??? //Create? JUGADOR CLASS
??? jugador jug(colorkey);

??? /* message pump /
??? while (!gameover)
??? {
??? ??? /
look for an event */
??? ??? if (SDL_WaitEvent(&evento))

SDL_PollEvent(), not SDL_WaitEvent(). SDL_WaitEvent() waits until a
event occurs, then returns that event. PollEvent returns immediately
whether there is an event or not.

??? ??? {
??? ??? ? if(evento.type == SDL_QUIT || evento.key.keysym.sym== SDLK_ESCAPE)

SDL_Event is a union, not a structure. evento.key.keysym.sym could be
anything, even SDLK_ESCAPE, if the event type is not SDL_KEYDOWN or
SDL_KEYUP.

This should be
if (evento.type == SDL_QUIT || (evento.type == SDL_KEYDOWN &&
evento.key.keysym.sym== SDLK_ESCAPE))

gameover=1;
??? ??? }

??? /pass the event to jugador class to process the movement/
??? ??? jug.rebre_moviment(&evento);

??? ??? /* draw the jugador */
??? ??? jug.dibuixar(screen);

??? ??? /* update the screen */
??? ??? SDL_UpdateRect(screen, 0, 0, 0, 0);
? ? ??? SDL_Delay(20);
??? }

? SDL_Quit();

? return 0;
}

If you’re using C++ anyway, you should wrap SDL_Init() and SDL_Quit()
in a class like so:
class SDL {
public:
SDL() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
std::cout<<"Error initializing SDL: "<<SDL_GetError()<<std::endl;
exit(1);
// or use an exception…
}
}
~SDL() {
SDL_Quit();
}
};

then create an instance of that class in main() like this:
SDL sdl;

This make sure that SDL_Quit() is called when your program exits, no
matter how it exits.

Hello !

Move this below SDL_SetVideoMode().

Sam said in his talk that there are some WMs that only respect
Title and Icon if you set them before creating a window
or in SDL terms setting a video mode.

CU

2009/9/24 Torsten Giebl :

Move this below SDL_SetVideoMode().

Sam said in his talk that there are some WMs that only respect
Title and Icon if you set them before creating a window
or in SDL terms setting a video mode.

If that’s true then it’s a bug. I doubt you’d find many OSs where you
can’t change the window title after window creation.

Still, if you’re using SDL 1.3 you should really just do something like this:
SDL_WindowID window;
window = SDL_CreateWindow(“Title”, SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, 640, 480, SDL_WINDOW_SHOWN);
if (window == 0) {
printf(“Error creating window: %s”, SDL_GetError());
exit(1);
}

/* insert renderer stuff here… */

Then use SDL_SetWindowTitle() and SDL_SetWindowIcon() if you need to
change the title and icon later.