a-game

2D platformer written from scratch.
Log | Files | Refs | README | LICENSE

commit 1ed16d25ee6990b9c5dae067cb338af49ad00821
parent 03f5e4940a204af67ba64fa7d7c2b0c1f6c7413a
Author: Amin Mesbah <dev@aminmesbah.com>
Date:   Sun,  9 Jun 2019 21:56:12 -0700

Fix two render order bugs

The first bug was that we were not binding an initial EBO at the
beginning of rendering each frame. That meant that the first frame was
rendered normally, but every subsequent used the EBO that was bound at
the end of the first.

The second was that we were using RENDER_LAYER_TILES as the initial
layer type when iterating the enum, which assumed that it was the first
value in the enum. Since we want to be able to change the order around,
we can't make that assumption. It caused very confusing render output
and was surprisingly difficult to track down.

Diffstat:
Msrc/render.c | 23++++++++++++++++++++---
Msrc/render.h | 2+-
2 files changed, 21 insertions(+), 4 deletions(-)

diff --git a/src/render.c b/src/render.c @@ -67,7 +67,8 @@ internal void render_jobs_draw(struct RendererState *renderer) shader_setm4(&renderer->shader, "view", &renderer->view); GLuint current_ebo = renderer->quad_ebo; - for (u32 i = 0; i < renderer->queue_count; i++) + glBindBuffer(GL_ELEMENT_ARRAY_BUFFER, current_ebo); + for (size_t i = 0; i < renderer->queue_count; i++) { struct RenderJob j = renderer->queue[i]; if (current_ebo != j.ebo) @@ -81,10 +82,16 @@ internal void render_jobs_draw(struct RendererState *renderer) { glDrawElements(GL_TRIANGLES, 6, GL_UNSIGNED_INT, 0); } - else + else if (j.ebo == renderer->rect_ebo) { glDrawElements(GL_LINES, 8, GL_UNSIGNED_INT, 0); } + else + { + assert(false); + } + + assert(j.layer < NUM_RENDER_LAYERS); } renderer->queue_count = 0; @@ -104,14 +111,24 @@ internal void render_jobs_sort(struct RendererState *renderer, struct StackAlloc layer_pop_count[j.layer] += 1; } + { + size_t sum = 0; + for (size_t i = 0; i < NUM_RENDER_LAYERS; i++) + { + sum += layer_pop_count[i]; + } + assert(sum == renderer->queue_count); + } + struct RenderJob *sorted = mem_st_alloc_buffer(allocator, struct RenderJob, renderer->queue_count); size_t layer_pop_count_sorted[NUM_RENDER_LAYERS] = {0}; for (size_t i = 0; i < renderer->queue_count; i++) { struct RenderJob j = queue[i]; + assert(j.layer < NUM_RENDER_LAYERS); size_t insertion_index = 0; - for (enum RenderLayer l = RENDER_LAYER_TILES; l < j.layer; l++) + for (enum RenderLayer l = 0; l < j.layer; l++) { insertion_index += layer_pop_count[l]; } diff --git a/src/render.h b/src/render.h @@ -3,8 +3,8 @@ enum RenderLayer { RENDER_LAYER_TILES = 0, - RENDER_LAYER_DEBUG, RENDER_LAYER_PLAYER, + RENDER_LAYER_DEBUG, NUM_RENDER_LAYERS, };