窗口在SDL2中随时间改变颜色

时间:2016-11-01 07:11:16

标签: c++ colors window sdl-2

我在SDL2中创建了一个以红色打开的窗口。我需要窗口在几秒钟后改变颜色,并且只要窗口打开就继续这样做。因此窗口可能会打开红色,停留五秒钟然后变为绿色五秒钟然后变为蓝色五秒钟然后循环回红色并再次开始整个过程​​。

int WindowOpen() {

bool quit = false;

SDL_Window *window;                    // Declare a pointer

SDL_Init(SDL_INIT_VIDEO);              // Initialize SDL2

                                       // Create an application window with the following settings:
window = SDL_CreateWindow(
    "My SDL2 window",                  // window title
    SDL_WINDOWPOS_UNDEFINED,           // initial x position
    SDL_WINDOWPOS_UNDEFINED,           // initial y position
    640,                               // width, in pixels
    480,                               // height, in pixels
    SDL_WINDOW_OPENGL                  // flags - see below
    );

SDL_Renderer *renderer = NULL;
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); 
SDL_Event* MainEvent = new SDL_Event();
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);

// Check that the window was successfully created
if (window == NULL) {
    // In the case that the window could not be made...
    printf("Could not create window: %s\n", SDL_GetError());
    return 1;
}

SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);//red. Was testing to see if window would open red and change to green then blue but this doesn't work.
//SDL_Delay(3000);
//SDL_SetRenderDrawColor(renderer, 300, 150, 0, 155);//green
//SDL_Delay(3000);
//SDL_SetRenderDrawColor(renderer, 129, 150, 500, 105);//blue

while (quit == false && MainEvent->type != SDL_QUIT) { //While quit is false, run window and renderer.
    SDL_PollEvent(MainEvent);
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
}


// The window is open: could enter program loop here (see SDL_PollEvent())

//SDL_Delay(3000);  // Pause execution for 3000 milliseconds, for example

// Close and destroy the window
SDL_DestroyWindow(window);
SDL_DestroyRenderer(renderer);
delete MainEvent;

// Clean up
SDL_Quit();

}

1 个答案:

答案 0 :(得分:0)

SDL_SetRenderDrawColor只是设置渲染器绘制的颜色,如果你要求的话。要使用该颜色填充后台缓冲区,您应该使用SDL_RenderClear进行后续处理。填充后备缓冲区,然后在屏幕上绘制它,您需要调用SDL_RenderPresent

while (quit == false && MainEvent->type != SDL_QUIT) { //While quit is false, run window and renderer.
    SDL_SetRenderDrawColor(renderer, 255, 0, 0, 255);//red. Was testing to see if window would open red and change to green then blue but this doesn't work.
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
    SDL_Delay(3000);
    SDL_SetRenderDrawColor(renderer, 300, 150, 0, 155);//green
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
    SDL_Delay(3000);
    SDL_SetRenderDrawColor(renderer, 129, 150, 500, 105);//blue
    SDL_RenderClear(renderer);
    SDL_RenderPresent(renderer);
    SDL_Delay(3000);

    SDL_PollEvent(MainEvent);
}

将主循环更改为类似于您想要的东西,但延迟时间为3秒。此外,这种方式您的主循环将花费9秒执行,这意味着退出的事件处理可能需要9秒。这不是很好,但修复它是留给读者的练习。