Skip to content

Commit

Permalink
Improve map grid size handling, fix fullscreen map cutting off (#35)
Browse files Browse the repository at this point in the history
* make grid size dynamic

* refactor: clean grid size leftovers (breaking change)

* feat: support different grid width and height

* update changelog

* remove todo info comment
  • Loading branch information
leumasme authored Sep 10, 2024
1 parent 6c82a24 commit f4631f5
Show file tree
Hide file tree
Showing 4 changed files with 33 additions and 27 deletions.
2 changes: 1 addition & 1 deletion doc/changelog.html
Original file line number Diff line number Diff line change
Expand Up @@ -23,5 +23,5 @@ <h1>JourneyMap ${version} for Minecraft ${mcversion}</h1>

<p>New in ${version}</p>
<ul>
<li>Fixed: Switched to an explicit lambda to fix remapper issues (ah-OOG-ah)</li>
<li>Fixed: Map no longer cuts off on larger screens</li>
</ul>
52 changes: 30 additions & 22 deletions src/main/java/journeymap/client/render/map/GridRenderer.java
Original file line number Diff line number Diff line change
Expand Up @@ -54,9 +54,8 @@ public class GridRenderer
StatTimer updateTilesTimer1 = StatTimer.get("GridRenderer.updateTiles(1)", 5, 500);
StatTimer updateTilesTimer2 = StatTimer.get("GridRenderer.updateTiles(2)", 5, 500);
private int glErrors = 0;

private int gridSize; // 5 = 2560px.
private double srcSize;
private int gridSizeHeight, gridSizeWidth; // Amount of 512x512 tiles that should be loaded in each direction.
private double srcSizeHeight, srcSizeWidth; // Total pixel height/width of the loaded grid tiles
private Rectangle2D.Double viewPort = null;
private Rectangle2D.Double screenBounds = null;
private int lastHeight = -1;
Expand All @@ -74,14 +73,13 @@ public class GridRenderer
private FloatBuffer winPosBuf;
private FloatBuffer objPosBuf;

public GridRenderer(int gridSize)
public GridRenderer()
{
viewportBuf = BufferUtils.createIntBuffer(16);
modelMatrixBuf = BufferUtils.createFloatBuffer(16);
projMatrixBuf = BufferUtils.createFloatBuffer(16);
winPosBuf = BufferUtils.createFloatBuffer(16);
objPosBuf = BufferUtils.createFloatBuffer(16);
setGridSize(gridSize);
}

public static void addDebugMessage(String key, String message)
Expand Down Expand Up @@ -122,8 +120,8 @@ public void setViewPort(Rectangle2D.Double viewPort)

private void populateGrid(Tile centerTile)
{
final int endRow = (gridSize - 1) / 2;
final int endCol = (gridSize - 1) / 2;
final int endRow = (gridSizeHeight - 1) / 2;
final int endCol = (gridSizeWidth - 1) / 2;
final int startRow = -endRow;
final int startCol = -endCol;

Expand Down Expand Up @@ -155,15 +153,20 @@ public boolean hasUnloadedTile()
return hasUnloadedTile(false);
}

public int getGridSize()
private void setGridSizes(int gridSizeHeight, int gridSizeWidth)
{
return gridSize;
}
if (this.gridSizeHeight == gridSizeHeight && this.gridSizeWidth == gridSizeWidth) return;
if (gridSizeHeight % 2 == 0) gridSizeHeight++;
if (gridSizeWidth % 2 == 0) gridSizeWidth++;

public void setGridSize(int gridSize)
{
this.gridSize = gridSize; // Must be an odd number so as to have a center tile.
srcSize = gridSize * Tile.TILESIZE;
// Both must be odd so that a center tile exists
this.gridSizeHeight = gridSizeHeight;
this.gridSizeWidth = gridSizeWidth;

this.srcSizeHeight = gridSizeHeight * Tile.TILESIZE;
this.srcSizeWidth = gridSizeWidth * Tile.TILESIZE;

clear();
}

public boolean hasUnloadedTile(boolean preview)
Expand Down Expand Up @@ -235,6 +238,7 @@ public void updateTiles(MapType mapType, int zoom, boolean highQuality, int widt

// Update screen dimensions
updateBounds(width, height);
updateGridSize();

// Get center tile, check if present and current
Tile centerTile = grid.get(centerPos);
Expand All @@ -249,9 +253,9 @@ public void updateTiles(MapType mapType, int zoom, boolean highQuality, int widt
// Derive offsets for centering the map
Point2D blockPixelOffset = centerTile.blockPixelOffsetInTile(centerBlockX, centerBlockZ);
final double blockSizeOffset = Math.pow(2, zoom) / 2;
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".

double displayOffsetX = xOffset + magic - ((srcSize - lastWidth) / 2);
int extraOffsetX = (gridSizeWidth / 2) * Tile.TILESIZE;
double displayOffsetX = xOffset + extraOffsetX - ((srcSizeWidth - lastWidth) / 2);
if (centerBlockX < 0)
{
displayOffsetX -= blockSizeOffset;
Expand All @@ -260,7 +264,9 @@ public void updateTiles(MapType mapType, int zoom, boolean highQuality, int widt
{
displayOffsetX += blockSizeOffset;
}
double displayOffsetY = yOffset + magic - ((srcSize - lastHeight) / 2);

int extraOffsetY = (gridSizeHeight / 2) * Tile.TILESIZE;
double displayOffsetY = yOffset + extraOffsetY - ((srcSizeHeight - lastHeight) / 2);
if (centerBlockZ < 0)
{
displayOffsetY -= blockSizeOffset;
Expand Down Expand Up @@ -583,6 +589,13 @@ private void updateBounds(int width, int height)
}
}

private void updateGridSize() {
int newGridSizeHeight = (int) Math.ceil(screenBounds.height / Tile.TILESIZE + 0.5);
int newGridSizeWidth = (int) Math.ceil(screenBounds.width / Tile.TILESIZE + 0.5);

setGridSizes(newGridSizeHeight, newGridSizeWidth);
}

private Tile findNeighbor(Tile tile, TilePos pos)
{
if (pos.deltaX == 0 && pos.deltaZ == 0)
Expand Down Expand Up @@ -674,11 +687,6 @@ public boolean setZoom(int zoom)
return center(mapType, centerBlockX, centerBlockZ, zoom);
}

public int getRenderSize()
{
return this.gridSize * Tile.TILESIZE;
}

public void clear()
{
grid.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@
public class Fullscreen extends JmUI
{
final static MapState state = new MapState();
final static GridRenderer gridRenderer = new GridRenderer(5);
final static GridRenderer gridRenderer = new GridRenderer();
final WaypointDrawStepFactory waypointRenderer = new WaypointDrawStepFactory();
final RadarDrawStepFactory radarRenderer = new RadarDrawStepFactory();
final LayerDelegate layerDelegate = new LayerDelegate();
Expand Down
4 changes: 1 addition & 3 deletions src/main/java/journeymap/client/ui/minimap/MiniMap.java
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ public class MiniMap
private static final float lightmapS = (float) (15728880 % 65536) / 1f;
private static final float lightmapT = (float) (15728880 / 65536) / 1f;
private static final long labelRefreshRate = 400;
private final static GridRenderer gridRenderer = new GridRenderer(3);
private final static GridRenderer gridRenderer = new GridRenderer();
private final IForgeHelper forgeHelper = ForgeHelper.INSTANCE;
private final Logger logger = Journeymap.getLogger();
private final Minecraft mc = ForgeHelper.INSTANCE.getClient();
Expand Down Expand Up @@ -98,8 +98,6 @@ private void initGridRenderer()

MapType mapType = state.getMapType(showCaves);

int gridSize = miniMapProperties.getSize() <= 768 ? 3 : 5;
gridRenderer.setGridSize(gridSize);
gridRenderer.setContext(state.getWorldDir(), mapType);
gridRenderer.center(mapType, mc.thePlayer.posX, mc.thePlayer.posZ, miniMapProperties.zoomLevel.get());

Expand Down

0 comments on commit f4631f5

Please sign in to comment.