Change background of SDL2 window?
Asked Answered
M

5

7

I can create a SDL2 window but I don't know how to change background color of this window.

My code:

#include "SDL.h"

SDL_Window *window;

void main()
{
    window = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);

    SDL_Delay(3000);
}

How can I change background color of this window to black?

Mirabel answered 21/5, 2015 at 15:11 Comment(1)
I googled "sdl background". Google said (gamedev.net/topic/331639-setting-background-color-for-sdl). Generally, when googling doesn't help with something, check the something's documentation.Mournful
E
24

You should set drawing colour with SDL_SetRenderDrawColor and then use SDL_RenderClear:

(code comes directly from SDL wiki)

int main(int argc, char* argv[])
{
        SDL_Window* window;
        SDL_Renderer* renderer;

        // Initialize SDL.
        if (SDL_Init(SDL_INIT_VIDEO) < 0)
                return 1;

        // Create the window where we will draw.
        window = SDL_CreateWindow("SDL_RenderClear",
                        SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
                        512, 512,
                        0);

        // We must call SDL_CreateRenderer in order for draw calls to affect this window.
        renderer = SDL_CreateRenderer(window, -1, 0);

        // Select the color for drawing. It is set to red here.
        SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);

        // Clear the entire screen to our selected color.
        SDL_RenderClear(renderer);

        // Up until now everything was drawn behind the scenes.
        // This will show the new, red contents of the window.
        SDL_RenderPresent(renderer);

        // Give us time to see the window.
        SDL_Delay(5000);

        // Always be sure to clean up
        SDL_Quit();
        return 0;
}
Edward answered 21/5, 2015 at 18:4 Comment(0)
B
2

If you do not use SDL2 renderer you can do this:

Just call:

SDL_GL_SwapWindow(window);

After :

SDL_GL_MakeCurrent(window, context);

That's all! Now you have a black screen.

Blacksnake answered 20/9, 2018 at 10:50 Comment(0)
G
1
#include <SDL2/SDL.h>
#include <iostream>
using namespace std;

int main(){
    int width=512, height=512;
    SDL_Window *window=SDL_CreateWindow("Window", SDL_WINDOWPOS_CENTERED, 
    SDL_WINDOWPOS_CENTERED, width, height, SDL_WINDOW_RESIZABLE);

    if(window==NULL)
        cout << "There was an error while initializing the window" << endl << SDL_GetError << endl;

    SDL_Event event;
    bool running=true;

    while(running){
        while(SDL_PollEvent(&event)){
            if(event.type==SDL_QUIT){
                running=false;
                break;
            }
        }
        SDL_GetWindowSize(window, &width, &height);

        SDL_Surface *surface=SDL_GetWindowSurface(window);
        Uint32 skyblue=SDL_MapRGB(surface->format, 65,193,193);
        SDL_FillRect(surface, NULL, skyblue);
        SDL_UpdateWindowSurface(window);
    }

    SDL_DestroyWindow(window);
    SDL_Quit();
}
Groggery answered 28/1, 2020 at 17:48 Comment(0)
K
0

Note, none of these solutions work properly with OpenGL. The question was how to init the window with predefined color, not how to paint the first frame. There's still a blink of white color before the first frame is painted.

Kape answered 24/10, 2022 at 13:33 Comment(0)
L
-1

Try this one :

SDL_Window *wind;
SDL_Surface *windSurface = NULL
void main()
{
    wind = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    windSurface = SDL_GetWindowSurface(wind);  
    SDL_FillRect(windSurface, NULL, SDL_MapRGB(windSurface->format, 240, 200, 112));
        SDL_UpdateWindowSurface(wind);
    SDL_Delay(3000);
}
 
Linseylinseywoolsey answered 19/4, 2019 at 20:39 Comment(1)
My downvote: you are calling SDL_GetWindowSurface with unitialized wind as argument. Afterwards you initialize it with SDL_CreateWindow. If anything, the order of those operations is reversed.Inveterate

© 2022 - 2024 — McMap. All rights reserved.