Skip to content

Commit

Permalink
new camera system
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Feb 26, 2024
1 parent f04cf23 commit 0c25320
Show file tree
Hide file tree
Showing 21 changed files with 238 additions and 166 deletions.
28 changes: 13 additions & 15 deletions src/core/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -26,17 +26,13 @@ w_bridge *create_bridge() {
return NULL;
}

td->camera = malloc(sizeof(Camera2D));
td->camera = create_camera(0, 0);
if (td->camera == NULL) {
LOG("failed to allocate memory for camera");
destroy_bridge(td);
return NULL;
}
memset(td->camera, 0, sizeof(Camera2D));
td->camera->zoom = 1.f;

td->camera->target = get_camera_target_player(td->player, td->camera);
td->camera_target = td->camera->target;
set_camera_center(td->camera, get_player_center(td->player));
td->camera_target = get_camera_vec(td->camera);

td->ctrl = create_controls();
if (td->ctrl == NULL) {
Expand Down Expand Up @@ -80,8 +76,8 @@ void destroy_bridge(w_bridge *td) {
destroy_chunkgroup(td->chunk_group);
destroy_chunkview(td->chunk_view);
destroy_player(td->player);
destroy_camera(td->camera);

sfree(td->camera);
sfree(td);
}

Expand All @@ -97,7 +93,10 @@ void physics_update(w_bridge *td) {
td->player->position.x += next_position.x;
td->player->position.y += next_position.y;

td->camera_target = get_camera_target_player(td->player, td->camera);
Vector2 player_center = get_player_center(td->player);
td->camera_target =
VEC(player_center.x - RENDER_W / 2, player_center.y - RENDER_H / 2);

animate_player(td->player, td->ctrl->left || td->ctrl->right);
clear_controls(td->ctrl);
}
Expand Down Expand Up @@ -129,18 +128,17 @@ void *update_bridge(void *arg)
clock_gettime(CLOCK_MONOTONIC, &time_start);
#endif // _WIN32

update_chunkview(td->chunk_view, td->chunk_group,
get_camera_view(td->camera));
update_chunkview(td->chunk_view, td->chunk_group, td->camera);

update_chunkview_lighting(td->chunk_view, get_player_center(td->player),
RENDER_CUBE_COUNT * CUBE_W);
do {

if (td->ctrl->key != 0 || td->camera->target.x != td->camera_target.x ||
td->camera->target.y != td->camera_target.y || td->force_update) {
if (td->ctrl->key != 0 ||
!Vector2Equals(td->camera_target, get_camera_vec(td->camera)) ||
td->force_update) {

if (!update_chunkview(td->chunk_view, td->chunk_group,
get_camera_view(td->camera))) {
if (!update_chunkview(td->chunk_view, td->chunk_group, td->camera)) {
LOG("failed to update chunk view");
td->is_active = false;

Expand Down
3 changes: 2 additions & 1 deletion src/core/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../terrain/chunk_group.h"
#include "../terrain/chunk_view.h"

#include "./utils/camera.h"
#include "controls.h"
#include "state.h"

Expand All @@ -21,7 +22,7 @@ typedef struct w_bridge {

w_player *player;

Camera2D *camera;
w_camera *camera;
Vector2 camera_target;

#ifdef _WIN32
Expand Down
1 change: 0 additions & 1 deletion src/core/controls.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "../gui/gui.h"
#include "../stdafx.h"
#include "view.h"

#pragma pack(push, 1)
typedef struct w_controls {
Expand Down
85 changes: 85 additions & 0 deletions src/core/utils/camera.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
#include "camera.h"

w_camera *create_camera(float x, float y) {
w_camera *camera = malloc(sizeof(w_camera));
if (camera == NULL) {
LOG("failed to allocate memory for camera");
return NULL;
}
memset(camera, 0, sizeof(w_camera));
camera->matrix = malloc(sizeof(float) * 16);
if (camera->matrix == NULL) {
LOG("failed to allocate memory for camera matrix");
free(camera);
return NULL;
}

Matrix m = MatrixTranslate(-x, -y, 0);
camera->matrix[0] = m.m0;
camera->matrix[1] = m.m1;
camera->matrix[2] = m.m2;
camera->matrix[3] = m.m3;
camera->matrix[4] = m.m4;
camera->matrix[5] = m.m5;
camera->matrix[6] = m.m6;
camera->matrix[7] = m.m7;
camera->matrix[8] = m.m8;
camera->matrix[9] = m.m9;
camera->matrix[10] = m.m10;
camera->matrix[11] = m.m11;
camera->matrix[12] = m.m12;
camera->matrix[13] = m.m13;
camera->matrix[14] = m.m14;
camera->matrix[15] = m.m15;

return camera;
}
void destroy_camera(w_camera *camera) {
if (camera == NULL) {
LOG("camera (null) already destroyed");
return;
}
sfree(camera->matrix);
free(camera);
}
void add_camera_vec(w_camera *camera, Vector2 vec) {
camera_x(camera) -= vec.x;
camera_y(camera) -= vec.y;
}
void set_camera_vec(w_camera *camera, Vector2 vec) {
camera_x(camera) = -vec.x;
camera_y(camera) = -vec.y;
}
void set_camera_center(w_camera *camera, Vector2 vec) {
camera_x(camera) = -vec.x + RENDER_W / 2;
camera_y(camera) = -vec.y + RENDER_H / 2;
}
Vector2 get_camera_vec(w_camera *camera) {
return VEC(-camera_x(camera), -camera_y(camera));
}
Vector2 get_camera_center(w_camera *camera) {
return VEC(-camera_x(camera) + RENDER_W / 2,
-camera_y(camera) + RENDER_H / 2);
}
Vector2 get_camera_object_center(w_camera *camera, Rectangle object) {
return VEC(-camera_x(camera) + object.x + (RENDER_W - object.width) / 2,
-camera_y(camera) + object.y + (RENDER_H - object.height) / 2);
}
Rectangle get_camera_view(w_camera *camera) {
return RECT(-camera_x(camera), -camera_y(camera), RENDER_W, RENDER_H);
}
Rectangle get_camera_view_with_gap(w_camera *camera) {
return RECT(-camera_x(camera) - RENDER_CUBE_GAP * CUBE_W,
-camera_y(camera) - RENDER_CUBE_GAP * CUBE_H,
RENDER_W + RENDER_CUBE_GAP * CUBE_W * 2,
RENDER_H + RENDER_CUBE_GAP * CUBE_H * 2);
}
void begin_camera(w_camera *camera) {
rlDrawRenderBatchActive();
rlLoadIdentity();
rlMultMatrixf(camera->matrix);
}
void end_camera() {
rlDrawRenderBatchActive();
rlLoadIdentity();
}
25 changes: 25 additions & 0 deletions src/core/utils/camera.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
#pragma once
#include "../../stdafx.h"

#define camera_x(camera) (camera->matrix[12])
#define camera_y(camera) (camera->matrix[13])

typedef struct w_camera {
float *matrix;
} w_camera;

w_camera *create_camera(float x, float y);
void destroy_camera(w_camera *camera);

void add_camera_vec(w_camera *camera, Vector2 vec);
void set_camera_vec(w_camera *camera, Vector2 vec);
void set_camera_center(w_camera *camera, Vector2 vec);

Vector2 get_camera_vec(w_camera *camera);
Vector2 get_camera_center(w_camera *camera);

Rectangle get_camera_view(w_camera *camera);
Rectangle get_camera_view_with_gap(w_camera *camera);

void begin_camera(w_camera *camera);
void end_camera();
19 changes: 19 additions & 0 deletions src/core/utils/smooth.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#include "smooth.h"

void smooth_vec(Vector2 *position, Vector2 target, float move) {
smooth_float(position->x, target.x, move);
smooth_float(position->y, target.y, move);
}

void smooth_rect(Rectangle *box, Rectangle target, float move) {
smooth_float(box->x, target.x, move);
smooth_float(box->y, target.y, move);
}
void smooth_camera(w_camera *camera, Vector2 target, float move) {
smooth_float(camera_x(camera), -target.x, move);
smooth_float(camera_y(camera), -target.y, move);
}
Vector2 vec_block_round(Vector2 vec) {
return VEC(roundf(vec.x / (float)CUBE_W) * (float)CUBE_W,
roundf(vec.y / (float)CUBE_H) * (float)CUBE_H);
}
14 changes: 14 additions & 0 deletions src/core/utils/smooth.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
#pragma once
#include "../../stdafx.h"
#include "camera.h"

#define smooth_float(current, target, speed) \
current = ((float)current < (float)target) \
? fmin((float)current + (float)speed, (float)target) \
: fmax((float)current - (float)speed, (float)target);

void smooth_vec(Vector2 *position, Vector2 target, float move);
void smooth_rect(Rectangle *box, Rectangle target, float move);
void smooth_camera(w_camera *camera, Vector2 target, float move);

Vector2 vec_block_round(Vector2 vec);
36 changes: 0 additions & 36 deletions src/core/view.c

This file was deleted.

29 changes: 0 additions & 29 deletions src/core/view.h

This file was deleted.

13 changes: 3 additions & 10 deletions src/entities/player.c
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ w_player *create_player(unsigned int x) {
player->dst = RECT((RENDER_W - CUBE_W) / 2.f, (RENDER_H - CUBE_H * 2) / 2.f,
CUBE_W * 0.7f, // TODO: adjust player size
CUBE_H * 2);
player->position =
VEC((x + CHUNK_GROUP_MID_LEN) * CHUNK_W * CUBE_W, CHUNK_MID_H * CUBE_H);
player->position = VEC((float)((x + CHUNK_GROUP_MID_LEN) * FULL_CHUNK_W),
CHUNK_MID_H * CUBE_H);
return player;
}

Expand Down Expand Up @@ -106,13 +106,6 @@ Vector2 get_player_center(w_player *player) {
player->position.y + player->dst.height / 2);
}

Vector2 get_camera_target_player(w_player *player, Camera2D *camera) {

return center_camera_on_object(camera,
RECT(player->position.x, player->position.y,
player->dst.width, player->dst.height));
}

void check_player_collision_vel(w_player *player, w_chunkview *view) {
Rectangle next_velx = {.x = player->position.x +
player->velocity.x * PLAYER_SPEED * PHYSICS_TICK,
Expand All @@ -126,7 +119,7 @@ void check_player_collision_vel(w_player *player, w_chunkview *view) {
.height = player->dst.height};

bool col_x = false;
for (size_t i = 0; i < view->textures_len; i++) {
for (size_t i = 0; i < view->len; i++) {
if (view->blocks[i].block.is_background)
continue;

Expand Down
2 changes: 0 additions & 2 deletions src/entities/player.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "../core/controls.h"
#include "../core/state.h"
#include "../core/view.h"
#include "../stdafx.h"
#include "../terrain/chunk_view.h"

Expand Down Expand Up @@ -62,5 +61,4 @@ void update_player_input(w_player *player, w_controls *ctrl);
void update_player_velocity(w_player *player);

Vector2 get_player_center(w_player *player);
Vector2 get_camera_target_player(w_player *player, Camera2D *camera);
void check_player_collision_vel(w_player *player, w_chunkview *view);
1 change: 0 additions & 1 deletion src/gui/action.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "../core/view.h"
#include "../core/controls.h"
#include "../stdafx.h"
#include "gui.h"
Expand Down
3 changes: 1 addition & 2 deletions src/gui/button.h
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
#pragma once
#include "gui.h"
#include "../core/view.h"
#include "../core/controls.h"
#include "gui.h"

typedef struct w_guibutton {
w_guicontext *ctx;
Expand Down
1 change: 0 additions & 1 deletion src/gui/joystick.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#pragma once
#include "../core/view.h"
#include "../core/controls.h"
#include "../stdafx.h"
#include "gui.h"
Expand Down
Loading

0 comments on commit 0c25320

Please sign in to comment.