fix mixing up of SDL and libc allocators, proper flushing of quad build buffers

This commit is contained in:
veclavtalica 2025-01-30 21:57:20 +03:00
parent 7074e7499a
commit bd89c4b938
16 changed files with 33 additions and 26 deletions

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

@ -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

@ -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())