Creating Multiple Windows with SDL2
How can I create multiple windows in an SDL2 application?
SDL2 allows you to create multiple windows within a single application. Here's an example of how to create two windows:
#include <SDL.h>
int main(int argc, char* argv[]) {
SDL_Init(SDL_INIT_VIDEO);
SDL_Window* window1{SDL_CreateWindow(
"Window 1",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
800, 600, SDL_WINDOW_SHOWN
)};
SDL_Window* window2{SDL_CreateWindow(
"Window 2",
SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED,
400, 300, SDL_WINDOW_SHOWN
)};
SDL_Event event;
bool quit = false;
while (!quit) {
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
quit = true;
}
}
}
SDL_DestroyWindow(window1);
SDL_DestroyWindow(window2);
SDL_Quit();
return 0;
}
In this example, we create two windows using SDL_CreateWindow
, each with different titles and sizes. The windows are displayed simultaneously, and the event loop handles events for both windows.
Remember to destroy each window using SDL_DestroyWindow
before quitting the application.
You can also create and manage windows dynamically during the application's runtime based on your specific requirements.
Building SDL2 from Source (GCC and Make)
This guide walks you through the process of compiling SDL2, SDL_image, and SDL_ttf libraries from source