Skip to content

Commit

Permalink
bug fixes + nice blue sky :)
Browse files Browse the repository at this point in the history
  • Loading branch information
julesgrc0 committed Feb 8, 2024
1 parent 100c058 commit c931f1e
Show file tree
Hide file tree
Showing 7 changed files with 76 additions and 48 deletions.
37 changes: 24 additions & 13 deletions src/core/bridge.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,14 @@ w_bridge *create_bridge() {
return NULL;
}
memset(td->player, 0, sizeof(w_player));
td->player->box =
(Rectangle){.x = CHUNK_MID_H * CUBE_H,
.y = td->chunk_group->position * CHUNK_W * CUBE_W,
.width = CUBE_W,
.height = CUBE_H * 2};
td->player->src = PLAYER_SRC_RECT;
td->player->dst = (Rectangle){.x = (RENDER_W - CUBE_W) / 2.f,
.y = (RENDER_H - CUBE_H * 2) / 2.f,
.width = CUBE_W,
.height = CUBE_H * 2};
td->player->position = (Vector2){
.x = (td->chunk_group->position + CHUNK_GROUP_MID_LEN) * CHUNK_W * CUBE_W,
.y = CHUNK_MID_H * CUBE_H};

td->camera = malloc(sizeof(Camera2D));
if (td->camera == NULL) {
Expand All @@ -41,7 +44,12 @@ w_bridge *create_bridge() {
}
memset(td->camera, 0, sizeof(Camera2D));
td->camera->zoom = 1.0f;
td->camera->target = center_camera_on_object(td->camera, td->player->box);
td->camera->target = center_camera_on_object(
td->camera, (Rectangle){.x = td->player->position.x,
.y = td->player->position.y,
.width = td->player->dst.width,
.height = td->player->dst.height});
td->camera_target = td->camera->target;

td->keyboard = malloc(sizeof(w_keyboard));
if (td->keyboard == NULL) {
Expand Down Expand Up @@ -107,9 +115,14 @@ void physics_update(w_bridge *td) {

if (td->keyboard->key != 0) {
td->player->velocity = Vector2Normalize(td->player->velocity);
Vector2 position = Vector2Scale(td->player->velocity, 1000 * PHYSICS_TICK);
td->player->box.x += position.x;
td->player->box.y += position.y;
td->player->position = Vector2Add(
td->player->position,
Vector2Scale(td->player->velocity, PLAYER_SPEED * PHYSICS_TICK));
td->camera_target = center_camera_on_object(
td->camera, (Rectangle){.x = td->player->position.x,
.y = td->player->position.y,
.width = td->player->dst.width,
.height = td->player->dst.height});
}

td->player->velocity = Vector2Scale(td->player->velocity, 0.9f);
Expand All @@ -132,11 +145,10 @@ void *update_bridge(void *arg)
update_chunkview(td->chunk_view, td->chunk_group,
get_camera_view(td->camera));

Vector2 camera_target = center_camera_on_object(td->camera, td->player->box);
while (td->is_active) {

if (td->keyboard->key != 0 || td->camera->target.x != camera_target.x ||
td->camera->target.y != camera_target.y) {
if (td->keyboard->key != 0 || td->camera->target.x != td->camera_target.x ||
td->camera->target.y != td->camera_target.y) {

if (!update_chunkview(td->chunk_view, td->chunk_group,
get_camera_view(td->camera))) {
Expand All @@ -152,7 +164,6 @@ void *update_bridge(void *arg)
continue;
QueryPerformanceCounter(&time_start);
physics_update(td);
camera_target = center_camera_on_object(td->camera, td->player->box);
}

LOG("exiting bridge thread");
Expand Down
1 change: 1 addition & 0 deletions src/core/bridge.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ typedef struct w_bridge {
w_player *player;

Camera2D *camera;
Vector2 camera_target;

#ifdef _WIN32
HANDLE handle;
Expand Down
8 changes: 3 additions & 5 deletions src/core/mainframe.c
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,11 @@ w_state *init_mainframe() {

#ifdef _DEBUG
SetTraceLogLevel(LOG_ALL);
SetRandomSeed(0);

#else
SetTraceLogLevel(LOG_NONE);
SetRandomSeed(time(NULL));
#endif // _DEBUG

unsigned int flags = 0;
Expand All @@ -35,11 +38,6 @@ w_state *init_mainframe() {

SetConfigFlags(flags);
SetTargetFPS(state->config->max_fps);
#ifdef _DEBUG
SetRandomSeed(0);
#else
SetRandomSeed(time(NULL));
#endif // _DEBUG

InitWindow(state->config->width, state->config->height, "Wispy");
return state;
Expand Down
12 changes: 11 additions & 1 deletion src/core/view.c
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Vector2 center_camera_on_object(Camera2D *camera, Rectangle box) {

return (Vector2){.x = box.x - (RENDER_W - box.width) / 2.f,
.y = box.y - (RENDER_W - box.width) / 2.f};
.y = box.y - (RENDER_H - box.height) / 2.f};
}
Vector2 center_object_on_camera(Rectangle box, Camera2D *camera) {
return (Vector2){.x = camera->target.x + (RENDER_W - box.width) / 2,
Expand All @@ -16,3 +16,13 @@ Rectangle get_camera_view(Camera2D *camera) {
.width = RENDER_W,
.height = RENDER_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);
}
8 changes: 5 additions & 3 deletions src/core/view.h
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
#pragma once
#include "../stdafx.h"

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

Vector2 center_camera_on_object(Camera2D *camera, Rectangle box);
Vector2 center_object_on_camera(Rectangle box, Camera2D *camera);
Rectangle get_camera_view(Camera2D *camera);
void smooth_vec(Vector2 *position, Vector2 target, float move);
void smooth_rect(Rectangle *box, Rectangle target, float move);
8 changes: 7 additions & 1 deletion src/entities/player.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include "../stdafx.h"
#include "../terrain/chunk.h"

#define PLAYER_SPEED 1000.f
#define PLAYER_SRC_RECT \
(Rectangle) { 0, 0, 8, 16 }

typedef enum w_playerstate {
P_IDLE_1,
P_IDLE_2,
Expand All @@ -11,8 +15,10 @@ typedef enum w_playerstate {
} w_playerstate;

typedef struct w_player {
Rectangle box;
Rectangle src;
Rectangle dst;

Vector2 position;
Vector2 velocity;

float delay;
Expand Down
50 changes: 25 additions & 25 deletions src/screen/game.c
Original file line number Diff line number Diff line change
Expand Up @@ -21,59 +21,59 @@ void game_screen(w_state *state) {

w_bridge *td = create_bridge();

Vector2 camera_target = center_camera_on_object(td->camera, td->player->box);
while (!WindowShouldClose() && td->is_active) {
update_keyboard(td->keyboard);

if (td->keyboard->key != 0) {
camera_target = center_camera_on_object(td->camera, td->player->box);
}

float speed = GetFrameTime() * 1000.f;

smooth_camera(td->camera->target.x, camera_target.x, speed);
smooth_camera(td->camera->target.y, camera_target.y, speed);
float speed = GetFrameTime() * PLAYER_SPEED * 0.8;

BeginTextureMode(state->render);
ClearBackground(BLACK);
BeginMode2D(*(td->camera));
DrawRectangleGradientV(0, 0, RENDER_W, RENDER_H, (Color){66, 135, 245, 255},
(Color){142, 184, 250, 255});

#ifdef _WIN32
WaitForSingleObject(td->chunk_view->mutex, INFINITE);
#else
pthread_mutex_lock(&td->chunk_view->mutex);
#endif // _WIN32

BeginMode2D(*(td->camera));
smooth_vec(&td->camera->target, td->camera_target, speed);
for (unsigned int i = 0; i < td->chunk_view->len; i++) {
DrawTexturePro(block_textures[td->chunk_view->blocks[i].block.type - 1],
td->chunk_view->blocks[i].src,
td->chunk_view->blocks[i].dst, VEC_ZERO, 0, WHITE);
}

EndMode2D();
DrawText(TextFormat("Block %d\n\nCamera %f %f\n\Player %f %f\n\nChunk:\n "
"Current: %d\n Next: %d\n Group: %d",
td->chunk_view->len, td->camera->target.x,
td->camera->target.y, td->player->box.x,
td->player->box.y, td->chunk_view->target->position,
td->chunk_view->next == NULL
? -1
: td->chunk_view->next->position,
td->chunk_group->position),
0, 20, 20, WHITE);
EndTextureMode();

DrawTexturePro(player_textures[0], td->player->src, td->player->dst,
VEC_ZERO, 0, WHITE);
/*
DrawText(
TextFormat(
"Block %d\n\nCamera %f %f\n\Player %f %f\n\nChunk:\n "
"Current: %d\n Next: %d\n Group: %d",
td->chunk_view->len, td->camera->target.x, td->camera->target.y,
td->player->position.x, td->player->position.y,
td->chunk_view->target->position,
td->chunk_view->next == NULL ? -1 : td->chunk_view->next->position,
td->chunk_group->position),
0, 20, 20, WHITE);
*/
#ifdef _WIN32
ReleaseMutex(td->chunk_view->mutex);
#else
pthread_mutex_unlock(&td->chunk_view->mutex);
#endif // _WIN32

EndTextureMode();

BeginDrawing();
ClearBackground(BLACK);
DrawTexturePro(state->render.texture, state->src_rnd, state->dest_rnd,
VEC_ZERO, 0.0f, WHITE);
DrawFPS(0, 0);

DrawText(
TextFormat("dt: %f\nfps: %f", GetFrameTime(), 1.f / GetFrameTime()), 0,
0, 20, WHITE);
EndDrawing();
}
destroy_bridge(td);
Expand Down

0 comments on commit c931f1e

Please sign in to comment.