eco2d/code/game/source/platform_raylib.c

275 lines
8.8 KiB
C
Raw Normal View History

2021-01-11 20:11:03 +00:00
#include "platform.h"
2021-01-10 16:42:01 +00:00
#include "raylib.h"
2021-05-07 22:11:09 +00:00
#include "raymath.h"
2021-05-05 09:25:05 +00:00
#include "network.h"
2021-05-04 20:44:55 +00:00
#include "game.h"
2021-05-05 09:25:05 +00:00
#include "entity_view.h"
#include "prediction.h"
2021-05-05 10:14:52 +00:00
#include "camera.h"
2021-05-08 15:42:47 +00:00
#include "math.h"
2021-05-12 16:28:39 +00:00
#include "world/blocks.h"
2021-05-12 17:26:41 +00:00
#include "assets.h"
2021-05-13 12:17:44 +00:00
#include "profiler.h"
#include "debug_ui.h"
2021-05-12 14:42:22 +00:00
#include "utils/raylib_helpers.h"
2021-01-10 16:42:01 +00:00
2021-05-14 05:59:33 +00:00
static uint16_t screenWidth = 1600;
static uint16_t screenHeight = 900;
2021-05-15 11:43:29 +00:00
static float target_zoom = 1.5f;
2021-05-14 05:59:33 +00:00
static float zoom_overlay_tran = 0.0f;
static bool request_shutdown;
2021-05-12 13:30:44 +00:00
#define CAM_OVERLAY_ZOOM_LEVEL 0.80f
2021-05-05 10:14:52 +00:00
static Camera2D render_camera;
2021-01-11 20:11:03 +00:00
void platform_init() {
2021-01-11 13:47:14 +00:00
InitWindow(screenWidth, screenHeight, "eco2d - client");
SetWindowState(FLAG_WINDOW_UNDECORATED|FLAG_WINDOW_MAXIMIZED|FLAG_WINDOW_RESIZABLE);
2021-01-10 16:42:01 +00:00
SetTargetFPS(60);
2021-05-05 10:14:52 +00:00
2021-05-08 16:38:33 +00:00
screenWidth = GetScreenWidth();
screenHeight = GetScreenHeight();
2021-05-05 10:14:52 +00:00
render_camera.target = (Vector2){0.0f,0.0f};
render_camera.offset = (Vector2){screenWidth/2.0f, screenHeight/2.0f};
render_camera.rotation = 0.0f;
2021-05-15 11:43:29 +00:00
render_camera.zoom = 1.5f;
// 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 = "zpl.eco2d is loading...";
int text_w = MeasureText(loading_text, 120);
DrawText(loading_text, GetScreenWidth()-text_w-15, GetScreenHeight()-135, 120, RAYWHITE);
EndDrawing();
2021-05-12 16:28:39 +00:00
blocks_setup();
2021-05-12 17:26:41 +00:00
assets_setup();
2021-01-10 16:42:01 +00:00
}
2021-01-11 20:11:03 +00:00
void platform_shutdown() {
2021-05-12 16:28:39 +00:00
blocks_destroy();
2021-05-12 17:26:41 +00:00
assets_destroy();
2021-01-10 16:42:01 +00:00
CloseWindow();
}
2021-01-11 20:11:03 +00:00
uint8_t platform_is_running() {
2021-01-11 20:08:16 +00:00
return !WindowShouldClose();
}
2021-05-07 12:27:51 +00:00
void platform_input() {
2021-05-09 19:12:11 +00:00
float mouse_z = (GetMouseWheelMove()*0.5f);
2021-05-07 12:27:51 +00:00
if (mouse_z != 0.0f) {
2021-05-15 11:43:29 +00:00
target_zoom = zpl_clamp(target_zoom+mouse_z, 0.1f, 10.0f);
2021-05-07 12:27:51 +00:00
}
2021-05-07 14:43:54 +00:00
// NOTE(zaklaus): keystate handling
{
2021-05-08 15:42:47 +00:00
float x=0.0f, y=0.0f;
2021-05-07 20:48:15 +00:00
uint8_t use, sprint;
2021-05-07 14:43:54 +00:00
if (IsKeyDown(KEY_RIGHT) || IsKeyDown(KEY_D)) x += 1.0f;
if (IsKeyDown(KEY_LEFT) || IsKeyDown(KEY_A)) x -= 1.0f;
if (IsKeyDown(KEY_UP) || IsKeyDown(KEY_W)) y -= 1.0f;
if (IsKeyDown(KEY_DOWN) || IsKeyDown(KEY_S)) y += 1.0f;
use = IsKeyPressed(KEY_SPACE);
2021-05-07 20:48:15 +00:00
sprint = IsKeyDown(KEY_LEFT_SHIFT) || IsKeyDown(KEY_RIGHT_SHIFT);
2021-05-07 14:43:54 +00:00
2021-05-07 22:11:09 +00:00
// NOTE(zaklaus): NEW! mouse movement
if (IsMouseButtonDown(MOUSE_RIGHT_BUTTON)) {
Vector2 mouse_pos = GetMousePosition();
mouse_pos.x /= screenWidth;
mouse_pos.y /= screenHeight;
2021-05-08 16:38:33 +00:00
mouse_pos.x -= 0.5f;
mouse_pos.y -= 0.5f;
mouse_pos = Vector2Normalize(mouse_pos);
x = mouse_pos.x;
y = mouse_pos.y;
2021-05-07 22:11:09 +00:00
}
2021-05-07 20:48:15 +00:00
game_action_send_keystate(x, y, use, sprint);
}
// NOTE(zaklaus): cycle through viewers
{
if (IsKeyPressed(KEY_Q)) {
game_world_view_cycle_active(-1);
}
else if (IsKeyPressed(KEY_E)) {
game_world_view_cycle_active(1);
}
2021-05-07 14:43:54 +00:00
}
2021-05-07 12:27:51 +00:00
}
2021-05-04 20:44:55 +00:00
void display_conn_status();
2021-05-09 22:40:25 +00:00
void DEBUG_draw_entities(uint64_t key, entity_view * data);
void DEBUG_draw_ground(uint64_t key, entity_view * data);
2021-05-05 09:25:05 +00:00
2021-05-09 22:40:25 +00:00
void lerp_entity_positions(uint64_t key, entity_view * data);
void do_entity_fadeinout(uint64_t key, entity_view * data);
2021-05-09 19:12:11 +00:00
float zpl_lerp(float,float,float);
2021-01-11 20:11:03 +00:00
void platform_render() {
2021-05-13 12:17:44 +00:00
profile(PROF_ENTITY_LERP) {
game_world_view_active_entity_map(lerp_entity_positions);
game_world_view_active_entity_map(do_entity_fadeinout);
}
2021-05-09 19:12:11 +00:00
render_camera.zoom = zpl_lerp(render_camera.zoom, target_zoom, 0.18);
camera_update();
2021-05-05 10:14:52 +00:00
camera game_camera = camera_get();
2021-05-09 19:12:11 +00:00
render_camera.target = (Vector2){game_camera.x, game_camera.y};
2021-05-12 13:30:44 +00:00
zoom_overlay_tran = zpl_lerp(zoom_overlay_tran, (target_zoom <= CAM_OVERLAY_ZOOM_LEVEL) ? 1.0f : 0.0f, GetFrameTime()*2.0f);
2021-01-10 16:42:01 +00:00
BeginDrawing();
2021-05-13 12:17:44 +00:00
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);
EndMode2D();
2021-05-13 12:17:44 +00:00
display_conn_status();
}
debug_draw();
2021-01-10 16:42:01 +00:00
EndDrawing();
2021-05-13 14:12:18 +00:00
if (request_shutdown) {
CloseWindow();
}
2021-01-10 16:42:01 +00:00
}
2021-05-04 20:44:55 +00:00
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);
}
2021-05-05 09:25:05 +00:00
}
2021-05-09 22:40:25 +00:00
void DEBUG_draw_ground(uint64_t key, entity_view * data) {
2021-05-12 13:30:44 +00:00
(void)key;
2021-05-09 22:40:25 +00:00
switch (data->kind) {
case EKIND_CHUNK: {
2021-05-08 15:42:47 +00:00
world_view *view = game_world_view_get_active();
2021-05-12 16:28:39 +00:00
int32_t size = view->chunk_size * WORLD_BLOCK_SIZE;
2021-05-12 13:30:44 +00:00
int16_t offset = 0;
2021-05-08 15:42:47 +00:00
2021-05-09 22:40:25 +00:00
float x = data->x * size + offset;
float y = data->y * size + offset;
RenderTexture2D tex = GetChunkTexture(key);
2021-07-19 16:16:34 +00:00
DrawTextureRec(tex.texture, (Rectangle){0, 0, tex.texture.width, -tex.texture.height}, (Vector2){x, y}, ColorAlpha(WHITE, data->tran_time));
2021-05-08 15:42:47 +00:00
2021-05-12 13:30:44 +00:00
if (zoom_overlay_tran > 0.02f) {
2021-07-27 11:30:43 +00:00
DrawRectangleEco(x, y, size-offset, size-offset, ColorAlpha(ColorFromHSV(data->color, 0.13f, 0.89f), data->tran_time*zoom_overlay_tran*0.75f));
2021-05-12 13:30:44 +00:00
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);
2021-05-12 13:30:44 +00:00
}
}break;
default:break;
}
}
2021-05-07 20:48:15 +00:00
static inline float lerp(float a, float b, float t) { return a * (1.0f - t) + b * t; }
2021-05-09 22:40:25 +00:00
void DEBUG_draw_entities(uint64_t key, entity_view * data) {
2021-05-15 11:43:29 +00:00
uint16_t size = 16;
2021-05-09 19:12:11 +00:00
uint16_t font_size = (uint16_t)lerp(4.0f, 32.0f, 0.5f/(float)render_camera.zoom);
2021-05-07 20:48:15 +00:00
float font_spacing = 1.1f;
float title_bg_offset = 4;
2021-07-27 16:34:31 +00:00
float fixed_title_offset = 8;
2021-05-06 18:24:01 +00:00
2021-05-09 22:40:25 +00:00
switch (data->kind) {
2021-07-27 15:57:50 +00:00
case EKIND_DEMO_NPC: {
2021-05-09 22:40:25 +00:00
float x = data->x;
float y = data->y;
2021-05-13 12:56:30 +00:00
#if 0
2021-05-08 09:05:15 +00:00
const char *title = TextFormat("Thing %d", key);
int title_w = MeasureTextEco(title, font_size, font_spacing);
2021-05-09 22:40:25 +00:00
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);
2021-05-13 12:56:30 +00:00
#endif
2021-05-09 22:40:25 +00:00
DrawCircleEco(x, y, size, ColorAlpha(BLUE, data->tran_time));
2021-05-08 09:05:15 +00:00
}break;
2021-05-06 18:24:01 +00:00
case EKIND_PLAYER: {
2021-05-09 22:40:25 +00:00
float x = data->x;
float y = data->y;
2021-07-27 16:34:31 +00:00
float health = (data->hp / data->max_hp);
2021-07-27 15:57:50 +00:00
#if 1
2021-05-07 20:48:15 +00:00
const char *title = TextFormat("Player %d", key);
int title_w = MeasureTextEco(title, font_size, font_spacing);
2021-05-09 22:40:25 +00:00
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));
2021-07-27 16:34:31 +00:00
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));
2021-05-09 22:40:25 +00:00
DrawTextEco(title, x-title_w/2, y-size-font_size-fixed_title_offset, font_size, ColorAlpha(RAYWHITE, data->tran_time), font_spacing);
2021-07-27 15:57:50 +00:00
#endif
2021-07-27 16:34:31 +00:00
DrawCircleEco(x, y, size, ColorAlpha(YELLOW, data->tran_time));
}break;
2021-05-06 18:24:01 +00:00
default:break;
}
}
2021-05-10 06:28:56 +00:00
void lerp_entity_positions(uint64_t key, entity_view *data) {
2021-05-12 13:30:44 +00:00
(void)key;
world_view *view = game_world_view_get_active();
2021-05-10 06:28:56 +00:00
if (data->flag == EFLAG_INTERP) {
#if 1
2021-05-10 06:28:56 +00:00
data->x = smooth_val(data->x, data->tx, view->delta_time[data->layer_id]);
data->y = smooth_val(data->y, data->ty, view->delta_time[data->layer_id]);
#else
2021-05-10 06:28:56 +00:00
data->x = data->tx;
data->y = data->ty;
#endif
}
2021-05-09 22:40:25 +00:00
}
2021-05-10 07:10:26 +00:00
float platform_frametime() {
return GetFrameTime();
}
2021-05-09 22:40:25 +00:00
void do_entity_fadeinout(uint64_t key, entity_view * data) {
2021-05-12 13:30:44 +00:00
(void)key;
2021-05-09 22:40:25 +00:00
switch (data->tran_effect) {
case ETRAN_FADEIN: {
data->tran_time += GetFrameTime();
if (data->tran_time > 1.0f) {
data->tran_effect = ETRAN_NONE;
data->tran_time = 1.0f;
}
}break;
case ETRAN_FADEOUT: {
data->tran_time -= GetFrameTime();
if (data->tran_time < 0.0f) {
data->tran_effect = ETRAN_REMOVE;
data->tran_time = 0.0f;
}
}break;
default: break;
}
2021-05-10 06:28:56 +00:00
}
2021-05-13 12:17:44 +00:00
float platform_zoom_get(void) {
return target_zoom;
2021-05-13 14:12:18 +00:00
}
void platform_request_close(void) {
request_shutdown = true;
2021-05-13 12:17:44 +00:00
}