SDL2: "Simplest" way to change window BG color

I already searched google multiple times and got varying answers that were either in a completely different context in my problem, or pertained to an older version of SDL which uses surfaces instead of windows.

I'm trying to organize the code I've been getting from lazyfoo's SDL tutorials into more neatly-packaged wrapper classes so I can build up from them and make this traffic data iterator program that I won't go into detail about right now, but will probably need help with, too, seeing how I can't even make a window black.

This is a "continuation" from this thread:

http://www.cplusplus.com/forum/beginner/122519/

As in, I solved the problems in that thread, but then ran into more.

Anyway:

main.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
#include "Event.h"

using namespace Init;
using namespace Event;

//global
SDL_Window* gWindow = NULL;
SDL_Renderer* gRenderer = NULL;

//main execution
int main(int argc, char* args[])
{
    initAll(gWindow, gRenderer);
    handleEvent();
    return 0;
}


Init.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
#include <SDL.h>
#include <SDL_image.h>
#include <SDL_ttf.h>
#include <stdio.h>

namespace Init
{
    void initAll(SDL_Window* window, SDL_Renderer* renderer);
    bool initVid();
    void initVSync();
    void initLinTextFilt();
    bool initWindow(SDL_Window* winArg);
    bool initRenderer(SDL_Window* winArg, SDL_Renderer* rendArg);
    bool initTTF();
    bool initIMG();
};


Init.cpp
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
#include "Init.h"

//global
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;

//calls all other Initialization functions
void Init::initAll(SDL_Window* window, SDL_Renderer* renderer)
{
    initVid();
    initVSync();
    initLinTextFilt();
    initWindow(window);
    initRenderer(window, renderer);
    initTTF();
    initIMG();
}
//Initialize SDL Video subsystem
bool Init::initVid()
{
    bool success = true;
    if(!SDL_Init(SDL_INIT_VIDEO) < 0)
    {
        printf("SDL_INIT_VIDEO failed. SDL Error: %s\n", SDL_GetError());
        success = false;
    }
    return success;
}
//Initialize vertical synchronization
void Init::initVSync()
{
    if(!SDL_SetHint(SDL_HINT_RENDER_VSYNC, "1"))
    {
        printf("Warning: VSync not enabled.");
    }
}
//initialize linear texture filtering
void Init::initLinTextFilt()
{
    if(!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1"))
    {
        printf("Warning: Linear texture filtering not enabled.");
    }
}
//initialize window
bool Init::initWindow(SDL_Window* winArg)
{
    bool success = true;
    winArg = SDL_CreateWindow("Count++: Traffic Data Iterator", SDL_WINDOWPOS_CENTERED,
                              SDL_WINDOWPOS_CENTERED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
    if(winArg == NULL)
    {
        printf("Window could not be created! SDL error: %s\n", SDL_GetError());
        success = false;
    }
    return success;
}
//initialize renderer
bool Init::initRenderer(SDL_Window* winArg, SDL_Renderer* rendArg)
{
    bool success = true;
    rendArg = SDL_CreateRenderer(winArg, -1, SDL_RENDERER_ACCELERATED);
    if(rendArg == NULL)
    {
        printf("Renderer could not be created! SDL error: %s\n", SDL_GetError());
        success = false;
    }
    else
    {
        SDL_SetRenderDrawColor(rendArg, 0x00, 0x00, 0x00, 0xFF);
    }
    return success;
}
//Initialize TrueType Font subsystem
bool Init::initTTF()
{
    bool success = true;
    if( TTF_Init() == -1 )
    {
        printf( "SDL_ttf could not initialize! SDL_ttf Error: %s\n", TTF_GetError() );
        success = false;
    }
    return success;
}
//Initialize PNG loading subsystem
bool Init::initIMG()
{
    bool success = true;
    int imgFlags = IMG_INIT_PNG;
    if (!(IMG_Init(imgFlags)&imgFlags))
    {
        printf("SDL_Image could not initialize! SDL_Image Error: %s\n", IMG_GetError() );
        success = false;
    }
    return success;
}


Event.h
1
2
3
4
5
6
7
#include "Init.h"

namespace Event
{
    void close(SDL_Window* winArg, SDL_Renderer* rendArg);
    void handleEvent();
};


Event.cpp
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
#include "Event.h"

//close down initialized subsystems, destroy renderer, and window
void Event::close(SDL_Window* winArg, SDL_Renderer* rendArg)
{
//Destroy Window & Renderer
SDL_DestroyWindow(winArg);
SDL_DestroyRenderer(rendArg);
//close down SDL subsystems
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
//handle events in main loop
void Event::handleEvent()
{
       //main loop flag
        bool quit = false;
        //Event handler
        SDL_Event e;
        while(!quit)
        {
            while(SDL_PollEvent(&e)!=0)
            {
                //user requests quit
                if(e.type == SDL_QUIT)
                {
                    quit = true;
                }
            }
        }
}



I've tried clearing the renderer in the main loop after setting the render draw color to black. I've tried filling a black rect the size of the screen in the event loop after the quit check. I've tried doing this in a seperate Event member function.

Each time I fail I reset everything to look like this.

What am I doing wrong, and why does this first step of many seem so difficult?
Last edited on
Try filling the screen with SDL_FillRect after the SDL_PollEvent loop.
I actually did that before. The program compiled and ran w/o compile-time or link-time errors, but the window was still white.

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
#include "Event.h"

//close down initialized subsystems, destroy renderer, and window
void Event::close(SDL_Window* winArg, SDL_Renderer* rendArg)
{
//Destroy Window & Renderer
SDL_DestroyWindow(winArg);
SDL_DestroyRenderer(rendArg);
//close down SDL subsystems
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
//handle events in main loop
void Event::handleEvent(SDL_Renderer* rendArg)
{
       //main loop flag
        bool quit = false;
        //Event handler
        SDL_Event e;
        while(!quit)
        {
            while(SDL_PollEvent(&e)!=0)
            {
                //user requests quit
                if(e.type == SDL_QUIT)
                {
                    quit = true;
                }
            }
            SDL_Rect screenRect = {0, 0, 640, 480};
            SDL_RenderFillRect(rendArg, &screenRect);
            SDL_RenderPresent(rendArg);
        }
}


This is excluding the changes I had to make to the declaration of handleEvent, and its argument list, in main.cpp and Event.h, respectively.

Also, I find it annoying that I couldn't use my consts SCREEN_WIDTH and SCREEN_HEIGHT and had to resort to using magic consts. I know i can forward declare, but I still have trouble working out when I'm forward declaring, and when I'm just declaring a new variable with same name and type, but in a different scope.
Last edited on
Bump. Still stumped.

I'm gonna resort to loading a black image texture with the same dimensions as my screen if I can't find another way.

Try changing what SDL uses for it's backend. From what I know on Windows it uses DirectX by default, try switching to OpenGL.
Thanks for the advice Avilius but I'm not at all sure how to do that.

I tried searching for a guide just now but had difficulty finding results/coming up with the proper search terms.
Topic archived. No new replies allowed.