@@ -54,9 +54,8 @@ public class GridRenderer
54
54
StatTimer updateTilesTimer1 = StatTimer .get ("GridRenderer.updateTiles(1)" , 5 , 500 );
55
55
StatTimer updateTilesTimer2 = StatTimer .get ("GridRenderer.updateTiles(2)" , 5 , 500 );
56
56
private int glErrors = 0 ;
57
-
58
- private int gridSize ; // 5 = 2560px.
59
- private double srcSize ;
57
+ private int gridSizeHeight , gridSizeWidth ; // Amount of 512x512 tiles that should be loaded in each direction.
58
+ private double srcSizeHeight , srcSizeWidth ; // Total pixel height/width of the loaded grid tiles
60
59
private Rectangle2D .Double viewPort = null ;
61
60
private Rectangle2D .Double screenBounds = null ;
62
61
private int lastHeight = -1 ;
@@ -74,14 +73,13 @@ public class GridRenderer
74
73
private FloatBuffer winPosBuf ;
75
74
private FloatBuffer objPosBuf ;
76
75
77
- public GridRenderer (int gridSize )
76
+ public GridRenderer ()
78
77
{
79
78
viewportBuf = BufferUtils .createIntBuffer (16 );
80
79
modelMatrixBuf = BufferUtils .createFloatBuffer (16 );
81
80
projMatrixBuf = BufferUtils .createFloatBuffer (16 );
82
81
winPosBuf = BufferUtils .createFloatBuffer (16 );
83
82
objPosBuf = BufferUtils .createFloatBuffer (16 );
84
- setGridSize (gridSize );
85
83
}
86
84
87
85
public static void addDebugMessage (String key , String message )
@@ -122,8 +120,8 @@ public void setViewPort(Rectangle2D.Double viewPort)
122
120
123
121
private void populateGrid (Tile centerTile )
124
122
{
125
- final int endRow = (gridSize - 1 ) / 2 ;
126
- final int endCol = (gridSize - 1 ) / 2 ;
123
+ final int endRow = (gridSizeHeight - 1 ) / 2 ;
124
+ final int endCol = (gridSizeWidth - 1 ) / 2 ;
127
125
final int startRow = -endRow ;
128
126
final int startCol = -endCol ;
129
127
@@ -155,15 +153,20 @@ public boolean hasUnloadedTile()
155
153
return hasUnloadedTile (false );
156
154
}
157
155
158
- public int getGridSize ( )
156
+ private void setGridSizes ( int gridSizeHeight , int gridSizeWidth )
159
157
{
160
- return gridSize ;
161
- }
158
+ if (this .gridSizeHeight == gridSizeHeight && this .gridSizeWidth == gridSizeWidth ) return ;
159
+ if (gridSizeHeight % 2 == 0 ) gridSizeHeight ++;
160
+ if (gridSizeWidth % 2 == 0 ) gridSizeWidth ++;
162
161
163
- public void setGridSize (int gridSize )
164
- {
165
- this .gridSize = gridSize ; // Must be an odd number so as to have a center tile.
166
- srcSize = gridSize * Tile .TILESIZE ;
162
+ // Both must be odd so that a center tile exists
163
+ this .gridSizeHeight = gridSizeHeight ;
164
+ this .gridSizeWidth = gridSizeWidth ;
165
+
166
+ this .srcSizeHeight = gridSizeHeight * Tile .TILESIZE ;
167
+ this .srcSizeWidth = gridSizeWidth * Tile .TILESIZE ;
168
+
169
+ clear ();
167
170
}
168
171
169
172
public boolean hasUnloadedTile (boolean preview )
@@ -235,6 +238,7 @@ public void updateTiles(MapType mapType, int zoom, boolean highQuality, int widt
235
238
236
239
// Update screen dimensions
237
240
updateBounds (width , height );
241
+ updateGridSize ();
238
242
239
243
// Get center tile, check if present and current
240
244
Tile centerTile = grid .get (centerPos );
@@ -249,9 +253,9 @@ public void updateTiles(MapType mapType, int zoom, boolean highQuality, int widt
249
253
// Derive offsets for centering the map
250
254
Point2D blockPixelOffset = centerTile .blockPixelOffsetInTile (centerBlockX , centerBlockZ );
251
255
final double blockSizeOffset = Math .pow (2 , zoom ) / 2 ;
252
- final int magic = (gridSize == 5 ? 2 : 1 ) * Tile .TILESIZE ; // TODO: Understand why "2" as it relates to gridSize. If gridSize is 3, this has to be "1".
253
256
254
- double displayOffsetX = xOffset + magic - ((srcSize - lastWidth ) / 2 );
257
+ int extraOffsetX = (gridSizeWidth / 2 ) * Tile .TILESIZE ;
258
+ double displayOffsetX = xOffset + extraOffsetX - ((srcSizeWidth - lastWidth ) / 2 );
255
259
if (centerBlockX < 0 )
256
260
{
257
261
displayOffsetX -= blockSizeOffset ;
@@ -260,7 +264,9 @@ public void updateTiles(MapType mapType, int zoom, boolean highQuality, int widt
260
264
{
261
265
displayOffsetX += blockSizeOffset ;
262
266
}
263
- double displayOffsetY = yOffset + magic - ((srcSize - lastHeight ) / 2 );
267
+
268
+ int extraOffsetY = (gridSizeHeight / 2 ) * Tile .TILESIZE ;
269
+ double displayOffsetY = yOffset + extraOffsetY - ((srcSizeHeight - lastHeight ) / 2 );
264
270
if (centerBlockZ < 0 )
265
271
{
266
272
displayOffsetY -= blockSizeOffset ;
@@ -583,6 +589,13 @@ private void updateBounds(int width, int height)
583
589
}
584
590
}
585
591
592
+ private void updateGridSize () {
593
+ int newGridSizeHeight = (int ) Math .ceil (screenBounds .height / Tile .TILESIZE + 0.5 );
594
+ int newGridSizeWidth = (int ) Math .ceil (screenBounds .width / Tile .TILESIZE + 0.5 );
595
+
596
+ setGridSizes (newGridSizeHeight , newGridSizeWidth );
597
+ }
598
+
586
599
private Tile findNeighbor (Tile tile , TilePos pos )
587
600
{
588
601
if (pos .deltaX == 0 && pos .deltaZ == 0 )
@@ -674,11 +687,6 @@ public boolean setZoom(int zoom)
674
687
return center (mapType , centerBlockX , centerBlockZ , zoom );
675
688
}
676
689
677
- public int getRenderSize ()
678
- {
679
- return this .gridSize * Tile .TILESIZE ;
680
- }
681
-
682
690
public void clear ()
683
691
{
684
692
grid .clear ();
0 commit comments