Skip to content

Commit

Permalink
feat: implement minimal camera
Browse files Browse the repository at this point in the history
  • Loading branch information
developer239 committed Dec 3, 2023
1 parent 7213ca9 commit ceaf19c
Show file tree
Hide file tree
Showing 5 changed files with 115 additions and 11 deletions.
3 changes: 2 additions & 1 deletion src/apps/day3/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ set(SOURCES
src/systems/RenderTextSystem.h
src/systems/RenderGridSystem.h
src/systems/RenderColliderSystem.h
src/systems/PuzzleSolverSystem.h)
src/systems/PuzzleSolverSystem.h
src/components/CameraComponent.h src/systems/CameraSystem.h)

add_executable(${APP_NAME} ${SOURCES})

Expand Down
12 changes: 12 additions & 0 deletions src/apps/day3/src/components/CameraComponent.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
#pragma once

#include "../common/Vec2.h"

struct CameraComponent {
Vec2 position;
int width;
int height;

CameraComponent(Vec2 position = Vec2(0, 0), int width = 0, int height = 0)
: position(position), width(width), height(height){};
};
18 changes: 17 additions & 1 deletion src/apps/day3/src/strategies/ECSStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "../common/Vec2.h"
#include "../components/TextComponent.h"
#include "../services/PuzzleInputParser.h"
#include "../systems/CameraSystem.h"
#include "../systems/CollisionSystem.h"
#include "../systems/KeyboardControlSystem.h"
#include "../systems/PuzzleSolverSystem.h"
Expand All @@ -18,6 +19,8 @@

class MinimalLoopStrategy : public Core::IStrategy {
public:
ECS::Entity cameraEntity = ECS::Registry::Instance().CreateEntity();

void Init(Core::Window& window, Core::Renderer& renderer) override {
Core::AssetStore::Instance().AddFont("pico8", "assets/fonts/arial.ttf", 24);

Expand All @@ -28,6 +31,7 @@ class MinimalLoopStrategy : public Core::IStrategy {
ECS::Registry::Instance().AddSystem<RenderTextSystem>();
ECS::Registry::Instance().AddSystem<RenderCollidersSystem>();
ECS::Registry::Instance().AddSystem<PuzzleSolverSystem>();
ECS::Registry::Instance().AddSystem<CameraSystem>();

// Events
ECS::Registry::Instance()
Expand All @@ -36,9 +40,20 @@ class MinimalLoopStrategy : public Core::IStrategy {

ECS::Registry::Instance().GetSystem<PuzzleSolverSystem>().SubscribeToEvents(
);
ECS::Registry::Instance().GetSystem<CameraSystem>().SubscribeToEvents(
);

// Entities & Components

// Camera
ECS::Registry::Instance().AddComponent<CameraComponent>(
cameraEntity,
Vec2(0, 0),
window.GetWidth() / 2,
window.GetHeight() / 2
);

// Puzzle related entities
auto inputData = ParseInput("assets/input-example-1.txt");
auto partsAndSymbols = FindPartsAndSymbols(inputData);

Expand Down Expand Up @@ -154,7 +169,8 @@ class MinimalLoopStrategy : public Core::IStrategy {
window
);
ECS::Registry::Instance().GetSystem<RenderRigidBodiesSystem>().Render(
renderer
renderer,
cameraEntity
);
ECS::Registry::Instance().GetSystem<RenderTextSystem>().Render(renderer);

Expand Down
49 changes: 49 additions & 0 deletions src/apps/day3/src/systems/CameraSystem.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
#pragma once

#include "../components/CameraComponent.h"
#include "../events/KeyPressedEvent.h"
#include "ecs/System.h"
#include "events/Bus.h"

class CameraSystem : public ECS::System {
public:
CameraSystem() {
RequireComponent<CameraComponent>();
SubscribeToEvents();
}

void SubscribeToEvents() {
Events::Bus::Instance().SubscribeToEvent<KeyPressedEvent>(
this,
&CameraSystem::OnKeyPressed
);
}

void OnKeyPressed(KeyPressedEvent& event) {
for (auto entity : GetSystemEntities()) {
auto& camera =
ECS::Registry::Instance().GetComponent<CameraComponent>(entity);

int cameraSpeed = 10;

switch (event.symbol) {
case SDLK_UP:
camera.position.y -= cameraSpeed;
break;
case SDLK_DOWN:
camera.position.y += cameraSpeed;
break;
case SDLK_LEFT:
camera.position.x -= cameraSpeed;
break;
case SDLK_RIGHT:
camera.position.x += cameraSpeed;
break;
}
}
}

void Update() {
// Additional logic for updating camera (if needed)
}
};
44 changes: 35 additions & 9 deletions src/apps/day3/src/systems/RenderRigidBodiesSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,17 +10,43 @@ class RenderRigidBodiesSystem : public ECS::System {
public:
RenderRigidBodiesSystem() { RequireComponent<RigidBodyComponent>(); }

void Render(Core::Renderer& renderer) {
void Render(Core::Renderer& renderer, ECS::Entity cameraEntity) {
auto& camera =
ECS::Registry::Instance().GetComponent<CameraComponent>(cameraEntity);

for (auto entity : GetSystemEntities()) {
auto rigidBodyComponent = ECS::Registry::Instance().GetComponent<RigidBodyComponent>(entity);
auto rigidBodyComponent =
ECS::Registry::Instance().GetComponent<RigidBodyComponent>(entity);

SDL_Rect rect = {
(int)rigidBodyComponent.position.x,
(int)rigidBodyComponent.position.y,
(int)rigidBodyComponent.width,
(int)rigidBodyComponent.height};
SDL_SetRenderDrawColor(renderer.Get().get(), rigidBodyComponent.color.r, rigidBodyComponent.color.g, rigidBodyComponent.color.b, rigidBodyComponent.color.a);
SDL_RenderFillRect(renderer.Get().get(), &rect);
if (IsEntityInView(rigidBodyComponent, camera)) {
SDL_Rect rect = {
(int)rigidBodyComponent.position.x,
(int)rigidBodyComponent.position.y,
(int)rigidBodyComponent.width,
(int)rigidBodyComponent.height};
SDL_SetRenderDrawColor(
renderer.Get().get(),
rigidBodyComponent.color.r,
rigidBodyComponent.color.g,
rigidBodyComponent.color.b,
rigidBodyComponent.color.a
);
SDL_RenderFillRect(renderer.Get().get(), &rect);
}
}
}

private:
bool IsEntityInView(
const RigidBodyComponent& rigidBody, const CameraComponent& camera
) {
// Check if the entity's bounds intersect with the camera's view
bool isHorizontalInView =
(rigidBody.position.x + rigidBody.width > camera.position.x) &&
(rigidBody.position.x < camera.position.x + camera.width);
bool isVerticalInView =
(rigidBody.position.y + rigidBody.height > camera.position.y) &&
(rigidBody.position.y < camera.position.y + camera.height);
return isHorizontalInView && isVerticalInView;
}
};

0 comments on commit ceaf19c

Please sign in to comment.