-
While thing
Hi!
I'm making a game, just to learn SDL. But i seem to have some trouble with my code. If i cpoy and paste my code from the while into the switch it works. It is the same code. But if i do like that, the piece wouldn't move if you hold down the button. It would only move when you press the button.
How can i make this work?
Code:
//While the user hasn't quit
while( quit == false )
{
//If there's an event to handle
if( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the proper holder surface
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
left = true;
break;
case SDLK_RIGHT:
right = true;
break;
case SDLK_ESCAPE:
quit = true;
break;
}
}
//Check if the right is pressed
while (right == true)
{
x = x + 10;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-right.png" );
apply_surface( x, y, piece, screen );
}
//Check if the left is pressed
while (left == true)
{
x = x - 10;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-left.png" );
apply_surface( x, y, piece, screen );
}
}
//If the user has Xed out the window
else if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
-
Consider this: why would "left" or "right" change while you're in the loops?
-
Hmm... I didn't thought of that. That was a very good question. My answer is: I don't know! :) Do you have a suggestion?
-
Change them to if, not while
-
No, cause if i do that. It would only happen 1 time. You see?
-
No - leave the outer while as while
Change the inner ones to if
See?
-
No i don't understand. :s Explain :)
Or maybe you mean limke this?
Code:
while (left = true)
{
if (left == true)
{
}
}
But if you mean like that, we still have the same problem. :p
EDIT:
You're right! If i add right = false; or left = false; to the while loop everything goes well.
But we still have the same problem. Holding the key doesn't work!
-
If i recall correctly, SDL only signals when keys are pressed or released. If you want to check if a key is *held down* for a longer time, you'd have to do it manually by also checking for KEYUP events. I'm too busy right now to dust off my SDL-fu and write an example, sorry.
-
Okey, meanwhile i wait for you to write an example, i'll search on internet.
-
> if( event.type == SDL_KEYDOWN )
There's also key up event as well I assume?
What about timer events? Do you (or can you) get a regular timer event as well?
On key down, set the flag and move in the required direction.
On key up, clear the flag
On timer, if the flag is set then also do a move.
-
What do you mean with timer? Never heard of it.
EDIT:
Tried like you said, with keyup. Didn't work.
Code:
//While the user hasn't quit
while( quit == false )
{
//If there's an event to handle
if( SDL_PollEvent( &event ) )
{
//If a key was pressed
if( event.type == SDL_KEYDOWN )
{
//Set the proper holder surface
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
left = true;
break;
case SDLK_RIGHT:
right = true;
break;
case SDLK_ESCAPE:
quit = true;
break;
}
}
//If a key was released
if( event.type == SDL_KEYUP )
{
//Set the proper holder surface
switch( event.key.keysym.sym )
{
case SDLK_LEFT:
left = false;
break;
case SDLK_RIGHT:
right = false;
break;
}
}
//Check if the right is pressed
while (right == true)
{
x = x + 10;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-right.png" );
apply_surface( x, y, piece, screen );
}
//Check if the left is pressed
while (left == true)
{
x = x - 10;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-left.png" );
apply_surface( x, y, piece, screen );
}
}
//If the user has Xed out the window
else if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
-
It's going to be hard to write anything, if you don't at least read something first.
Go and read (or at least skim slowly) all the SDL material you have on hand at the moment.
Something which takes a good few hours, not bouncing it straight back in 10 minutes.
-
But i want the game to work. This is the way im learning stuff. Doing it first, so i know how they work. And after that, i read about them. But if you know somewhere hey have KEYUP or something like that, tell me!
-
Here's the first thing that popped up when I typed "SDL KEYUP" in google:
http://gpwiki.org/index.php/SDL:Tuto...Keyboard_Input
> This is the way im learning stuff. Doing it first, so i know how they work. And after that, i read about them.
I think you might benefit from switching up the paradigm a little bit.
-
Well, actuallt i learn better from first testing, then reading. It's a fact.
I played around with your link but didn't get it to work. You can see my whole main-function here:
Code:
int main( int argc, char* args[] )
{
//The pieces coordinates
int x = 200;
int y = 200;
//Left flag
bool left = false;
//Right flag
bool right = false;
//Quit flag
bool quit = false;
//Initialize
if( init() == false )
{
return 1;
}
//Load the files
if( load_files() == false )
{
return 1;
}
//Apply the background
apply_surface( 0, 0, background, screen );
//Apply the piece
apply_surface( 70, 178, piece, screen );
//While the user hasn't quit
while( quit == false )
{
//If there's an event to handle
while (SDL_PollEvent(&event))
{
if (event.type == SDL_KEYDOWN)
{
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
left = true;
break;
case SDLK_RIGHT:
right = true;
break;
case SDLK_ESCAPE:
quit = true;
break;
} }
if (event.type == SDL_KEYUP)
{
switch(event.key.keysym.sym)
{
case SDLK_LEFT:
left = false;
break;
case SDLK_RIGHT:
right = false;
break;
}
}
//Check if the right is pressed
while (right == true)
{
x = x + 10;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-right.png" );
apply_surface( x, y, piece, screen );
}
//Check if the left is pressed
while (left == true)
{
x = x - 10;
SDL_FillRect( screen, NULL, NULL );
apply_surface( 0, 0, background, screen );
piece = load_image( "piece-left.png" );
apply_surface( x, y, piece, screen );
}
}
//If the user has Xed out the window
if( event.type == SDL_QUIT )
{
//Quit the program
quit = true;
}
//Update the screen
if( SDL_Flip( screen ) == -1 )
{
return 1;
}
}
//Clean up
clean_up();
return 0;
}