Line data Source code
1 : /* Copyright (C) 2022 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 "maths/MathUtil.h"
25 : #include "ps/Game.h"
26 : #include "ps/World.h"
27 : #include "renderer/TerrainOverlay.h"
28 : #include "simulation2/Simulation2.h"
29 : #include "simulation2/system/SimContext.h"
30 :
31 : #include <algorithm>
32 :
33 : using namespace AtlasMessage;
34 :
35 0 : class BrushTerrainOverlay : public TerrainOverlay
36 : {
37 : public:
38 0 : BrushTerrainOverlay(const Brush* brush)
39 0 : : TerrainOverlay(g_Game->GetSimulation2()->GetSimContext(), 300), m_Brush(brush)
40 : {
41 0 : }
42 :
43 0 : void GetTileExtents(
44 : ssize_t& min_i_inclusive, ssize_t& min_j_inclusive,
45 : ssize_t& max_i_inclusive, ssize_t& max_j_inclusive)
46 : {
47 0 : m_Brush->GetBottomLeft(min_i_inclusive, min_j_inclusive);
48 0 : m_Brush->GetTopRight(max_i_inclusive, max_j_inclusive);
49 :
50 : // But since brushes deal with vertices instead of tiles,
51 : // we don't want to include the top/right row
52 0 : --max_i_inclusive;
53 0 : --max_j_inclusive;
54 0 : }
55 :
56 0 : void ProcessTile(
57 : Renderer::Backend::IDeviceCommandContext* deviceCommandContext,
58 : ssize_t i, ssize_t j)
59 : {
60 : ssize_t i0, j0;
61 0 : m_Brush->GetBottomLeft(i0, j0);
62 : // Color this tile based on the average of the surrounding vertices
63 : float avg = (
64 0 : m_Brush->Get(i-i0, j-j0) + m_Brush->Get(i-i0+1, j-j0) +
65 0 : m_Brush->Get(i-i0, j-j0+1) + m_Brush->Get(i-i0+1, j-j0+1)
66 0 : ) / 4.f;
67 0 : RenderTile(deviceCommandContext, CColor(0, 1, 0, avg*0.8f), false);
68 0 : if (avg > 0.1f)
69 0 : RenderTileOutline(deviceCommandContext, CColor(1, 1, 1, std::min(0.4f, avg-0.1f)), true);
70 0 : }
71 :
72 : const AtlasMessage::Brush* m_Brush;
73 : };
74 :
75 1 : Brush::Brush()
76 1 : : m_W(0), m_H(0), m_TerrainOverlay(NULL)
77 : {
78 1 : }
79 :
80 2 : Brush::~Brush()
81 : {
82 1 : delete m_TerrainOverlay;
83 1 : }
84 :
85 0 : void Brush::SetData(ssize_t w, ssize_t h, const std::vector<float>& data)
86 : {
87 0 : m_W = w;
88 0 : m_H = h;
89 :
90 0 : m_Data = data;
91 :
92 0 : ENSURE(data.size() == (size_t)(w*h));
93 0 : }
94 :
95 0 : void Brush::GetCentre(ssize_t& x, ssize_t& y) const
96 : {
97 0 : CVector3D c = m_Centre;
98 0 : if (m_W % 2) c.X += TERRAIN_TILE_SIZE/2.f;
99 0 : if (m_H % 2) c.Z += TERRAIN_TILE_SIZE/2.f;
100 : ssize_t cx, cy;
101 0 : CTerrain::CalcFromPosition(c, cx, cy);
102 :
103 0 : x = cx;
104 0 : y = cy;
105 0 : }
106 :
107 0 : void Brush::GetBottomLeft(ssize_t& x, ssize_t& y) const
108 : {
109 0 : GetCentre(x, y);
110 0 : x -= (m_W-1)/2;
111 0 : y -= (m_H-1)/2;
112 0 : }
113 :
114 0 : void Brush::GetTopRight(ssize_t& x, ssize_t& y) const
115 : {
116 0 : GetBottomLeft(x, y);
117 0 : x += m_W-1;
118 0 : y += m_H-1;
119 0 : }
120 :
121 0 : void Brush::SetRenderEnabled(bool enabled)
122 : {
123 0 : if (enabled && !m_TerrainOverlay)
124 : {
125 0 : m_TerrainOverlay = new BrushTerrainOverlay(this);
126 : }
127 0 : else if (!enabled && m_TerrainOverlay)
128 : {
129 0 : delete m_TerrainOverlay;
130 0 : m_TerrainOverlay = NULL;
131 : }
132 0 : }
133 :
134 2 : Brush AtlasMessage::g_CurrentBrush;
|