From d0a31f78a528d22af196f68cdb81b1e9d14c6bce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 11 Aug 2021 14:16:23 +0200 Subject: [PATCH] cli: refactor renderer --- code/game/src/platform_raylib.c | 137 +++----------------------------- code/game/src/renderer_v0.c | 118 +++++++++++++++++++++++++++ 2 files changed, 128 insertions(+), 127 deletions(-) create mode 100644 code/game/src/renderer_v0.c diff --git a/code/game/src/platform_raylib.c b/code/game/src/platform_raylib.c index bf1e9f6..69ffa4e 100644 --- a/code/game/src/platform_raylib.c +++ b/code/game/src/platform_raylib.c @@ -23,6 +23,8 @@ static bool request_shutdown; static Camera2D render_camera; +#include "renderer_v0.c" + void platform_init() { InitWindow(screenWidth, screenHeight, "eco2d - client"); SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE); @@ -107,17 +109,8 @@ void platform_input() { } } -void display_conn_status(); - -void DEBUG_draw_entities_low(uint64_t key, entity_view * data); -void DEBUG_draw_entities(uint64_t key, entity_view * data); -void DEBUG_draw_ground(uint64_t key, entity_view * data); - -void lerp_entity_positions(uint64_t key, entity_view * data); void do_entity_fadeinout(uint64_t key, entity_view * data); - -float zpl_lerp(float,float,float); -float zpl_to_degrees(float); +void lerp_entity_positions(uint64_t key, entity_view *data); void platform_render() { profile(PROF_ENTITY_LERP) { @@ -131,123 +124,25 @@ void platform_render() { render_camera.target = (Vector2){game_camera.x, game_camera.y}; zoom_overlay_tran = zpl_lerp(zoom_overlay_tran, (target_zoom <= CAM_OVERLAY_ZOOM_LEVEL) ? 1.0f : 0.0f, GetFrameTime()*2.0f); - BeginDrawing(); - profile (PROF_RENDER) { - 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); - EndMode2D(); - display_conn_status(); - } - debug_draw(); - EndDrawing(); + renderer_draw(); if (request_shutdown) { CloseWindow(); } } -void display_conn_status() { - if (game_is_networked()) { - if (network_client_is_connected()) { - DrawText("Connection: online", 5, 5, 12, GREEN); - } else { - DrawText("Connection: offline", 5, 5, 12, RED); - } - } else { - DrawText("Connection: single-player", 5, 5, 12, BLUE); - } +float platform_frametime() { + return GetFrameTime(); } -void DEBUG_draw_ground(uint64_t key, entity_view * data) { - (void)key; - switch (data->kind) { - case EKIND_CHUNK: { - world_view *view = game_world_view_get_active(); - int32_t size = view->chunk_size * WORLD_BLOCK_SIZE; - int16_t offset = 0; - - float x = data->x * size + offset; - float y = data->y * size + offset; - - RenderTexture2D tex = GetChunkTexture(key); - DrawTextureRec(tex.texture, (Rectangle){0, 0, tex.texture.width, -tex.texture.height}, (Vector2){x, y}, ColorAlpha(WHITE, data->tran_time)); - - if (zoom_overlay_tran > 0.02f) { - DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV(data->color, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran*0.75f)); - - DrawTextEco(TextFormat("%d %d", (int)data->x, (int)data->y), (int16_t)x+15, (int16_t)y+15, 200 , ColorAlpha(BLACK, data->tran_time*zoom_overlay_tran), 0.0); - - } - }break; - - default:break; - } +float platform_zoom_get(void) { + return target_zoom; } -static inline float lerp(float a, float b, float t) { return a * (1.0f - t) + b * t; } - -void DEBUG_draw_entities(uint64_t key, entity_view * data) { - uint16_t size = 16; - uint16_t font_size = (uint16_t)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; - - switch (data->kind) { - case EKIND_DEMO_NPC: { - float x = data->x; - float y = data->y; -#if 0 - const char *title = TextFormat("Thing %d", key); - int title_w = MeasureTextEco(title, font_size, font_spacing); - DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(BLACK, data->tran_time)); - DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing); -#endif - 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); -#if 1 - const char *title = TextFormat("Player %d", key); - int title_w = MeasureTextEco(title, font_size, font_spacing); - DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(BLACK, data->tran_time)); - DrawRectangleEco(x-title_w/2-title_bg_offset/2, 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, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing); -#endif - DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time)); - }break; - case EKIND_MACRO_BOT: { - float x = data->x; - float y = data->y; - const char *title = TextFormat("Bot %d", key); - int title_w = MeasureTextEco(title, font_size, font_spacing); - DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(GRAY, data->tran_time)); - DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(BLACK, data->tran_time), font_spacing); - DrawCircleEco(x, y, size, ColorAlpha(PURPLE, data->tran_time)); - }break; - default:break; - } +void platform_request_close(void) { + request_shutdown = true; } -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 = 80; - float const h = 50; - DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(RED, data->tran_time)); - }break; - default:break; - } -} void lerp_entity_positions(uint64_t key, entity_view *data) { (void)key; @@ -267,10 +162,6 @@ void lerp_entity_positions(uint64_t key, entity_view *data) { } } -float platform_frametime() { - return GetFrameTime(); -} - void do_entity_fadeinout(uint64_t key, entity_view * data) { (void)key; switch (data->tran_effect) { @@ -295,11 +186,3 @@ void do_entity_fadeinout(uint64_t key, entity_view * data) { default: break; } } - -float platform_zoom_get(void) { - return target_zoom; -} - -void platform_request_close(void) { - request_shutdown = true; -} \ No newline at end of file diff --git a/code/game/src/renderer_v0.c b/code/game/src/renderer_v0.c new file mode 100644 index 0000000..0f8c8df --- /dev/null +++ b/code/game/src/renderer_v0.c @@ -0,0 +1,118 @@ + +float zpl_lerp(float,float,float); +float zpl_to_degrees(float); + +void display_conn_status() { + if (game_is_networked()) { + if (network_client_is_connected()) { + DrawText("Connection: online", 5, 5, 12, GREEN); + } else { + DrawText("Connection: offline", 5, 5, 12, RED); + } + } else { + DrawText("Connection: single-player", 5, 5, 12, BLUE); + } +} + +void DEBUG_draw_ground(uint64_t key, entity_view * data) { + (void)key; + switch (data->kind) { + case EKIND_CHUNK: { + world_view *view = game_world_view_get_active(); + int32_t size = view->chunk_size * WORLD_BLOCK_SIZE; + int16_t offset = 0; + + float x = data->x * size + offset; + float y = data->y * size + offset; + + RenderTexture2D tex = GetChunkTexture(key); + DrawTextureRec(tex.texture, (Rectangle){0, 0, tex.texture.width, -tex.texture.height}, (Vector2){x, y}, ColorAlpha(WHITE, data->tran_time)); + + if (zoom_overlay_tran > 0.02f) { + DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV(data->color, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran*0.75f)); + + DrawTextEco(TextFormat("%d %d", (int)data->x, (int)data->y), (int16_t)x+15, (int16_t)y+15, 200 , ColorAlpha(BLACK, data->tran_time*zoom_overlay_tran), 0.0); + + } + }break; + + default:break; + } +} + +static inline float lerp(float a, float b, float t) { return a * (1.0f - t) + b * t; } + +void DEBUG_draw_entities(uint64_t key, entity_view * data) { + uint16_t size = 16; + uint16_t font_size = (uint16_t)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; + + switch (data->kind) { + case EKIND_DEMO_NPC: { + float x = data->x; + float y = data->y; +#if 0 + const char *title = TextFormat("Thing %d", key); + int title_w = MeasureTextEco(title, font_size, font_spacing); + DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(BLACK, data->tran_time)); + DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing); +#endif + 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); +#if 1 + const char *title = TextFormat("Player %d", key); + int title_w = MeasureTextEco(title, font_size, font_spacing); + DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(BLACK, data->tran_time)); + DrawRectangleEco(x-title_w/2-title_bg_offset/2, 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, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing); +#endif + DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time)); + }break; + case EKIND_MACRO_BOT: { + float x = data->x; + float y = data->y; + const char *title = TextFormat("Bot %d", key); + int title_w = MeasureTextEco(title, font_size, font_spacing); + DrawRectangleEco(x-title_w/2-title_bg_offset/2, y-size-font_size-fixed_title_offset, title_w+title_bg_offset, font_size, ColorAlpha(GRAY, data->tran_time)); + DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(BLACK, data->tran_time), font_spacing); + DrawCircleEco(x, y, size, ColorAlpha(PURPLE, data->tran_time)); + }break; + default:break; + } +} + +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 = 80; + float const h = 50; + DrawRectanglePro((Rectangle){x,y,w,h}, (Vector2){w/2.0f,h/2.0f}, zpl_to_degrees(data->heading), ColorAlpha(RED, data->tran_time)); + }break; + default:break; + } +} + +void renderer_draw(void) { + BeginDrawing(); + profile (PROF_RENDER) { + 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); + EndMode2D(); + display_conn_status(); + } + debug_draw(); + EndDrawing(); +} \ No newline at end of file