SDL Sprite Stays there

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
 #include "SDL/SDL.h"

 int main(int argc, char *args[]){
    SDL_Init(SDL_INIT_EVERYTHING);
    SDL_WM_SetCaption("Test", NULL);
    SDL_Surface *buffer;
    SDL_Surface *Player;
    SDL_Surface *fPlayer;
    bool fullscreen = false;
    if(fullscreen==true){
        buffer = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE|SDL_FULLSCREEN);
    }else{
        buffer = SDL_SetVideoMode(800,600,32,SDL_SWSURFACE);
    }
    SDL_Rect rect;
    rect.x = 1;
    rect.y = 1;
    rect.w = 10;
    rect.h = 10;
    fPlayer = SDL_LoadBMP("Player.bmp");
    Player = SDL_DisplayFormat(fPlayer);
    SDL_SetColorKey(Player, SDL_SRCCOLORKEY | SDL_RLEACCEL, SDL_MapRGB(buffer->format,255,255,255));
    Uint8 *key;
    key = SDL_GetKeyState(NULL);
    SDL_Event event;
    while(event.type != SDL_QUIT){
        SDL_BlitSurface(Player, NULL, buffer, &rect);
        if(key[SDLK_RIGHT]){
            rect.x++;
            SDL_UpdateRect(buffer,0,0,0,0);
        }
        if(key[SDLK_LEFT]){
            rect.x--;
            SDL_UpdateRect(buffer,0,0,0,0);
        }
        if(key[SDLK_UP]){
            rect.y--;
            SDL_UpdateRect(buffer,0,0,0,0);
        }
        if(key[SDLK_DOWN]){
            rect.y++;
            SDL_UpdateRect(buffer,0,0,0,0);
        }
        SDL_PollEvent(&event);
        SDL_Flip(buffer);
    }
    SDL_FreeSurface(fPlayer);
    SDL_FreeSurface(Player);
    SDL_FreeSurface(buffer);
    SDL_Quit();
    return 0;
 }


Is something wrong with this code?
When I move my Player my Player leaves a clone before. How can I fixed that?
closed account (N36fSL3A)
If you're using SDL 1.x, you're doing it wrong. Use SDL2. It's newer, and more powerful.

You also should considering initializing pointers to nullptr or if your compiler can't support it (in that case you should consider an upgrade... this implies you aren't using C of course.) NULL.

When I move my Player my Player leaves a clone before. How can I fixed that?
Can you elaborate? I don't understand what you're saying.
Topic archived. No new replies allowed.