Skip to content

Commit

Permalink
feat: implement minimal camera movement
Browse files Browse the repository at this point in the history
  • Loading branch information
developer239 committed Dec 3, 2023
1 parent ceaf19c commit d8c4c28
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 43 deletions.
9 changes: 4 additions & 5 deletions src/apps/day3/src/strategies/ECSStrategy.h
Original file line number Diff line number Diff line change
Expand Up @@ -49,8 +49,8 @@ class MinimalLoopStrategy : public Core::IStrategy {
ECS::Registry::Instance().AddComponent<CameraComponent>(
cameraEntity,
Vec2(0, 0),
window.GetWidth() / 2,
window.GetHeight() / 2
window.GetWidth() + 100,
window.GetHeight() + 100
);

// Puzzle related entities
Expand Down Expand Up @@ -172,9 +172,8 @@ class MinimalLoopStrategy : public Core::IStrategy {
renderer,
cameraEntity
);
ECS::Registry::Instance().GetSystem<RenderTextSystem>().Render(renderer);
ECS::Registry::Instance().GetSystem<RenderTextSystem>().Render(renderer, cameraEntity);

ECS::Registry::Instance().GetSystem<RenderCollidersSystem>().Render(renderer
);
ECS::Registry::Instance().GetSystem<RenderCollidersSystem>().Render(renderer, cameraEntity);
}
};
24 changes: 20 additions & 4 deletions src/apps/day3/src/systems/RenderColliderSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,25 @@ class RenderCollidersSystem : public ECS::System {
RequireComponent<RigidBodyComponent>();
}

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

for (auto entity : GetSystemEntities()) {
auto& boxCollider =
ECS::Registry::Instance().GetComponent<BoxColliderComponent>(entity);
auto& rigidBody =
ECS::Registry::Instance().GetComponent<RigidBodyComponent>(entity);

// Calculate the position of the collider relative to the camera
int relativeX = static_cast<int>(
rigidBody.position.x + boxCollider.offset.x - camera.position.x
);
int relativeY = static_cast<int>(
rigidBody.position.y + boxCollider.offset.y - camera.position.y
);

SDL_SetRenderDrawColor(
renderer.Get().get(),
boxCollider.color.r,
Expand All @@ -35,14 +45,20 @@ class RenderCollidersSystem : public ECS::System {

for (int i = 0; i < borderWidth; ++i) {
SDL_Rect colliderRect = {
static_cast<int>(rigidBody.position.x + boxCollider.offset.x - i),
static_cast<int>(rigidBody.position.y + boxCollider.offset.y - i),
relativeX - i,
relativeY - i,
boxCollider.width + i * 2,
boxCollider.height + i * 2};
SDL_RenderDrawRect(renderer.Get().get(), &colliderRect);
}
}

SDL_SetRenderDrawColor(renderer.Get().get(), 0, 0, 0, 255);
SDL_SetRenderDrawColor(
renderer.Get().get(),
0,
0,
0,
255
); // Reset render color
}
};
31 changes: 13 additions & 18 deletions src/apps/day3/src/systems/RenderRigidBodiesSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,26 +11,21 @@ class RenderRigidBodiesSystem : public ECS::System {
RenderRigidBodiesSystem() { RequireComponent<RigidBodyComponent>(); }

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

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

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
);
auto& rigidBodyComponent = ECS::Registry::Instance().GetComponent<RigidBodyComponent>(entity);

// Calculate the position of the rigid body relative to the camera
int relativeX = static_cast<int>(rigidBodyComponent.position.x - camera.position.x);
int relativeY = static_cast<int>(rigidBodyComponent.position.y - camera.position.y);

// Check if the entity is within the camera's view before rendering
if (relativeX + rigidBodyComponent.width > 0 && relativeX < camera.width &&
relativeY + rigidBodyComponent.height > 0 && relativeY < camera.height) {

SDL_Rect rect = { relativeX, relativeY, rigidBodyComponent.width, 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);
}
}
Expand Down
26 changes: 10 additions & 16 deletions src/apps/day3/src/systems/RenderTextSystem.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,37 +13,31 @@ class RenderTextSystem : public ECS::System {
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()) {
const auto& textLabel = ECS::Registry::Instance().GetComponent<TextComponent>(entity);
const auto& rigidBody = ECS::Registry::Instance().GetComponent<RigidBodyComponent>(entity);

TTF_Font* font = Core::AssetStore::Instance().GetFont(textLabel.fontId).get();
if (!font) {
continue;
}
if (!font) continue;

SDL_Surface* surface = TTF_RenderText_Blended(font, textLabel.text.c_str(), textLabel.color);
if (!surface) {
continue;
}
if (!surface) continue;

SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer.Get().get(), surface);
SDL_FreeSurface(surface);
if (!texture) {
continue;
}
if (!texture) continue;

int textWidth = 0, textHeight = 0;
SDL_QueryTexture(texture, nullptr, nullptr, &textWidth, &textHeight);

// Center

int xPosition = static_cast<int>(rigidBody.position.x + (rigidBody.width - textWidth) / 2);
int yPosition = static_cast<int>(rigidBody.position.y + (rigidBody.height - textHeight) / 2);

SDL_Rect dstRect = { xPosition, yPosition, textWidth, textHeight };
// Calculate text position relative to the camera
int relativeX = rigidBody.position.x + (rigidBody.width - textWidth) / 2 - camera.position.x;
int relativeY = rigidBody.position.y + (rigidBody.height - textHeight) / 2 - camera.position.y;

SDL_Rect dstRect = { relativeX, relativeY, textWidth, textHeight };
SDL_RenderCopy(renderer.Get().get(), texture, nullptr, &dstRect);

SDL_DestroyTexture(texture);
Expand Down

0 comments on commit d8c4c28

Please sign in to comment.