我正在遵循this教程,作者说代码应打开一个窗口,并用白色矩形填充该窗口的表面。
但是,我所看到的只是一个黑色的窗户。我试图更改颜色,但它仍然绘制黑色屏幕。

这是我正在使用的代码:

#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;
}


这是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)


这就是我运行程序时出现的内容:

最佳答案

您需要在SDL中手动更新窗口,也许是这样的:

   SDL_UpdateWindowSurface(window);

就个人而言,我认为cocos2dx比SDL更方便。

关于c++ - SDL2无法在屏幕上绘制白色矩形,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/30301124/

10-10 21:15