Line data Source code
1 : /* Copyright (C) 2025 Wildfire Games.
2 : * This file is part of 0 A.D.
3 : *
4 : * 0 A.D. is free software: you can redistribute it and/or modify
5 : * it under the terms of the GNU General Public License as published by
6 : * the Free Software Foundation, either version 2 of the License, or
7 : * (at your option) any later version.
8 : *
9 : * 0 A.D. is distributed in the hope that it will be useful,
10 : * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 : * GNU General Public License for more details.
13 : *
14 : * You should have received a copy of the GNU General Public License
15 : * along with 0 A.D. If not, see <http://www.gnu.org/licenses/>.
16 : */
17 :
18 : #include "precompiled.h"
19 :
20 : #include "Brushes.h"
21 :
22 : #include "graphics/Color.h"
23 : #include "graphics/Terrain.h"
24 : #include "lib/debug.h"
25 : #include "ps/Game.h"
26 : #include "renderer/TerrainOverlay.h"
27 : #include "simulation2/Simulation2.h"
28 :
29 : #include <algorithm>
30 : #include <cstddef>
31 :
32 : using namespace AtlasMessage;
33 :
34 : class BrushTerrainOverlay : public TerrainOverlay
35 : {
36 : public:
37 0 : BrushTerrainOverlay(const Brush* brush)
38 0 : : TerrainOverlay(g_Game->GetSimulation2()->GetSimContext(), 300), m_Brush(brush)
39 : {
40 0 : }
41 :
42 0 : void GetTileExtents(
43 : ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
44 : ssize_t& max_i_inclusive, ssize_t& max_j_inclusive)
45 : {
46 0 : m_Brush->GetBottomLeft(min_i_inclusive, min_j_inclusive);
47 0 : m_Brush->GetTopRight(max_i_inclusive, max_j_inclusive);
48 :
49 : // But since brushes deal with vertices instead of tiles,
50 : // we don't want to include the top/right row
51 0 : --max_i_inclusive;
52 0 : --max_j_inclusive;
53 0 : }
54 :
55 0 : void ProcessTile(
56 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
57 : ssize_t i, ssize_t j)
58 : {
59 0 : ssize_t i0, j0;
60 0 : m_Brush->GetBottomLeft(i0, j0);
61 : // Color this tile based on the average of the surrounding vertices
62 0 : float avg = (
63 0 : m_Brush->Get(i-i0, j-j0) + m_Brush->Get(i-i0+1, j-j0) +
64 0 : m_Brush->Get(i-i0, j-j0+1) + m_Brush->Get(i-i0+1, j-j0+1)
65 0 : ) / 4.f;
66 0 : RenderTile(deviceCommandContext, CColor(0, 1, 0, avg*0.8f), false);
67 0 : if (avg > 0.1f)
68 0 : RenderTileOutline(deviceCommandContext, CColor(1, 1, 1, std::min(0.4f, avg-0.1f)), true);
69 0 : }
70 :
71 : const AtlasMessage::Brush* m_Brush;
72 : };
73 :
74 0 : Brush::Brush()
75 0 : : m_W(0), m_H(0), m_TerrainOverlay(NULL)
76 : {
77 0 : }
78 :
79 1 : Brush::~Brush()
80 : {
81 1 : delete m_TerrainOverlay;
82 1 : }
83 :
84 0 : void Brush::SetData(ssize_t w, ssize_t h, const std::vector<float>& data)
85 : {
86 0 : m_W = w;
87 0 : m_H = h;
88 :
89 0 : m_Data = data;
90 :
91 0 : ENSURE(data.size() == (size_t)(w*h));
92 0 : }
93 :
94 0 : void Brush::GetCentre(ssize_t& x, ssize_t& y) const
95 : {
96 0 : CVector3D c = m_Centre;
97 0 : if (m_W % 2) c.X += TERRAIN_TILE_SIZE/2.f;
98 0 : if (m_H % 2) c.Z += TERRAIN_TILE_SIZE/2.f;
99 0 : ssize_t cx, cy;
100 0 : CTerrain::CalcFromPosition(c, cx, cy);
101 :
102 0 : x = cx;
103 0 : y = cy;
104 0 : }
105 :
106 0 : void Brush::GetBottomLeft(ssize_t& x, ssize_t& y) const
107 : {
108 0 : GetCentre(x, y);
109 0 : x -= (m_W-1)/2;
110 0 : y -= (m_H-1)/2;
111 0 : }
112 :
113 0 : void Brush::GetTopRight(ssize_t& x, ssize_t& y) const
114 : {
115 0 : GetBottomLeft(x, y);
116 0 : x += m_W-1;
117 0 : y += m_H-1;
118 0 : }
119 :
120 0 : void Brush::SetRenderEnabled(bool enabled)
121 : {
122 0 : if (enabled && !m_TerrainOverlay)
123 : {
124 0 : m_TerrainOverlay = new BrushTerrainOverlay(this);
125 : }
126 0 : else if (!enabled && m_TerrainOverlay)
127 : {
128 0 : delete m_TerrainOverlay;
129 0 : m_TerrainOverlay = NULL;
130 : }
131 0 : }
132 :
133 : Brush AtlasMessage::g_CurrentBrush;
|