Attempted to move window to the world of arenas

This commit is contained in:
Warwick 2025-11-27 21:03:26 +00:00
parent 311facef8f
commit 538d7db406
3 changed files with 30 additions and 19 deletions

View file

@ -57,8 +57,10 @@ int main(int argc, char *argv[]) {
*/
int main(int argc, char *argv[]) {
wn_window window = {0};
if (!wn_window_init(&window)) {
Arena *global_arena = arena_init(NULL, 0);
wn_window *window = wn_window_init(global_arena);
if (wn_window_init(global_arena) == NULL) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize wn_window");
return EXIT_FAILURE;
}
@ -122,13 +124,13 @@ int main(int argc, char *argv[]) {
bool wants_running = true;
while (wants_running) {
// TODO: Create event handler files
while (SDL_PollEvent(&window.event)) {
switch (window.event.type) {
while (SDL_PollEvent(&window->event)) {
switch (window->event.type) {
case SDL_EVENT_QUIT:
wants_running = false;
break;
case SDL_EVENT_KEY_DOWN:
if (window.event.key.key == SDLK_ESCAPE) {
if (window->event.key.key == SDLK_ESCAPE) {
wants_running = false;
}
break;
@ -158,7 +160,7 @@ int main(int argc, char *argv[]) {
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
// glBindVertexArray(0); // no need to unbind it every time
wn_swapwindow(&window);
wn_swapwindow(window);
}
glDeleteVertexArrays(1, &VAO);
@ -166,7 +168,6 @@ int main(int argc, char *argv[]) {
glDeleteBuffers(1, &EBO);
wn_shader_deinit(shader);
wn_window_deinit(&window);
wn_window_deinit(global_arena);
return EXIT_SUCCESS;
}

View file

@ -1,17 +1,18 @@
#include "window.h"
#include "SDL3/SDL_init.h"
#include "arena_allocator.h"
#include <GL/glew.h>
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_opengl.h>
bool wn_window_init(wn_window *window) {
wn_window *wn_window_init(Arena *arena) {
wn_window *window = arena_alloc(arena, sizeof(wn_window));
window->window = SDL_CreateWindow("Hello SDL3", 320, 240,
SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
if (!SDL_Init(SDL_INIT_VIDEO)) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't initialize SDL: %s",
SDL_GetError());
return false;
return NULL;
}
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
@ -22,14 +23,14 @@ bool wn_window_init(wn_window *window) {
if (!window->window) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION, "Couldn't create window: %s",
SDL_GetError());
return false;
return NULL;
}
window->glcontext = SDL_GL_CreateContext(window->window);
if (!window->glcontext) {
SDL_LogError(SDL_LOG_CATEGORY_APPLICATION,
"Couldn't create OpenGL Context: %s", SDL_GetError());
return false;
return NULL;
}
GLenum glewError = glewInit();
@ -47,12 +48,20 @@ bool wn_window_init(wn_window *window) {
SDL_SetWindowRelativeMouseMode(window->window, true);
SDL_GetRelativeMouseState(NULL, NULL);
return true;
// add deinit function to arena deinit stack
ArenaDeinitTask deinitTask = (ArenaDeinitTask){.func_param = (void *)window,
.func_ptr = *wn_window_deinit,
.next = NULL};
arena_deinit_task_push(arena, deinitTask);
return window;
}
void wn_window_deinit(wn_window *window) {
SDL_GL_DestroyContext(window->glcontext);
SDL_DestroyWindow(window->window);
void wn_window_deinit(void *window) {
wn_window *cast_window = (wn_window *)window;
SDL_GL_DestroyContext(cast_window->glcontext);
SDL_DestroyWindow(cast_window->window);
SDL_Quit();
}

View file

@ -3,6 +3,7 @@
#include "SDL3/SDL_events.h"
#include "SDL3/SDL_render.h"
#include "SDL3/SDL_video.h"
#include "arena_allocator.h"
#include <stdbool.h>
struct wn_window_s {
@ -13,7 +14,7 @@ struct wn_window_s {
};
typedef struct wn_window_s wn_window;
bool wn_window_init(wn_window *window);
void wn_window_deinit(wn_window *window);
wn_window *wn_window_init(Arena *arena);
void wn_window_deinit(void *window);
void wn_swapwindow(wn_window *window);