a-game

2D platformer written from scratch.
git clone git://git.amin.space/a-game.git
Log | Files | Refs | README | LICENSE

commit 7ebb1b3de8641cf9fd5841599a1b1ba069da639d
parent 5a27d3f5c4e4ad57d311a5de1f74316dedd9f8dc
Author: amin <dev@aminmesbah.com>
Date:   Thu,  4 Jul 2019 21:31:19 +0000

Return file length of loaded files

FossilOrigin-Name: a5963a8e34800f8928e848b93a324fe39d497744693102a86bef9b649154b55e
Diffstat:
Msrc/game.c | 16++++++++++++++--
Msrc/platform.h | 2+-
Msrc/platform_linux.c | 7++++++-
Msrc/platform_wasm.c | 1+
Msrc/platform_windows.c | 7++++++-
5 files changed, 28 insertions(+), 5 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -170,8 +170,8 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) struct PlatformApi platform = game_memory->platform; // game_shader_load { - char *v_source = (char *)platform.platform_read_entire_file("shader/main_v.glsl"); - char *f_source = (char *)platform.platform_read_entire_file("shader/main_f.glsl"); + char *v_source = (char *)platform.platform_read_entire_file("shader/main_v.glsl", NULL); + char *f_source = (char *)platform.platform_read_entire_file("shader/main_f.glsl", NULL); struct Shader main_shader = {0}; if (!shader_compile(v_source, f_source, &main_shader)) { @@ -182,6 +182,18 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) game_state->renderer.shader = main_shader; } + // game_texture_load + { + //size_t file_len = 0; + //u8 *t = platform.platform_read_entire_file("assets/test_rle_sw_origin.tga", &file_len); + //assert(t); + //i32 img_w = 0; + //i32 img_h = 0; + //u8 img = img_load_from_memory(t, file_len, &img_w, &img_h); + + //platform.platform_memory_free(t); + } + renderer_init(&game_state->renderer, &game_state->world_allocator); } diff --git a/src/platform.h b/src/platform.h @@ -5,7 +5,7 @@ #define KIBIBYTES(n) (n) * 1024LL #define MEBIBYTES(n) KIBIBYTES((n) * 1024LL) -#define PLATFORM_READ_ENTIRE_FILE(name) u8 *(name)(char *file_path) +#define PLATFORM_READ_ENTIRE_FILE(name) u8 *(name)(char *file_path, size_t *out_num_bytes) typedef PLATFORM_READ_ENTIRE_FILE(platform_read_entire_file_func); #define PLATFORM_MEMORY_FREE(name) void (name)(void *ptr) diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -219,8 +219,13 @@ internal PLATFORM_READ_ENTIRE_FILE(linux_read_entire_file) u32 num_bytes_in_file = ftell(handle); rewind(handle); + if (out_num_bytes) + { + *out_num_bytes = num_bytes_in_file; + } + // TODO: replace malloc with own allocator so I stop having nightmares - buffer = (u8*) malloc(sizeof(u8) * (num_bytes_in_file + 1) ); + buffer = malloc(sizeof(u8) * (num_bytes_in_file + 1) ); u32 bytes_read = fread(buffer, sizeof(u8), num_bytes_in_file, handle); // IMPORTANT! fread() doesn't add the '\0' diff --git a/src/platform_wasm.c b/src/platform_wasm.c @@ -115,6 +115,7 @@ PLATFORM_MEMORY_FREE(wasm_memory_free) // do nothing } +// TODO: robustly return file length PLATFORM_READ_ENTIRE_FILE(wasm_read_entire_file) { g_mem_buffer_i++; diff --git a/src/platform_windows.c b/src/platform_windows.c @@ -183,8 +183,13 @@ internal PLATFORM_READ_ENTIRE_FILE(windows_read_entire_file) u32 num_bytes_in_file = ftell(handle); rewind(handle); + if (out_num_bytes) + { + *out_num_bytes = num_bytes_in_file; + } + // TODO: replace malloc with own allocator so I stop having nightmares - buffer = (u8*) malloc(sizeof(u8) * (num_bytes_in_file + 1) ); + buffer = malloc(sizeof(u8) * (num_bytes_in_file + 1)); u32 bytes_read = fread(buffer, sizeof(u8), num_bytes_in_file, handle); // IMPORTANT! fread() doesn't add the '\0'