SDL_Event in a function

I am trying to create a function to process an SDL Event. It looks like
this:

int MyPollEvent(SDL_Event event)
{
switch (event.type) {
//blah blah
}
}

I use it like this:

while (SDL_PollEvent(&event))
{
MyPollEvent(event);
}

Unfortunately this causes a crash. I have tried changing the prototype
to both
MyPollEvent(SDL_Event& event), and MyPollEvent(SDL_Event* event),
and adjusting the way ‘event’ is used so that it SHOULD work, but this also
crashes. Any suggestions?

-TomT64

try changing it to…

int MyPollEvent(SDL_Event &event)

there might be a struct/class copy constructor problem> ----- Original Message -----

From: tomt64@users.sf.net (TomT64)
To: "A list for developers using the SDL library. (includes SDL-announce)"

Sent: Wednesday, September 08, 2004 3:06 PM
Subject: [SDL] SDL_Event in a function

I am trying to create a function to process an SDL Event. It looks like
this:

int MyPollEvent(SDL_Event event)
{
switch (event.type) {
//blah blah
}
}

I use it like this:

while (SDL_PollEvent(&event))
{
MyPollEvent(event);
}

Unfortunately this causes a crash. I have tried changing the prototype
to both
MyPollEvent(SDL_Event& event), and MyPollEvent(SDL_Event* event),
and adjusting the way ‘event’ is used so that it SHOULD work, but this
also
crashes. Any suggestions?

-TomT64


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

try changing it to…
int MyPollEvent(SDL_Event &event)

TomT64 said he already tried that.On Sep 8, 2004, at 6:23 PM, Alan Wolfe wrote:

On Sep 8, 2004, at 6:06 PM, TomT64 wrote:

I have tried changing the prototype to both
MyPollEvent(SDL_Event& event),

Tom, nothing you have shown us would suggest a problem. You’ve probably
made some sort of mistake, if you want help, you should show us more.
If you trace your code and see where it crashes I would guess it would
probably become fairly obvious to you what the problem is.

When I leave it as is (no & or *) then the game crashes as soon as the
function is called.
However, if I make it a & or *, and make the proper adjustments, it
crashes when it gets
to the switch statement (attempting to access event.type or
event->type). I can’t get
more specific than that since I am unsure what possible reasons there
could be for
either problem.

-TomT64

Donny Viszneki wrote:>

On Sep 8, 2004, at 6:23 PM, Alan Wolfe wrote:

try changing it to…
int MyPollEvent(SDL_Event &event)

TomT64 said he already tried that.

On Sep 8, 2004, at 6:06 PM, TomT64 wrote:

I have tried changing the prototype to both
MyPollEvent(SDL_Event& event),

Tom, nothing you have shown us would suggest a problem. You’ve
probably made some sort of mistake, if you want help, you should show
us more. If you trace your code and see where it crashes I would guess
it would probably become fairly obvious to you what the problem is.


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

When I leave it as is (no & or *) then the game crashes as soon as the
function is called.
However, if I make it a & or *, and make the proper adjustments, it
crashes when it gets
to the switch statement (attempting to access event.type or
event->type). I can’t get
more specific than that since I am unsure what possible reasons there
could be for
either problem.

-TomT64

Hi,

Take a look at this, and notice the definition of the SDL_Event in Run(), and
how it gets passed around. How does this compare with your (as yet unseen)
code?

File: event-test.c
----------------------------------------------------------->8-------------
#include “stdlib.h”
#include “SDL.h”

static int MyHandleEvent(const SDL_Event *event)
{
switch (event->type) {
case SDL_QUIT :
return 0;
case SDL_KEYDOWN :
if (event->key.keysym.sym == SDLK_ESCAPE)
return 0;
break;
}
return 1;
}

static int Run()
{
SDL_Event event;

while(SDL_PollEvent(&event))
{
    if ( !MyHandleEvent(&event) ) return 0;
}
return 1;

}

#define SC_WIDTH 640
#define SC_HEIGHT 480
#define SC_BPP 16

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

if((SDL_Init(SDL_INIT_VIDEO) == -1))
{
    printf("Could not initialize SDL: %s.\n", SDL_GetError());
    exit(1);
}
atexit(SDL_Quit);

screen = SDL_SetVideoMode(SC_WIDTH, SC_HEIGHT, SC_BPP, SDL_SWSURFACE);
if (screen == NULL)
{
    printf("Couldn't set %dx%dx%d video mode: %s\n",
            SC_WIDTH, SC_HEIGHT, SC_BPP, SDL_GetError());
    exit(2);
}

while(Run()) SDL_Delay(1);

SDL_Quit();
return 0;

}
/* gcc -Wall event-test.c sdl-config --cflags sdl-config --libs */
---------8<---------------------------------------------------------------
hope that helps,

cheers,
John Popplewell.On Thursday 09 September 2004 07:00, TomT64 wrote:

Donny Viszneki wrote:

On Sep 8, 2004, at 6:23 PM, Alan Wolfe wrote:

try changing it to…
int MyPollEvent(SDL_Event &event)

TomT64 said he already tried that.

On Sep 8, 2004, at 6:06 PM, TomT64 wrote:

I have tried changing the prototype to both
MyPollEvent(SDL_Event& event),

Tom, nothing you have shown us would suggest a problem. You’ve
probably made some sort of mistake, if you want help, you should show
us more. If you trace your code and see where it crashes I would guess
it would probably become fairly obvious to you what the problem is.


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


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

Actually you all were right and I had it just fine to begin with. The
problem was that it
never got to the function except once, and the debugger skipped the
first try for some
reason. I didn’t initialize something else that needed to be displayed
in the loop correctly.
So case closed!

-TomT64

John Popplewell wrote:>On Thursday 09 September 2004 07:00, TomT64 wrote:

When I leave it as is (no & or *) then the game crashes as soon as the
function is called.
However, if I make it a & or *, and make the proper adjustments, it
crashes when it gets
to the switch statement (attempting to access event.type or
event->type). I can’t get
more specific than that since I am unsure what possible reasons there
could be for
either problem.

-TomT64

Hi,

Take a look at this, and notice the definition of the SDL_Event in Run(), and
how it gets passed around. How does this compare with your (as yet unseen)
code?

File: event-test.c
----------------------------------------------------------->8-------------
#include “stdlib.h”
#include “SDL.h”

static int MyHandleEvent(const SDL_Event *event)
{
switch (event->type) {
case SDL_QUIT :
return 0;
case SDL_KEYDOWN :
if (event->key.keysym.sym == SDLK_ESCAPE)
return 0;
break;
}
return 1;
}

static int Run()
{
SDL_Event event;

while(SDL_PollEvent(&event))
{
if ( !MyHandleEvent(&event) ) return 0;
}
return 1;
}

#define SC_WIDTH 640
#define SC_HEIGHT 480
#define SC_BPP 16

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

if((SDL_Init(SDL_INIT_VIDEO) == -1))
{
printf(“Could not initialize SDL: %s.\n”, SDL_GetError());
exit(1);
}
atexit(SDL_Quit);

screen = SDL_SetVideoMode(SC_WIDTH, SC_HEIGHT, SC_BPP, SDL_SWSURFACE);
if (screen == NULL)
{
printf(“Couldn’t set %dx%dx%d video mode: %s\n”,
SC_WIDTH, SC_HEIGHT, SC_BPP, SDL_GetError());
exit(2);
}

while(Run()) SDL_Delay(1);

SDL_Quit();
return 0;
}
/* gcc -Wall event-test.c sdl-config --cflags sdl-config --libs */
---------8<---------------------------------------------------------------
hope that helps,

cheers,
John Popplewell.

Donny Viszneki wrote:

On Sep 8, 2004, at 6:23 PM, Alan Wolfe wrote:

try changing it to…
int MyPollEvent(SDL_Event &event)

TomT64 said he already tried that.

On Sep 8, 2004, at 6:06 PM, TomT64 wrote:

I have tried changing the prototype to both
MyPollEvent(SDL_Event& event),

Tom, nothing you have shown us would suggest a problem. You’ve
probably made some sort of mistake, if you want help, you should show
us more. If you trace your code and see where it crashes I would guess
it would probably become fairly obvious to you what the problem is.


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


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


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