refactor: Add everything to main Bettola class.

This commit is contained in:
Ritchie Cunningham 2025-09-12 20:41:49 +01:00
parent 557211c505
commit 7eb86f9389
4 changed files with 160 additions and 104 deletions

View File

@ -11,7 +11,9 @@ find_package(SDL3 REQUIRED)
find_package(GLEW REQUIRED) find_package(GLEW REQUIRED)
find_package(OpenGL 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) target_link_libraries(bettola PRIVATE SDL3::SDL3 GLEW::glew OpenGL::GL)

128
src/bettola.cpp Normal file
View File

@ -0,0 +1,128 @@
#include <GL/glew.h>
#include <SDL3/SDL_error.h>
#include <SDL3/SDL_stdinc.h>
#include <SDL3/SDL_timer.h>
#include <SDL3/SDL_video.h>
#include <chrono>
#include <cstddef>
#include <cstdio>
#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;
}

26
src/bettola.h Normal file
View File

@ -0,0 +1,26 @@
#pragma once
#include <SDL3/SDL.h>
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;
};

View File

@ -1,105 +1,5 @@
#include <SDL3/SDL.h> #include "bettola.h"
#include <GL/glew.h>
#include <cstdio>
const int SCREEN_WIDTH = 800;
const int SCREEN_HEIGHT = 600;
int main(int argc, char* argv[]) { int main(int argc, char* argv[]) {
/* Initialise SDL. */ Bettola bettola;
if(!SDL_Init(SDL_INIT_VIDEO)) { return bettola.run();
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;
} }