commit 7cefbbfaaf9a00f366afce85b9f7d3d9e65ed015
parent 7ebb1b3de8641cf9fd5841599a1b1ba069da639d
Author: amin <dev@aminmesbah.com>
Date: Fri, 5 Jul 2019 01:39:11 +0000
Load an upside down tga image
FossilOrigin-Name: 38db4646141af80958449ba84c24cb32878d36cd06f44c7ac4c9585d901c4fd9
Diffstat:
M | src/game.c | | | 101 | ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++------- |
M | src/render.c | | | 31 | ------------------------------- |
2 files changed, 92 insertions(+), 40 deletions(-)
diff --git a/src/game.c b/src/game.c
@@ -11,6 +11,70 @@
#include "world.c"
#include "input.c"
+#pragma pack(push, 1)
+struct ImgTgaHeader
+{
+ // http://paulbourke.net/dataformats/tga/
+ u8 id_length;
+ u8 color_map_type;
+ u8 data_type_code;
+ u16 color_map_origin;
+ u16 color_map_length;
+ u8 color_map_depth;
+ u16 x_origin;
+ u16 y_origin;
+ u16 width;
+ u16 height;
+ u8 bits_per_pixel;
+ u8 image_descriptor;
+};
+#pragma pack(pop)
+static_assert(sizeof(struct ImgTgaHeader) == 18, "");
+
+void img_tga_header_print(struct ImgTgaHeader h)
+{
+ printf("id_length: %u\n", h.id_length);
+ printf("color_map_type: %u\n", h.color_map_type);
+ printf("data_type_code: %u\n", h.data_type_code);
+ printf("color_map_origin: %u\n", h.color_map_origin);
+ printf("color_map_length: %u\n", h.color_map_length);
+ printf("color_map_depth: %u\n", h.color_map_depth);
+ printf("x_origin: %u\n", h.x_origin);
+ printf("y_origin: %u\n", h.y_origin);
+ printf("width: %u\n", h.width);
+ printf("height: %u\n", h.height);
+ printf("bits_per_pixel: %u\n", h.bits_per_pixel);
+ printf("image_descriptor: %u\n", h.image_descriptor);
+}
+
+internal u8 *img_load_from_memory(u8 *buffer, size_t len, i32 *out_width, i32 *out_height, struct StackAllocator *allocator)
+{
+ struct ImgTgaHeader h = *(struct ImgTgaHeader *)buffer;
+ img_tga_header_print(h);
+ // Color maps not currently supported
+ assert(h.color_map_type == 0);
+ // Only uncompressed RGB is supported
+ assert(h.data_type_code == 2);
+
+ if (out_width && out_height)
+ {
+ *out_width = h.width;
+ *out_height = h.height;
+ }
+
+ buffer += sizeof(struct ImgTgaHeader);
+
+ u32 bytes_per_pixel = h.bits_per_pixel / 8;
+ size_t pixel_buffer_length = h.width * h.height * bytes_per_pixel;
+ u8 *pixels = mem_st_alloc_buffer(allocator, u8, pixel_buffer_length);
+ for (size_t i = 0; i < pixel_buffer_length; i++)
+ {
+ pixels[i] = *buffer++;
+ }
+
+ return pixels;
+}
+
internal void move_mode_print(enum MoveMode s)
{
switch(s)
@@ -184,14 +248,30 @@ internal void game_init(struct GameMemory *game_memory, v2u framebuffer)
// 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);
+ size_t file_len = 0;
+ u8 *t = platform.platform_read_entire_file("assets/test_rle_sw_origin.tga", &file_len);
+ assert(t);
+
+ size_t image_load_free_marker = mem_st_get_marker(&game_state->world_allocator);
+ i32 img_w = 0;
+ i32 img_h = 0;
+ u8 *img = img_load_from_memory(t, file_len, &img_w, &img_h, &game_state->world_allocator);
+ 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_NEAREST_MIPMAP_NEAREST);
+ glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
+ glActiveTexture(GL_TEXTURE0);
+ }
+ mem_st_free_to_marker(&game_state->world_allocator, image_load_free_marker);
}
renderer_init(&game_state->renderer, &game_state->world_allocator);
@@ -260,7 +340,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
// game_update_player
{
struct Entity *player = &game_state->player;
- move_mode_print(player->move_mode);
+ //move_mode_print(player->move_mode);
u32 button_states = game_input->button_states;
if (dt >= 0.5f)
{
@@ -600,6 +680,8 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
// texture test
{
+ glEnable(GL_BLEND);
+ glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
m4 model = math_m4_init_id();
model = math_translate(model, (v3) {12.5f, 5.0f, 0.0f});
model = math_scale(model, (v3) {10.0f, 10.0f, 10.0f});
@@ -618,6 +700,7 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga
glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0);
shader_setf(&game_state->renderer.shader, SHADER_UNIFORM_INTERP, 0.0f);
+ glDisable(GL_BLEND);
}
}
diff --git a/src/render.c b/src/render.c
@@ -53,37 +53,6 @@ internal void renderer_init(struct RendererState *renderer, struct StackAllocato
renderer->rect_ebo = rect_ebo;
renderer->queue = mem_st_alloc_buffer(allocator, struct RenderJob, RENDER_QUEUE_SIZE);
renderer->queue_count = 0;
-
- u32 texture_id;
- {
- // TODO: flip textures
-#define TEX_DIM 32
- u32 test_texture[TEX_DIM][TEX_DIM];
-
- for (u32 y = 0; y < TEX_DIM; ++y)
- {
- for (u32 x = 0; x < TEX_DIM; ++x)
- {
- u32 xor_value = (x * 256 / TEX_DIM) ^ (y * 256 / TEX_DIM);
- u32 r = xor_value << 16;
- u32 g = xor_value << 8;
- u32 b = xor_value;
- test_texture[y][x] = r | g | b;
- }
- }
-
- glGenTextures(1, &texture_id);
- glBindTexture(GL_TEXTURE_2D, texture_id);
- glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, TEX_DIM, TEX_DIM, 0, GL_RGBA, GL_UNSIGNED_BYTE, test_texture);
- glGenerateMipmap(GL_TEXTURE_2D);
-
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST_MIPMAP_NEAREST);
- glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
- glActiveTexture(GL_TEXTURE0);
-#undef TEX_DIM
- }
}
internal void renderer_jobs_draw(struct RendererState *renderer)