While thing

This is a discussion on While thing within the C++ Programming forums, part of the General Programming Boards category; Hi! I'm making a game, just to learn SDL. But i seem to have some trouble with my code. If ...

  1. #1
    Registered User
    Join Date
    Jan 2007
    Posts
    188

    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;    
            }
        }

  2. #2
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,893
    Consider this: why would "left" or "right" change while you're in the loops?
    All the buzzt!
    CornedBee

    "There is not now, nor has there ever been, nor will there ever be, any programming language in which it is the least bit difficult to write bad code."
    - Flon's Law

  3. #3
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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?

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    Change them to if, not while
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    No, cause if i do that. It would only happen 1 time. You see?

  6. #6
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    No - leave the outer while as while
    Change the inner ones to if
    See?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  7. #7
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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.

    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!
    Last edited by Livijn; 05-08-2007 at 02:46 PM.

  8. #8
    Registered User
    Join Date
    Apr 2007
    Location
    Sweden
    Posts
    41
    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.

  9. #9
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Okey, meanwhile i wait for you to write an example, i'll search on internet.

  10. #10
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    > 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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  11. #11
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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;    
            }
        }
    Last edited by Livijn; 05-08-2007 at 11:40 PM.

  12. #12
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,687
    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.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  13. #13
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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!

  14. #14
    Registered User
    Join Date
    May 2007
    Posts
    88
    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.

  15. #15
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    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;    
    }

Page 1 of 3 123 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Pause/idle thing
    By freedik in forum Windows Programming
    Replies: 13
    Last Post: 08-22-2003, 09:46 AM
  2. A very strange thing
    By gustavosserra in forum C++ Programming
    Replies: 4
    Last Post: 04-15-2003, 12:43 PM
  3. most challenging thing to program
    By volk in forum A Brief History of Cprogramming.com
    Replies: 52
    Last Post: 03-28-2003, 02:56 PM
  4. newbie needs help comprehending simple thing
    By A helpless one in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2002, 08:23 PM
  5. PingPong But how to make 2 thing at the same time..
    By Gugge in forum C Programming
    Replies: 5
    Last Post: 04-02-2002, 05:13 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21