cristid9
cristid9

Reputation: 1178

SDL2 fails to draw a white rectangle on the screen

I am following this tutorial, and the author said that the code should open a window and fill the surface of that window with a white rectangle. But, all I see is a black window. I have tried to change the colors, but it still draws a black screen.

Here is the code that I am using:

#include <cstdio>
#include <SDL2/SDL.h>

int main(int argc, char *argv[])
{
    const int width  = 500;
    const int height = 500;

    SDL_Window* window          = NULL;
    SDL_Surface* windowSurface = NULL;

    if(SDL_Init(SDL_INIT_VIDEO) < -1)
    {
        printf("[Error at initialization] %s", SDL_GetError());
    } 
    else 
    {
        window = SDL_CreateWindow("Test window", SDL_WINDOWPOS_UNDEFINED,
                SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);

        if(window == NULL) 
        {
            printf("[Error at creating the window] %s", SDL_GetError());
        }
        else 
        {
            windowSurface = SDL_GetWindowSurface(window);

            SDL_FillRect(windowSurface, NULL, SDL_MapRGB(windowSurface->format,
                      0xFF, 0xFF, 0xFF)); 

            SDL_Delay(5000);
        }

    }

    SDL_DestroyWindow(window);

    SDL_Quit();

    return 0;
}

And here is the makefile:

OBJS = try1.cpp

CC = g++

LINKER_FLAGS = -lSDL2

COMPOILER_FLAGS = -W


OBJS_NAMES = 01_hello_SDL

all:
    g++ $(OBJS) $(COMPILER_FLAGS) $(LINKER_FLAGS) -o $(OBJS_NAMES)

And that is what appears when I run the program: What I see when I run the program

Upvotes: 0

Views: 271

Answers (1)

Nya
Nya

Reputation: 310

You need to update the window manually in SDL, maybe something like this:

   SDL_UpdateWindowSurface(window);

Personly speaking, I think cocos2d-x is more convenient than SDL.

Upvotes: 3

Related Questions