Jump to content
  • Advertisement
Sign in to follow this  
Mozly

SDL timing not working!

This topic is 2955 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

So I read lazyFoo's SDL tutorial and now I wanna make my own game.
I'm making my own rendition of tetris.

The Problem:
the blocks wont fall down unless my mouse is moving over the window. How do i fix this.


PS. I think the code might be irrelevant but i'll add my main() and the snippet that explains my blocks falling. Incase it helps.


look at line 13 of the first snippet(main()) and line 29 of the second(cube::handleEvent)


int main(int argc, char* args[])
{
bool quit = false;

if(!init())
return 1;

// 2 timers 1 to control fps and other to control drop movement
Timer t1;

Cube jj;

down.start();
while(!quit)
{
t1.start();


while(SDL_PollEvent(&event))
{
if(event.type == SDL_QUIT)
quit = true;
jj.handleEvent();
}


SDL_FillRect(screen,&screen->clip_rect,SDL_MapRGB(screen->format,0xFF,0xFF,0xFF));
applysurface(0,0,background,screen);
jj.show();

if(SDL_Flip(screen) ==-1)
return 1;

if(t1.get_ticks() < 1000/FPS)
SDL_Delay(1000/FPS - t1.get_ticks());

}

bye();
return 0;
}


void Cube::handleEvent()
{
if(event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_RIGHT: xVel += 34;break;
case SDLK_LEFT: xVel -= 34; break;
case SDLK_DOWN: yVel += 34; break;
}
}

cube.x += xVel;
b1.move(xVel,0);
b2.move(xVel,0);
b3.move(xVel,0);
b4.move(xVel,0);

//dont go outside the grid
if(cube.x < 100 || cube.x > 440-cube.w)
{
cube.x -= xVel;
b1.move(-xVel,0);
b2.move(-xVel,0);
b3.move(-xVel,0);
b4.move(-xVel,0);
}

//fall autonomously
if(down.get_ticks() >= 1000)
{
yVel += 34;
down.start();
}


cube.y += yVel;
b1.move(0,yVel);
b2.move(0,yVel);
b3.move(0,yVel);
b4.move(0,yVel);



if(cube.y+ 68 > 710)
{
cube.y -= yVel;
b1.move(0,-yVel);
b2.move(0,-yVel);
b3.move(0,-yVel);
b4.move(0,-yVel);
}

// stop movement when not intended
xVel = 0;
yVel = 0;
}

Share this post


Link to post
Share on other sites
Advertisement
SDL's timing is working fine.

Your code is the problem. The "handleEvent" function is only called when an event (such as mouse movement) occurs. Your code for moving the block is in the same function, hence the block only moves when there is input.

The solution is to break that function in two, an update() or tick() function that is unconditionally called every frame, and the handleEvent() logic which updates the velocity only when necessary.

Share this post


Link to post
Share on other sites
Ya but my HandleEvent() function is already in my main() Loop which happens every Frame. So it still dosen't solve my problem.

Also there is no mousemovement eventhandling. I didn't put any mousemovement in my code.

Share this post


Link to post
Share on other sites
Quote:
Original post by Mozly
Ya but my HandleEvent() function is already in my main() Loop which happens every Frame. So it still dosen't solve my problem.

Also there is no mousemovement eventhandling. I didn't put any mousemovement in my code.

HandleEvent() is inside the SDL_PollEvent() loop. This loop only executes if there is a pending input event (of any type, including mouse events). When there is no pending input, this loop does nothing, and your object doesn't move.

The solution remains the same.

Share this post


Link to post
Share on other sites
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!