Skip to content

Commit c47b01a

Browse files
committed
perf(heightmap): Update terrain tiles only if the terrain draw origin has really changed (TheSuperHackers#2677)
1 parent 534167e commit c47b01a

3 files changed

Lines changed: 61 additions & 42 deletions

File tree

Core/GameEngineDevice/Include/W3DDevice/GameClient/WorldHeightMap.h

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ class WorldHeightMap : public RefCountClass,
108108
#define NO_EVAL_TILING_MODES
109109

110110
public:
111+
112+
struct DrawArea
113+
{
114+
Int originX;
115+
Int originY;
116+
Int sizeX;
117+
Int sizeY;
118+
};
119+
111120
#ifdef EVAL_TILING_MODES
112121
enum {TILE_4x4, TILE_6x6, TILE_8x8} m_tileMode;
113122
#endif
@@ -260,6 +269,8 @@ class WorldHeightMap : public RefCountClass,
260269

261270
void getUVForBlend(Int edgeClass, Region2D *range);
262271

272+
DrawArea createDrawArea(Int xOrg, Int yOrg);
273+
Bool setDrawArea(const DrawArea& drawArea);
263274
Bool setDrawOrg(Int xOrg, Int yOrg);
264275

265276
static void freeListOfMapObjects();

Core/GameEngineDevice/Source/W3DDevice/GameClient/HeightMap.cpp

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1183,15 +1183,13 @@ void HeightMapRenderObjClass::oversizeTerrain(Int tilesToOversize)
11831183
}
11841184
Int dx = width-m_map->getDrawWidth();
11851185
Int dy = height-m_map->getDrawHeight();
1186-
m_map->setDrawWidth(width);
1186+
m_map->setDrawWidth(width);
11871187
m_map->setDrawHeight(height);
11881188
dx /= 2;
11891189
dy /= 2;
11901190
Int newOrgX = m_map->getDrawOrgX()-dx;
1191-
Int newOrgy = m_map->getDrawOrgY()-dy;
1192-
if (newOrgX<0) newOrgX=0;
1193-
if (newOrgy<0) newOrgy=0;
1194-
m_map->setDrawOrg(newOrgX,newOrgy);
1191+
Int newOrgY = m_map->getDrawOrgY()-dy;
1192+
m_map->setDrawOrg(newOrgX,newOrgY);
11951193
m_originX = 0;
11961194
m_originY = 0;
11971195
if (m_shroud)
@@ -1742,26 +1740,29 @@ void HeightMapRenderObjClass::updateCenter(CameraClass *camera , RefRenderObjLis
17421740
newOrgX = (visMaxX+visMinX)/2 - m_x/2.0;
17431741
newOrgY = (visMaxY+visMinY)/2 - m_y/2.0;
17441742

1743+
WorldHeightMap::DrawArea newDrawArea = m_map->createDrawArea(newOrgX, newOrgY);
1744+
17451745
m_updating = true;
17461746
if (m_needFullUpdate)
17471747
{
17481748
m_needFullUpdate = false;
1749-
m_map->setDrawOrg(newOrgX, newOrgY);
1749+
m_map->setDrawArea(newDrawArea);
17501750
updateBlock(0, 0, m_x-1, m_y-1, m_map, pLightsIterator);
17511751
m_updating = false;
17521752
return;
17531753
}
17541754
else
17551755
{
17561756
constexpr const Int cellOffset = 1;
1757-
Int deltaX = newOrgX - m_map->getDrawOrgX();
1758-
Int deltaY = newOrgY - m_map->getDrawOrgY();
1757+
const Int deltaX = newDrawArea.originX - m_map->getDrawOrgX();
1758+
const Int deltaY = newDrawArea.originY - m_map->getDrawOrgY();
17591759

17601760
if (IABS(deltaX) > m_x/2 || IABS(deltaY)>m_x/2) {
1761-
m_map->setDrawOrg(newOrgX, newOrgY);
1762-
m_originY = 0;
1763-
m_originX = 0;
1764-
updateBlock(0, 0, m_x-1, m_y-1, m_map, pLightsIterator);
1761+
if (m_map->setDrawArea(newDrawArea)) {
1762+
m_originY = 0;
1763+
m_originX = 0;
1764+
updateBlock(0, 0, m_x-1, m_y-1, m_map, pLightsIterator);
1765+
}
17651766
m_updating = false;
17661767
return;
17671768
}
@@ -1770,7 +1771,6 @@ void HeightMapRenderObjClass::updateCenter(CameraClass *camera , RefRenderObjLis
17701771
if (m_map->setDrawOrg(m_map->getDrawOrgX(), newOrgY)) {
17711772
Int minY = 0;
17721773
Int maxY = 0;
1773-
deltaY -= newOrgY - m_map->getDrawOrgY();
17741774
m_originY += deltaY;
17751775
if (m_originY >= m_y-1) m_originY -= m_y-1;
17761776
if (deltaY<0) {
@@ -1806,7 +1806,6 @@ void HeightMapRenderObjClass::updateCenter(CameraClass *camera , RefRenderObjLis
18061806
if (m_map->setDrawOrg(newOrgX, m_map->getDrawOrgY())) {
18071807
Int minX = 0;
18081808
Int maxX = 0;
1809-
deltaX -= newOrgX - m_map->getDrawOrgX();
18101809
m_originX += deltaX;
18111810
if (m_originX >= m_x-1) m_originX -= m_x-1;
18121811
if (deltaX<0) {

Core/GameEngineDevice/Source/W3DDevice/GameClient/WorldHeightMap.cpp

Lines changed: 37 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -2201,43 +2201,52 @@ TerrainTextureClass *WorldHeightMap::getFlatTexture(Int xCell, Int yCell, Int ce
22012201
}
22022202

22032203

2204-
Bool WorldHeightMap::setDrawOrg(Int xOrg, Int yOrg)
2204+
WorldHeightMap::DrawArea WorldHeightMap::createDrawArea(Int xOrg, Int yOrg)
22052205
{
2206-
Int newX, newY;
2207-
Int newWidth, newHeight;
2208-
newX = xOrg;
2209-
newY = yOrg;
2210-
newWidth = m_drawWidthX;
2211-
newHeight = m_drawHeightY;
2206+
DrawArea area;
2207+
area.sizeX = m_drawWidthX;
2208+
area.sizeY = m_drawHeightY;
2209+
22122210
if (TheGlobalData && TheGlobalData->m_stretchTerrain) {
2213-
newWidth=STRETCH_DRAW_WIDTH;
2214-
newHeight=STRETCH_DRAW_HEIGHT;
2211+
area.sizeX = STRETCH_DRAW_WIDTH;
2212+
area.sizeY = STRETCH_DRAW_HEIGHT;
22152213
}
22162214
if (TheGlobalData && TheGlobalData->m_drawEntireTerrain) {
2217-
newWidth=m_width;
2218-
newHeight=m_height;
2219-
}
2220-
if (newWidth > m_width) newWidth = m_width;
2221-
if (newHeight > m_height) newHeight = m_height;
2222-
if (newX > m_width - newWidth) newX = m_width-newWidth;
2223-
if (newX<0) newX=0;
2224-
if (newY > m_height - newHeight) newY = m_height - newHeight;
2225-
if (newY<0) newY=0;
2226-
Bool anythingDifferent = (m_drawOriginX!=newX) ||
2227-
(m_drawOriginY!=newY) ||
2228-
(m_drawWidthX!=newWidth) ||
2229-
(m_drawHeightY!=newHeight) ;
2215+
area.sizeX = m_width;
2216+
area.sizeY = m_height;
2217+
}
2218+
area.sizeX = std::min(area.sizeX, m_width);
2219+
area.sizeY = std::min(area.sizeY, m_height);
2220+
area.originX = clamp(0, xOrg, m_width - area.sizeX);
2221+
area.originY = clamp(0, yOrg, m_height - area.sizeY);
2222+
2223+
return area;
2224+
}
2225+
2226+
Bool WorldHeightMap::setDrawArea(const DrawArea& area)
2227+
{
2228+
Bool anythingDifferent =
2229+
m_drawOriginX != area.originX ||
2230+
m_drawOriginY != area.originY ||
2231+
m_drawWidthX != area.sizeX ||
2232+
m_drawHeightY != area.sizeY;
22302233

22312234
if (anythingDifferent) {
2232-
m_drawOriginX=newX;
2233-
m_drawOriginY=newY;
2234-
m_drawWidthX=newWidth;
2235-
m_drawHeightY=newHeight;
2236-
return(true);
2235+
m_drawOriginX = area.originX;
2236+
m_drawOriginY = area.originY;
2237+
m_drawWidthX = area.sizeX;
2238+
m_drawHeightY = area.sizeY;
2239+
return true;
22372240
}
2238-
return(false);
2241+
return false;
2242+
}
2243+
2244+
Bool WorldHeightMap::setDrawOrg(Int xOrg, Int yOrg)
2245+
{
2246+
return setDrawArea(createDrawArea(xOrg, yOrg));
22392247
}
22402248

2249+
22412250
/** Gets global texture class. */
22422251
Int WorldHeightMap::getTextureClass(Int xIndex, Int yIndex, Bool baseClass)
22432252
{

0 commit comments

Comments
 (0)