Merge remote-tracking branch 'origin/main'

This commit is contained in:
veclavtalica 2025-01-31 02:49:33 +03:00
commit a7557fceb4
21 changed files with 46 additions and 31 deletions

View File

@ -210,7 +210,7 @@ function(give_options_without_warnings target)
target_compile_definitions(${target} PRIVATE target_compile_definitions(${target} PRIVATE
$<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME> $<$<BOOL:${TWN_FEATURE_DYNLIB_GAME}>:TWN_FEATURE_DYNLIB_GAME>
$<$<BOOL:${LINUX}>:_GNU_SOURCE>) _GNU_SOURCE)
endfunction() endfunction()

View File

@ -66,7 +66,7 @@ void game_tick(void)
{ // First tick, initalizing data { // First tick, initalizing data
// Allocating State struct to store data there // Allocating State struct to store data there
if (!ctx.udata) if (!ctx.udata)
ctx.udata = ccalloc(1, sizeof(State)); ctx.udata = calloc(1, sizeof(State));
} }
input_action("add_a_bit", CONTROL_LEFT_MOUSE); input_action("add_a_bit", CONTROL_LEFT_MOUSE);

View File

@ -12,7 +12,7 @@
void game_tick(void) { void game_tick(void) {
if (ctx.initialization_needed) { if (ctx.initialization_needed) {
if (!ctx.udata) { if (!ctx.udata) {
ctx.udata = ccalloc(1, sizeof (State)); ctx.udata = calloc(1, sizeof (State));
State *state = ctx.udata; State *state = ctx.udata;
state->ctx = &ctx; state->ctx = &ctx;

View File

@ -203,7 +203,7 @@ static void calc_collisions_y(Player *player) {
Player *player_create(World *world) { Player *player_create(World *world) {
Player *player = cmalloc(sizeof *player); Player *player = malloc(sizeof *player);
*player = (Player) { *player = (Player) {
.world = world, .world = world,

View File

@ -34,7 +34,7 @@ static void ingame_end(State *state) {
Scene *ingame_scene(State *state) { Scene *ingame_scene(State *state) {
(void)state; (void)state;
SceneIngame *new_scene = ccalloc(1, sizeof *new_scene); SceneIngame *new_scene = calloc(1, sizeof *new_scene);
new_scene->base.tick = ingame_tick; new_scene->base.tick = ingame_tick;
new_scene->base.end = ingame_end; new_scene->base.end = ingame_end;

View File

@ -5,9 +5,8 @@
#include "twn_game_api.h" #include "twn_game_api.h"
#include <SDL2/SDL.h>
#include <stdio.h> #include <stdio.h>
#include <stdlib.h>
static void title_tick(State *state) { static void title_tick(State *state) {
@ -26,8 +25,9 @@ static void title_tick(State *state) {
/* draw the tick count as an example of dynamic text */ /* draw the tick count as an example of dynamic text */
size_t text_str_len = snprintf(NULL, 0, "%llu", (unsigned long long)state->ctx->frame_number) + 1; size_t text_str_len = snprintf(NULL, 0, "%llu", (unsigned long long)state->ctx->frame_number) + 1;
char *text_str = cmalloc(text_str_len); char *text_str = malloc(text_str_len);
snprintf(text_str, text_str_len, "%llu", (unsigned long long)state->ctx->frame_number); snprintf(text_str, text_str_len, "%llu", (unsigned long long)state->ctx->frame_number);
free(text_str);
const char *font = "fonts/kenney-pixel.ttf"; const char *font = "fonts/kenney-pixel.ttf";
float text_h = 32; float text_h = 32;
@ -64,7 +64,7 @@ static void title_end(State *state) {
Scene *title_scene(State *state) { Scene *title_scene(State *state) {
(void)state; (void)state;
SceneTitle *new_scene = ccalloc(1, sizeof *new_scene); SceneTitle *new_scene = calloc(1, sizeof *new_scene);
new_scene->base.tick = title_tick; new_scene->base.tick = title_tick;
new_scene->base.end = title_end; new_scene->base.end = title_end;

View File

@ -45,7 +45,7 @@ static void drawdef_debug(struct World *world) {
struct World *world_create(void) { struct World *world_create(void) {
struct World *world = cmalloc(sizeof *world); struct World *world = malloc(sizeof *world);
*world = (struct World) { *world = (struct World) {
.tiles = NULL, .tiles = NULL,
@ -58,9 +58,9 @@ struct World *world_create(void) {
/* create the tilemap */ /* create the tilemap */
/* it simply stores what's in each tile as a 2d array */ /* it simply stores what's in each tile as a 2d array */
/* on its own, it's entirely unrelated to drawing or logic */ /* on its own, it's entirely unrelated to drawing or logic */
world->tilemap = cmalloc(sizeof *world->tilemap * world->tilemap_height); world->tilemap = malloc(sizeof *world->tilemap * world->tilemap_height);
for (size_t i = 0; i < world->tilemap_height; ++i) { for (size_t i = 0; i < world->tilemap_height; ++i) {
world->tilemap[i] = cmalloc(sizeof **world->tilemap * world->tilemap_width); world->tilemap[i] = malloc(sizeof **world->tilemap * world->tilemap_width);
for (size_t j = 0; j < world->tilemap_width; ++j) { for (size_t j = 0; j < world->tilemap_width; ++j) {
world->tilemap[i][j] = TILE_TYPE_VOID; world->tilemap[i][j] = TILE_TYPE_VOID;
@ -81,7 +81,7 @@ struct World *world_create(void) {
/* the tiles array contains data meant to be used by other logic */ /* the tiles array contains data meant to be used by other logic */
/* most importantly, it is used to draw the tiles */ /* most importantly, it is used to draw the tiles */
const size_t tile_count = world->tilemap_height * world->tilemap_width; const size_t tile_count = world->tilemap_height * world->tilemap_width;
world->tiles = cmalloc(sizeof *world->tiles * tile_count); world->tiles = malloc(sizeof *world->tiles * tile_count);
update_tiles(world); update_tiles(world);
return world; return world;

View File

@ -14,11 +14,11 @@
void game_tick(void) { void game_tick(void) {
if (ctx.initialization_needed) { if (ctx.initialization_needed) {
if (!ctx.udata) { if (!ctx.udata) {
ctx.udata = ccalloc(1, sizeof (State)); ctx.udata = calloc(1, sizeof (State));
State *state = ctx.udata; State *state = ctx.udata;
state->ctx = &ctx; state->ctx = &ctx;
state->scene = ingame_scene(state); state->scene = title_scene(state);
} }
} }

View File

@ -13,7 +13,7 @@
#define TERRAIN_FREQUENCY 0.15f #define TERRAIN_FREQUENCY 0.15f
#define TERRAIN_DISTANCE 100 #define TERRAIN_DISTANCE 64
#define HALF_TERRAIN_DISTANCE ((float)TERRAIN_DISTANCE / 2) #define HALF_TERRAIN_DISTANCE ((float)TERRAIN_DISTANCE / 2)
#define PLAYER_HEIGHT 0.6f #define PLAYER_HEIGHT 0.6f
@ -237,7 +237,7 @@ static void ingame_end(State *state) {
Scene *ingame_scene(State *state) { Scene *ingame_scene(State *state) {
(void)state; (void)state;
SceneIngame *new_scene = ccalloc(1, sizeof *new_scene); SceneIngame *new_scene = calloc(1, sizeof *new_scene);
new_scene->base.tick = ingame_tick; new_scene->base.tick = ingame_tick;
new_scene->base.end = ingame_end; new_scene->base.end = ingame_end;

View File

@ -23,7 +23,7 @@ static void title_tick(State *state) {
/* draw the tick count as an example of dynamic text */ /* draw the tick count as an example of dynamic text */
size_t text_str_len = snprintf(NULL, 0, "%llu", (unsigned long long)state->ctx->frame_number) + 1; size_t text_str_len = snprintf(NULL, 0, "%llu", (unsigned long long)state->ctx->frame_number) + 1;
char *text_str = cmalloc(text_str_len); char *text_str = malloc(text_str_len);
snprintf(text_str, text_str_len, "%llu", (unsigned long long)state->ctx->frame_number); snprintf(text_str, text_str_len, "%llu", (unsigned long long)state->ctx->frame_number);
const char *font = "/fonts/kenney-pixel.ttf"; const char *font = "/fonts/kenney-pixel.ttf";
@ -34,8 +34,8 @@ static void title_tick(State *state) {
(Rect) { (Rect) {
.x = 0, .x = 0,
.y = 0, .y = 0,
.w = text_w, .w = (float)text_w,
.h = text_h, .h = (float)text_h,
}, },
(Color) { 0, 0, 0, 255 } (Color) { 0, 0, 0, 255 }
); );
@ -53,7 +53,7 @@ static void title_end(State *state) {
Scene *title_scene(State *state) { Scene *title_scene(State *state) {
(void)state; (void)state;
SceneTitle *new_scene = ccalloc(1, sizeof *new_scene); SceneTitle *new_scene = calloc(1, sizeof *new_scene);
new_scene->base.tick = title_tick; new_scene->base.tick = title_tick;
new_scene->base.end = title_end; new_scene->base.end = title_end;

View File

@ -18,12 +18,9 @@
#define DEG2RAD (M_PI / 180) #define DEG2RAD (M_PI / 180)
#define RAD2DEG (180 / M_PI) #define RAD2DEG (180 / M_PI)
/* TODO: shouldn't be a thing */
#ifndef TWN_NOT_C #ifndef TWN_NOT_C
TWN_API void *cmalloc(size_t size);
TWN_API void *crealloc(void *ptr, size_t size);
TWN_API void *ccalloc(size_t num, size_t size);
TWN_API void log_info(const char *restrict format, ...); TWN_API void log_info(const char *restrict format, ...);
TWN_API void log_critical(const char *restrict format, ...); TWN_API void log_critical(const char *restrict format, ...);
TWN_API void log_warn(const char *restrict format, ...); TWN_API void log_warn(const char *restrict format, ...);

View File

@ -43,7 +43,7 @@ typedef struct {
TextureKey texture_key; TextureKey texture_key;
GPUTexture gpu_texture; GPUTexture gpu_texture;
/* could be either `element_count` with supplied `element_buffer`, or this, but not both */ /* could be either `element_count` with supplied `element_buffer`, or `primitive_count`, but not both */
uint32_t primitive_count; uint32_t primitive_count;
uint32_t element_buffer; uint32_t element_buffer;
uint32_t element_count; uint32_t element_count;

View File

@ -415,7 +415,7 @@ void render(void) {
void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom) { void draw_camera(Vec3 position, Vec3 direction, Vec3 up, float fov, float zoom) {
bool const orthographic = fabsf(0.0f - fov) < 0.00001f; bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
if (!orthographic && fov >= M_PIf) if (!orthographic && fov >= (float)(M_PI))
log_warn("Invalid fov given (%f)", (double)fov); log_warn("Invalid fov given (%f)", (double)fov);
Camera const camera = { Camera const camera = {
@ -447,7 +447,7 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
float zoom) float zoom)
{ {
bool const orthographic = fabsf(0.0f - fov) < 0.00001f; bool const orthographic = fabsf(0.0f - fov) < 0.00001f;
if (!orthographic && fov >= M_PIf) if (!orthographic && fov >= (float)(M_PI))
log_warn("Invalid fov given (%f)", (double)fov); log_warn("Invalid fov given (%f)", (double)fov);
(void)roll; (void)roll;

View File

@ -229,7 +229,8 @@ VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes) {
void finish_vertex_builder(VertexBufferBuilder *builder) { void finish_vertex_builder(VertexBufferBuilder *builder) {
glUnmapBuffer(GL_ARRAY_BUFFER); if (!glUnmapBuffer(GL_ARRAY_BUFFER))
CRY("finish_vertex_builder", "Error unmapping a vertex array buffer");
glBindBuffer(GL_ARRAY_BUFFER, 0); glBindBuffer(GL_ARRAY_BUFFER, 0);
builder->base = 0; builder->base = 0;

View File

@ -31,6 +31,7 @@ void delete_vertex_buffer(VertexBuffer buffer) {
void specify_vertex_buffer(VertexBuffer buffer, void const *data, size_t bytes) { void specify_vertex_buffer(VertexBuffer buffer, void const *data, size_t bytes) {
glBindBuffer(GL_ARRAY_BUFFER, buffer); glBindBuffer(GL_ARRAY_BUFFER, buffer);
glBufferData(GL_ARRAY_BUFFER, bytes, data, GL_STREAM_DRAW); glBufferData(GL_ARRAY_BUFFER, bytes, data, GL_STREAM_DRAW);
glBindBuffer(GL_ARRAY_BUFFER, 0);
} }

View File

@ -90,6 +90,8 @@ void render_rect_batch(const Primitive2D primitives[],
(Vec2){0}, (Vec2){0}, (Vec2){0}, (Vec2){0}, (Vec2){0}, (Vec2){0}, (Vec2){0}, (Vec2){0},
rect.color); rect.color);
} }
finish_vertex_builder(&payload);
} }
finally_render_quads(primitives, batch, vertex_array); finally_render_quads(primitives, batch, vertex_array);

View File

@ -244,6 +244,8 @@ void render_sprite_batch(const Primitive2D primitives[],
push_quad_payload_to_vertex_buffer_builder(batch, i, &payload, v0, v1, v2, v3, uv0, uv1, uv2, uv3, sprite.color); push_quad_payload_to_vertex_buffer_builder(batch, i, &payload, v0, v1, v2, v3, uv0, uv1, uv2, uv3, sprite.color);
} }
finish_vertex_builder(&payload);
} }
finally_render_quads(primitives, batch, vertex_array); finally_render_quads(primitives, batch, vertex_array);

View File

@ -29,6 +29,8 @@ static void filewatch_callback(dmon_watch_id watch_id,
const char* oldfilepath, const char* oldfilepath,
void* user) void* user)
{ {
(void)watch_id; (void)rootdir; (void)filepath; (void)oldfilepath;
enum FilewatchAction faction; enum FilewatchAction faction;
switch (action) { switch (action) {
@ -41,6 +43,7 @@ static void filewatch_callback(dmon_watch_id watch_id,
case DMON_ACTION_MODIFY: case DMON_ACTION_MODIFY:
faction = FILEWATCH_ACTION_FILE_MODIFIED; faction = FILEWATCH_ACTION_FILE_MODIFIED;
break; break;
case DMON_ACTION_MOVE:
default: default:
return; return;
} }
@ -74,6 +77,8 @@ bool filewatch_add_directory(char const *dir, FileatchCallback callback) {
}; };
arrpush(filewatch_directories, w); arrpush(filewatch_directories, w);
dmon_watch(dir, filewatch_callback, DMON_WATCHFLAGS_RECURSIVE, (void *)(intptr_t)(arrlen(filewatch_directories) - 1)); dmon_watch(dir, filewatch_callback, DMON_WATCHFLAGS_RECURSIVE, (void *)(intptr_t)(arrlen(filewatch_directories) - 1));
return true;
} }
@ -91,6 +96,8 @@ bool filewatch_add_file(char const *filepath, FileatchCallback callback) {
}; };
arrpush(filewatch_files, f); arrpush(filewatch_files, f);
dmon_watch("./", filewatch_callback, 0, (void *)(intptr_t)(-arrlen(filewatch_files))); dmon_watch("./", filewatch_callback, 0, (void *)(intptr_t)(-arrlen(filewatch_files)));
return true;
} }

View File

@ -2,6 +2,7 @@
#include "twn_engine_context_c.h" #include "twn_engine_context_c.h"
#include <SDL2/SDL.h> #include <SDL2/SDL.h>
#include <stdlib.h>
static SDL_TimerID sanity_timer; static SDL_TimerID sanity_timer;
@ -19,7 +20,7 @@ static uint32_t sanity_timer_handler(uint32_t interval, void *data) {
/* TODO: figure out the most portable way to do it */ /* TODO: figure out the most portable way to do it */
/* TODO: different type of behavior is possible, especially for debugging */ /* TODO: different type of behavior is possible, especially for debugging */
quick_exit(EXIT_FAILURE); exit(EXIT_FAILURE);
} }

View File

@ -2,7 +2,7 @@
#define TWN_TIMER_H #define TWN_TIMER_H
#include <stdbool.h> #include <stdbool.h>
#include <stdbit.h> #include <stdint.h>
bool start_sanity_timer(uint32_t milliseconds_to_expire); bool start_sanity_timer(uint32_t milliseconds_to_expire);
bool end_sanity_timer(void); bool end_sanity_timer(void);

View File

@ -11,6 +11,10 @@
#define MAX SDL_max #define MAX SDL_max
#define MIN SDL_min #define MIN SDL_min
void *cmalloc(size_t size);
void *crealloc(void *ptr, size_t size);
void *ccalloc(size_t num, size_t size);
void cry_impl(const char *file, const int line, const char *title, const char *text); void cry_impl(const char *file, const int line, const char *title, const char *text);
#define CRY(title, text) cry_impl(__FILE__, __LINE__, title, text) #define CRY(title, text) cry_impl(__FILE__, __LINE__, title, text)
#define CRY_SDL(title) cry_impl(__FILE__, __LINE__, title, SDL_GetError()) #define CRY_SDL(title) cry_impl(__FILE__, __LINE__, title, SDL_GetError())