Compare commits

..

2 Commits

Author SHA1 Message Date
veclavtalica
bd89c4b938 fix mixing up of SDL and libc allocators, proper flushing of quad build buffers 2025-01-30 21:57:20 +03:00
veclavtalica
7074e7499a ugh? 2025-01-30 05:10:38 +03:00
22 changed files with 46 additions and 56 deletions

View File

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

View File

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

View File

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

View File

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

View File

@ -34,7 +34,7 @@ static void ingame_end(State *state) {
Scene *ingame_scene(State *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.end = ingame_end;

View File

@ -5,9 +5,8 @@
#include "twn_game_api.h"
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
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 */
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);
free(text_str);
const char *font = "fonts/kenney-pixel.ttf";
float text_h = 32;
@ -64,7 +64,7 @@ static void title_end(State *state) {
Scene *title_scene(State *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.end = title_end;

View File

@ -45,7 +45,7 @@ static void drawdef_debug(struct World *world) {
struct World *world_create(void) {
struct World *world = cmalloc(sizeof *world);
struct World *world = malloc(sizeof *world);
*world = (struct World) {
.tiles = NULL,
@ -58,9 +58,9 @@ struct World *world_create(void) {
/* create the tilemap */
/* it simply stores what's in each tile as a 2d array */
/* 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) {
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) {
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 */
/* most importantly, it is used to draw the tiles */
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);
return world;

View File

@ -14,11 +14,11 @@
void game_tick(void) {
if (ctx.initialization_needed) {
if (!ctx.udata) {
ctx.udata = ccalloc(1, sizeof (State));
ctx.udata = calloc(1, sizeof (State));
State *state = ctx.udata;
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_DISTANCE 100
#define TERRAIN_DISTANCE 64
#define HALF_TERRAIN_DISTANCE ((float)TERRAIN_DISTANCE / 2)
#define PLAYER_HEIGHT 0.6f
@ -237,7 +237,7 @@ static void ingame_end(State *state) {
Scene *ingame_scene(State *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.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 */
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);
const char *font = "/fonts/kenney-pixel.ttf";
@ -34,8 +34,8 @@ static void title_tick(State *state) {
(Rect) {
.x = 0,
.y = 0,
.w = text_w,
.h = text_h,
.w = (float)text_w,
.h = (float)text_h,
},
(Color) { 0, 0, 0, 255 }
);
@ -53,7 +53,7 @@ static void title_end(State *state) {
Scene *title_scene(State *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.end = title_end;

View File

@ -18,12 +18,9 @@
#define DEG2RAD (M_PI / 180)
#define RAD2DEG (180 / M_PI)
/* TODO: shouldn't be a thing */
#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_critical(const char *restrict format, ...);
TWN_API void log_warn(const char *restrict format, ...);

View File

@ -43,7 +43,7 @@ typedef struct {
TextureKey texture_key;
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 element_buffer;
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) {
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);
Camera const camera = {
@ -447,7 +447,7 @@ DrawCameraFromPrincipalAxesResult draw_camera_from_principal_axes(Vec3 position,
float zoom)
{
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);
(void)roll;

View File

@ -229,7 +229,8 @@ VertexBufferBuilder build_vertex_buffer(VertexBuffer buffer, size_t bytes) {
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);
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) {
glBindBuffer(GL_ARRAY_BUFFER, buffer);
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},
rect.color);
}
finish_vertex_builder(&payload);
}
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);
}
finish_vertex_builder(&payload);
}
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,
void* user)
{
(void)watch_id; (void)rootdir; (void)filepath; (void)oldfilepath;
enum FilewatchAction faction;
switch (action) {
@ -41,6 +43,7 @@ static void filewatch_callback(dmon_watch_id watch_id,
case DMON_ACTION_MODIFY:
faction = FILEWATCH_ACTION_FILE_MODIFIED;
break;
case DMON_ACTION_MOVE:
default:
return;
}
@ -74,6 +77,8 @@ bool filewatch_add_directory(char const *dir, FileatchCallback callback) {
};
arrpush(filewatch_directories, w);
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);
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 <SDL2/SDL.h>
#include <stdlib.h>
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: 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
#include <stdbool.h>
#include <stdbit.h>
#include <stdint.h>
bool start_sanity_timer(uint32_t milliseconds_to_expire);
bool end_sanity_timer(void);

View File

@ -11,6 +11,10 @@
#define MAX SDL_max
#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);
#define CRY(title, text) cry_impl(__FILE__, __LINE__, title, text)
#define CRY_SDL(title) cry_impl(__FILE__, __LINE__, title, SDL_GetError())

View File

@ -1,25 +0,0 @@
BSD 2-Clause License
Copyright (c) 2019, Sepehr Taghdisian (septag@protonmail.com)
All rights reserved.
Redistribution and use in source and binary forms, with or without
modification, are permitted provided that the following conditions are met:
* Redistributions of source code must retain the above copyright notice, this
list of conditions and the following disclaimer.
* Redistributions in binary form must reproduce the above copyright notice,
this list of conditions and the following disclaimer in the documentation
and/or other materials provided with the distribution.
THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.