add camera

isolation_bkp/dynres
Dominik Madarász 2021-05-05 12:14:52 +02:00
parent c679773e51
commit 14fd6dd634
7 changed files with 79 additions and 3 deletions

View File

@ -5,6 +5,7 @@ add_library(client-common STATIC
source/network.c source/network.c
source/game.c source/game.c
source/main.c source/main.c
source/camera.c
source/entity_view.c source/entity_view.c
source/utils/options.c source/utils/options.c

View File

@ -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);

View File

@ -5,8 +5,8 @@
#include "zpl.h" #include "zpl.h"
typedef struct entity_view { typedef struct entity_view {
double X; double x;
double Y; double y;
} entity_view; } entity_view;
ZPL_TABLE_DECLARE(, entity_view_tbl, entity_view_tbl_, entity_view); ZPL_TABLE_DECLARE(, entity_view_tbl, entity_view_tbl_, entity_view);

View File

@ -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;
}

View File

@ -5,6 +5,7 @@
#include "signal_handling.h" #include "signal_handling.h"
#include "network.h" #include "network.h"
#include "entity_view.h" #include "entity_view.h"
#include "camera.h"
#include "flecs/flecs.h" #include "flecs/flecs.h"
#include "flecs/flecs_dash.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; is_networked_play = play_mode;
platform_init(); platform_init();
entity_view_init(); entity_view_init();
camera_reset();
if (is_networked_play) { if (is_networked_play) {
world_init_minimal(0, 0, 0, pkt_reader, mp_pkt_writer); world_init_minimal(0, 0, 0, pkt_reader, mp_pkt_writer);

View File

@ -3,13 +3,21 @@
#include "network.h" #include "network.h"
#include "game.h" #include "game.h"
#include "entity_view.h" #include "entity_view.h"
#include "camera.h"
const uint16_t screenWidth = 800; const uint16_t screenWidth = 800;
const uint16_t screenHeight = 450; const uint16_t screenHeight = 450;
static Camera2D render_camera;
void platform_init() { void platform_init() {
InitWindow(screenWidth, screenHeight, "eco2d - client"); InitWindow(screenWidth, screenHeight, "eco2d - client");
SetTargetFPS(60); 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() { void platform_shutdown() {
@ -25,9 +33,14 @@ void display_conn_status();
void DEBUG_draw_entities(uint64_t key, entity_view data); void DEBUG_draw_entities(uint64_t key, entity_view data);
void platform_render() { void platform_render() {
camera game_camera = camera_get();
render_camera.target = (Vector2){game_camera.x, game_camera.y};
BeginDrawing(); BeginDrawing();
ClearBackground(BLACK); ClearBackground(BLACK);
BeginMode2D(render_camera);
entity_view_map(DEBUG_draw_entities); entity_view_map(DEBUG_draw_entities);
EndMode2D();
display_conn_status(); display_conn_status();
EndDrawing(); EndDrawing();
} }
@ -45,5 +58,5 @@ void display_conn_status() {
} }
void DEBUG_draw_entities(uint64_t key, entity_view data) { 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);
} }

View File

@ -5,6 +5,7 @@
#ifdef CLIENT #ifdef CLIENT
#include "entity_view.h" #include "entity_view.h"
#include "camera.h"
#endif #endif
pkt_desc pkt_01_welcome_desc[] = { 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}); entity_view_update_or_create(table.ent_id, (entity_view){0});
camera_set_follow(table.ent_id);
#endif #endif
return 0; return 0;
} }