diff options
author | BoredGuy <osome3717@gmail.com> | 2025-07-31 21:20:08 +0300 |
---|---|---|
committer | BoredGuy <osome3717@gmail.com> | 2025-07-31 21:20:08 +0300 |
commit | 2c2910c33c71878c8fb8083c1fbc1a2d282a979b (patch) | |
tree | 5a058b1e03906ef3238c5bb8b60ba5d50443d8bb | |
parent | aa15ed6f4c60e0e0693d4624e3554948078e9a88 (diff) |
Camera System!!!
-rw-r--r-- | include/game.h | 1 | ||||
-rw-r--r-- | src/game.c | 42 | ||||
-rw-r--r-- | src/main.c | 2 | ||||
-rw-r--r-- | src/player.c | 8 |
4 files changed, 38 insertions, 15 deletions
diff --git a/include/game.h b/include/game.h index eb6da29..fbfe2e1 100644 --- a/include/game.h +++ b/include/game.h @@ -50,6 +50,7 @@ typedef struct Entity { typedef struct Game { bool paused; Entity entities[MAX_ENTITY_COUNT]; + Camera2D camera; #ifdef BEATEMUP_DEBUG //Debug information @@ -2,6 +2,7 @@ #include <string.h> #include "stdio.h" +#include "constants.h" #include "game.h" #include "assets.h" #include "player.h" @@ -32,7 +33,7 @@ void AddEntity(Entity* e) { e->id = nextId++; e->flags |= ENTITY_ALLOCATED; game.entities[i] = *e; - break; + return; } } @@ -42,12 +43,13 @@ void AddEntity(Entity* e) { } void InitGame() { - game.paused = false; -#ifdef BEATEMUP_DEBUG - game.enableDebugOverlay = false; -#endif + memset(&game, 0, sizeof(Game)); - memset(game.entities, 0, sizeof(game.entities)); + game.camera = (Camera2D) { + .offset = (Vector2) {WINDOW_WIDTH / 2.0f, 0.0f}, + .rotation = 0.0f, + .zoom = 1.0f + }; } void UpdateEntity(Entity* e, float deltaTime) { @@ -93,26 +95,38 @@ void DrawEntity(const Entity* e) { } } +void DrawEntitiesInLayer(int layer) { + for (int i = 0; i < MAX_ENTITY_COUNT; i++) { + Entity* currentEntity = &game.entities[i]; + + if (ShouldDrawEntity(currentEntity, layer)) + DrawEntity(currentEntity); + } +} + void DrawGame() { BeginDrawing(); ClearBackground(RAYWHITE); - for (DrawLayer layer = Background_Layer; layer <= Foreground_Layer; layer++) { - for (int j = 0; j < MAX_ENTITY_COUNT; j++) { - Entity* currentEntity = &game.entities[j]; + BeginMode2D(game.camera); - if (ShouldDrawEntity(currentEntity, layer)) - DrawEntity(currentEntity); - } - } + for (DrawLayer layer = Background_Layer; layer <= Foreground_Layer; layer++) + DrawEntitiesInLayer(layer); #ifdef BEATEMUP_DEBUG if (game.enableDebugOverlay) { for (int i = 0; i < MAX_ENTITY_COUNT; i++) { DebugHighlights(&game.entities[i]); } + } +#endif - DrawFPS(10, 10); + EndMode2D(); + +#ifdef BEATEMUP_DEBUG + if (game.enableDebugOverlay) { + const int posX = 10, posY = 10; + DrawFPS(posX, posY); } #endif @@ -1,9 +1,9 @@ #include <stdio.h> #include <raylib.h> #include "constants.h" -#include "assets.h" #include "game.h" #include "background.h" +#include "player.h" int main() { SetConfigFlags(FLAG_VSYNC_HINT); //Always better than fixed FPS imo diff --git a/src/player.c b/src/player.c index c8e3f7d..6426db7 100644 --- a/src/player.c +++ b/src/player.c @@ -1,6 +1,11 @@ +#include <math.h> +#include "constants.h" +#include "game.h" #include "player.h" #include "physics.h" +extern Game game; + #define PLAYER_SPEED 300.0f void UpdatePlayer(Entity* player, float deltaTime) { @@ -24,6 +29,9 @@ void UpdatePlayer(Entity* player, float deltaTime) { player->velocity = Vector2Scale(player->velocity, PLAYER_SPEED); MoveAndSlide(player, deltaTime); + + game.camera.target = + (Vector2) {fmax(WINDOW_WIDTH / 2.0f, player->position.x), 0.0f}; } void AddPlayer(float xpos, float ypos) { |