From 14fd6dd63457c14921571627cd0e30974d178ff9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Dominik=20Madar=C3=A1sz?= Date: Wed, 5 May 2021 12:14:52 +0200 Subject: [PATCH] add camera --- code/apps/client/CMakeLists.txt | 1 + code/apps/client/header/camera.h | 20 ++++++++++++ code/apps/client/header/entity_view.h | 4 +-- code/apps/client/source/camera.c | 38 +++++++++++++++++++++++ code/apps/client/source/game.c | 2 ++ code/apps/client/source/platform_raylib.c | 15 ++++++++- code/common/packets/pkt_01_welcome.c | 2 ++ 7 files changed, 79 insertions(+), 3 deletions(-) create mode 100644 code/apps/client/header/camera.h create mode 100644 code/apps/client/source/camera.c diff --git a/code/apps/client/CMakeLists.txt b/code/apps/client/CMakeLists.txt index bc5d382..e707f6d 100644 --- a/code/apps/client/CMakeLists.txt +++ b/code/apps/client/CMakeLists.txt @@ -5,6 +5,7 @@ add_library(client-common STATIC source/network.c source/game.c source/main.c + source/camera.c source/entity_view.c source/utils/options.c diff --git a/code/apps/client/header/camera.h b/code/apps/client/header/camera.h new file mode 100644 index 0000000..61dab7f --- /dev/null +++ b/code/apps/client/header/camera.h @@ -0,0 +1,20 @@ +#pragma once +#include "system.h" + +typedef enum { + CAMERA_MODE_STATIONARY, + CAMERA_MODE_FOLLOW, + CAMERA_MODE_FORCE_UINT8 = UINT8_MAX +} camera_mode; + +typedef struct { + camera_mode mode; + uint64_t ent_id; + double x, y; +} camera; + +void camera_reset(void); +void camera_update(void); +void camera_set_follow(uint64_t ent_id); +void camera_set_pos(double x, double y); +camera camera_get(void); diff --git a/code/apps/client/header/entity_view.h b/code/apps/client/header/entity_view.h index e08b7ad..a2be650 100644 --- a/code/apps/client/header/entity_view.h +++ b/code/apps/client/header/entity_view.h @@ -5,8 +5,8 @@ #include "zpl.h" typedef struct entity_view { - double X; - double Y; + double x; + double y; } entity_view; ZPL_TABLE_DECLARE(, entity_view_tbl, entity_view_tbl_, entity_view); diff --git a/code/apps/client/source/camera.c b/code/apps/client/source/camera.c new file mode 100644 index 0000000..2af9030 --- /dev/null +++ b/code/apps/client/source/camera.c @@ -0,0 +1,38 @@ +#include "zpl.h" +#include "camera.h" +#include "entity_view.h" + +static camera main_camera; + +void camera_reset(void) { + zpl_zero_item(&main_camera); + main_camera.mode = CAMERA_MODE_STATIONARY; +} + +void camera_update(void) { + switch (main_camera.mode) { + case CAMERA_MODE_FOLLOW: { + entity_view *view = entity_view_get(main_camera.ent_id); + if (!view) break; + + main_camera.x = view->x; + main_camera.y = view->y; + }break; + + default: { + + }break; + } +} +void camera_set_follow(uint64_t ent_id) { + main_camera.mode = CAMERA_MODE_FOLLOW; + main_camera.ent_id = ent_id; +} +void camera_set_pos(double x, double y) { + main_camera.mode = CAMERA_MODE_STATIONARY; + main_camera.x = x; + main_camera.y = y; +} +camera camera_get(void) { + return main_camera; +} diff --git a/code/apps/client/source/game.c b/code/apps/client/source/game.c index 7af14bc..e7a08fc 100644 --- a/code/apps/client/source/game.c +++ b/code/apps/client/source/game.c @@ -5,6 +5,7 @@ #include "signal_handling.h" #include "network.h" #include "entity_view.h" +#include "camera.h" #include "flecs/flecs.h" #include "flecs/flecs_dash.h" @@ -43,6 +44,7 @@ void game_init(int8_t play_mode, int32_t seed, uint16_t block_size, uint16_t chu is_networked_play = play_mode; platform_init(); entity_view_init(); + camera_reset(); if (is_networked_play) { world_init_minimal(0, 0, 0, pkt_reader, mp_pkt_writer); diff --git a/code/apps/client/source/platform_raylib.c b/code/apps/client/source/platform_raylib.c index 2505ee1..ab4b5f4 100644 --- a/code/apps/client/source/platform_raylib.c +++ b/code/apps/client/source/platform_raylib.c @@ -3,13 +3,21 @@ #include "network.h" #include "game.h" #include "entity_view.h" +#include "camera.h" const uint16_t screenWidth = 800; const uint16_t screenHeight = 450; +static Camera2D render_camera; + void platform_init() { InitWindow(screenWidth, screenHeight, "eco2d - client"); SetTargetFPS(60); + + render_camera.target = (Vector2){0.0f,0.0f}; + render_camera.offset = (Vector2){screenWidth/2.0f, screenHeight/2.0f}; + render_camera.rotation = 0.0f; + render_camera.zoom = 1.0f; } void platform_shutdown() { @@ -25,9 +33,14 @@ void display_conn_status(); void DEBUG_draw_entities(uint64_t key, entity_view data); void platform_render() { + camera game_camera = camera_get(); + render_camera.target = (Vector2){game_camera.x, game_camera.y}; + BeginDrawing(); ClearBackground(BLACK); + BeginMode2D(render_camera); entity_view_map(DEBUG_draw_entities); + EndMode2D(); display_conn_status(); EndDrawing(); } @@ -45,5 +58,5 @@ void display_conn_status() { } void DEBUG_draw_entities(uint64_t key, entity_view data) { - DrawCircle(data.X, data.Y, 15.0f, RAYWHITE); + DrawCircle(data.x, data.y, 15.0f, RAYWHITE); } \ No newline at end of file diff --git a/code/common/packets/pkt_01_welcome.c b/code/common/packets/pkt_01_welcome.c index a2b768a..d0cc518 100644 --- a/code/common/packets/pkt_01_welcome.c +++ b/code/common/packets/pkt_01_welcome.c @@ -5,6 +5,7 @@ #ifdef CLIENT #include "entity_view.h" +#include "camera.h" #endif pkt_desc pkt_01_welcome_desc[] = { @@ -33,6 +34,7 @@ int32_t pkt_01_welcome_handler(pkt_header *header) { } entity_view_update_or_create(table.ent_id, (entity_view){0}); + camera_set_follow(table.ent_id); #endif return 0; }