From d8c4c28f57cec553939f0e3bbb5d0667aa704c71 Mon Sep 17 00:00:00 2001 From: michaljarnot Date: Sun, 3 Dec 2023 14:30:35 +0100 Subject: [PATCH] feat: implement minimal camera movement --- src/apps/day3/src/strategies/ECSStrategy.h | 9 +++--- .../day3/src/systems/RenderColliderSystem.h | 24 +++++++++++--- .../src/systems/RenderRigidBodiesSystem.h | 31 ++++++++----------- src/apps/day3/src/systems/RenderTextSystem.h | 26 ++++++---------- 4 files changed, 47 insertions(+), 43 deletions(-) diff --git a/src/apps/day3/src/strategies/ECSStrategy.h b/src/apps/day3/src/strategies/ECSStrategy.h index ba641aa..beb67a5 100644 --- a/src/apps/day3/src/strategies/ECSStrategy.h +++ b/src/apps/day3/src/strategies/ECSStrategy.h @@ -49,8 +49,8 @@ class MinimalLoopStrategy : public Core::IStrategy { ECS::Registry::Instance().AddComponent( cameraEntity, Vec2(0, 0), - window.GetWidth() / 2, - window.GetHeight() / 2 + window.GetWidth() + 100, + window.GetHeight() + 100 ); // Puzzle related entities @@ -172,9 +172,8 @@ class MinimalLoopStrategy : public Core::IStrategy { renderer, cameraEntity ); - ECS::Registry::Instance().GetSystem().Render(renderer); + ECS::Registry::Instance().GetSystem().Render(renderer, cameraEntity); - ECS::Registry::Instance().GetSystem().Render(renderer - ); + ECS::Registry::Instance().GetSystem().Render(renderer, cameraEntity); } }; diff --git a/src/apps/day3/src/systems/RenderColliderSystem.h b/src/apps/day3/src/systems/RenderColliderSystem.h index e79f688..dbdec42 100644 --- a/src/apps/day3/src/systems/RenderColliderSystem.h +++ b/src/apps/day3/src/systems/RenderColliderSystem.h @@ -16,8 +16,10 @@ class RenderCollidersSystem : public ECS::System { RequireComponent(); } - void Render(Core::Renderer& renderer) { + void Render(Core::Renderer& renderer, ECS::Entity cameraEntity) { const int borderWidth = 5; + auto& camera = + ECS::Registry::Instance().GetComponent(cameraEntity); for (auto entity : GetSystemEntities()) { auto& boxCollider = @@ -25,6 +27,14 @@ class RenderCollidersSystem : public ECS::System { auto& rigidBody = ECS::Registry::Instance().GetComponent(entity); + // Calculate the position of the collider relative to the camera + int relativeX = static_cast( + rigidBody.position.x + boxCollider.offset.x - camera.position.x + ); + int relativeY = static_cast( + rigidBody.position.y + boxCollider.offset.y - camera.position.y + ); + SDL_SetRenderDrawColor( renderer.Get().get(), boxCollider.color.r, @@ -35,14 +45,20 @@ class RenderCollidersSystem : public ECS::System { for (int i = 0; i < borderWidth; ++i) { SDL_Rect colliderRect = { - static_cast(rigidBody.position.x + boxCollider.offset.x - i), - static_cast(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 } }; diff --git a/src/apps/day3/src/systems/RenderRigidBodiesSystem.h b/src/apps/day3/src/systems/RenderRigidBodiesSystem.h index fdb5ef2..a268068 100644 --- a/src/apps/day3/src/systems/RenderRigidBodiesSystem.h +++ b/src/apps/day3/src/systems/RenderRigidBodiesSystem.h @@ -11,26 +11,21 @@ class RenderRigidBodiesSystem : public ECS::System { RenderRigidBodiesSystem() { RequireComponent(); } void Render(Core::Renderer& renderer, ECS::Entity cameraEntity) { - auto& camera = - ECS::Registry::Instance().GetComponent(cameraEntity); + auto& camera = ECS::Registry::Instance().GetComponent(cameraEntity); for (auto entity : GetSystemEntities()) { - auto rigidBodyComponent = - ECS::Registry::Instance().GetComponent(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(entity); + + // Calculate the position of the rigid body relative to the camera + int relativeX = static_cast(rigidBodyComponent.position.x - camera.position.x); + int relativeY = static_cast(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); } } diff --git a/src/apps/day3/src/systems/RenderTextSystem.h b/src/apps/day3/src/systems/RenderTextSystem.h index 166bcd9..716d66c 100644 --- a/src/apps/day3/src/systems/RenderTextSystem.h +++ b/src/apps/day3/src/systems/RenderTextSystem.h @@ -13,37 +13,31 @@ class RenderTextSystem : public ECS::System { RequireComponent(); } - void Render(Core::Renderer& renderer) { + void Render(Core::Renderer& renderer, ECS::Entity cameraEntity) { + auto& camera = ECS::Registry::Instance().GetComponent(cameraEntity); + for (auto entity : GetSystemEntities()) { const auto& textLabel = ECS::Registry::Instance().GetComponent(entity); const auto& rigidBody = ECS::Registry::Instance().GetComponent(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(rigidBody.position.x + (rigidBody.width - textWidth) / 2); - int yPosition = static_cast(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);