Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ECS_Systems_Rendering_Camera.cpp
Go to the documentation of this file.
1/*
2Olympe Engine V2 - 2025
3Nicolas Chereau
4nchereau@gmail.com
5
6This file is part of Olympe Engine V2.
7
8Camera Rendering Integration: Provides camera transformation utilities for the
9RenderingSystem, including world-to-screen coordinate conversion and frustum culling.
10
11*/
12
14#include "ECS_Components.h"
15#include "ECS_Register.h"
16#include "World.h"
17#include "vector.h"
20#include <cmath>
21#include <SDL3/SDL.h>
23
24
25// Get the active camera transform for a specific player
27{
29 transform.isActive = false;
30
31 // Iterate through all entities to find the camera for this player
32 World& world = World::Get();
33
34 for (const auto& pair : world.m_entitySignatures)
35 {
36 EntityID entity = pair.first;
37 const ComponentSignature& signature = pair.second;
38
39 // Check if entity has Camera_data component
41 if (!signature.test(camTypeID))
42 continue;
43
44 Camera_data& cam = world.GetComponent<Camera_data>(entity);
45
46 // For player cameras (playerID >= 0), only consider cameras with matching playerID
47 // Never fall back to default camera (playerId=-1) for player rendering
48 if (playerID >= 0)
49 {
50 if (cam.playerId == playerID && cam.isActive)
51 {
52 // Found the active camera for this player
53 // Apply all world-space offsets to worldPosition (control + base + shake)
54 transform.worldPosition = cam.position + cam.controlOffset + cam.baseOffset;
55
56 // Add shake offset if present
57 if (world.HasComponent<CameraEffects_data>(entity))
58 {
60 if (effects.isShaking)
61 {
62 transform.worldPosition += effects.shakeOffset;
63 }
64 }
65
66 transform.zoom = cam.zoom;
67 transform.rotation = cam.rotation;
68
69 // Get the actual current viewport for this player from ViewportManager
72 {
73 transform.viewport = playerViewport;
74 }
75 else
76 {
77 // Fallback: use stored viewport if player not found (shouldn't happen for active players)
78 transform.viewport = cam.viewportRect;
79 }
80
81 transform.isActive = true;
82
83 // Screen offset is zero for world rendering (offsets are applied in world space)
84 transform.screenOffset = Vector(0.f, 0.f, 0.f);
85
86 break;
87 }
88 }
89 else
90 {
91 // For single-view/no-players case, allow default camera (playerId=-1)
92
93 if (cam.playerId == -1 && cam.isActive)
94 {
95 // Found the default camera
96 // Apply all world-space offsets to worldPosition (control + base + shake)
97 transform.worldPosition = cam.position + cam.controlOffset + cam.baseOffset;
98
99 // Add shake offset if present
100 if (world.HasComponent<CameraEffects_data>(entity))
101 {
103 if (effects.isShaking)
104 {
105 transform.worldPosition += effects.shakeOffset;
106 }
107 }
108
109 transform.zoom = cam.zoom;
110 transform.rotation = cam.rotation;
111 transform.viewport = cam.viewportRect;
112 transform.isActive = true;
113
114 // Screen offset is zero for world rendering (offsets are applied in world space)
115 transform.screenOffset = Vector(0.f, 0.f, 0.f);
116
117 break;
118 }
119 }
120 }
121
122 return transform;
123}
Core ECS component definitions.
std::bitset< MAX_COMPONENTS > ComponentSignature
Definition ECS_Entity.h:31
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
std::uint64_t EntityID
Definition ECS_Entity.h:21
std::uint64_t ComponentTypeID
Definition ECS_Entity.h:27
CameraTransform GetActiveCameraTransform(short playerID)
World and ECS Manager for Olympe Engine.
bool GetViewRectForPlayer(short playerID, SDL_FRect &outRect) const
static ViewportManager & Get()
Core ECS manager and world coordinator.
Definition World.h:210
static World & Get()
Get singleton instance (short form)
Definition World.h:232
bool HasComponent(EntityID entity) const
Definition World.h:451
T & GetComponent(EntityID entity)
Definition World.h:438
std::unordered_map< EntityID, ComponentSignature > m_entitySignatures
Definition World.h:636