Image on screen dragging on `

This is a discussion on Image on screen dragging on ` within the C++ Programming forums, part of the General Programming Boards category; This is a 2 part question 1.So when I move my block around it doesn't move the image just extends ...

  1. #1
    Registered User
    Join Date
    Nov 2009
    Posts
    151

    Image on screen dragging on `

    This is a 2 part question
    1.So when I move my block around it doesn't move the image just extends it for example:
    this is the image when the program starts up
    Code:
    ~
    this is the image when you press the right key
    Code:
    ~~
    while it should be
    Code:
     ~
    why does that happen.(I'll add attachments).

    2. you have to keep on tapping a key for it to move in that direction, you can't just hold it down.

    here is my code
    Code:
    #include <SDL/SDL.h>
    #include <SDL/SDL_image.h>
    #include <string>
    
    using namespace std;
    
    //block coordinates
    int x = 0;
    int y = 0;
    
    SDL_Surface *screen = NULL;
    SDL_Surface *block = NULL;
    SDL_Surface *wall = NULL;
    
    bool quit = false;
    
    SDL_Event event;
    
    void handle_events(){
        if(SDL_PollEvent(&event)){
                if(event.type == SDL_QUIT){
                    quit = true;
                }
                if(event.type = SDL_KEYDOWN){
                    switch(event.key.keysym.sym){
                        case SDLK_UP: y--; break;
                        case SDLK_DOWN: y++;break;
                        case SDLK_RIGHT: x++; break;
                        case SDLK_LEFT: x--; break;
                        case SDLK_ESCAPE: quit = true; break;
                        default: break;
                    }
            }
    
        }
    }
    
    
    void apply_surface(int X, int Y, SDL_Surface *source, SDL_Surface *destination){
        SDL_Rect pos;
        pos.x = X;
        pos.y = Y;
    
        SDL_BlitSurface(source, NULL, destination, &pos);
    }
    
    SDL_Surface *load_image(string filename){
        SDL_Surface *image;
    
        image = IMG_Load(filename.c_str());
    
        return image;
    }
    
    bool init(){
        if(SDL_Init(SDL_INIT_EVERYTHING) == -1){
            return false;
        }
    
        screen = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE);
    
        if(screen == NULL){
            return false;
        }
    
        SDL_WM_SetCaption("Test", NULL);
    
        return true;
    }
    
    int main( int argc, char* args[] )
    {
        if(init() == false){
            return 1;
        }
    
        block = load_image("block.png");
        wall = load_image("wall.png");
    
        if(block == NULL){
            return 1;
        }
        if(wall == NULL){
            return 1;
        }
        //make screen color
        SDL_FillRect(screen, &screen->clip_rect, SDL_MapRGB(screen->format, 0x12, 0x24, 0x48));
    
    
    
        while(quit == false){
            handle_events();
    
            apply_surface(x, y, block, screen);
            apply_surface(320, 0, wall, screen);
    
            SDL_Flip(screen);
        }
    
        //Quit SDL
        SDL_Quit();
    
        return 0;
    }
    Thank you
    Attached Images Attached Images   
    Last edited by bijan311; 05-16-2010 at 03:37 PM.

  2. #2
    Registered User jdragyn's Avatar
    Join Date
    Sep 2009
    Posts
    93
    Not sure about part 1, but the second part of your question is easy to answer. When you press a key repeatedly, how many times do you press the KEY DOWN? How about when you press the KEY DOWN and hold it?
    C+/- programmer extraordinaire

  3. #3
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    Oh I get it, when you hold it down it only presses it once, so how do I make it so you can hold it down
    Last edited by bijan311; 05-16-2010 at 03:38 PM.

  4. #4
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    I will be curious to see the answer to this one...the first part has so little information about what is going on, platform, language, code intent, everything that I don't see how to even begin answering the question...not even sure what the question is...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  5. #5
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Looks like lack of double buffering to me or perhaps failure to clear the back buffer per frame.

  6. #6
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    OK I figured out #1, i just but fill rect in the loop
    Last edited by bijan311; 05-16-2010 at 04:41 PM.

  7. #7
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    OK, I still need an answer to #2

  8. #8
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,598
    Sounds like you have a bad key handler.

  9. #9
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    i know exactly what is wrong ...

    when an event registers as KEYDOWN it means you just pressed the key down, and it alters x by 1 , just because you are holding it doesnt incur another event ... when you release the key it sends another event KEYUP.

    Since only 1 event is passed when you press the KEYDOWN, x is only altered one time, by 1.

    make a variable called velocity_x, velocity_y

    when keydown is registered change velocity to 1, now in your while loop alter x+=velocity.

    when keyup is registered change velocity back to 0.

    heres what your while loop would look like
    Code:
        while(quit == false){
            handle_events();
             x+=velocity_x;
             y+=velocity_y;
            apply_surface(x, y, block, screen);
            apply_surface(320, 0, wall, screen);
    
            SDL_Flip(screen);
        }
    NOTE: your block's speed of movement will be relative to how fast your computer can render, you need frame control for this to work 'as intended' (google SDL_Delay)

    To sum everything up: Pressing a key , holding it down for whatever time, and releasing it is only 2 events .. and that is all SDL sees, therefore you have to handle 2 events not just KEYDOWN
    Last edited by rodrigorules; 05-18-2010 at 07:20 PM.

  10. #10
    Registered User
    Join Date
    Nov 2009
    Posts
    151
    now all the block goes is going diagonal
    here are my additions:
    Code:
                if(event.type == SDL_KEYDOWN){
                    velocity_x = 1;
                    velocity_y = 1;
                    switch(event.key.keysym.sym){
    Code:
    if(event.type == SDL_KEYUP){
        velocity_x = 0;
        velocity_y = 0;
    }
    Code:
    handle_events();
    x += velocity_x;
    y += velocity_y;

  11. #11
    Registered User
    Join Date
    Aug 2005
    Posts
    266
    You are changing both x and y to 1 , obviously it is gonna change BOTH x and y directions, pick one or be more specific;

    KEYDOWN means any key was pressed down, you have to be more specific than that

    example:
    Code:
    switch(event.type)
    			{
    				case SDL_KEYUP:
    					if(event.key.keysym.sym == SDLK_UP)
    					{
    						veloy1=0;
    					}
    					else	if(event.key.keysym.sym == SDLK_DOWN)
    					{
    						veloy1=0;
    					}
    
    					break;
    				case SDL_KEYDOWN:
    					if(event.key.keysym.sym == SDLK_UP)
    					{
    						veloy1=-1;
    					}
    					else if(event.key.keysym.sym == SDLK_DOWN)
    					{
    						veloy1=1;
    					}
    
    					break;
    			}
    You are never specifing which key it is that your pressing.


    You have it
    set so that if you pressed ANY KEY DOWN then you set velo_x =1 AND velo_y =1 , can't you tell that it doesn't make sense to change both velocities if you press the DOWN key only?
    Last edited by rodrigorules; 05-18-2010 at 09:36 PM.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problem reading tiff image files?
    By compz in forum C++ Programming
    Replies: 9
    Last Post: 10-30-2009, 05:17 AM
  2. How to get image from the screen and then save it to a file?
    By nomer in forum Windows Programming
    Replies: 2
    Last Post: 05-25-2006, 09:46 AM
  3. Feedback: Functional Specification Wording
    By Ragsdale85 in forum C++ Programming
    Replies: 0
    Last Post: 01-18-2006, 04:56 PM
  4. char copy
    By variable in forum C Programming
    Replies: 8
    Last Post: 02-06-2005, 10:18 PM
  5. i am not able to figure ot the starting point of this
    By youngashish in forum C++ Programming
    Replies: 7
    Last Post: 10-07-2004, 03:41 AM

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