Thread: While thing

  1. #16
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > while (right == true)
    Like has been said already.

    If right is false, if never gets into the loop.
    If right is true, it never gets out of the loop.

    Roughly speaking (5 mins reading the manual), I would try
    - SDL_AddTimer to set a timer.
    - In the timer callback function, call SDL_PushEvent to send a 'user' event, which in this context will mean that time has elapsed.

    Replace the while (right == true) with if (right == true)
    You'll now be getting periodic stimulus of the event queue (through the timer).
    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.

  2. #17
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    I'm not good at english. I don't really understand what you're saying. Show me some code.
    EDIT:
    What manual?
    Last edited by Livijn; 05-09-2007 at 02:45 PM.

  3. #18
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    This is hard

  4. #19
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Livijn View Post
    But i want the game to work.
    Everybody wants their stuff to work.

    So why should you succeed in getting it to work? You need to put the effort into it. Playing around with tests is fine if you know what you're doing, but you have to be willing to invest in reading up on the library you're using.

  5. #20
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    > What manual?
    The one from http://www.libsdl.org/docs.php
    I mean, c'mon - this isn't rocket science.
    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.

  6. #21
    Deathray Engineer MacGyver's Avatar
    Join Date
    Mar 2007
    Posts
    3,210
    Quote Originally Posted by Salem View Post
    I mean, c'mon - this isn't rocket science.
    That would be ironic if his game involves spaceships.

  7. #22
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    You know, if you say the title of this thread quickly it sounds sort of like this. </offtopic>
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  8. #23
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Lol, now it works when i'm moving the mouse. So if i hold down left button without moving the mouse, the piece goes 1 way to the left. If i move the mouse all the time the piece moves exactly like i want it to do without moving the mouse.

  9. #24
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Understand?

  10. #25
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    Please don't bump threads to get them moved up on the list. If someone has some help to offer, they'll post. Until then, keep working on the problem yourself.

  11. #26
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Okey! But i am working on it. I've been for the whole day. Only breaks for food and fresh air, which is together something like 2h.

  12. #27
    Registered User
    Join Date
    Jan 2005
    Posts
    7,366
    >> Okey! But i am working on it. I've been for the whole day.
    Just keep at it. One piece at a time. Break it down as much as you can to narrow down the problem. Good luck.

  13. #28
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    I almsot got it now! When i move the mouse and hold down left or right, it moves! I think it is because the mouse gives the program a new event. Right? So if i would like to do this without moving the mouse. How should i do?

    Code:
    Code:
        //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;
                                                                          SDL_FillRect( screen, NULL, NULL ); 
                                                                          apply_surface( 0, 0, background, screen );
                                                                          piece = load_image( "piece.png" ); 
                                                                          apply_surface( x, y, piece, screen ); 
                                                                          break;
                                                                    
                                                                    case SDLK_RIGHT:
                                                                         right = false;
                                                                         SDL_FillRect( screen, NULL, NULL ); 
                                                                         apply_surface( 0, 0, background, screen );
                                                                         piece = load_image( "piece.png" ); 
                                                                         apply_surface( x, y, piece, screen ); 
                                                                         break;
                                     }
                   }      
                //Check if the right is pressed
                if (right == true)
                {
                             x = x + 1; 
                             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
                if (left == true)
                {
                             x = x - 1; 
                             SDL_FillRect( screen, NULL, NULL ); 
                             apply_surface( 0, 0, background, screen );
                             piece = load_image( "piece-left.png" ); 
                             apply_surface( x, y, piece, screen ); 
                }
                
                //Apply the square
                apply_surface( 400, 150, square, 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;    
            }
        }
    Last edited by Livijn; 05-10-2007 at 01:42 PM.

  14. #29
    Registered User
    Join Date
    Jan 2007
    Posts
    188
    Bump

  15. #30
    Cat without Hat CornedBee's Avatar
    Join Date
    Apr 2003
    Posts
    8,895
    Put the if(right == true) (by the way, why don't you simply use if(right) ?) outside the while(SDL_PollEvent...)
    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

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, 03:56 PM
  4. newbie needs help comprehending simple thing
    By A helpless one in forum C++ Programming
    Replies: 6
    Last Post: 12-16-2002, 09: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, 06:13 PM