The createWindow
function initializes SDL and OpenGL, creates a window with the title "C++ SDL2 Window | OpenGL" and size 1000x800, and enables double buffering and a core profile, before returning the window ID.
This function uses SDL functions such as SDL_Init
, SDL_GL_SetAttribute
, SDL_GL_CreateContext
, SDL_CreateWindow
, and SDL_GL_SwapWindow
for event handling and window management.
npm run import -- "opengl context"
#include <iostream>
//#include <glad/glad.h>
#include <SDL.h>
#if __cplusplus
extern "C" {
#endif
static SDL_Window* window;
int createWindow(void) {
window = nullptr;
int screen_width = 1000;
int screen_height = 800;
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cout << "SDL failed initialization. " << SDL_GetError();
return 0;
} else {
std::cout << "SDL successfully initialized.";
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
window = SDL_CreateWindow("C++ SDL2 Window | OpenGL",
0,
0,
screen_width,
screen_height,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
SDL_GLContext context;
context = SDL_GL_CreateContext(window);
bool done = false;
while(!done) {
//glViewport(0, 0, screen_width, screen_height);
SDL_Event event;
while(SDL_PollEvent(&event)){
if(event.type == SDL_QUIT) {
done = true;
}
}
// glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
SDL_GL_SwapWindow(window);
done = true;
}
//SDL_DestroyWindow(window);
//SDL_Quit();
return SDL_GetWindowID(window);
}
#if __cplusplus
}
#endif
#include
#include
// Forward declaration of SDL window function
SDL_Window* createWindow();
int main() {
int windowId = createWindow();
if (windowId!= 0) {
std::cout << "Window created successfully with ID: " << windowId << std::endl;
} else {
std::cout << "Failed to create window." << std::endl;
}
return 0;
}
// Function to create an SDL window with OpenGL context
SDL_Window* createWindow() {
// Initialize SDL
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
std::cerr << "SDL failed initialization. Error: " << SDL_GetError() << std::endl;
return nullptr;
}
// Create an SDL window with OpenGL context
SDL_Window* window = SDL_CreateWindow("C++ SDL2 Window | OpenGL",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1000, 800,
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL);
if (!window) {
std::cerr << "Failed to create window. Error: " << SDL_GetError() << std::endl;
SDL_Quit();
return nullptr;
}
// Create an OpenGL context
SDL_GLContext context = SDL_GL_CreateContext(window);
if (!context) {
std::cerr << "Failed to create OpenGL context. Error: " << SDL_GetError() << std::endl;
SDL_DestroyWindow(window);
SDL_Quit();
return nullptr;
}
// Event loop
bool running = true;
while (running) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = false;
}
}
SDL_GL_SwapWindow(window);
}
// Clean up
SDL_GL_DeleteContext(context);
SDL_DestroyWindow(window);
SDL_Quit();
// Return the window ID
return SDL_GetWindowID(window);
}
Code Breakdown
#include <iostream>
: Includes the iostream library for input/output operations.#include <SDL.h>
: Includes the SDL library for handling graphics and user interface.Creates a window with SDL and OpenGL initialized.
Returns the ID of the created window.
SDL_Init(SDL_INIT_VIDEO)
. If initialization fails, it prints an error message and returns 0.SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL
SDL_GL_CreateContext
.SDL_QUIT
event to exit the loop.SDL_GL_SwapWindow
.SDL_Init
SDL_GL_SetAttribute
SDL_GL_CreateContext
SDL_CreateWindow
SDL_GL_SwapWindow
SDL_GetError
SDL_GetWindowID