Change background of SDL2 window?

I can create an SDL2 window, but I don't know how to change the background color of that 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 the background color of this window to black?

+3


source to share


3 answers


You have to set the color of the picture with SDL_SetRenderDrawColor

and then use SDL_RenderClear

:

(the code comes directly from the 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;
}

      

+11


source


If you are not using the SDL2 renderer, you can do this:

Just call:

SDL_GL_SwapWindow(window);

      



After:

SDL_GL_MakeCurrent(window, context);

      

All this! You now have a black screen.

0


source


Try it:

SDL_Window *wind;
SDL_Surface *windSurface = NULL
void main()
{
    windSurface = SDL_GetWindowSurface(wind);  
   wind = SDL_CreateWindow("TEST", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 640, 480, SDL_WINDOW_SHOWN);
    SDL_FillRect(windSurface, NULL, SDL_MapRGB(windSurface->format, 240, 200, 112));
        SDL_UpdateWindowSurface(wind);
    SDL_Delay(3000);
}

      

0


source







All Articles