Merge 'remotes/trunk'
[0ad.git] / source / graphics / Patch.cpp
blob92e0b307bb76fa70ccff04645eaa895d47c2c0a3
1 /* Copyright (C) 2010 Wildfire Games.
2 * This file is part of 0 A.D.
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.
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.
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/>.
19 * A patch of terrain holding NxN MiniPatch tiles
22 #include "precompiled.h"
24 #include "Patch.h"
25 #include "Terrain.h"
28 ///////////////////////////////////////////////////////////////////////////////
29 // CPatch constructor
30 CPatch::CPatch()
31 : m_Parent(0)
35 ///////////////////////////////////////////////////////////////////////////////
36 // CPatch destructor
37 CPatch::~CPatch()
42 ///////////////////////////////////////////////////////////////////////////////
43 // Initialize: setup patch data
44 void CPatch::Initialize(CTerrain* parent,ssize_t x,ssize_t z)
46 delete m_RenderData;
47 m_RenderData=0;
49 m_Parent=parent;
50 m_X=x;
51 m_Z=z;
53 InvalidateBounds();
56 ///////////////////////////////////////////////////////////////////////////////
57 // CalcBounds: calculating the bounds of this patch
58 void CPatch::CalcBounds()
60 m_WorldBounds.SetEmpty();
62 for (ssize_t j=0;j<PATCH_SIZE+1;j++)
64 for (ssize_t i=0;i<PATCH_SIZE+1;i++)
66 CVector3D pos;
67 m_Parent->CalcPosition(m_X*PATCH_SIZE+i,m_Z*PATCH_SIZE+j,pos);
68 m_WorldBounds+=pos;
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);
78 int CPatch::GetSideFlags()
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;