【发布时间】:2016-11-01 07:11:16
【问题描述】:
我在 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();
}
【问题讨论】: