Jump to content
  • Advertisement
Sign in to follow this  
Lith

SDL event

This topic is 3179 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I dont know what's wrong with this code, i have done events in SDL before but this is not working.
if(IsFalling == false){
	log("IsFalling is false");
	if(SDL_PollEvent(&Event)){
		log("There is an event");
                if(Event.type == SDL_KEYDOWN){
			log("The event is a keydown");
			switch(Event.key.keysym.sym){
				case SDLK_UP:
					//if the user pressed the up key
					log("up was presed");
					IsJumping = true;
				break;
			}
		}
	}
	}

in my log file i see "IsFalling is false", but nothing else even when i press the up key Do any of you know whats wrong?

Share this post


Link to post
Share on other sites
Advertisement
That doesn't seem like enough code to tell you what is wrong, only make guesses. That said, some things that it could theoretically be might include you never actually initialized SDL correctly or that your SDL_PollEvent is in a different thread from the thread that set the video mode.

So, did you actually init SDL correctly and/or are you using multithreading?

Share this post


Link to post
Share on other sites
I am not using multithreading.

This is my initilization code:

//init SDL
if(SDL_Init(SDL_INIT_EVERYTHING) == -1){ return 1; log("Unable to Initialize SDL");}
else { log("Initialized SDL"); }


//init the screen
screen = SDL_SetVideoMode(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);
if(screen == NULL){ return 1; log("Unable to initialize screen");}
else{ log("Initialized screen"); }


//init TTF
if(TTF_Init() == -1)
{ log("Unable to initialize SDL_TTF"); return 1; }
else{ log("Initialized SDL TTF"); }


//init audio
if(Mix_OpenAudio(22050, MIX_DEFAULT_FORMAT, 2, 4096) == -1)
{ log("Unable to initialize SDL_Mixer"); }
else{ log("Initialized SDL Mixer"); }


SDL_WM_SetCaption( "Platformer", NULL );

Share this post


Link to post
Share on other sites
In addition to what nobodynews said, I'll go ahead and mention that your logic looks highly suspect.

In a typical game loop, you would process all pending events (using a while loop) every update cycle. Even with your current logic it seems you should be receiving the 'key down' event eventually, so I'm guessing something else is wrong as well. However, you'll probably want to re-arrange things so that events are processed on every update, rather than only when isFalling is false. (Remember that there are other events being dispatched besides the ones you happen to be interested in here. For example, with the current logic, your game will ignore quit requests whenever the player happens to be falling :)

Also, make sure you're only processing the events in one location in your code. If you have a 'normal' event processing 'while' loop elsewhere in your code, you may simply be processing all pending events before this block of code executes.

Finally, you can just write:
if (!IsFalling) { ... }
Rather than:
if (IsFalling == false) { ... }

Share this post


Link to post
Share on other sites
I think the way i handle my events is ok because that code in a player class.
the function is called every frame and it updates the player.

In my main loop i first fill the screen black
then i check to see if the X button has been pressed and break from the loop if it is
after that i handle the player by calling that function
i then center the camera to the player
draw the tile map
EDIT:
i draw the player
and update the screen

Share this post


Link to post
Share on other sites
Quote:
I think the way i handle my events is ok because that code in a player class.
But it doesn't work :| What makes you think that it's 'ok'?
Quote:
the function is called every frame and it updates the player.

In my main loop i first fill the screen black
then i check to see if the X button has been pressed and break from the loop if it is
after that i handle the player by calling that function
i then center the camera to the player
draw the tile map
EDIT:
i draw the player
and update the screen
No, that doesn't sound right. It sounds like you're processing events in two places, which could easily cause exactly the behavior you're seeing.

Once again, you should poll for events once only somewhere in your main loop and then dispatch the events to whomever might be interested in them (e.g. the player object).

Share this post


Link to post
Share on other sites
Hidden
Try this:


void read_input() {
SDL_Event event;

while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
switch (event.key.keysym.sym) {
case SDLK_ESCAPE:
// escape was pressed
break;
case SDLK_UP:
// up was pressed
break;
}
break;
}
}
}




Put that in your code and call read_input() ONCE from your main loop. If that don't work, nothing will.

EDIT: I realize now that this does exactly the same thing your code does, except that it looks nicer. You could try it and see if it helps, but it shouldn't make any difference. I therefor suspect that your error is somewhere else in your code. So it's down the debug path... which means, start commenting out code and see if there is any other function causing this particular problem.

Share this post


Link to post
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!