a-game

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

commit 66f4366df065100336460b81068e9d870d35199d
parent 57e5470d7c9c0622306388f50da065d6ba573246
Author: amin <dev@aminmesbah.com>
Date:   Sat,  6 Jul 2019 01:58:36 +0000

Set up texture in render init

FossilOrigin-Name: 3b41cef8cc73f494935bec54c397f08238b2c508777b1e826ab5987fa804a43c
Diffstat:
Msrc/game.c | 23+++++++----------------
Msrc/image.h | 6++++++
Msrc/render.c | 18+++++++++++++++++-
3 files changed, 30 insertions(+), 17 deletions(-)

diff --git a/src/game.c b/src/game.c @@ -190,7 +190,8 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) } size_t temp_free_marker = mem_st_get_marker(&game_state->temp_allocator); - // game_texture_load + struct Image img = {0}; + // game_load_images { size_t file_len = 0; //u8 *t = platform.platform_read_entire_file("assets/test_sw_origin.tga", &file_len); @@ -199,25 +200,15 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer) i32 img_w = 0; i32 img_h = 0; - u8 *img = img_load_from_memory(t, file_len, &img_w, &img_h, &game_state->temp_allocator); + u8 *data = img_load_from_memory(t, file_len, &img_w, &img_h, &game_state->temp_allocator); + assert(data); platform.platform_memory_free(t); - u32 texture_id; - { - glGenTextures(1, &texture_id); - glBindTexture(GL_TEXTURE_2D, texture_id); - glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img_w, img_h, 0, GL_BGRA, GL_UNSIGNED_BYTE, img); - glGenerateMipmap(GL_TEXTURE_2D); - - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); - glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); - glActiveTexture(GL_TEXTURE0); - } + img.data = data; + img.dim = (v2u) {img_w, img_h}; } - renderer_init(&game_state->renderer, &game_state->world_allocator); + renderer_init(&game_state->renderer, &img, 1, &game_state->world_allocator); mem_st_free_to_marker(&game_state->temp_allocator, temp_free_marker); } diff --git a/src/image.h b/src/image.h @@ -1,3 +1,9 @@ +struct Image +{ + v2u dim; + u8 *data; +}; + #pragma pack(push, 1) struct ImgTgaHeader { diff --git a/src/render.c b/src/render.c @@ -1,4 +1,4 @@ -internal void renderer_init(struct RendererState *renderer, struct StackAllocator *allocator) +internal void renderer_init(struct RendererState *renderer, struct Image *images, size_t num_images, struct StackAllocator *allocator) { GLfloat quad_vertices[] = { 0.5f, 0.5f, 1.0f, 0.0f, @@ -47,6 +47,22 @@ internal void renderer_init(struct RendererState *renderer, struct StackAllocato glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, quad_ebo); + for (u32 i = 0; i < num_images; i++) + { + struct Image img = images[i]; + u32 texture_id; + glGenTextures(1, &texture_id); + glBindTexture(GL_TEXTURE_2D, texture_id); + glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, img.dim.width, img.dim.height, 0, GL_BGRA, GL_UNSIGNED_BYTE, img.data); + glGenerateMipmap(GL_TEXTURE_2D); + + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_LINEAR); + glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST); + glActiveTexture(GL_TEXTURE0); + } + renderer->vao = vao; renderer->quad_vbo = quad_vbo; renderer->quad_ebo = quad_ebo;