SDL_PollEvent

Hello everyone.

I have a problem whit my code and can find a solution. To be more exact the logic already works but my while-loop don’t update my jump/fall-animations.
Iam startes this project 3 weeks ago and it is my first one, too. So I have limits of knowledge even concerning the basics ^^’

After some tests this is what I recognized:

  • Iam using the while (SDL_PollEvent()) loop because all tutorials i’ve found using this kind of loop (I didnt found another/better way to solve it)

  • if I press “Space” the animation starts but onlycontinues as long there is any kind of input by mous or keyboard (like moving the mouse over the screen)

    // only an example to show the problem
    fall= false
    collground = false
    while (runwindow)
    {

      while (SDL_PollEvent(&run_e))
      	{
                    if (state [SDL_SCANCODE_D] {fall = true;}
                    if (fall && !jump) {p1.y += 3;SDL_BlitSurface (x,&x_fall_r,screen,&p1);}
                  }
          }
    

my p1 rect move 3 pix down per loop sequence but only as long as i set fall = true (using D) and only for one sequence (means i have to update the loop whit my mouse manually)

  • I guess there must be a SDL-funktion like “SDL_EnableKeyRepeat()” but iam not sure if this is the one I need in this case (because i dont want to repaet an input?!?)
  • also I wonder why i have to define this function whit an integer?!? (int SDL_EnableKeyRepeat(int delay, int interval); otherwise it sais not defined #define SDL_EnableKeyRepeat(int delay, int interval) dindt work,too )

hopefully someone can help me as I said the logic is working but I have to fix anytime.javascript:emoticon(’:?’)

I’m using Win8 64 bit and SDL 2.0 [/code] (may it`s incompatible?) :?

my code(complete):

#include <SDL.h>
#include <Windows.h>
#include <stdio.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include

SDL_Window* window;
SDL_Surface* screen;

SDL_Surface* background1;
SDL_Surface* background5;
SDL_Surface* ground;
SDL_Surface* ground2;
SDL_Surface* x;

SDL_Event run_e;
SDL_Event key_e;

float jumphigth = 5;
float gravity = 0.12f;

int FPS = 25;
int fps;

const Uint8* state = SDL_GetKeyboardState(0);

bool runwindow = true;
bool jump = false;
bool collground = false;
bool collenemy = false;
bool collwall = false;

SDL_Rect background;

SDL_Rect p1;
SDL_Rect p2;
SDL_Rect p3;

SDL_Rect x_stand_r;
SDL_Rect x_stand_l;
SDL_Rect x_fall_r;
SDL_Rect x_fall_l;

SDL_Rect gr1;
SDL_Rect gr2;

int main (int argc, char* args[])
{
	//init window
	SDL_Init(SDL_INIT_VIDEO);
	window = SDL_CreateWindow ("Window",200,200,800,600,SDL_WINDOW_RESIZABLE);
	screen = SDL_GetWindowSurface (window);	
	//draw background
	background1 = SDL_LoadBMP ("blacksun.bmp");
	/*Uint32 black = SDL_MapRGB (background5->format,0,0,0); 
	SDL_SetColorKey (background5,SDL_TRUE,black);*/
	//load iamges
	x = SDL_LoadBMP ("x.bmp");
	ground = SDL_LoadBMP ("ground.bmp");
	ground2 = SDL_LoadBMP ("ground2.bmp");
	background5 = SDL_LoadBMP ("background5.bmp");
	//positions

	p1.x = 250;
	p1.y = 100;
	p1.h = 100;
	p1.w = 100;

		p2.x = 200;
		p2.y = 200;
		p2.h = 100;
		p2.w = 100;

	p3.x = 200;
	p3.y = 200;
	p3.h = 100;
	p3.w = 100;
		
	//Sprite

	background.x = 800;
	background.y = 350;
	background.h = 600;
	background.w = 800;

	x_stand_r.x = 0;
	x_stand_r.y = 200;
	x_stand_r.h = 100;
	x_stand_r.w = 100;

		x_stand_l.x = 100;
		x_stand_l.y = 200;
		x_stand_l.h = 100;
		x_stand_l.w = 100;

	x_fall_r.x =200;
	x_fall_r.y =200;
	x_fall_r.h =100;
	x_fall_r.w =100;

	//ground

	gr1.y =375;
	gr1.w =500;

	gr2.x =300;
	gr2.y =200;
	gr2.w =250;

	while (runwindow)
	{
		
		while (SDL_PollEvent(&run_e))
		{
			SDL_FillRect(screen, NULL, 0x000000);
			SDL_KEYUP;
			SDL_BlitSurface (background1,&background,screen,0);
			SDL_BlitSurface (background5,0,screen,0);
			SDL_BlitSurface (ground2,0,screen,&gr2);
			SDL_BlitSurface (x,&x_stand_r,screen,&p1);
				
				//animation jump p1
				if (jump) {p1.y -= jumphigth;jumphigth -= gravity; printf("");}
				/*if (jump) {p1.y -= jumphigth;jumphigth -= gravity; printf("");}*/
				if (state[SDL_SCANCODE_SPACE]) {jump = true;}
				if (jumphigth <=0) {jump = false;}
				
				//animation run p1
				if (state [SDL_SCANCODE_D] && p1.x <= 600) {p1.x += 5;}
				if (state [SDL_SCANCODE_A] && p1.x >= 125) {p1.x -= 5;}
				

				//collision ground p1
				if (!collground && !jump) {p1.y += 3;SDL_BlitSurface (x,&x_fall_r,screen,&p1);}
				if (p1.y + p1.h >= gr1.y && p1.x >= gr1.x && p1.x <= gr1.x + gr1.w){collground = true;jumphigth = 5;} else{collground = false;}			 
				/*if (p1.y + p1.h >= gr2.y && p1.x >= gr2.x && p1.x <= gr2.x +gr2.w){collground = true;} else{collground = false;}*/

			SDL_UpdateWindowSurface (window);
				if(1000/FPS>SDL_GetTicks()-fps) {SDL_Delay(1000/FPS-(SDL_GetTicks()-fps));}
				if (state [SDL_SCANCODE_ESCAPE]){runwindow = false;}
		}
		
	}
SDL_Quit();
return 0;
}

I can find another topic about this problem so any kind of comment would be nice :frowning:

no suggestions, tipps or even stupid jokes about my bad spelling?

I can find another topic about this problem so any kind of comment would be nice :frowning:

no suggestions, tipps or even stupid jokes about my bad spelling?

I’m looking at your code and I’m slightly curious.

I don’t want to judge your code since I as well am fairly new to the SDL library, but are you using the SDL_GetError() function at all in this program to determine where problems may arise? In every tutorial I have seen, there was a way to incorporate that function in there and if you ran into any errors, the function told you what was wrong.

I’d help you out with your SDL_PollEvent loop problem, but sadly I’m not that experienced. I will get back to you when I find out what’s wrong with it, though.

Well I think this is not really an SDL problem. If you change to ,say, DirectX, you would find the same exact problem :-). SDL_GetError() won’t complain anything.

From your code, you process everything insides event-polling loop, even if it’s nothing about event/input. SDL_PollEvent() pull event from the event queue, it returns false when there is no event left in the queue. Since there is no event in the event queue (as there’s no input), the loop is just get skipped altogether.

You might want to try moving the logic that does not involved with the inputs/events out of the poll loop, place it outside just below that loop (inside the render loop). You should see some animation even if there’s no input.

I think you might want to pick up a good game programming book for beginner. I couldn’t come up with any title right now btw (sorry about that).