introduce a sorted render queue

efd/v1
Dominik Madarász 2022-10-15 23:10:46 +02:00
parent 2040984666
commit a7622ceeec
6 changed files with 902 additions and 779 deletions

View File

@ -10,6 +10,7 @@
#include "world/entity_view.h"
#include "core/camera.h"
#include "platform/profiler.h"
#include "platform/renderer.h"
#include "flecs/flecs_os_api_stdcpp.h"
#include "flecs.h"
@ -267,3 +268,44 @@ void game_request_close() {
platform_request_close();
}
}
typedef struct {
uint64_t key;
entity_view *data;
zpl_f32 y;
} game_world_render_entry;
static game_world_render_entry* render_queue = NULL;
static void game__world_view_render_push_entry(uint64_t key, entity_view * data) {
if (data->kind == EKIND_CHUNK) return;
if (!data) return;
game_world_render_entry entry = {
.key = key,
.data = data,
.y = data->y,
};
zpl_array_append(render_queue, entry);
}
static void game__world_view_render_ground(uint64_t key, entity_view * data) {
if (data->kind != EKIND_CHUNK) return;
renderer_draw_entry(key, data);
}
void game_world_view_render_world(void) {
if (!render_queue) {
zpl_array_init(render_queue, zpl_heap());
}
zpl_array_clear(render_queue);
game_world_view_active_entity_map(game__world_view_render_push_entry);
zpl_sort_array(render_queue, zpl_array_count(render_queue), zpl_f32_cmp(zpl_offset_of(game_world_render_entry, y)));
game_world_view_active_entity_map(game__world_view_render_ground);
for (zpl_isize i = 0; i < zpl_array_count(render_queue); i++) {
renderer_draw_entry(render_queue[i].key, render_queue[i].data);
}
}

View File

@ -32,6 +32,7 @@ void game_world_view_set_active(world_view *view);
void game_world_view_cycle_active(int8_t dir);
void game_world_view_active_entity_map(void (*map_proc)(uint64_t key, entity_view * value));
entity_view *game_world_view_active_get_entity(uint64_t ent_id);
void game_world_view_render_world(void);
//~ NOTE(zaklaus): viewer -> host actions
void game_action_send_keystate(game_keystate_data *data);

View File

@ -9,5 +9,6 @@ void renderer_shutdown(void);
void renderer_debug_draw(void);
float renderer_zoom_get(void);
void renderer_draw_single(float x, float y, asset_id id, Color color);
void renderer_draw_entry(uint64_t key, entity_view * data);
void renderer_bake_chunk(uint64_t key, entity_view * data);
void renderer_switch(int kind);

View File

@ -7,7 +7,41 @@ static float zoom_overlay_tran = 0.0f;
float zpl_lerp(float,float,float);
float zpl_to_degrees(float);
void DEBUG_draw_ground(uint64_t key, entity_view * data) {
void DrawNametag(const char* name, uint64_t key, entity_view *data, float x, float y) {
float size = 16.f;
float font_size = lerp(4.0f, 32.0f, 0.5f/(float)render_camera.zoom);
float font_spacing = 1.1f;
float title_bg_offset = 4;
float fixed_title_offset = 8.f;
float health = (data->hp / data->max_hp);
const char *title = TextFormat("%s %llu", name, key);
float title_w = MeasureTextEco(title, font_size, font_spacing);
DrawRectangleEco(x-title_w/2.f-title_bg_offset/2.f, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(BLACK, data->tran_time));
DrawRectangleEco(x-title_w/2.f-title_bg_offset/2.f, y-size-fixed_title_offset, title_w*health+title_bg_offset, font_size*0.2f, ColorAlpha(RED, data->tran_time));
DrawTextEco(title, x-title_w/2.f, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing);
}
void DEBUG_draw_overlay(uint64_t key, entity_view * data) {
switch (data->kind) {
case EKIND_CHUNK: {
world_view *view = game_world_view_get_active();
float size = (float)(view->chunk_size * WORLD_BLOCK_SIZE);
float offset = 0.0;
float x = data->x * size + offset;
float y = data->y * size + offset;
DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV((float)data->color, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran*0.75f));
DrawTextEco(TextFormat("%d %d", (int)data->x, (int)data->y), x+15.0f, y+15.0f, 200 , ColorAlpha(BLACK, data->tran_time*zoom_overlay_tran), 0.0);
}break;
default:break;
}
}
void renderer_draw_entry(uint64_t key, entity_view *data) {
float size = 16.f;
switch (data->kind) {
case EKIND_CHUNK: {
world_view *view = game_world_view_get_active();
@ -32,19 +66,70 @@ void DEBUG_draw_ground(uint64_t key, entity_view * data) {
}
}
}break;
case EKIND_VEHICLE: {
float x = data->x;
float y = data->y;
float const w = (float)(data->veh_kind == 0 ? 80 : data->veh_kind == 1 ? 120 : 135);
float const h = 50;
Color color = data->veh_kind == 0 ? RED : data->veh_kind == 1 ? GREEN : BLUE;
DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(color, data->tran_time));
}break;
case EKIND_DEVICE:{
float x = data->x - 32.f;
float y = data->y - 32.f;
DrawTexturePro(GetSpriteTexture2D(assets_find(data->asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
default:break;
}
}
void DEBUG_draw_entities(uint64_t key, entity_view * data) {
float size = 16.f;
switch (data->kind) {
if (data->progress_active) {
float w = 64.f;
float h = 8.f;
float p = data->progress_value;
float x = data->x - w/2.f;
float y = data->y - 32.f - h;
DrawRectangleEco(x, y, w, h, ColorAlpha(BLACK, data->tran_time));
DrawRectangleEco(x, y, w*p, h, ColorAlpha(GREEN, data->tran_time));
}
}break;
case EKIND_DEMO_NPC: {
float x = data->x;
float y = data->y;
DrawNametag("Demo", key, data, x, y);
DrawCircleEco(x, y, size, ColorAlpha(BLUE, data->tran_time));
}break;
case EKIND_PLAYER: {
float x = data->x;
float y = data->y;
float health = (data->hp / data->max_hp);
DrawNametag("Player", key, data, x, y);
DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time));
if (data->has_items && !data->inside_vehicle) {
float ix = data->x;
float iy = data->y;
if (data->items[data->selected_item].quantity > 0) {
asset_id it_kind = data->items[data->selected_item].kind;
uint32_t qty = data->items[data->selected_item].quantity;
DrawTexturePro(GetSpriteTexture2D(assets_find(it_kind)), ASSET_SRC_RECT(), ((Rectangle){ix, iy, 32, 32}), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
}
}
}break;
case EKIND_MACRO_BOT: {
float x = data->x;
float y = data->y;
DrawNametag("Bot", key, data, x, y);
}break;
case EKIND_ITEM: {
float x = data->x - 32.f;
float y = data->y - 32.f;
DrawTexturePro(GetSpriteTexture2D(assets_find(data->asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
if (data->quantity > 1) {
DrawTextEco(zpl_bprintf("%d", data->quantity), x, y, 10, ALPHA(RAYWHITE), 0.0f);
}
if (data->durability < 1.0f) {
DrawRectangleEco(x, y+32, 4, 32, BlendColor(RED, GREEN, data->durability));
DrawRectangleEco(x, y+32, 4, 32*(1.0f-data->durability), ColorAlpha(BLACK, data->tran_time));
}
}break;
default:break;
}
@ -57,11 +142,17 @@ void renderer_draw(void) {
camera game_camera = camera_get();
render_camera.target = (Vector2){(float)game_camera.x, (float)game_camera.y};
zoom_overlay_tran = zpl_lerp(zoom_overlay_tran, (target_zoom <= CAM_OVERLAY_ZOOM_LEVEL) ? 1.0f : 0.0f, GetFrameTime()*2.0f);
ClearBackground(GetColor(0x222034));
BeginMode2D(render_camera);
game_world_view_active_entity_map(DEBUG_draw_ground);
game_world_view_active_entity_map(DEBUG_draw_entities);
game_world_view_render_world();
if (zoom_overlay_tran > 0.02f) {
game_world_view_active_entity_map(DEBUG_draw_overlay);
}
EndMode2D();
}
@ -73,12 +164,15 @@ void renderer_init(void) {
render_camera.target = (Vector2){0.0f,0.0f};
render_camera.offset = (Vector2){(float)(screenWidth >> 1), (float)(screenHeight >> 1)};
render_camera.rotation = 0.0f;
render_camera.zoom = 1.5f;
render_camera.zoom = 2.9f;
// NOTE(zaklaus): Paint the screen before we load the game
// TODO(zaklaus): Render a cool loading screen background maybe? :wink: :wink:
BeginDrawing();
ClearBackground(GetColor(0x222034));
char const *loading_text = "demo is loading...";
char const *loading_text = "zpl.eco2d is loading...";
int text_w = MeasureText(loading_text, 120);
DrawText(loading_text, GetScreenWidth()-text_w-15, GetScreenHeight()-135, 120, RAYWHITE);
EndDrawing();

View File

@ -21,7 +21,29 @@ void DrawNametag(const char* name, uint64_t key, entity_view *data, float x, flo
DrawTextEco(title, x-title_w/2.f, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing);
}
void DEBUG_draw_ground(uint64_t key, entity_view * data) {
void DEBUG_draw_overlay(uint64_t key, entity_view * data) {
switch (data->kind) {
case EKIND_CHUNK: {
world_view *view = game_world_view_get_active();
float size = (float)(view->chunk_size * WORLD_BLOCK_SIZE);
float offset = 0.0;
float x = data->x * size + offset;
float y = data->y * size + offset;
DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV((float)data->color, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran*0.75f));
DrawTextEco(TextFormat("%d %d", (int)data->x, (int)data->y), x+15.0f, y+15.0f, 200 , ColorAlpha(BLACK, data->tran_time*zoom_overlay_tran), 0.0);
}break;
default:break;
}
}
extern bool inv_is_open;
void renderer_draw_entry(uint64_t key, entity_view *data) {
float size = 16.f;
switch (data->kind) {
case EKIND_CHUNK: {
world_view *view = game_world_view_get_active();
@ -46,35 +68,29 @@ void DEBUG_draw_ground(uint64_t key, entity_view * data) {
}
}
}break;
default:break;
}
}
void DEBUG_draw_overlay(uint64_t key, entity_view * data) {
switch (data->kind) {
case EKIND_CHUNK: {
world_view *view = game_world_view_get_active();
float size = (float)(view->chunk_size * WORLD_BLOCK_SIZE);
float offset = 0.0;
float x = data->x * size + offset;
float y = data->y * size + offset;
DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV((float)data->color, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran*0.75f));
DrawTextEco(TextFormat("%d %d", (int)data->x, (int)data->y), x+15.0f, y+15.0f, 200 , ColorAlpha(BLACK, data->tran_time*zoom_overlay_tran), 0.0);
case EKIND_VEHICLE: {
float x = data->x;
float y = data->y;
float const w = (float)(data->veh_kind == 0 ? 80 : data->veh_kind == 1 ? 120 : 135);
float const h = 50;
Color color = data->veh_kind == 0 ? RED : data->veh_kind == 1 ? GREEN : BLUE;
DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(color, data->tran_time));
}break;
case EKIND_DEVICE:{
float x = data->x - 32.f;
float y = data->y - 32.f;
DrawTexturePro(GetSpriteTexture2D(assets_find(data->asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
default:break;
}
}
extern bool inv_is_open;
void DEBUG_draw_entities(uint64_t key, entity_view * data) {
float size = 16.f;
switch (data->kind) {
if (data->progress_active) {
float w = 64.f;
float h = 8.f;
float p = data->progress_value;
float x = data->x - w/2.f;
float y = data->y - 32.f - h;
DrawRectangleEco(x, y, w, h, ColorAlpha(BLACK, data->tran_time));
DrawRectangleEco(x, y, w*p, h, ColorAlpha(GREEN, data->tran_time));
}
}break;
case EKIND_DEMO_NPC: {
float x = data->x;
float y = data->y;
@ -124,37 +140,6 @@ void DEBUG_draw_entities(uint64_t key, entity_view * data) {
}
}
void DEBUG_draw_entities_low(uint64_t key, entity_view * data) {
(void)key;
switch (data->kind) {
case EKIND_VEHICLE: {
float x = data->x;
float y = data->y;
float const w = (float)(data->veh_kind == 0 ? 80 : data->veh_kind == 1 ? 120 : 135);
float const h = 50;
Color color = data->veh_kind == 0 ? RED : data->veh_kind == 1 ? GREEN : BLUE;
DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(color, data->tran_time));
}break;
case EKIND_DEVICE:{
float x = data->x - 32.f;
float y = data->y - 32.f;
DrawTexturePro(GetSpriteTexture2D(assets_find(data->asset)), ASSET_SRC_RECT(), ASSET_DST_RECT(x,y), (Vector2){0.5f,0.5f}, 0.0f, ALPHA(WHITE));
if (data->progress_active) {
float w = 64.f;
float h = 8.f;
float p = data->progress_value;
float x = data->x - w/2.f;
float y = data->y - 32.f - h;
DrawRectangleEco(x, y, w, h, ColorAlpha(BLACK, data->tran_time));
DrawRectangleEco(x, y, w*p, h, ColorAlpha(GREEN, data->tran_time));
}
}break;
default:break;
}
}
void renderer_draw(void) {
render_camera.offset = (Vector2){(float)(screenWidth >> 1), (float)(screenHeight >> 1)};
render_camera.zoom = zpl_lerp(render_camera.zoom, target_zoom, GetFrameTime()*2.9978f);
@ -167,9 +152,8 @@ void renderer_draw(void) {
ClearBackground(GetColor(0x222034));
BeginMode2D(render_camera);
game_world_view_active_entity_map(DEBUG_draw_ground);
game_world_view_active_entity_map(DEBUG_draw_entities_low);
game_world_view_active_entity_map(DEBUG_draw_entities);
game_world_view_render_world();
if (zoom_overlay_tran > 0.02f) {
game_world_view_active_entity_map(DEBUG_draw_overlay);

19
code/vendors/zpl.h vendored
View File

@ -34,6 +34,7 @@ GitHub:
https://github.com/zpl-c/zpl
Version History:
18.1.1 - fix zpl sort procs
18.1.0 - added table _clear method
18.0.4 - fix memory arena alignment & added tests
18.0.3 - fix emscripten support
@ -406,7 +407,7 @@ License:
#define ZPL_VERSION_MAJOR 18
#define ZPL_VERSION_MINOR 1
#define ZPL_VERSION_PATCH 1
#define ZPL_VERSION_PATCH 2
#define ZPL_VERSION_PRE ""
// file: zpl_hedley.h
@ -5977,14 +5978,14 @@ License:
// e.g. zpl_i32_cmp(zpl_offset_of(Thing, value))
// Use 0 if it's just the type instead.
ZPL_DEF ZPL_COMPARE_PROC_PTR(i16_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(u8_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(i32_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(i64_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(isize_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(str_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(f32_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(f64_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(zpl_i16_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(zpl_u8_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(zpl_i32_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(zpl_i64_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(zpl_isize_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(zpl_str_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(zpl_f32_cmp(zpl_isize offset));
ZPL_DEF ZPL_COMPARE_PROC_PTR(zpl_f64_cmp(zpl_isize offset));
// TODO: Better sorting algorithms