a-game

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

commit 57ecaa4dadad18e18bf815bdf6c4a5138d490318
parent 66f4366df065100336460b81068e9d870d35199d
Author: amin <dev@aminmesbah.com>
Date:   Sat,  6 Jul 2019 02:12:39 +0000

Introduce textures to the rendering system

FossilOrigin-Name: b0d89636983a56f988f61c628f5b9a93d01287c31218a512b588c6ff87e8bda3
Diffstat:
Mshader/main_f.glsl | 4++--
Msrc/game.c | 27++++++++++++---------------
Msrc/render.c | 4++++
Msrc/render.h | 2++
Msrc/shader.c | 4++--
Msrc/shader.h | 2+-
6 files changed, 23 insertions(+), 20 deletions(-)

diff --git a/shader/main_f.glsl b/shader/main_f.glsl @@ -10,10 +10,10 @@ in vec2 tex_coord; out vec4 frag_color; -uniform float interp; +uniform float tex_interp; uniform sampler2D main_texture; void main() { - frag_color = mix(vertex_color, texture(main_texture, tex_coord), interp); + frag_color = mix(vertex_color, texture(main_texture, tex_coord), tex_interp); } diff --git a/src/game.c b/src/game.c @@ -610,13 +610,8 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga } } - renderer_jobs_sort(&game_state->renderer, &game_state->world_allocator); - renderer_jobs_draw(&game_state->renderer); - // 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}); @@ -624,19 +619,21 @@ void game_update_and_render(struct GameMemory *game_memory, struct GameInput *ga bork += dt; f32 scale_factor = fabsf(1.0f * sinf(bork)); model = math_scale(model, (v3) {scale_factor, scale_factor, 0.0f}); - - shader_setm4(&game_state->renderer.shader, SHADER_UNIFORM_MODEL, &model); - shader_setm4(&game_state->renderer.shader, SHADER_UNIFORM_PROJECTION, &game_state->renderer.projection); - shader_setm4(&game_state->renderer.shader, SHADER_UNIFORM_VIEW, &game_state->renderer.view); v3 color = {1.0f, 1.0f, 1.0f}; - shader_setv3(&game_state->renderer.shader, SHADER_UNIFORM_COLOR, &color); - shader_setf(&game_state->renderer.shader, SHADER_UNIFORM_INTERP, 1.0f); - glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, game_state->renderer.quad_ebo); - glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); - shader_setf(&game_state->renderer.shader, SHADER_UNIFORM_INTERP, 0.0f); - glDisable(GL_BLEND); + renderer_job_enqueue( + &game_state->renderer, + (struct RenderJob) { + .ebo = game_state->renderer.quad_ebo, + .color = color, + .model = model, + .tex_interp = 1.0f, + .layer = RENDER_LAYER_DEBUG, + }); } + + renderer_jobs_sort(&game_state->renderer, &game_state->world_allocator); + renderer_jobs_draw(&game_state->renderer); } internal void game_cleanup(struct GameMemory *game_memory) diff --git a/src/render.c b/src/render.c @@ -69,6 +69,9 @@ internal void renderer_init(struct RendererState *renderer, struct Image *images renderer->rect_ebo = rect_ebo; renderer->queue = mem_st_alloc_buffer(allocator, struct RenderJob, RENDER_QUEUE_SIZE); renderer->queue_count = 0; + + glEnable(GL_BLEND); + glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA); } internal void renderer_jobs_draw(struct RendererState *renderer) @@ -98,6 +101,7 @@ internal void renderer_jobs_draw(struct RendererState *renderer) } shader_setv3(&renderer->shader, SHADER_UNIFORM_COLOR, &j.color); shader_setm4(&renderer->shader, SHADER_UNIFORM_MODEL, &j.model); + shader_setf(&renderer->shader, SHADER_UNIFORM_TEX_INTERP, j.tex_interp); if (j.ebo == renderer->quad_ebo) { glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); diff --git a/src/render.h b/src/render.h @@ -9,11 +9,13 @@ enum RenderLayer NUM_RENDER_LAYERS, }; +// TODO: optimize packing struct RenderJob { GLuint ebo; v3 color; m4 model; + f32 tex_interp; enum RenderLayer layer; }; diff --git a/src/shader.c b/src/shader.c @@ -168,8 +168,8 @@ char *shader_uniform_get_name(enum ShaderUniform u) case SHADER_UNIFORM_COLOR: name = "color"; break; - case SHADER_UNIFORM_INTERP: - name = "interp"; + case SHADER_UNIFORM_TEX_INTERP: + name = "tex_interp"; break; case NUM_SHADER_UNIFORMS: // fallthrough diff --git a/src/shader.h b/src/shader.h @@ -4,7 +4,7 @@ enum ShaderUniform SHADER_UNIFORM_VIEW, SHADER_UNIFORM_PROJECTION, SHADER_UNIFORM_COLOR, - SHADER_UNIFORM_INTERP, + SHADER_UNIFORM_TEX_INTERP, NUM_SHADER_UNIFORMS, };