Locking up!

This is a discussion on Locking up! within the Game Programming forums, part of the General Programming Boards category; Ok, so I did a tutorial on basic animation and then I made a sprite map and altered the program ...

  1. #1
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Locking up!

    Ok, so I did a tutorial on basic animation and then I made a sprite map and altered the program to use the numpad to go in all 8 directions (tut was 4). This all worked just peachy, then I decided to try and alter it again to chase the mouse around the screen, which is does but if it goes to the right or bot of the screen it locks up.

    Code:
    void SPRITE::HandleEvent(SDL_Event* event)
    {
        switch(event->type)
        {
            case SDL_QUIT:
                    {
                    Running = false;
                    break;
                    }
    
            case SDL_MOUSEMOTION:
                    {
                    MousePosition(event);
                    break;
                    }
    
            default: break;
        }
    }
    
    
    bool SPRITE::MousePosition(SDL_Event* event)
    {
        int mX = event->motion.x;
        int mY = event->motion.y;
    
        if(mX != mouseX || mY != mouseY)
        {
            mouseX = mX;
            mouseY = mY;
            return true;
        }
        return false;
    }
    
    
    
    void SPRITE::ChaseMouse()
    {
        int direction = 0;
        int moveX, moveY;
    
        // is sprite at cursor?
        while( (x != mouseX) || (y != mouseY))
        {
            moveX = ( mouseX - x );
            moveY = ( mouseY - y );
    
            // decide which direction to move towards cursor
            if(moveX > 0 && moveY > 0)
                        direction = 3;
            else if(moveX > 0 && moveY < 0)
                        direction = 9;
            else if(moveX < 0 && moveY > 0)
                        direction = 1;
            else if(moveX < 0 && moveY < 0)
                        direction = 7;
            else if(moveX > 0)
                        direction = 6;
            else if(moveX < 0)
                        direction = 4;
            else if(moveY > 0)
                        direction = 2;
            else if(moveY < 0)
                        direction = 8;
    
            // Set velocity and section of spritemap
            switch(direction)
            {
                case 8:  {
                         yVel -= SPRITE_HEIGHT / 63;
                         section.x = 65;
                         section.y = 0;
                         break;
                         }
    
                case 2:  {
                         yVel += SPRITE_HEIGHT / 63;
                         section.x = 65;
                         section.y = 130;
                         break;
                         }
    
                case 4:  {
                         xVel -= SPRITE_WIDTH / 63;
                         section.x = 0;
                         section.y = 65;
                         break;
                         }
    
                case 6:  {
                         xVel += SPRITE_WIDTH / 63;
                         section.x = 130;
                         section.y = 65;
                         break;
                         }
    
                case 7:  {
                         xVel -= SPRITE_WIDTH / 63;
                         yVel -= SPRITE_HEIGHT / 63;
                         section.x = 0;
                         section.y = 0;
                         break;
                         }
    
                case 9:  {
                         xVel += SPRITE_WIDTH / 63;
                         yVel -= SPRITE_HEIGHT / 63;
                         section.x = 130;
                         section.y = 0;
                         break;
                         }
    
                case 1:  {
                         xVel -= SPRITE_WIDTH / 63;
                         yVel += SPRITE_HEIGHT / 63;
                         section.x = 0;
                         section.y = 130;
                         break;
                         }
    
                case 3:  {
                         xVel += SPRITE_HEIGHT / 63;
                         yVel += SPRITE_HEIGHT / 63;
                         section.x = 130;
                         section.y = 130;
                         break;
                         }
    
                default: break;
            }
            Move();
    
            switch( direction )
                {
                    case 8: yVel += SPRITE_HEIGHT / 63; break;
                    case 2: yVel -= SPRITE_HEIGHT / 63; break;
                    case 4: xVel += SPRITE_WIDTH / 63; break;
                    case 6: xVel -= SPRITE_WIDTH / 63; break;
                    case 7: xVel += SPRITE_WIDTH/63; yVel += SPRITE_HEIGHT/63; break;
                    case 9: xVel -= SPRITE_WIDTH/63; yVel += SPRITE_HEIGHT/63; break;
                    case 1: xVel += SPRITE_WIDTH/63; yVel -= SPRITE_HEIGHT/63; break;
                    case 3: xVel -= SPRITE_WIDTH/63; yVel -= SPRITE_HEIGHT/63; break;
                    default: break;
                }
    
        }
        section.x = 65;
        section.y = 65;
        Move();
    }
    
    bool SPRITE::Execute(TIMER& GameTimer, SDL_Event* event)
    {
        Running = true;
    
      if(Initialize() == false)
            {
            return -1;
            }
    
        while(Running)
        {
    
            GameTimer.Start();
            SDL_PollEvent(event);
                {
                HandleEvent(event);
                }
           ChaseMouse();
    
    
            if( GameTimer.Get_Ticks() < 1000 / FRAMES_PER_SECOND )
                {
                SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - GameTimer.Get_Ticks() );
                }
            GameTimer.Stop();
        }
    
        SDL_Quit();
    
        return 0;
    }
    
    void SPRITE::Move()
    {
        //Move the sprite left or right
        x += xVel;
    
        //If the sprite went too far to the left or right
        if( ( x < 0 ) || ( x + SPRITE_WIDTH > SCREEN_WIDTH ) )
        {
            //move back
            x -= xVel;
        }
    
        //Move the sprite up or down
        y += yVel;
    
        //If the sprite went too far up or down
        if( ( y < 0 ) || ( y + SPRITE_HEIGHT > SCREEN_HEIGHT ) )
        {
            //move back
            y -= yVel;
        }
    
        //Fill the screen white
        SDL_FillRect( screen, &screen->clip_rect, SDL_MapRGB( screen->format, 0xFF, 0xFF, 0xFF ) );
        //Show the sprite on the screen
        Show();
        //Update the screen
        SDL_Flip( screen );
    }
    
    
    void SPRITE::Show()
    {
        //Show the sprite    ApplySurface( sprite, section, screen, x, y );
    }
    This is all the relevant code. I tried changing the right and bot boundries in Move(), but it just locked up farther away from the edge. Being ignorant while learning is such a pain.

    Any feedback on the ChaseMouse() and MousePosition() funcs would be cool as well, as these are mostly mine. Please feel free to straighten me out on what I did wrong and how. EDIT:: My commenting is really poor, this is something I need to work on, thanks for your patience.
    Last edited by Incantrix; 12-04-2010 at 09:00 PM.

  2. #2
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Code:
        //If the sprite went too far to the left or right
        if( ( x < 0 ) || ( x + SPRITE_WIDTH > SCREEN_WIDTH ) )
        {
            //move back
            x -= xVel;
        }
    What happens when x is between SCREEN_WIDTH and SCREEN_WIDTH - SPRITE_WIDTH ?
    It gets moved back to its previous position and the condition here:
    Code:
        while( (x != mouseX) || (y != mouseY))
        {
    will never fail.

    Try something like this (assuming x can never be less than 0 or greater than SCREEN_WIDTH-1
    Code:
        if( x < 0 )
        {
            x=0;
        }
        if ( x + SPRITE_WIDTH > SCREEN_WIDTH )
        {
            x = SCREEN_WIDTH - SPRITE_WIDTH;
        }
    And something similar for y.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Is SDL sensitive to sprites moving outside the frustum or the screen?

  4. #4
    Registered User
    Join Date
    Oct 2010
    Posts
    45
    Code:
        if ( x + SPRITE_WIDTH > SCREEN_WIDTH )
        {
            x = SCREEN_WIDTH - SPRITE_WIDTH;
        }
    I changed to your suggestion, but it still ends up with the same result, seizes on the right and bot still.

    AND just screwing around I changed to

    Code:
       if( x + (SPRITE_WIDTH - 63) > SCREEN_WIDTH )
    This results in the sprite moving completely off the screen if you move the cursor beyond the window. But, it does not lock up and will come back on-screen if you cursor back over the window again... (My sprite width/height is 63)

    Is SDL sensitive to sprites moving outside the frustum or the screen?
    I've run the sprite off the edge of the window without locking it up, so I don't believe so. EDIT::Which I just did above (again).

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Then I seriously doubt that is the source of the lockup. It may be something to look at in your code as another potential problem but I do not think that is the source of your current issue.

    I do not understand most of what you are doing in your code but I have built many sprite systems and have never had an issue like what you are describing. My guess would be, and it is only a guess, that you are somehow addressing an area that is off of the sprite sheet in memory and thus causing problems. If you do this in pure D3D nothing happens b/c u,v coords will simply wrap around.

  6. #6
    Registered User
    Join Date
    Oct 2010
    Posts
    45

    Next

    Well, I have gotten it to work, but it seems wrong.

    My main loop is now:

    Code:
    while(Running)
        {
    
            GameTimer.Start();
            SDL_PollEvent(event);
                {
                HandleEvent(event);
                }
    
           if( x != mouseX || y != mouseY )
                {
                ChaseMouse();
                Move();
                StopChase();
                }
           else    // Set sprite to center
                {
                section.x = 65;
                section.y = 65;
                Move();
                }
    
            if( GameTimer.Get_Ticks() < 1000 / FRAMES_PER_SECOND )
                {
                SDL_Delay( ( 1000 / FRAMES_PER_SECOND ) - GameTimer.Get_Ticks() );
                }
            GameTimer.Stop();
        }
    I broke up the ChaseMouse() - which now only sets direction and velocity, and added StopChase().

    Code:
    void SPRITE::StopChase()
    {
            switch( direction )
                {
                    case 8: yVel += SPRITE_HEIGHT / VELOCITY; break;
                    case 2: yVel -= SPRITE_HEIGHT / VELOCITY; break;
                    case 4: xVel += SPRITE_WIDTH / VELOCITY; break;
                    case 6: xVel -= SPRITE_WIDTH / VELOCITY; break;
                    case 7: xVel += SPRITE_WIDTH/VELOCITY; yVel +=  SPRITE_HEIGHT/VELOCITY; break;
                    case 9: xVel -= SPRITE_WIDTH/VELOCITY; yVel += SPRITE_HEIGHT/VELOCITY; break;
                    case 1: xVel += SPRITE_WIDTH/VELOCITY; yVel -= SPRITE_HEIGHT/VELOCITY; break;
                    case 3: xVel -= SPRITE_WIDTH/VELOCITY; yVel -= SPRITE_HEIGHT/VELOCITY; break;
                    default: break;
                }
    
            direction = 0;
    }
    I have a couple new issues however. Anything above a sprite velocity of 1 causes the sprite to jitter when it gets to the mouse position as if it can't quite reach the x and y coords.

    Also, why do I have to completely stop the sprite's movement after every move? When I don't, it loops in circles or figure eights...

    Is there someplace I can link these things for people to look at if they want? I see some people have been scolded for linking in msgs.

  7. #7
    Registered User NeonBlack's Avatar
    Join Date
    Nov 2007
    Posts
    435
    Probably because you're moving your sprites in fixed lengths. If something is 1 foot away from you, but you can only take 3-foot steps, how are you going to reach it?

    You can upload your files as attachments or use something like pastebin for smaller code segments. If someone was scolded for posting links, it was probably because he was spamming his personal website.
    I copied it from the last program in which I passed a parameter, which would have been pre-1989 I guess. - esbo

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Approach to File Locking?
    By BigAngryDog in forum Linux Programming
    Replies: 9
    Last Post: 09-19-2009, 03:53 PM
  2. Mac - File locking with fcntl() fails on shared volumes!?
    By idelovski in forum Linux Programming
    Replies: 3
    Last Post: 11-10-2008, 07:37 PM
  3. Double-Checked Locking pattern issue
    By George2 in forum C++ Programming
    Replies: 3
    Last Post: 01-02-2008, 04:29 AM
  4. My Linksys router keeps locking my connection!
    By BuezaWebDev in forum Tech Board
    Replies: 3
    Last Post: 03-27-2005, 09:50 AM
  5. UNIX file locking
    By Kent in forum C Programming
    Replies: 1
    Last Post: 07-30-2002, 03:58 AM

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