Problems with moving a sprite (SDL)

Hello!
Recently i have started to use the SDL libery, because of the easy game programming benefits with it. First of all I,m not a very experience programmer, so it's probably just a easy/small problem to fix. The problem is that the sprite only duplicate it's self, instead of actually moving. I have tried to search on tutorials on the internett, but I get the same results every time I have tried.

Here is a picture of the sprite before I have moved it:
http://tinypic.com/r/153rx2t/8

Here is a picture of the sprite after I have moved it:
http://tinypic.com/r/24c9vfn/8

Here is my code:

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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
#include <SDL.h>
#include <iostream>

//Declare variables
const unsigned int SCREEN_WIDTH = 640;
const unsigned int SCRREN_HEIGHT = 480;

bool succsess;
bool quit = false;

unsigned int colorRed,colorGreen,colorBlue;
int spriteDstX = 0;
int spriteDstY = 0;

//Create SDL_Surfaces and SDL_Window
SDL_Surface *spriteSurface = NULL;
SDL_Surface *screenSurface = NULL;
SDL_Window *window = NULL;

//Declare functions
bool init();
void drawSprite(int spriteDstX,int spriteDstY,SDL_Surface *sprite,SDL_Surface *screen);
bool loadMedia();
void close();

int main(int argc,char* args[])
{
    init();
    SDL_Event event;
    spriteSurface = SDL_LoadBMP("sprite.bmp");

    //Create game loop
    while(!quit)
    {
        while(SDL_PollEvent(&event)!= 0)
        {
            if (event.type == SDL_QUIT)
            {
                quit = true;
            }
            else if (event.type == SDL_KEYDOWN)
            {
                switch(event.key.keysym.sym)
                {
                case SDLK_UP:
                spriteDstY-=10;
                break;
                case SDLK_DOWN:
                spriteDstY+=10;
                break;
                case SDLK_LEFT:
                spriteDstX-=10;
                break;
                case SDLK_RIGHT:
                spriteDstX+=10;
                break;
                }
            }
        }
        drawSprite(spriteDstX,spriteDstY,spriteSurface,screenSurface);
        SDL_UpdateWindowSurface(window);

    }
    close();
    return 0;
}

bool init()
{
    succsess = true;
    //Create window
    window = SDL_CreateWindow("SDL TEST",
                              SDL_WINDOWPOS_UNDEFINED,
                              SDL_WINDOWPOS_UNDEFINED,
                              SCREEN_WIDTH,
                              SCRREN_HEIGHT,
                              SDL_WINDOW_SHOWN);
    if (window == 0)
    {
        succsess = false;
        std::cout << "Could not create window: " << SDL_GetError();
    }
    else
    {
    screenSurface = SDL_GetWindowSurface(window);
    //Give the background a color
    SDL_FillRect(screenSurface,NULL,SDL_MapRGB(screenSurface->format,colorRed,colorGreen,colorBlue));
    }
    return succsess;
}


void close()
{
    SDL_FreeSurface(spriteSurface);
    SDL_FreeSurface(screenSurface);
    SDL_DestroyWindow(window);
    SDL_Quit();
}


void drawSprite(int spriteDstX,int spriteDstY,SDL_Surface *sprite,SDL_Surface *screen)
{
    SDL_Rect spriteDst;
    spriteDst.x = spriteDstX;
    spriteDst.y = spriteDstY;

    SDL_BlitSurface(sprite,NULL,screen,&spriteDst);
}


Sorry for bad english, and thanks for answers


Last edited on
closed account (10X9216C)
You have to clear the depth and color buffers every frame otherwise you'll keep what was previously drawn.

https://wiki.libsdl.org/SDL_RenderClear
You have to clear the depth and color buffers every frame otherwise you'll keep what was previously drawn.


But how could I do that with surfaces... must I change my surfaces to textures?

If you could give me an example that would be nice.

Thanks for fast reply :D

Draw the background on top of the old sprite to make it disappear.
Chances are, if you're using software rendering, you're doing t wrong. As peter said, try drawing the background over the sprite.

If your program doesn't use a background, try using SDL_CreateRGBSurface to create an empty surface, fill that with black pixels, and blit that to the screen before blitting anything else.
... or just use SDL_FillRect like you do in your init() function. No point creating a surface if all pixels are going to have the same colour anyway.
@Peter87 and @Avilius

It works perfect with drawing the background on top of the old sprite. Thanks for fast reply and your help :D
Last edited on
No point creating a surface if all pixels are going to have the same colour anyway.


Also, no point in surface blitting in 2014
Topic archived. No new replies allowed.