From 886110a29fcf47474228145dcc6fadd0516c22d1 Mon Sep 17 00:00:00 2001 From: julesgrc0 Date: Thu, 7 Mar 2024 11:17:54 +0100 Subject: [PATCH] fix camera for desktop --- src/core/utils/camera.c | 12 +++++------- src/core/utils/camera.h | 3 +-- src/core/utils/smooth.c | 4 ++-- src/screens/game/game.c | 28 ++++++++++------------------ src/screens/menu/menu.c | 2 +- 5 files changed, 19 insertions(+), 30 deletions(-) diff --git a/src/core/utils/camera.c b/src/core/utils/camera.c index f2c3659..e2298f9 100644 --- a/src/core/utils/camera.c +++ b/src/core/utils/camera.c @@ -35,6 +35,8 @@ w_camera *create_camera(float x, float y) { camera->matrix[13] = m.m13; camera->matrix[14] = m.m14; camera->matrix[15] = m.m15; + + camera->target_position = VEC(x, y); #endif return camera; } @@ -95,13 +97,10 @@ Rectangle get_camera_view_with_gap(w_camera *camera) { } #if defined(WISPY_ANDROID) -Rectangle get_rectangle_camera(Rectangle rect, w_camera *camera) { +Rectangle get_rect_to_camera(Rectangle rect, w_camera *camera) { return RECT(rect.x - camera->position.x, rect.y - camera->position.y, rect.width, rect.height); } -Vector2 get_vector_camera(Vector2 vec, w_camera *camera) { - return VEC(vec.x - camera->position.x, vec.y - camera->position.y); -} #else void add_camera_vec(w_camera *camera, Vector2 vec) { camera_x(camera) -= vec.x; @@ -112,7 +111,7 @@ void set_camera_vec(w_camera *camera, Vector2 vec) { camera_y(camera) = -vec.y; } Vector2 get_camera_vec(w_camera *camera) { - return VEC(camera_x(camera), camera_y(camera)); + return VEC(-camera_x(camera), -camera_y(camera)); } void begin_camera(w_camera *camera) { @@ -121,7 +120,6 @@ void begin_camera(w_camera *camera) { rlMultMatrixf(camera->matrix); } void end_camera() { - rlDrawRenderBatchActive(); - rlLoadIdentity(); + EndMode2D(); } #endif \ No newline at end of file diff --git a/src/core/utils/camera.h b/src/core/utils/camera.h index b863684..7c777e5 100644 --- a/src/core/utils/camera.h +++ b/src/core/utils/camera.h @@ -27,8 +27,7 @@ Rectangle get_camera_view_with_gap(w_camera *camera); #if defined(WISPY_ANDROID) -Rectangle get_rectangle_camera(Rectangle rect, w_camera *camera); -Vector2 get_vector_camera(Vector2 vec, w_camera *camera); +Rectangle get_rect_to_camera(Rectangle rect, w_camera *camera); #else void add_camera_vec(w_camera *camera, Vector2 vec); void set_camera_vec(w_camera *camera, Vector2 vec); diff --git a/src/core/utils/smooth.c b/src/core/utils/smooth.c index 4a78631..ecd61c9 100644 --- a/src/core/utils/smooth.c +++ b/src/core/utils/smooth.c @@ -14,8 +14,8 @@ void smooth_camera(w_camera *camera, float move) { smooth_float(camera->position.x, camera->target_position.x, move); smooth_float(camera->position.y, camera->target_position.y, move); #else - smooth_float(camera_x(camera), camera->target_position.x, move); - smooth_float(camera_y(camera), camera->target_position.y, move); + smooth_float(camera_x(camera), -camera->target_position.x, move); + smooth_float(camera_y(camera), -camera->target_position.y, move); #endif } Vector2 vec_block_round(Vector2 vec) { diff --git a/src/screens/game/game.c b/src/screens/game/game.c index 87faeea..659064e 100644 --- a/src/screens/game/game.c +++ b/src/screens/game/game.c @@ -66,11 +66,10 @@ void game_screen(w_state *state) { destroy_gui(ctx); return; } -#else - Vector2 player_position = td->player->position; #endif w_breakstate bstate = BS_NONE; + float dt = 0.f; while (!WindowShouldClose() && td->is_active) { #if defined(WISPY_ANDROID) update_controls(td->ctrl); @@ -79,10 +78,12 @@ void game_screen(w_state *state) { update_bridge(td); #else update_controls(td->ctrl); - float speed = GetFrameTime() * PLAYER_SPEED; - smooth_camera(td->camera, speed); - smooth_vec(&player_position, td->player->position, - Vector2Distance(td->player->position, player_position) * speed); + dt = GetFrameTime(); + if (dt <= MIN_FRAME_TIME) { + set_camera_vec(td->camera, td->camera->target_position); + } else { + smooth_camera(td->camera, dt * PLAYER_SPEED); + } #endif #if defined(WISPY_WINDOWS) @@ -107,14 +108,14 @@ void game_screen(w_state *state) { block_textures[td->chunk_view->blocks[i].block.type - 1], td->chunk_view->blocks[i].src, #if defined(WISPY_ANDROID) - get_rectangle_camera(td->chunk_view->blocks[i].dst, td->camera), + get_rect_to_camera(td->chunk_view->blocks[i].dst, td->camera), #else td->chunk_view->blocks[i].dst, #endif VEC_ZERO, 0, td->chunk_view->blocks[i].light); } #if defined(WISPY_WINDOWS) || defined(WISPY_LINUX) - bstate = update_blockbreaker(bb, td->ctrl, td->player, GetFrameTime()); + bstate = update_blockbreaker(bb, td->ctrl, td->player, dt); #endif if (bstate == BS_BREAKING) { draw_blockbreaker(bb, td->camera); @@ -122,24 +123,15 @@ void game_screen(w_state *state) { td->force_update = true; } -#if defined(WISPY_WINDOWS) || defined(WISPY_LINUX) - DrawTexturePro(player_textures[td->player->state], td->player->src, - RECT(player_position.x, player_position.y, - td->player->dst.width, td->player->dst.height), - VEC_ZERO, 0, WHITE); -#endif - #if defined(WISPY_WINDOWS) || defined(WISPY_LINUX) end_camera(); #endif - -#if defined(WISPY_ANDROID) DrawTexturePro(player_textures[td->player->state], td->player->src, RECT((RENDER_W - td->player->dst.width) / 2, (RENDER_H - td->player->dst.height) / 2, td->player->dst.width, td->player->dst.height), VEC_ZERO, 0, WHITE); - +#if defined(WISPY_ANDROID) td->ctrl->joystick = update_joystick(js); td->ctrl->is_breaking = update_action(break_button); td->ctrl->is_jumping = update_action(jump_button); diff --git a/src/screens/menu/menu.c b/src/screens/menu/menu.c index 1731d52..365b74c 100644 --- a/src/screens/menu/menu.c +++ b/src/screens/menu/menu.c @@ -80,7 +80,7 @@ void menu_screen(w_state *state) { DrawTexturePro(block_textures[view->blocks[i].block.type - 1], view->blocks[i].src, #if defined(WISPY_ANDROID) - get_rectangle_camera(view->blocks[i].dst, camera), + get_rect_to_camera(view->blocks[i].dst, camera), #else view->blocks[i].dst, #endif