a-game

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

commit fbf146edf989a236edea2b95bf4eacc75e9cd106
parent c4db8d188ef28e4f6a8c817a82e19dbc0556af38
Author: amin <dev@aminmesbah.com>
Date:   Tue, 16 Apr 2019 22:04:09 +0000

Load shader files via the platform api

FossilOrigin-Name: fd8ee1dc5cddf96e8ea0e71ea87a86e13617e242f70f74db63143f264028e57d
Diffstat:
Msrc/game.c | 13++++++++++---
Msrc/platform_linux.c | 73+++++++++++++++++++++++++++++++++++++++++++++++++------------------------
Msrc/platform_linux.h | 12++++++++++++
Msrc/shader.c | 45+--------------------------------------------
Msrc/shader.h | 2+-
5 files changed, 73 insertions(+), 72 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -59,9 +59,16 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) game_state->tiles.count = 1; } - struct Shader main_shader = shader_compile("shader/main_v.glsl", "shader/main_f.glsl"); - game_state->tiles.shader = main_shader; - game_state->player.shader = main_shader; + { + struct PlatformApi platform = game_memory->platform; + char *v_source = platform.platform_read_entire_file("shader/main_v.glsl"); + char *f_source = platform.platform_read_entire_file("shader/main_f.glsl"); + struct Shader main_shader = shader_compile(v_source, f_source); + free(v_source); + free(f_source); + game_state->tiles.shader = main_shader; + game_state->player.shader = main_shader; + } } // NOTE(amin): For now updating and rendering are interleaved. We simulate the diff --git a/src/platform_linux.c b/src/platform_linux.c @@ -10,24 +10,6 @@ #include "platform.h" #include "platform_linux.h" -internal void error_callback(int error, const char* description); -internal void framebuffer_size_callback(GLFWwindow* window, int width, int height); -internal void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); - -#ifdef PLATFORM_HOTLOAD_GAME_CODE -internal time_t file_get_modified_time(char *file_path); -internal void unload_game_code(struct GameCode *game_code); -internal struct GameCode load_game_code(char *source_lib_path); -#endif - -#ifdef GLAD_DEBUG -internal void pre_gl_callback(const char *func_name, void *func_ptr, int len_args, ...) -{ - printf("Calling: %s (%d arguments)\n", func_name, len_args); -} -#endif - - int main(void) { glfwSetErrorCallback(error_callback); @@ -69,8 +51,12 @@ int main(void) #endif struct GameState game_state = {0}; - struct GameMemory game_memory = {0}; - game_memory.game_state = &game_state; + struct GameMemory game_memory = { + .game_state = &game_state, + .platform = { + &linux_read_entire_file, + }, + }; struct GameInput game_input = {0}; glfwSetWindowUserPointer(window, &game_input); @@ -136,19 +122,16 @@ int main(void) return 0; } - internal void error_callback(int error, const char* description) { fprintf(stderr, "Error: %s\n", description); } - internal void framebuffer_size_callback(GLFWwindow* window, int width, int height) { glViewport(0, 0, width, height); } - internal void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) { struct GameInput *game_input = (struct GameInput*)glfwGetWindowUserPointer(window); @@ -195,7 +178,6 @@ internal void key_callback(GLFWwindow* window, int key, int scancode, int action } } - internal time_t file_get_modified_time(char *file_path) { time_t mtime = 0; @@ -212,6 +194,42 @@ internal time_t file_get_modified_time(char *file_path) return mtime; } +internal PLATFORM_READ_ENTIRE_FILE(linux_read_entire_file) +{ + FILE *handle = fopen(file_path, "r"); + char *buffer = NULL; + + if (handle) + { + // get file size + fseek(handle, 0, SEEK_END); + u32 num_bytes_in_file = ftell(handle); + rewind(handle); + + // TODO: replace malloc with own allocator so I stop having nightmares + buffer = (char*) malloc(sizeof(char) * (num_bytes_in_file + 1) ); + + u32 bytes_read = fread(buffer, sizeof(char), num_bytes_in_file, handle); + // IMPORTANT! fread() doesn't add the '\0' + buffer[num_bytes_in_file] = '\0'; + + if (num_bytes_in_file != bytes_read) + { + free(buffer); + buffer = NULL; + } + + fclose(handle); + } + else + { + printf("Error: Couldn't open file at path: %s", file_path); + // TODO: handle errors here in a better way + exit(1); + } + + return buffer; +} #ifdef PLATFORM_HOTLOAD_GAME_CODE internal void unload_game_code(struct GameCode *game_code) @@ -266,3 +284,10 @@ internal struct GameCode load_game_code(char *source_lib_path) return game_code; } #endif // PLATFORM_HOTLOAD_GAME_CODE + +#ifdef GLAD_DEBUG +internal void pre_gl_callback(const char *func_name, void *func_ptr, int len_args, ...) +{ + printf("Calling: %s (%d arguments)\n", func_name, len_args); +} +#endif diff --git a/src/platform_linux.h b/src/platform_linux.h @@ -1,6 +1,18 @@ #define PLATFORM_SCR_WIDTH 600u #define PLATFORM_SCR_HEIGHT 600u +internal void error_callback(int error, const char* description); +internal void framebuffer_size_callback(GLFWwindow* window, int width, int height); +internal void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods); +internal PLATFORM_READ_ENTIRE_FILE(linux_read_entire_file); + #ifdef PLATFORM_HOTLOAD_GAME_CODE #define PLATFORM_GAME_LIB_PATH "./out/release/game.so" +internal time_t file_get_modified_time(char *file_path); +internal void unload_game_code(struct GameCode *game_code); +internal struct GameCode load_game_code(char *source_lib_path); +#endif + +#ifdef GLAD_DEBUG +internal void pre_gl_callback(const char *func_name, void *func_ptr, int len_args, ...); #endif diff --git a/src/shader.c b/src/shader.c @@ -1,45 +1,5 @@ -internal char *read_file(char *file_path) +internal struct Shader shader_compile(const GLchar *vertex_shader_source, const GLchar *fragment_shader_source) { - FILE *handle = fopen(file_path, "r"); - char *buffer = NULL; - - if (handle) - { - // get file size - fseek(handle, 0, SEEK_END); - u32 num_bytes_in_file = ftell(handle); - rewind(handle); - - buffer = (char*) malloc(sizeof(char) * (num_bytes_in_file + 1) ); - - u32 bytes_read = fread(buffer, sizeof(char), num_bytes_in_file, handle); - // IMPORTANT! fread() doesn't add the '\0' - buffer[num_bytes_in_file] = '\0'; - - if (num_bytes_in_file != bytes_read) - { - free(buffer); - buffer = NULL; - } - - fclose(handle); - } - else - { - printf("Error: Couldn't open file at path: %s", file_path); - // TODO: handle errors here in a better way - exit(1); - } - - return buffer; -} - - -internal struct Shader shader_compile(GLchar *vertex_path, GLchar *fragment_path) -{ - const GLchar *vertex_shader_source = read_file(vertex_path); - const GLchar *fragment_shader_source = read_file(fragment_path); - GLint success; GLchar info_log[512]; @@ -91,9 +51,6 @@ internal struct Shader shader_compile(GLchar *vertex_path, GLchar *fragment_path glDeleteShader(fragment_shader); glDeleteShader(vertex_shader); - free((GLchar*)fragment_shader_source); - free((GLchar*)vertex_shader_source); - return s; } } diff --git a/src/shader.h b/src/shader.h @@ -4,7 +4,7 @@ struct Shader }; internal char *read_file(char *file_path); -internal struct Shader shader_compile(GLchar *vertex_path, GLchar *fragment_path); +internal struct Shader shader_compile(const GLchar *vertex_shader_source, const GLchar *fragment_shader_source); internal void shader_use(struct Shader *s); internal void shader_setb(struct Shader *s, char *name, bool value); internal void shader_seti(struct Shader *s, char *name, int value);