add custom memory functions
This commit is contained in:
parent
9504e5a3db
commit
4683e3b4ad
11 changed files with 157 additions and 159 deletions
2
Makefile
2
Makefile
|
@ -2,7 +2,7 @@ BINARY=gear-bin
|
||||||
|
|
||||||
SHADERC=sokol-shdc
|
SHADERC=sokol-shdc
|
||||||
|
|
||||||
CFLAGS=-I. -Iinclude -Wall -Wextra -Werror -Wno-header-guard -Wno-comment -g
|
CFLAGS=-I. -Iinclude -Wall -Wextra -Werror -Wno-header-guard -Wno-comment -Ofast
|
||||||
LDFLAGS=-g
|
LDFLAGS=-g
|
||||||
SHADER_FLAGS=--slang glsl430:hlsl5:metal_macos
|
SHADER_FLAGS=--slang glsl430:hlsl5:metal_macos
|
||||||
|
|
||||||
|
|
16
include/gear/memory.h
Normal file
16
include/gear/memory.h
Normal file
|
@ -0,0 +1,16 @@
|
||||||
|
#ifndef __G_MEMORY_H__
|
||||||
|
#define __G_MEMORY_H__
|
||||||
|
|
||||||
|
#include <gear/base.h>
|
||||||
|
|
||||||
|
#define MEM_ALLOC_FUNC malloc
|
||||||
|
|
||||||
|
// TODO: use macros in order to get location that function was called and log with that info instead
|
||||||
|
|
||||||
|
// src/memory/alloc.c
|
||||||
|
void* g_mem_alloc_named(const char* resource, sz size);
|
||||||
|
void* g_mem_calloc_named(const char* resource, sz size);
|
||||||
|
void* g_mem_alloc(sz size);
|
||||||
|
void* g_mem_calloc(sz size);
|
||||||
|
|
||||||
|
#endif
|
|
@ -1,21 +1,14 @@
|
||||||
#include <gear/camera.h>
|
#include <gear/camera.h>
|
||||||
|
#include <gear/memory.h>
|
||||||
#include <log.c/log.h>
|
#include <log.c/log.h>
|
||||||
|
|
||||||
g_camera* g_camera_create(vec3 position, vec3 rotation, float fov) {
|
g_camera* g_camera_create(vec3 position, vec3 rotation, float fov) {
|
||||||
g_camera* camera = calloc(1, sizeof(g_camera));
|
g_camera* camera = g_mem_calloc_named("Camera", sizeof(g_camera));
|
||||||
if(camera != NULL) {
|
camera->position = position;
|
||||||
log_info("Camera allocated successfully");
|
camera->rotation = rotation;
|
||||||
log_debug("Camera pointer: %p", camera);
|
camera->fov = fov;
|
||||||
|
camera->near = 0.1f;
|
||||||
camera->position = position;
|
camera->far = 1000.0f;
|
||||||
camera->rotation = rotation;
|
|
||||||
camera->fov = fov;
|
|
||||||
camera->near = 0.1f;
|
|
||||||
camera->far = 1000.0f;
|
|
||||||
} else {
|
|
||||||
log_fatal("Failed to allocate camera");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return camera;
|
return camera;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,36 +1,31 @@
|
||||||
#include <gear/graphics/models.h>
|
#include <gear/graphics/models.h>
|
||||||
|
#include <gear/memory.h>
|
||||||
|
|
||||||
#include <log.c/log.h>
|
#include <log.c/log.h>
|
||||||
|
|
||||||
g_model* g_model_create(g_vertex_vec* vertices, g_index_vec* indices) {
|
g_model* g_model_create(g_vertex_vec* vertices, g_index_vec* indices) {
|
||||||
g_model* model = calloc(1, sizeof(g_model));
|
g_model* model = g_mem_calloc_named("Model", sizeof(g_model));
|
||||||
if(model != NULL) {
|
|
||||||
log_info("Model allocated successfully");
|
|
||||||
log_debug("Model pointer: %p", model);
|
|
||||||
|
|
||||||
if(vertices != NULL) {
|
if(vertices != NULL) {
|
||||||
for(sz i = 0; i < vertices->size; i++) {
|
for(sz i = 0; i < vertices->size; i++) {
|
||||||
sl_vec_push(model->vertices, vertices->data[i]);
|
sl_vec_push(model->vertices, vertices->data[i]);
|
||||||
}
|
|
||||||
|
|
||||||
model->vertex_buffer = sg_make_buffer(&(sg_buffer_desc){
|
|
||||||
.type = SG_BUFFERTYPE_VERTEXBUFFER,
|
|
||||||
.data = SL_SG_RANGE(model->vertices)
|
|
||||||
});
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if(indices != NULL) {
|
model->vertex_buffer = sg_make_buffer(&(sg_buffer_desc){
|
||||||
for(sz i = 0; i < indices->size; i++) {
|
.type = SG_BUFFERTYPE_VERTEXBUFFER,
|
||||||
sl_vec_push(model->indices, indices->data[i]);
|
.data = SL_SG_RANGE(model->vertices)
|
||||||
}
|
});
|
||||||
|
}
|
||||||
|
|
||||||
model->index_buffer = sg_make_buffer(&(sg_buffer_desc){
|
if(indices != NULL) {
|
||||||
.type = SG_BUFFERTYPE_INDEXBUFFER,
|
for(sz i = 0; i < indices->size; i++) {
|
||||||
.data = SL_SG_RANGE(model->indices)
|
sl_vec_push(model->indices, indices->data[i]);
|
||||||
});
|
|
||||||
}
|
}
|
||||||
} else {
|
|
||||||
log_fatal("Failed to allocate model");
|
model->index_buffer = sg_make_buffer(&(sg_buffer_desc){
|
||||||
exit(EXIT_FAILURE);
|
.type = SG_BUFFERTYPE_INDEXBUFFER,
|
||||||
|
.data = SL_SG_RANGE(model->indices)
|
||||||
|
});
|
||||||
}
|
}
|
||||||
|
|
||||||
return model;
|
return model;
|
||||||
|
|
|
@ -1,4 +1,6 @@
|
||||||
#include <gear/graphics/renderer.h>
|
#include <gear/graphics/renderer.h>
|
||||||
|
#include <gear/memory.h>
|
||||||
|
|
||||||
#include <log.c/log.h>
|
#include <log.c/log.h>
|
||||||
|
|
||||||
g_renderer* g_renderer_create(g_color clear_color) {
|
g_renderer* g_renderer_create(g_color clear_color) {
|
||||||
|
@ -7,19 +9,11 @@ g_renderer* g_renderer_create(g_color clear_color) {
|
||||||
.clear_value = G_COLOR_TO_FLOAT_ARR(clear_color),
|
.clear_value = G_COLOR_TO_FLOAT_ARR(clear_color),
|
||||||
};
|
};
|
||||||
|
|
||||||
g_renderer* renderer = calloc(1, sizeof(g_renderer));
|
g_renderer* renderer = g_mem_calloc_named("Renderer", sizeof(g_renderer));
|
||||||
if(renderer != NULL) {
|
renderer->pass_action = (sg_pass_action){
|
||||||
log_info("Renderer allocated successfully");
|
.colors = { color_action, color_action, color_action, color_action },
|
||||||
log_debug("Renderer pointer: %p", renderer);
|
.depth = { .load_action = SG_LOADACTION_CLEAR, .clear_value = 1.0f },
|
||||||
|
};
|
||||||
renderer->pass_action = (sg_pass_action){
|
|
||||||
.colors = { color_action, color_action, color_action, color_action },
|
|
||||||
.depth = { .load_action = SG_LOADACTION_CLEAR, .clear_value = 1.0f },
|
|
||||||
};
|
|
||||||
} else {
|
|
||||||
log_fatal("Failed to allocate renderer");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return renderer;
|
return renderer;
|
||||||
}
|
}
|
||||||
|
|
|
@ -1,5 +1,7 @@
|
||||||
#include <gear/graphics/textures.h>
|
#include <gear/graphics/textures.h>
|
||||||
#include <gear/images.h>
|
#include <gear/images.h>
|
||||||
|
#include <gear/memory.h>
|
||||||
|
|
||||||
#include <log.c/log.h>
|
#include <log.c/log.h>
|
||||||
|
|
||||||
g_texture* g_texture_create(g_image* image) {
|
g_texture* g_texture_create(g_image* image) {
|
||||||
|
@ -22,19 +24,11 @@ g_texture* g_texture_create_filtered(g_image* image, sg_filter min_filter, sg_fi
|
||||||
.mag_filter = mag_filter
|
.mag_filter = mag_filter
|
||||||
});
|
});
|
||||||
|
|
||||||
g_texture* texture = calloc(1, sizeof(g_texture));
|
g_texture* texture = g_mem_calloc_named("Texture", sizeof(g_texture));
|
||||||
if(texture != NULL) {
|
texture->width = image->width;
|
||||||
log_info("Texture allocated successfully");
|
texture->height = image->height;
|
||||||
log_debug("Texture pointer: %p", texture);
|
texture->image = sg_img;
|
||||||
|
texture->sampler = sg_samp;
|
||||||
texture->width = image->width;
|
|
||||||
texture->height = image->height;
|
|
||||||
texture->image = sg_img;
|
|
||||||
texture->sampler = sg_samp;
|
|
||||||
} else {
|
|
||||||
log_fatal("Failed to allocate texture");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
g_image_destroy(image);
|
g_image_destroy(image);
|
||||||
|
|
||||||
|
|
|
@ -1,34 +1,26 @@
|
||||||
#include <gear/images.h>
|
#include <gear/images.h>
|
||||||
|
#include <gear/memory.h>
|
||||||
|
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
#include <log.c/log.h>
|
#include <log.c/log.h>
|
||||||
|
|
||||||
g_image* g_image_create(u8* pixels, i32 width, i32 height) {
|
g_image* g_image_create(u8* pixels, i32 width, i32 height) {
|
||||||
g_image* p_image = calloc(1, sizeof(g_image));
|
g_image* image = g_mem_calloc_named("Image", sizeof(g_image));
|
||||||
if (p_image != NULL) {
|
image->width = width;
|
||||||
log_info("Image allocated successfully");
|
image->height = height;
|
||||||
log_debug("Image pointer: %p", p_image);
|
image->channels = G_IMAGE_CHANNELS;
|
||||||
|
image->pixels = pixels;
|
||||||
|
|
||||||
p_image->width = width;
|
return image;
|
||||||
p_image->height = height;
|
|
||||||
p_image->channels = G_IMAGE_CHANNELS;
|
|
||||||
p_image->pixels = pixels;
|
|
||||||
} else {
|
|
||||||
log_fatal("Failed to allocate image");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return p_image;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
g_image* g_image_default() {
|
g_image* g_image_default() {
|
||||||
g_image* p_image = calloc(1, sizeof(g_image));
|
g_image* image = g_mem_calloc_named("Image", sizeof(g_image));
|
||||||
if(p_image != NULL) {
|
image->width = G_IMAGE_MISSING_DIMENSIONS,
|
||||||
p_image->width = G_IMAGE_MISSING_DIMENSIONS,
|
image->height = G_IMAGE_MISSING_DIMENSIONS,
|
||||||
p_image->height = G_IMAGE_MISSING_DIMENSIONS,
|
image->channels = G_IMAGE_CHANNELS,
|
||||||
p_image->channels = G_IMAGE_CHANNELS,
|
image->pixels = g_mem_alloc(G_IMAGE_MISSING_BYTESIZE);
|
||||||
p_image->pixels = calloc(1, G_IMAGE_MISSING_BYTESIZE);
|
memcpy(image->pixels, G_IMAGE_MISSING_PIXELS, G_IMAGE_MISSING_BYTESIZE);
|
||||||
memcpy(p_image->pixels, G_IMAGE_MISSING_PIXELS, G_IMAGE_MISSING_BYTESIZE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return p_image;
|
return image;
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,6 +9,7 @@
|
||||||
#include <gear/ui/gui.h>
|
#include <gear/ui/gui.h>
|
||||||
#include <gear/ui/console.h>
|
#include <gear/ui/console.h>
|
||||||
#include <gear/resources.h>
|
#include <gear/resources.h>
|
||||||
|
#include <gear/memory.h>
|
||||||
#include <gear/graphics/models.h>
|
#include <gear/graphics/models.h>
|
||||||
#include <shaders/transform.glsl.h>
|
#include <shaders/transform.glsl.h>
|
||||||
|
|
||||||
|
@ -65,7 +66,7 @@ void init() {
|
||||||
|
|
||||||
model = g_model_create(&vertices_vec, &indices_vec);
|
model = g_model_create(&vertices_vec, &indices_vec);
|
||||||
|
|
||||||
bind = calloc(1, sizeof(sg_bindings));
|
bind = g_mem_calloc_named("Bindings", sizeof(sg_bindings));
|
||||||
bind->vertex_buffers[0] = model->vertex_buffer;
|
bind->vertex_buffers[0] = model->vertex_buffer;
|
||||||
bind->index_buffer = model->index_buffer;
|
bind->index_buffer = model->index_buffer;
|
||||||
|
|
||||||
|
|
45
src/memory/alloc.c
Normal file
45
src/memory/alloc.c
Normal file
|
@ -0,0 +1,45 @@
|
||||||
|
#include <gear/memory.h>
|
||||||
|
#include <log.c/log.h>
|
||||||
|
|
||||||
|
void* g_mem_alloc_named(const char* resource, sz size) {
|
||||||
|
void* ptr = MEM_ALLOC_FUNC(size);
|
||||||
|
if(ptr != NULL) {
|
||||||
|
if(resource != NULL) {
|
||||||
|
log_info("%s allocated successfully (%zu bytes)", resource, size);
|
||||||
|
log_debug("%s pointer: %p", resource, ptr);
|
||||||
|
} else {
|
||||||
|
log_info("%zu bytes allocated successfully", size);
|
||||||
|
log_debug("Pointer: %p", ptr);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
} else {
|
||||||
|
if(resource != NULL) {
|
||||||
|
log_fatal("Failed to allocate %s (%zu bytes)", resource, size);
|
||||||
|
} else {
|
||||||
|
log_fatal("Failed to allocate %zu bytes", size);
|
||||||
|
}
|
||||||
|
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void* g_mem_calloc_named(const char* resource, sz size) {
|
||||||
|
void* ptr = g_mem_alloc_named(resource, size);
|
||||||
|
memset(ptr, 0, size);
|
||||||
|
if(resource != NULL) {
|
||||||
|
log_info("Zeroed %zu bytes for %s", size, resource);
|
||||||
|
} else {
|
||||||
|
log_info("Zeroid %zu bytes", size);
|
||||||
|
}
|
||||||
|
|
||||||
|
return ptr;
|
||||||
|
}
|
||||||
|
|
||||||
|
void* g_mem_alloc(sz size) {
|
||||||
|
return g_mem_alloc_named(NULL, size);
|
||||||
|
}
|
||||||
|
|
||||||
|
void* g_mem_calloc(sz size) {
|
||||||
|
return g_mem_calloc_named(NULL, size);
|
||||||
|
}
|
|
@ -1,17 +1,8 @@
|
||||||
|
#include "gear/memory.h"
|
||||||
#include <gear/ui/gui.h>
|
#include <gear/ui/gui.h>
|
||||||
#include <gear/ui/console.h>
|
#include <gear/ui/console.h>
|
||||||
#include <log.c/log.h>
|
#include <log.c/log.h>
|
||||||
|
|
||||||
/*
|
|
||||||
sz combined_len = console->output.len + len;
|
|
||||||
|
|
||||||
if(combined_len > G_CONSOLE_MAX_LEN) {
|
|
||||||
sz required_trim = combined_len - G_CONSOLE_MAX_LEN;
|
|
||||||
if(required_trim < G_CONSOLE_MAX_LEN / 10) required_trim = G_CONSOLE_MAX_LEN / 5;
|
|
||||||
memcpy(console->output.buf, console->output.buf + required_trim, G_CONSOLE_MAX_LEN - required_trim);
|
|
||||||
}*/
|
|
||||||
|
|
||||||
|
|
||||||
char format_input_buf[G_CONSOLE_MAX_LEN] = { 0 };
|
char format_input_buf[G_CONSOLE_MAX_LEN] = { 0 };
|
||||||
char fmt[G_CONSOLE_MAX_LEN];
|
char fmt[G_CONSOLE_MAX_LEN];
|
||||||
|
|
||||||
|
@ -39,21 +30,13 @@ void log_callback(log_Event* event) {
|
||||||
}
|
}
|
||||||
|
|
||||||
g_gui_layer* g_console_create() {
|
g_gui_layer* g_console_create() {
|
||||||
g_console* console = calloc(1, sizeof(g_console));
|
g_console* console = g_mem_calloc_named("Console", sizeof(g_console));
|
||||||
if(console != NULL) {
|
memset(&console->input, 0, G_CONSOLE_BUFFER_SIZE);
|
||||||
log_info("Console allocated successfully");
|
memset(&console->output, 0, G_CONSOLE_BUFFER_SIZE);
|
||||||
log_debug("Console pointer: %p", console);
|
|
||||||
|
|
||||||
memset(&console->input, 0, G_CONSOLE_BUFFER_SIZE);
|
|
||||||
memset(&console->output, 0, G_CONSOLE_BUFFER_SIZE);
|
|
||||||
} else {
|
|
||||||
log_fatal("Failed to allocate console");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
log_add_callback(log_callback, console, LOG_INFO);
|
log_add_callback(log_callback, console, LOG_INFO);
|
||||||
|
|
||||||
g_gui_layer* layer = calloc(1, sizeof(g_gui_layer));
|
g_gui_layer* layer = g_mem_calloc_named("Console GUI Layer", sizeof(g_gui_layer));
|
||||||
layer->name = "Console";
|
layer->name = "Console";
|
||||||
layer->draw = g_console_draw;
|
layer->draw = g_console_draw;
|
||||||
layer->data = console;
|
layer->data = console;
|
||||||
|
|
|
@ -1,4 +1,5 @@
|
||||||
#include <gear/ui/gui.h>
|
#include <gear/ui/gui.h>
|
||||||
|
#include <gear/memory.h>
|
||||||
#include <gear/ui/console.h>
|
#include <gear/ui/console.h>
|
||||||
#include <gear/resources.h>
|
#include <gear/resources.h>
|
||||||
#include <sokol/sokol_log.h>
|
#include <sokol/sokol_log.h>
|
||||||
|
@ -13,62 +14,46 @@ g_gui* g_gui_create() {
|
||||||
});
|
});
|
||||||
log_info("Sokol nuklear initialized successfully");
|
log_info("Sokol nuklear initialized successfully");
|
||||||
|
|
||||||
g_gui* gui = calloc(1, sizeof(g_gui));
|
g_gui* gui = g_mem_calloc_named("GUI", sizeof(g_gui));
|
||||||
if(gui != NULL) {
|
g_gui_add_layer(&gui->layers, g_console_create, g_console_destroy);
|
||||||
log_info("GUI allocated successfully");
|
|
||||||
log_debug("GUI pointer: %p", gui);
|
|
||||||
|
|
||||||
g_gui_add_layer(&gui->layers, g_console_create, g_console_destroy);
|
struct nk_font_atlas* atlas = g_mem_calloc_named("Font atlas", sizeof(struct nk_font_atlas));
|
||||||
} else {
|
nk_font_atlas_init_default(atlas);
|
||||||
log_fatal("Failed to allocate GUI");
|
nk_font_atlas_begin(atlas);
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
struct nk_font_atlas* atlas = calloc(1, sizeof(struct nk_font_atlas));
|
f32 font_size = 22.05f;
|
||||||
if(atlas != NULL) {
|
struct nk_font_config config = nk_font_config(font_size);
|
||||||
log_info("Font atlas allocated successfully");
|
config.pixel_snap = true;
|
||||||
log_debug("Font atlas pointer: %p", atlas);
|
|
||||||
|
|
||||||
nk_font_atlas_init_default(atlas);
|
g_gui_font = nk_font_atlas_add_from_memory(atlas,
|
||||||
nk_font_atlas_begin(atlas);
|
res_OpenSans_Regular_ttf,
|
||||||
|
res_OpenSans_Regular_ttf_len,
|
||||||
|
font_size, &config);
|
||||||
|
|
||||||
f32 font_size = 22.05f;
|
i32 atlas_width, atlas_height;
|
||||||
struct nk_font_config config = nk_font_config(font_size);
|
const u8* pixels = nk_font_atlas_bake(atlas, &atlas_width, &atlas_height, NK_FONT_ATLAS_RGBA32);
|
||||||
config.pixel_snap = true;
|
|
||||||
|
|
||||||
g_gui_font = nk_font_atlas_add_from_memory(atlas,
|
sg_image font_image = sg_make_image(&(sg_image_desc){
|
||||||
res_OpenSans_Regular_ttf,
|
.label = "Font image",
|
||||||
res_OpenSans_Regular_ttf_len,
|
.width = atlas_width,
|
||||||
font_size, &config);
|
.height = atlas_height,
|
||||||
|
.data.subimage[0][0] = (sg_range){
|
||||||
|
.ptr = pixels,
|
||||||
|
.size = atlas_width * atlas_height * 4
|
||||||
|
},
|
||||||
|
.pixel_format = SG_PIXELFORMAT_RGBA8
|
||||||
|
});
|
||||||
|
|
||||||
i32 atlas_width, atlas_height;
|
sg_sampler font_sampler = sg_make_sampler(&(sg_sampler_desc){
|
||||||
const u8* pixels = nk_font_atlas_bake(atlas, &atlas_width, &atlas_height, NK_FONT_ATLAS_RGBA32);
|
.label = "Font sampler"
|
||||||
|
});
|
||||||
|
|
||||||
sg_image font_image = sg_make_image(&(sg_image_desc){
|
snk_image_t snk_img = snk_make_image(&(snk_image_desc_t){
|
||||||
.label = "Font image",
|
.image = font_image,
|
||||||
.width = atlas_width,
|
.sampler = font_sampler
|
||||||
.height = atlas_height,
|
});
|
||||||
.data.subimage[0][0] = (sg_range){
|
|
||||||
.ptr = pixels,
|
|
||||||
.size = atlas_width * atlas_height * 4
|
|
||||||
},
|
|
||||||
.pixel_format = SG_PIXELFORMAT_RGBA8
|
|
||||||
});
|
|
||||||
|
|
||||||
sg_sampler font_sampler = sg_make_sampler(&(sg_sampler_desc){
|
nk_font_atlas_end(atlas, snk_nkhandle(snk_img), NULL);
|
||||||
.label = "Font sampler"
|
|
||||||
});
|
|
||||||
|
|
||||||
snk_image_t snk_img = snk_make_image(&(snk_image_desc_t){
|
|
||||||
.image = font_image,
|
|
||||||
.sampler = font_sampler
|
|
||||||
});
|
|
||||||
|
|
||||||
nk_font_atlas_end(atlas, snk_nkhandle(snk_img), NULL);
|
|
||||||
} else {
|
|
||||||
log_fatal("Failed to allocate font atlas");
|
|
||||||
exit(EXIT_FAILURE);
|
|
||||||
}
|
|
||||||
|
|
||||||
return gui;
|
return gui;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue