refactor file structure

This commit is contained in:
2026-10-06 12:07:12 +02:00
parent 6ee6b1482b
commit 18b7df5f0c
14 changed files with 204 additions and 217 deletions
+3 -1
View File
@@ -1,4 +1,6 @@
#include "engine.h"
#include <dangerzone/engine.h>
#include "internal/engine_internal.h"
engine_internal_state_t *internal_state = NULL;
game_state_t *game_state = NULL;
+4 -5
View File
@@ -1,8 +1,7 @@
#include "engine.h"
#include "error.h"
#include <SDL3/SDL_render.h>
#include <SDL3/SDL_video.h>
#include <stdint.h>
#include <dangerzone/engine.h>
#include "internal/engine_internal.h"
#include "internal/util/error.h"
bool
engine_sdl_init_state(
+48
View File
@@ -0,0 +1,48 @@
#include <dangerzone/engine.h>
#include <dangerzone/render/misc.h>
#include <dangerzone/render/rect.h>
#include "internal/util/error.h"
#include <stdint.h>
// alter a pixel at an imaginary x & y coordinates
void
put_pixel(
unsigned int x,
unsigned int y,
uint32_t color
) {
uint16_t w = game_state->width;
uint16_t h = game_state->height;
if (x < 0 || x >= w || y < 0 || y >= h) {
eprintf(
"CRITICAL: attempted buffer overflow in put_pixel\n"
"at x = %d, y = %d", x, y
);
} else {
game_state->framebuffer[game_state->width * y + x]
= color;
mark_dirty_rect(x, y, 1, 1);
}
}
// fill the entire framebuffer with a given color
void
clear_framebuffer(
uint32_t color
) {
for (size_t i = 0;
i < (size_t)game_state->width * game_state->height;
++i) {
game_state->framebuffer[i] = color;
}
mark_dirty_rect(
0,
0,
game_state->width,
game_state->height
);
}
@@ -1,30 +1,9 @@
#include "engine.h"
#include "error.h"
#include <dangerzone/engine.h>
#include <dangerzone/render/misc.h>
#include <dangerzone/render/rect.h>
#include <stdint.h>
// alter a pixel at an imaginary x & y coordinates
void
put_pixel(
unsigned int x,
unsigned int y,
uint32_t color
) {
uint16_t w = game_state->width;
uint16_t h = game_state->height;
if (x < 0 || x >= w || y < 0 || y >= h) {
eprintf(
"CRITICAL: attempted buffer overflow in put_pixel\n"
"at x = %d, y = %d", x, y
);
} else {
game_state->framebuffer[game_state->width * y + x]
= color;
mark_dirty_rect(x, y, 1, 1);
}
}
/**
* draws a rectangle with its left-top corner at (x,y) with
* a width of 'w' and height of 'h' filled with color 'color'
@@ -45,25 +24,6 @@ put_rect(
mark_dirty_rect(x, y, w, h);
}
// fill the entire framebuffer with a given color
void
clear_framebuffer(
uint32_t color
) {
for (size_t i = 0;
i < (size_t)game_state->width * game_state->height;
++i) {
game_state->framebuffer[i] = color;
}
mark_dirty_rect(
0,
0,
game_state->width,
game_state->height
);
}
void
clear_rect(int x, int y, int w, int h, uint32_t color)
{
@@ -1,7 +1,10 @@
// rendering characters in the framebuffer
#include "engine.h"
#include <stdint.h>
#include <dangerzone/engine.h>
#include <dangerzone/render/rect.h>
#include <dangerzone/render/text.h>
#include "internal/engine_internal.h"
/*
* each character has 8 rows
+3 -1
View File
@@ -1,4 +1,6 @@
#include "engine.h"
#include <dangerzone/engine.h>
#include <dangerzone/render/misc.h>
#include <dangerzone/render/text.h>
// polling events every frame
void
+48
View File
@@ -0,0 +1,48 @@
#pragma once
#include <SDL3/SDL.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#include <stdint.h>
#define WINDOW_NAME_MAX_LENGTH 128
#define FONT_ROWS 7
#define FONT_COLS 5
typedef struct {
char window_name[WINDOW_NAME_MAX_LENGTH];
uint16_t width, height;
SDL_Window *window;
SDL_Renderer *renderer;
SDL_Texture *texture;
} engine_sdl_state_t;
typedef struct {
/**
* this function runs inside of the SDL_PollEvent loop
* useful for user input, quitting the program and so on
*/
void (*engine_poll_events)(void);
/**
* meant as the "area" for manupulating the framebuffer;
* after this is executed, the contents of
* game_state->framebuffer get drawn
*/
void (*engine_update_framebuffer)(void);
engine_sdl_state_t *sdl_state;
} engine_internal_state_t;
extern engine_internal_state_t *internal_state;
engine_sdl_state_t*
engine_sdl_init(
uint32_t window_width,
uint32_t window_height,
const char window_name[]
);
void
engine_sdl_destroy(
engine_sdl_state_t *state
);
+10
View File
@@ -0,0 +1,10 @@
#pragma once
#include <stdio.h>
#include <stdbool.h>
#define eprintf(fmt, ...) \
fprintf(stderr, "[ERROR] " fmt "\n", ##__VA_ARGS__)
#define RCODE_FAILURE false
#define RCODE_SUCCESS true