From 7eb86f9389fd4a112ff5a0017e7d79651ee5c30e Mon Sep 17 00:00:00 2001 From: Ritchie Cunningham Date: Fri, 12 Sep 2025 20:41:49 +0100 Subject: [PATCH] refactor: Add everything to main Bettola class. --- CMakeLists.txt | 4 +- src/bettola.cpp | 128 ++++++++++++++++++++++++++++++++++++++++++++++++ src/bettola.h | 26 ++++++++++ src/main.cpp | 106 ++------------------------------------- 4 files changed, 160 insertions(+), 104 deletions(-) create mode 100644 src/bettola.cpp create mode 100644 src/bettola.h diff --git a/CMakeLists.txt b/CMakeLists.txt index 1bf8cb4..1b07cab 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -11,7 +11,9 @@ find_package(SDL3 REQUIRED) find_package(GLEW REQUIRED) find_package(OpenGL REQUIRED) -add_executable(bettola src/main.cpp) +# Will need to clean build each time you add a new file though -.- +file(GLOB_RECURSE SOURCES "src/*.cpp") +add_executable(bettola ${SOURCES}) target_link_libraries(bettola PRIVATE SDL3::SDL3 GLEW::glew OpenGL::GL) diff --git a/src/bettola.cpp b/src/bettola.cpp new file mode 100644 index 0000000..7d02714 --- /dev/null +++ b/src/bettola.cpp @@ -0,0 +1,128 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +#include "bettola.h" + +/* Dacav's resolution ;) */ +const int SCREEN_WIDTH = 800; +const int SCREEN_HEIGHT = 600; + +Bettola::Bettola(void) : + _is_running(false), + _window(nullptr), + _gl_context(nullptr) {} + +Bettola::~Bettola(void) { + if(_gl_context) { + SDL_GL_DestroyContext(_gl_context); + } + + if(_window) { + SDL_DestroyWindow(_window); + } + + SDL_Quit(); +} + +int Bettola::run(void) { + if(!init_sdl()) return -1; + if(!create_window()) return -1; + if(!create_gl_context()) return -1; + if(!init_glew()) return -1; + + glViewport(0,0,SCREEN_WIDTH, SCREEN_HEIGHT); + + _is_running = true; + + Uint64 perf_freq = SDL_GetPerformanceFrequency(); + Uint64 last_count = SDL_GetPerformanceCounter(); + double delta_time = 0; + + while(_is_running) { + Uint64 current_counter = SDL_GetPerformanceCounter(); + delta_time = (double)(current_counter-last_count) / (double)perf_freq; + last_count = current_counter; + + process_events(); + update(delta_time); + render(); + } + + return 0; +} + +void Bettola::process_events(void) { + SDL_Event event; + while(SDL_PollEvent(&event)) { + if(event.type == SDL_EVENT_QUIT) { + _is_running = false; + } + } +} + +void Bettola::update(double dt) { + static char window_title[256]; + static double time_since_title_update = 0.0; + + time_since_title_update += dt; + if(time_since_title_update > 0.25) { + snprintf(window_title, 256, "Bettola - FPS %.2f", 1.0/dt); + SDL_SetWindowTitle(_window, window_title); + time_since_title_update = 0.0; + } +} + +void Bettola::render(void) { + glClearColor(0.1f, 0.1f, 0.3f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); + SDL_GL_SwapWindow(_window); +} + +bool Bettola::init_sdl(void) { + if(!SDL_Init(SDL_INIT_VIDEO)) { + fprintf(stderr, "Failed to iniit SDL! SDL ERROR: %s\n", SDL_GetError()); + return false; + } + return true; +} + +bool Bettola::init_glew(void) { + glewExperimental = GL_TRUE; + GLenum glew_error = glewInit(); + if(glew_error != GLEW_OK) { + fprintf(stderr, "Failed to init GLEW! %s\n", glewGetErrorString(glew_error)); + return false; + } + return true; +} + +bool Bettola::create_window(void) { + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); + SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); + + if(!_window) { + /* Don't you dare f.ckin fail to open!!!!! */ + _window = SDL_CreateWindow("Bettola Makes No Sense!", SCREEN_WIDTH, SCREEN_HEIGHT, + SDL_WINDOW_OPENGL); + fprintf(stderr, "Failed to create window! SDL_ERROR: %s\n", SDL_GetError()); + } + + return true; +} + +bool Bettola::create_gl_context(void) { + _gl_context = SDL_GL_CreateContext(_window); + if(!_gl_context) { + fprintf(stderr, "Failed to create OpenGL context! SDL_ERROR: %s\n", SDL_GetError()); + return false; + } + return true; +} + diff --git a/src/bettola.h b/src/bettola.h new file mode 100644 index 0000000..05b6068 --- /dev/null +++ b/src/bettola.h @@ -0,0 +1,26 @@ +#pragma once + +#include + +class Bettola { +public: + Bettola(void); + ~Bettola(void); + + int run(void); + +private: + void process_events(void); + void update(double dt); + void render(void); + + bool init_sdl(void); + bool init_glew(void); + bool create_window(void); + bool create_gl_context(void); + + bool _is_running; + + SDL_Window* _window; + SDL_GLContext _gl_context; +}; diff --git a/src/main.cpp b/src/main.cpp index c084688..b3fb0a3 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,105 +1,5 @@ -#include -#include -#include - -const int SCREEN_WIDTH = 800; -const int SCREEN_HEIGHT = 600; - +#include "bettola.h" int main(int argc, char* argv[]) { - /* Initialise SDL. */ - if(!SDL_Init(SDL_INIT_VIDEO)) { - fprintf(stderr, "Failed to init SDL! SDL ERROR: %s\n", SDL_GetError()); - return -1; - } - - /* OpenGL context attributes */ - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3); - SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE); - - /* Create the window. */ - SDL_Window* window = SDL_CreateWindow( - "Bettola Makes No Sense!", - SCREEN_WIDTH, - SCREEN_HEIGHT, - SDL_WINDOW_OPENGL - ); - - if(!window) { - fprintf(stderr, "Failed to create window! SDL_ERROR: %s\n", SDL_GetError()); - SDL_Quit(); - return -1; - } - - /* OpenGL context. */ - SDL_GLContext gl_context = SDL_GL_CreateContext(window); - if(!gl_context) { - fprintf(stderr, "Failed to create OpenGL context! SDL_ERROR: %s\n", SDL_GetError()); - SDL_DestroyWindow(window); - SDL_Quit(); - return -1; - } - - /* GLEW init - Must be done AFTER creating the GL context. */ - glewExperimental = GL_TRUE; - GLenum glew_error = glewInit(); - if(glew_error != GLEW_OK) { - fprintf(stderr, "Failed to init GLEW! %s\n", glewGetErrorString(glew_error)); - SDL_GL_DestroyContext(gl_context); - SDL_DestroyWindow(window); - SDL_Quit(); - return -1; - } - - /* Set viewport. */ - glViewport(0,0,SCREEN_WIDTH,SCREEN_HEIGHT); - - /* Main game loop.. For now. */ - bool is_running = true; - SDL_Event event; - - /* Delta time calculations. */ - Uint64 perf_frequency = SDL_GetPerformanceFrequency(); - Uint64 last_counter = SDL_GetPerformanceCounter(); - double delta_time = 0; - - /* FPS counter for window title. */ - char window_title[256]; - double time_since_title_update = 0.0; - - while(is_running) { - /* Calc delta time. */ - Uint64 current_counter = SDL_GetPerformanceCounter(); - delta_time = (double)(current_counter-last_counter) / (double)perf_frequency; - last_counter = current_counter; - - /* Event handling. */ - while(SDL_PollEvent(&event)) { - if(event.type == SDL_EVENT_QUIT) { - is_running = false; - } - } - - /* Update window title with FPS counter. */ - time_since_title_update += delta_time; - if(time_since_title_update > 0.25) { - snprintf(window_title, 256, "Bettola - FPS: %.2f", 1.0/delta_time); - SDL_SetWindowTitle(window, window_title); - time_since_title_update = 0.0; - } - - /* Render that b*tch. */ - glClearColor(0.1f, 0.1f, 0.3f, 1.0f); /* Dark blue or something. */ - glClear(GL_COLOR_BUFFER_BIT); - - /* Swap the buffers. */ - SDL_GL_SwapWindow(window); - } - - /* Clean up after ourselves. */ - SDL_GL_DestroyContext(gl_context); - SDL_DestroyWindow(window); - SDL_Quit(); - - return 0; + Bettola bettola; + return bettola.run(); }