Add logic for game image stretching in Hooks#draw

Cache stretched dimensions and image

Fix rendering hint not being set until you resize window
This commit is contained in:
Lotto
2018-03-03 00:41:21 +01:00
parent 22f931a438
commit 4e198b6e0e
2 changed files with 70 additions and 13 deletions

View File

@@ -26,8 +26,10 @@ package net.runelite.client.callback;
import com.google.common.eventbus.EventBus;
import com.google.inject.Injector;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.RenderingHints;
import java.awt.event.KeyEvent;
import java.awt.event.MouseEvent;
import java.awt.event.MouseWheelEvent;
@@ -83,6 +85,10 @@ public class Hooks
private static final DeathChecker death = new DeathChecker(client, eventBus);
private static final GameTick tick = new GameTick();
private static Dimension lastStretchedDimensions;
private static BufferedImage stretchedImage;
private static Graphics2D stretchedGraphics;
private static long lastCheck;
public static void clientMainLoop(Client client, boolean arg1)
@@ -204,7 +210,12 @@ public class Hooks
public static void draw(MainBufferProvider mainBufferProvider, Graphics graphics, int x, int y)
{
final BufferedImage image = (BufferedImage) mainBufferProvider.getImage();
if (graphics == null)
{
return;
}
BufferedImage image = (BufferedImage) mainBufferProvider.getImage();
final Graphics2D graphics2d = (Graphics2D) image.getGraphics();
try
@@ -216,6 +227,39 @@ public class Hooks
log.warn("Error during overlay rendering", ex);
}
// Stretch the game image if the user has that enabled
if (!client.isResized() && client.isStretchedEnabled())
{
Dimension stretchedDimensions = client.getStretchedDimensions();
if (lastStretchedDimensions == null || !lastStretchedDimensions.equals(stretchedDimensions))
{
/*
Reuse the resulting image instance to avoid creating an extreme amount of objects
*/
stretchedImage = new BufferedImage(stretchedDimensions.width, stretchedDimensions.height, BufferedImage.TYPE_INT_RGB);
if (stretchedGraphics != null)
{
stretchedGraphics.dispose();
}
stretchedGraphics = (Graphics2D) stretchedImage.getGraphics();
lastStretchedDimensions = stretchedDimensions;
}
stretchedGraphics.setRenderingHint(RenderingHints.KEY_INTERPOLATION,
client.isStretchedFast()
? RenderingHints.VALUE_INTERPOLATION_NEAREST_NEIGHBOR
: RenderingHints.VALUE_INTERPOLATION_BILINEAR);
stretchedGraphics.drawImage(image, 0, 0, stretchedDimensions.width, stretchedDimensions.height, null);
image = stretchedImage;
}
// Draw the image onto the game canvas
graphics.drawImage(image, 0, 0, client.getCanvas());
renderer.provideScreenshot(image);
}

View File

@@ -43,6 +43,12 @@ public abstract class StretchedFixedModeMixin implements RSClient
@Inject
private static boolean stretchedKeepAspectRatio;
@Inject
private static Dimension cachedStretchedDimensions;
@Inject
private static Dimension lastCanvasDimensions;
@Inject
@Override
public boolean isStretchedEnabled()
@@ -76,6 +82,7 @@ public abstract class StretchedFixedModeMixin implements RSClient
public void setStretchedKeepAspectRatio(boolean state)
{
stretchedKeepAspectRatio = state;
cachedStretchedDimensions = null;
}
@Inject
@@ -84,23 +91,29 @@ public abstract class StretchedFixedModeMixin implements RSClient
{
Canvas canvas = getCanvas();
int newWidth = canvas.getWidth();
int newHeight = canvas.getHeight();
int width = canvas.getWidth();
int height = canvas.getHeight();
if (stretchedKeepAspectRatio)
if (cachedStretchedDimensions == null || width != lastCanvasDimensions.width || height != lastCanvasDimensions.height)
{
int tempNewWidth = (int) (newHeight * Constants.GAME_FIXED_ASPECT_RATIO);
if (stretchedKeepAspectRatio)
{
int tempNewWidth = (int) (height * Constants.GAME_FIXED_ASPECT_RATIO);
if (tempNewWidth > canvas.getWidth())
{
newHeight = (int) (newWidth / Constants.GAME_FIXED_ASPECT_RATIO);
}
else
{
newWidth = tempNewWidth;
if (tempNewWidth > canvas.getWidth())
{
height = (int) (width / Constants.GAME_FIXED_ASPECT_RATIO);
}
else
{
width = tempNewWidth;
}
}
cachedStretchedDimensions = new Dimension(width, height);
lastCanvasDimensions = new Dimension(width, height);
}
return new Dimension(newWidth, newHeight);
return cachedStretchedDimensions;
}
}