Olympe Engine 2.0
2D Game Engine with ECS Architecture
Loading...
Searching...
No Matches
IsometricRenderer.cpp
Go to the documentation of this file.
1/*
2 * Olympe Engine V2 - 2025
3 * Isometric Renderer Implementation
4 *
5 * -> REFACTORED: Now provides utility functions only
6 * Batching and sorting moved to unified rendering pipeline in ECS_Systems.cpp
7 */
8
9#include "IsometricRenderer.h"
10#include "../system/system_utils.h"
11#include <algorithm>
12#include <cmath>
13#include "../drawing.h"
14
15namespace Olympe {
16namespace Rendering {
17
19 : m_renderer(nullptr)
20 , m_tileWidth(64)
21 , m_tileHeight(32)
22 , m_cameraX(0.0f)
23 , m_cameraY(0.0f)
24 , m_zoom(1.0f)
25 , m_screenWidth(800)
26 , m_screenHeight(600)
27 {
28 }
29
33
34 void IsometricRenderer::Initialize(SDL_Renderer* renderer, int tileWidth, int tileHeight)
35 {
37 m_tileWidth = tileWidth;
38 m_tileHeight = tileHeight;
39
40 SYSTEM_LOG << "IsometricRenderer: Initialized with tile size "
41 << tileWidth << "x" << tileHeight << "\n";
42 }
43
44 void IsometricRenderer::SetCamera(float camX, float camY, float zoom)
45 {
48 m_zoom = zoom;
49 }
50
51 void IsometricRenderer::SetViewport(int screenWidth, int screenHeight)
52 {
53 m_screenWidth = screenWidth;
54 m_screenHeight = screenHeight;
55 }
56
57 Vector IsometricRenderer::WorldToScreen(float worldX, float worldY) const
58 {
59 // Isometric projection: world grid coordinates to screen coordinates
60 float isoX = (worldX - worldY) * (m_tileWidth / 2.0f);
61 float isoY = (worldX + worldY) * (m_tileHeight / 2.0f);
62
63 // Apply camera transform and center in viewport
64 float screenX = (isoX - m_cameraX) * m_zoom + m_screenWidth / 2.0f;
65 float screenY = (isoY - m_cameraY) * m_zoom + m_screenHeight / 2.0f;
66
67 // Add isometric Y offset to ensure tiles with negative world coordinates are visible
69
72 screen.y = screenY;
73 screen.z = 0.0f;
74 return screen;
75 }
76
78 {
79 // Inverse isometric projection
81
82 float isoX = (screenX - m_screenWidth / 2.0f) / m_zoom + m_cameraX;
83 float isoY = (screenY - m_screenHeight / 2.0f) / m_zoom + m_cameraY;
84
85 Vector world;
86 float halfTileW = m_tileWidth / 2.0f;
87 float halfTileH = m_tileHeight / 2.0f;
88
89 world.x = (isoX / halfTileW + isoY / halfTileH) / 2.0f;
90 world.y = (isoY / halfTileH - isoX / halfTileW) / 2.0f;
91 world.z = 0.0f;
92 return world;
93 }
94
95 bool IsometricRenderer::IsTileVisible(int worldX, int worldY) const
96 {
97 // Convert tile position to screen coordinates
98 Vector screenPos = WorldToScreen(static_cast<float>(worldX),
99 static_cast<float>(worldY));
100
101 // Check if tile is within screen bounds with margin
103
104 bool visible = (screenPos.x >= -totalMargin && screenPos.x <= m_screenWidth + totalMargin &&
106
107 return visible;
108 }
109
110 void IsometricRenderer::GetVisibleTileRange(int& minX, int& minY, int& maxX, int& maxY) const
111 {
112 // Get screen corners in world coordinates
113 Vector topLeft = ScreenToWorld(0.0f, 0.0f);
114 Vector topRight = ScreenToWorld(static_cast<float>(m_screenWidth), 0.0f);
115 Vector bottomLeft = ScreenToWorld(0.0f, static_cast<float>(m_screenHeight));
116 Vector bottomRight = ScreenToWorld(static_cast<float>(m_screenWidth), static_cast<float>(m_screenHeight));
117
118 // Find bounding box in world coordinates
119 float worldMinX = std::min({topLeft.x, topRight.x, bottomLeft.x, bottomRight.x});
120 float worldMaxX = std::max({topLeft.x, topRight.x, bottomLeft.x, bottomRight.x});
121 float worldMinY = std::min({topLeft.y, topRight.y, bottomLeft.y, bottomRight.y});
122 float worldMaxY = std::max({topLeft.y, topRight.y, bottomLeft.y, bottomRight.y});
123
124 // Add padding for tile size
125 minX = static_cast<int>(std::floor(worldMinX)) - VISIBLE_TILE_PADDING;
126 minY = static_cast<int>(std::floor(worldMinY)) - VISIBLE_TILE_PADDING;
127 maxX = static_cast<int>(std::ceil(worldMaxX)) + VISIBLE_TILE_PADDING;
128 maxY = static_cast<int>(std::ceil(worldMaxY)) + VISIBLE_TILE_PADDING;
129 }
130
132 {
133 // Calculate total culling margin for tall tiles
135 return padding + CULL_MARGIN;
136 }
137
138} // namespace Rendering
139} // namespace Olympe
ComponentTypeID GetComponentTypeID_Static()
Definition ECS_Entity.h:56
static SDL_Renderer * renderer
Vector WorldToScreen(float worldX, float worldY) const
void SetViewport(int screenWidth, int screenHeight)
Vector ScreenToWorld(float screenX, float screenY) const
static constexpr float TALL_TILE_MULTIPLIER
void Initialize(SDL_Renderer *renderer, int tileWidth, int tileHeight)
void GetVisibleTileRange(int &minX, int &minY, int &maxX, int &maxY) const
bool IsTileVisible(int worldX, int worldY) const
static constexpr float ISOMETRIC_OFFSET_Y
void SetCamera(float camX, float camY, float zoom)
float z
Definition vector.h:27
float y
Definition vector.h:27
float x
Definition vector.h:27
#define SYSTEM_LOG