0
我在SDL2中創建了一個以紅色打開的窗口。我需要窗口在幾秒鐘之後改變顏色,並且只要窗口打開,就繼續這樣做。因此,窗戶可能會打開紅色,停留五秒鐘,然後變爲綠色五秒鐘,然後變爲藍色五秒鐘,然後循環回紅色並再次開始整個過程。在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();
}