1 /* Copyright (C) 2010 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 /*
19 * A patch of terrain holding NxN MiniPatch tiles
20 */
21
22 #include "precompiled.h"
23
24 #include "Patch.h"
25 #include "Terrain.h"
26
27
28 ///////////////////////////////////////////////////////////////////////////////
29 // CPatch constructor
CPatch()30 CPatch::CPatch()
31 : m_Parent(0)
32 {
33 }
34
35 ///////////////////////////////////////////////////////////////////////////////
36 // CPatch destructor
~CPatch()37 CPatch::~CPatch()
38 {
39
40 }
41
42 ///////////////////////////////////////////////////////////////////////////////
43 // Initialize: setup patch data
Initialize(CTerrain * parent,ssize_t x,ssize_t z)44 void CPatch::Initialize(CTerrain* parent,ssize_t x,ssize_t z)
45 {
46 delete m_RenderData;
47 m_RenderData=0;
48
49 m_Parent=parent;
50 m_X=x;
51 m_Z=z;
52
53 InvalidateBounds();
54 }
55
56 ///////////////////////////////////////////////////////////////////////////////
57 // CalcBounds: calculating the bounds of this patch
CalcBounds()58 void CPatch::CalcBounds()
59 {
60 m_WorldBounds.SetEmpty();
61
62 for (ssize_t j=0;j<PATCH_SIZE+1;j++)
63 {
64 for (ssize_t i=0;i<PATCH_SIZE+1;i++)
65 {
66 CVector3D pos;
67 m_Parent->CalcPosition(m_X*PATCH_SIZE+i,m_Z*PATCH_SIZE+j,pos);
68 m_WorldBounds+=pos;
69 }
70 }
71
72 // If this a side patch, the sides go down to height 0, so add them
73 // into the bounds
74 if (GetSideFlags())
75 m_WorldBounds[0].Y = std::min(m_WorldBounds[0].Y, 0.f);
76 }
77
GetSideFlags()78 int CPatch::GetSideFlags()
79 {
80 int flags = 0;
81 if (m_X == 0)
82 flags |= CPATCH_SIDE_NEGX;
83 if (m_Z == 0)
84 flags |= CPATCH_SIDE_NEGZ;
85 if (m_X == m_Parent->GetPatchesPerSide()-1)
86 flags |= CPATCH_SIDE_POSX;
87 if (m_Z == m_Parent->GetPatchesPerSide()-1)
88 flags |= CPATCH_SIDE_POSZ;
89 return flags;
90 }
91