Unwanted Trailing Image SDL2/C++

I run this program and get trailing copies of the currentImage. I don't know what I am doing wrong. I think there is a function that clears the old image as the new one is blitted. But I can't remember. Am I wrong. Am I just not preparing the image correctly?

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
#include <iostream>
#include <SDL.h>

SDL_Surface *optimizedSurface(std::string filePath, SDL_Surface *windowSurface)
{
    SDL_Surface *optimizedSurface = nullptr;
    SDL_Surface *surface = SDL_LoadBMP(filePath.c_str());
    if(surface == NULL)
        std::cout << "Surface Error: " << SDL_GetError() << std::endl;
    else
    {
        optimizedSurface = SDL_ConvertSurface(surface, windowSurface->format, 0);
        if(optimizedSurface == NULL)
            std::cout << "OptimizedSurface Error: " << SDL_GetError() << std::endl;
    }
    
    SDL_FreeSurface(surface);
    return optimizedSurface;
    
}

int main(int argc, char** argv) 
{
    SDL_Window *window = nullptr; //Prepare a spot in memory for window
    SDL_Surface *windowSurface = nullptr; //Prepare a spot in memory for the windows builtin surface
    SDL_Surface *currentImage = nullptr;
    
    SDL_Init(SDL_INIT_VIDEO); //Make SDL set itself up for use on my system
        //Setup the window parameters
    window = SDL_CreateWindow("SDL MY CODING MADE DIFFICULT", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    windowSurface = SDL_GetWindowSurface(window); //Window Created
    currentImage = optimizedSurface("testsmall.bmp", windowSurface);
    
    SDL_Rect drawingRect;
    drawingRect.x = drawingRect.y = 60;
    drawingRect.w = 240;
    drawingRect.h = 180;
    
    bool isRunning = true;
    SDL_Event ev;
    //Start of game loop
    while(isRunning)
    {
        while(SDL_PollEvent(&ev) != 0)
        {
            else if(ev.type == SDL_KEYDOWN)
            {
                switch(ev.key.keysym.sym)
                {
                    case SDLK_LEFT:
                        drawingRect.x -= 20;
                        break;
                    case SDLK_RIGHT:
                        drawingRect.x += 20;
                        break;
                    case SDLK_x:
                        isRunning = false;
                        break;
                }
            }
        }
        
            //SDL_BlitSurface(currentImage, NULL, windowSurface, NULL); //place imageSurface on windowSurface
        
        //SDL_FreeSurface(currentImage);
        SDL_BlitScaled(currentImage, NULL, windowSurface, &drawingRect);
        SDL_UpdateWindowSurface(window); //Show updated surface on screen  
            
    }

    SDL_FreeSurface(currentImage);
    currentImage = nullptr;
    SDL_DestroyWindow(window);
    window = nullptr;
    
    SDL_Quit(); //Quit SDL2
    return 0;
}


Thanks for looking at my code. Please help.
Last edited on
You need to redraw the background. If the background is a single colour you can use SDL_FillRect.

1
2
// Makes the whole window surface black.
SDL_FillRect(windowSurface, nullptr, 0);

https://wiki.libsdl.org/SDL_FillRect
Last edited on
Thank you, for your response. :)
What do i do, if the background will be a picture? And if it is a picture, do I use SDL_FillRect for each Rect? Do I need to put each layer in a Rect?
You can draw the background using SDL_BlitSurface or SDL_BlitScaled the same way you do with your other image.
Thank you, Peter87.
Topic archived. No new replies allowed.