Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
ViewportManager.cpp
Go to the documentation of this file.
1#include "ViewportManager.h"
2#include "../GameEngine.h"
3
4// Implementation file intentionally empty: all behavior implemented inline in header for simplicity.
5// If later needed, complex logic can be moved here.
6
11
16//-------------------------------------------------------------
18{
19 // render viewport rectangles
20
22 for (const auto &rect : m_viewRects)
23 {
25 }
26}
27//-------------------------------------------------------------
29{
30 m_width = w; m_height = h;
31 m_players.clear();
32 m_playerIndexMap.clear();
33 m_viewRects.clear();
34 // default single viewport
36 SYSTEM_LOG << "Viewport Initialized\n";
37}
38//-------------------------------------------------------------
40{
41 m_players.clear();
42 m_viewRects.clear();
43 m_playerIndexMap.clear();
44 SYSTEM_LOG << "Viewport Shutdown\n";
45}
46//-------------------------------------------------------------
47// Add a player id. Returns true if added, false if already present or full
49{
50 if (m_playerIndexMap.find(playerID) != m_playerIndexMap.end())
51 return false;
52
53 if (m_players.size() >= MAX_PLAYERS)
54 return false;
55
56 m_players.push_back(playerID);
57 m_playerIndexMap[playerID] = static_cast<int>(m_players.size() - 1);
59 return true;
60}
61//-------------------------------------------------------------
63{
64 auto it = std::find(m_players.begin(), m_players.end(), playerID);
65 if (it == m_players.end()) return false;
66 m_players.erase(it);
67 m_playerIndexMap.clear();
68 for (size_t i = 0; i < m_players.size(); ++i) m_playerIndexMap[m_players[i]] = static_cast<int>(i);
70 return true;
71}
72//-------------------------------------------------------------
74{
76}
77//-------------------------------------------------------------
78// If a player exists, fill out outRect and return true
80{
81 auto it = m_playerIndexMap.find(playerID);
82 if (it == m_playerIndexMap.end()) return false;
83 int idx = it->second;
84 if (idx < 0 || idx >= static_cast<int>(m_viewRects.size())) return false;
86 return true;
87}
88// -------------------------------------------------------------
90{
91 m_viewRects.clear();
92 int n = static_cast<int>(m_players.size());
93
94 // If no players, create a single full-screen viewport but don't treat it as a player
95 if (n == 0)
96 {
97 SDL_FRect r{ 0,0,static_cast<float>(m_width),static_cast<float>(m_height) };
98 m_viewRects.push_back(r);
99 return; // Exit early - no player mapping needed
100 }
101
102 switch (n)
103 {
104 case 1:
105 {
106 SDL_FRect r{ 0,0,static_cast<float>(m_width),static_cast<float>(m_height) };
107 m_viewRects.push_back(r);
108 break;
109 }
110 case 2:
111 {
112 int w = m_width / 2;
113 SDL_FRect r1{ 0,0,static_cast<float>(w),static_cast<float>(m_height) };
114 SDL_FRect r2{ static_cast<float>(w),0,static_cast<float>(m_width - w),static_cast<float>(m_height) };
115 m_viewRects.push_back(r1);
116 m_viewRects.push_back(r2);
117 break;
118 }
119 case 3:
120 {
121 int w = m_width / 3;
122 SDL_FRect r1{ 0,0,static_cast<float>(w),static_cast<float>(m_height) };
123 SDL_FRect r2{ static_cast<float>(w),0,static_cast<float>(w),static_cast<float>(m_height) };
124 SDL_FRect r3{ static_cast<float>(2 * w),0,static_cast<float>(m_width - 2 * w),static_cast<float>(m_height) };
125 m_viewRects.push_back(r1);
126 m_viewRects.push_back(r2);
127 m_viewRects.push_back(r3);
128 break;
129 }
130 case 4:
131 {
132 int w = m_width / 2;
133 int h = m_height / 2;
134 SDL_FRect r1{ 0,0,static_cast<float>(w),static_cast<float>(h) };
135 SDL_FRect r2{ static_cast<float>(w),0,static_cast<float>(m_width - w),static_cast<float>(h) };
136 SDL_FRect r3{ 0,static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
137 SDL_FRect r4{ static_cast<float>(w),static_cast<float>(h),static_cast<float>(m_width - w),static_cast<float>(m_height - h) };
138 m_viewRects.push_back(r1);
139 m_viewRects.push_back(r2);
140 m_viewRects.push_back(r3);
141 m_viewRects.push_back(r4);
142 break;
143 }
144 case 5:
145 {
146 int w = m_width / 3;
147 int h = m_height / 2;
148 SDL_FRect r1{ 0,0,static_cast<float>(w),static_cast<float>(h) };
149 SDL_FRect r2{ static_cast<float>(w),0,static_cast<float>(w),static_cast<float>(h) };
150 SDL_FRect r3{ static_cast<float>(2 * w),0,static_cast<float>(m_width - 2 * w),static_cast<float>(h) };
151 SDL_FRect r4{ 0,static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
152 SDL_FRect r5{ static_cast<float>(w),static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
153 m_viewRects.push_back(r1);
154 m_viewRects.push_back(r2);
155 m_viewRects.push_back(r3);
156 m_viewRects.push_back(r4);
157 m_viewRects.push_back(r5);
158 break;
159 }
160 case 6:
161 {
162 int w = m_width / 3;
163 int h = m_height / 2;
164 SDL_FRect r1{ 0,0,static_cast<float>(w),static_cast<float>(h) };
165 SDL_FRect r2{ static_cast<float>(w),0,static_cast<float>(w),static_cast<float>(h) };
166 SDL_FRect r3{ static_cast<float>(2 * w),0,static_cast<float>(m_width - 2 * w),static_cast<float>(h) };
167 SDL_FRect r4{ 0,static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
168 SDL_FRect r5{ static_cast<float>(w),static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
169 SDL_FRect r6{ static_cast<float>(2 * w),static_cast<float>(h),static_cast<float>(m_width - 2 * w),static_cast<float>(m_height - h) };
170 m_viewRects.push_back(r1);
171 m_viewRects.push_back(r2);
172 m_viewRects.push_back(r3);
173 m_viewRects.push_back(r4);
174 m_viewRects.push_back(r5);
175 m_viewRects.push_back(r6);
176 break;
177 }
178 case 7:
179 {
180 int w = m_width / 4;
181 int h = m_height / 2;
182 SDL_FRect r1{ 0,0,static_cast<float>(w),static_cast<float>(h) };
183 SDL_FRect r2{ static_cast<float>(w),0,static_cast<float>(w),static_cast<float>(h) };
184 SDL_FRect r3{ static_cast<float>(2 * w),0,static_cast<float>(w),static_cast<float>(h) };
185 SDL_FRect r4{ static_cast<float>(3 * w),0,static_cast<float>(m_width - 3 * w),static_cast<float>(h) };
186 SDL_FRect r5{ 0,static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
187 SDL_FRect r6{ static_cast<float>(w),static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
188 SDL_FRect r7{ static_cast<float>(2 * w),static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
189 m_viewRects.push_back(r1);
190 m_viewRects.push_back(r2);
191 m_viewRects.push_back(r3);
192 m_viewRects.push_back(r4);
193 m_viewRects.push_back(r5);
194 m_viewRects.push_back(r6);
195 m_viewRects.push_back(r7);
196 break;
197 }
198 case 8:
199 {
200 int w = m_width / 4;
201 int h = m_height / 2;
202 SDL_FRect r1{ 0,0,static_cast<float>(w),static_cast<float>(h) };
203 SDL_FRect r2{ static_cast<float>(w),0,static_cast<float>(w),static_cast<float>(h) };
204 SDL_FRect r3{ static_cast<float>(2 * w),0,static_cast<float>(w),static_cast<float>(h) };
205 SDL_FRect r4{ static_cast<float>(3 * w),0,static_cast<float>(m_width - 3 * w),static_cast<float>(h) };
206 SDL_FRect r5{ 0,static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
207 SDL_FRect r6{ static_cast<float>(w),static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
208 SDL_FRect r7{ static_cast<float>(2 * w),static_cast<float>(h),static_cast<float>(w),static_cast<float>(m_height - h) };
209 SDL_FRect r8{ static_cast<float>(3 * w),static_cast<float>(h),static_cast<float>(m_width - 3 * w),static_cast<float>(m_height - h) };
210 m_viewRects.push_back(r1);
211 m_viewRects.push_back(r2);
212 m_viewRects.push_back(r3);
213 m_viewRects.push_back(r4);
214 m_viewRects.push_back(r5);
215 m_viewRects.push_back(r6);
216 m_viewRects.push_back(r7);
217 m_viewRects.push_back(r8);
218 break;
219 }
220 default:
221 break;
222 }
223
224
225 // Rebuild playerIndexMap to match players to their viewport indices
226 m_playerIndexMap.clear();
227 for (size_t i = 0; i < m_players.size() && i < m_viewRects.size(); ++i)
228 m_playerIndexMap[m_players[i]] = static_cast<int>(i);
229}
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
ViewportLayout
static int screenWidth
Screen width in pixels.
Definition GameEngine.h:123
static int screenHeight
Screen height in pixels.
Definition GameEngine.h:126
static SDL_Renderer * renderer
Main SDL renderer.
Definition GameEngine.h:129
void SetSize(int w, int h)
bool GetViewRectForPlayer(short playerID, SDL_FRect &outRect) const
std::unordered_map< short, int > m_playerIndexMap
void Initialize(int w, int h)
std::vector< SDL_FRect > m_viewRects
bool AddPlayer(short playerID, ViewportLayout viewportLayout)
std::vector< short > m_players
virtual void Render()
bool RemovePlayer(short playerID)
static const short MAX_PLAYERS
#define SYSTEM_LOG