sdl and opengl contexts
This commit is contained in:
44
src/engine/opengl.cpp
Normal file
44
src/engine/opengl.cpp
Normal file
@@ -0,0 +1,44 @@
|
||||
#include "engine/opengl.hpp"
|
||||
|
||||
// OpenGLContext Constructor
|
||||
OpenGLContext::OpenGLContext(SDLWindow& window): window(window), lastTime(SDL_GetTicks()), frameTime(0.0f) {
|
||||
glContext = SDL_GL_CreateContext(window.getSDLWindow());
|
||||
if (!glContext) {
|
||||
// Handle context creation error
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create OpenGL context: %s", SDL_GetError());
|
||||
return;
|
||||
}
|
||||
SDL_GL_MakeCurrent(window.getSDLWindow(), glContext);
|
||||
}
|
||||
|
||||
// OpenGLContext Destructor
|
||||
OpenGLContext::~OpenGLContext() {
|
||||
if (glContext) {
|
||||
SDL_GL_DestroyContext(glContext);
|
||||
}
|
||||
}
|
||||
|
||||
// Check if Initialized
|
||||
bool OpenGLContext::isInitialized() const {
|
||||
return glContext != nullptr;
|
||||
}
|
||||
|
||||
// Make Context Current
|
||||
void OpenGLContext::makeCurrent() {
|
||||
SDL_GL_MakeCurrent(window.getSDLWindow(), glContext);
|
||||
}
|
||||
|
||||
// Swap Buffers
|
||||
void OpenGLContext::swapBuffers() {
|
||||
SDL_GL_SwapWindow(window.getSDLWindow());
|
||||
|
||||
// Update frame time
|
||||
Uint32 currentTime = SDL_GetTicks();
|
||||
frameTime = (currentTime - lastTime) / 1000.0f;
|
||||
lastTime = currentTime;
|
||||
}
|
||||
|
||||
// Get Frame Time
|
||||
float OpenGLContext::getFrameTime() {
|
||||
return frameTime;
|
||||
}
|
||||
72
src/engine/sdl.cpp
Normal file
72
src/engine/sdl.cpp
Normal file
@@ -0,0 +1,72 @@
|
||||
#include "engine/sdl.hpp"
|
||||
|
||||
// SDLWindow Constructor
|
||||
SDLWindow::SDLWindow(const char* title, int width, int height, SDL_WindowFlags flags): running(true) {
|
||||
if (!SDL_Init(SDL_INIT_VIDEO)) {
|
||||
// Handle initialization error
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to initialize SDL: %s", SDL_GetError());
|
||||
running = false;
|
||||
return;
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow(title, width, height, flags);
|
||||
if (!window) {
|
||||
// Handle window creation error
|
||||
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Failed to create SDL window: %s", SDL_GetError());
|
||||
SDL_Quit();
|
||||
running = false;
|
||||
}
|
||||
|
||||
return;
|
||||
}
|
||||
|
||||
// SDLWindow Destructor
|
||||
SDLWindow::~SDLWindow()
|
||||
{
|
||||
if (window) {
|
||||
SDL_DestroyWindow(window);
|
||||
}
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
// Poll Events
|
||||
void SDLWindow::pollEvents() {
|
||||
while (SDL_PollEvent(&event)) {
|
||||
if (event.type == SDL_EVENT_QUIT) {
|
||||
running = false;
|
||||
}
|
||||
if (eventCallback) {
|
||||
eventCallback(event);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Set Event Callback
|
||||
void SDLWindow::setEventCallback(void (*callback)(const SDL_Event&)) {
|
||||
eventCallback = callback;
|
||||
}
|
||||
|
||||
// Check if Window is Running
|
||||
bool SDLWindow::isRunning() const {
|
||||
return running;
|
||||
}
|
||||
|
||||
// Show Window
|
||||
void SDLWindow::showWindow() {
|
||||
SDL_ShowWindow(window);
|
||||
}
|
||||
|
||||
// Hide Window
|
||||
void SDLWindow::hideWindow() {
|
||||
SDL_HideWindow(window);
|
||||
}
|
||||
|
||||
// Set Window Position
|
||||
void SDLWindow::setPosition(int x, int y) {
|
||||
SDL_SetWindowPosition(window, x, y);
|
||||
}
|
||||
|
||||
// Return raw SDL_Window pointer
|
||||
SDL_Window* SDLWindow::getSDLWindow() const {
|
||||
return window;
|
||||
}
|
||||
19
src/main.cpp
19
src/main.cpp
@@ -0,0 +1,19 @@
|
||||
#include "engine/sdl.hpp"
|
||||
#include "engine/opengl.hpp"
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
SDLWindow window("My SDL Application", 800, 600);
|
||||
if (!window.isRunning()) { return -1; }
|
||||
OpenGLContext glContext(window);
|
||||
if (!glContext.isInitialized()) { return -1; }
|
||||
|
||||
window.setPosition(SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED);
|
||||
window.showWindow();
|
||||
while (window.isRunning()) {
|
||||
window.pollEvents();
|
||||
// Application logic and rendering would go here
|
||||
glContext.swapBuffers();
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
Reference in New Issue
Block a user