Skip to content

Commit

Permalink
fix camera for desktop
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Mar 7, 2024
1 parent e3689dc commit 886110a
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 30 deletions.
12 changes: 5 additions & 7 deletions src/core/utils/camera.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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) {
Expand All @@ -121,7 +120,6 @@ void begin_camera(w_camera *camera) {
rlMultMatrixf(camera->matrix);
}
void end_camera() {
rlDrawRenderBatchActive();
rlLoadIdentity();
EndMode2D();
}
#endif
3 changes: 1 addition & 2 deletions src/core/utils/camera.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions src/core/utils/smooth.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down
28 changes: 10 additions & 18 deletions src/screens/game/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -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)
Expand All @@ -107,39 +108,30 @@ 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);
} else if (bstate == BS_BROKEN) {
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);
Expand Down
2 changes: 1 addition & 1 deletion src/screens/menu/menu.c
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 886110a

Please sign in to comment.