Zappy GUI
C++ graphic client: renderer, camera, network
Loading...
Searching...
No Matches
GridRenderer.cpp
Go to the documentation of this file.
1#include "GridRenderer.hpp"
2
3void GridRenderer::drawTiles(int width, int height, float tileSize)
4{
5 float offsetX = (width * tileSize) / 2.0f;
6 float offsetZ = (height * tileSize) / 2.0f;
7
8 for (int x = 0; x < width; x++) {
9 for (int z = 0; z < height; z++) {
10 float cx = x * tileSize - offsetX + tileSize / 2.0f;
11 float cz = z * tileSize - offsetZ + tileSize / 2.0f;
12 Color c = ((x + z) % 2 == 0) ? TILE_COLOR : TILE_COLOR_ALT;
13 DrawCube({cx, -0.01f, cz}, tileSize, 0.001f, tileSize, c);
14 }
15 }
16}
17
18void GridRenderer::drawGrid(int width, int height, float tileSize, Color color)
19{
20 float offsetX = (width * tileSize) / 2.0f;
21 float offsetZ = (height * tileSize) / 2.0f;
22
23 // Vertical lines (along Z axis)
24 for (int x = 0; x <= width; x++) {
25 DrawLine3D({x * tileSize - offsetX, 0.0f, -offsetZ},
26 {x * tileSize - offsetX, 0.0f, height * tileSize - offsetZ}, color);
27 }
28
29 // Horizontal lines (along X axis)
30 for (int z = 0; z <= height; z++) {
31 DrawLine3D({-offsetX, 0.0f, z * tileSize - offsetZ},
32 {width * tileSize - offsetX, 0.0f, z * tileSize - offsetZ}, color);
33 }
34}
35
36void GridRenderer::drawTileHighlight(int tileX, int tileY, int worldWidth, int worldHeight,
37 float tileSize, Color color, float lineThickness)
38{
39 float offsetX = (worldWidth * tileSize) / 2.0f;
40 float offsetZ = (worldHeight * tileSize) / 2.0f;
41
42 // Slightly above ground to avoid z-fighting
43 float y = 0.01f;
44
45 // Calculate tile corner positions
46 float x0 = tileX * tileSize - offsetX;
47 float x1 = (tileX + 1) * tileSize - offsetX;
48 float z0 = tileY * tileSize - offsetZ;
49 float z1 = (tileY + 1) * tileSize - offsetZ;
50
51 float thickness = lineThickness * 0.01f; // Convert pixel thickness to world units
52
53 // Draw 4 rectangles forming thick outline
54 DrawCube({(x0 + x1) / 2.0f, y, z0}, x1 - x0, 0.001f, thickness, color);
55 DrawCube({(x0 + x1) / 2.0f, y, z1}, x1 - x0, 0.001f, thickness, color);
56 DrawCube({x0, y, (z0 + z1) / 2.0f}, thickness, 0.001f, z1 - z0, color);
57 DrawCube({x1, y, (z0 + z1) / 2.0f}, thickness, 0.001f, z1 - z0, color);
58}
static constexpr Color TILE_COLOR
static void drawGrid(int width, int height, float tileSize, Color color=GRAY)
Draws a custom grid centered at (0, 0, 0).
static void drawTiles(int width, int height, float tileSize)
Draws filled tile quads for the entire grid in a checkerboard pattern.
static constexpr Color TILE_COLOR_ALT
static void drawTileHighlight(int tileX, int tileY, int worldWidth, int worldHeight, float tileSize, Color color, float lineThickness=1.0f)
Draws a highlight outline around a specific tile.