Skip to content

Commit

Permalink
[HaCreator] Fixed DPI scaling with map simulator
Browse files Browse the repository at this point in the history
XNA isnt DPI aware.
  • Loading branch information
lastbattle committed Mar 15, 2021
1 parent 0edc041 commit c62ebb3
Show file tree
Hide file tree
Showing 4 changed files with 138 additions and 93 deletions.
87 changes: 56 additions & 31 deletions HaCreator/MapSimulator/MapSimulator.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@
using HaCreator.MapSimulator.Objects;
using HaCreator.MapSimulator.Objects.FieldObject;
using HaCreator.MapSimulator.Objects.UIObject;
using HaRepacker.Utils;
using HaSharedLibrary;
using HaSharedLibrary.Render.DX;
using HaSharedLibrary.Util;
using MapleLib.WzLib;
using MapleLib.WzLib.Spine;
using MapleLib.WzLib.WzProperties;
using MapleLib.WzLib.WzStructure.Data;
using Microsoft.Xna.Framework;
Expand Down Expand Up @@ -39,10 +41,14 @@ public class MapSimulator : Microsoft.Xna.Framework.Game
public int mapShiftY = 0;
public Point minimapPos;

private int Width;
private int Height;
private int RenderWidth;
private int RenderHeight;
private float RenderObjectScaling = 1.0f;
private float UserScreenScaleFactor = 1.0f;
private RenderResolution mapRenderResolution;
private Matrix matrixScale;

private GraphicsDeviceManager _DxDeviceManager;
private readonly TexturePool texturePool = new TexturePool();
Expand Down Expand Up @@ -103,7 +109,7 @@ public MapSimulator(Board mapBoard, string titleName)
this.mapBoard = mapBoard;

this.mapRenderResolution = UserSettings.SimulateResolution;
InitialiseMapWidthHeight();
InitialiseWindowAndMap_WidthHeight();

//RenderHeight += System.Windows.Forms.SystemInformation.CaptionHeight; // window title height

Expand Down Expand Up @@ -145,64 +151,71 @@ void graphics_DeviceCreated(object sender, EventArgs e)
{
}

private void InitialiseMapWidthHeight()
private void InitialiseWindowAndMap_WidthHeight()
{
RenderObjectScaling = 1.0f;
this.RenderObjectScaling = 1.0f;
switch (this.mapRenderResolution)
{
case RenderResolution.Res_1024x768: // 1024x768
RenderHeight = 768;
RenderWidth = 1024;
Height = 768;
Width = 1024;
break;
case RenderResolution.Res_1280x720: // 1280x720
RenderHeight = 720;
RenderWidth = 1280;
Height = 720;
Width = 1280;
break;
case RenderResolution.Res_1366x768: // 1366x768
RenderHeight = 768;
RenderWidth = 1366;
Height = 768;
Width = 1366;
break;


case RenderResolution.Res_1920x1080: // 1920x1080
RenderHeight = 1080;
RenderWidth = 1920;
Height = 1080;
Width = 1920;
break;
case RenderResolution.Res_1920x1080_120PercScaled: // 1920x1080
RenderHeight = 1080;
RenderWidth = 1920;
Height = 1080;
Width = 1920;
RenderObjectScaling = 1.2f;
break;
case RenderResolution.Res_1920x1080_150PercScaled: // 1920x1080
RenderHeight = 1080;
RenderWidth = 1920;
Height = 1080;
Width = 1920;
RenderObjectScaling = 1.5f;
this.mapRenderResolution |= RenderResolution.Res_1366x768; // 1920x1080 is just 1366x768 with 150% scale.
break;


case RenderResolution.Res_1920x1200: // 1920x1200
RenderHeight = 1200;
RenderWidth = 1920;
Height = 1200;
Width = 1920;
break;
case RenderResolution.Res_1920x1200_120PercScaled: // 1920x1200
RenderHeight = 1200;
RenderWidth = 1920;
Height = 1200;
Width = 1920;
RenderObjectScaling = 1.2f;
break;
case RenderResolution.Res_1920x1200_150PercScaled: // 1920x1200
RenderHeight = 1200;
RenderWidth = 1920;
Height = 1200;
Width = 1920;
RenderObjectScaling = 1.5f;
break;

case RenderResolution.Res_All:
case RenderResolution.Res_800x600: // 800x600
default:
RenderHeight = 600;
RenderWidth = 800;
Height = 600;
Width = 800;
break;
}
this.UserScreenScaleFactor = (float) ScreenDPIUtil.GetScreenScaleFactor();

this.RenderHeight = (int) (Height * UserScreenScaleFactor);
this.RenderWidth = (int)(Width * UserScreenScaleFactor);
this.RenderObjectScaling = (RenderObjectScaling * UserScreenScaleFactor);

this.matrixScale = Matrix.CreateScale(RenderObjectScaling);
}

protected override void Initialize()
Expand Down Expand Up @@ -362,7 +375,7 @@ protected override void LoadContent()
WzSubProperty farmFrameParent = (WzSubProperty)UIWZFile["UIToolTip.img"]?["Item"]?["FarmFrame"];
foreach (ToolTipInstance tooltip in mapBoard.BoardItems.ToolTips)
{
TooltipItem item = MapSimulatorLoader.CreateTooltipFromProperty(texturePool, farmFrameParent, tooltip, _DxDeviceManager.GraphicsDevice);
TooltipItem item = MapSimulatorLoader.CreateTooltipFromProperty(texturePool, UserScreenScaleFactor, farmFrameParent, tooltip, _DxDeviceManager.GraphicsDevice);

mapObjects_tooltips.Add(item);
}
Expand All @@ -380,16 +393,17 @@ protected override void LoadContent()
{
skeletonMeshRenderer = new SkeletonMeshRenderer(GraphicsDevice)
{
PremultipliedAlpha = false
PremultipliedAlpha = false,
};
skeletonMeshRenderer.Effect.World = this.matrixScale;
});

// Minimap
Task t_minimap = Task.Run(() =>
{
if (!mapBoard.MapInfo.hideMinimap)
{
miniMap = MapSimulatorLoader.CreateMinimapFromProperty(UIWZFile, mapBoard, GraphicsDevice, mapBoard.MapInfo.strMapName, mapBoard.MapInfo.strStreetName, SoundWZFile, bBigBangUpdate);
miniMap = MapSimulatorLoader.CreateMinimapFromProperty(UIWZFile, mapBoard, GraphicsDevice, UserScreenScaleFactor, mapBoard.MapInfo.strMapName, mapBoard.MapInfo.strStreetName, SoundWZFile, bBigBangUpdate);
}
});

Expand Down Expand Up @@ -449,6 +463,16 @@ protected override void LoadContent()
// clear used items
foreach (WzObject obj in usedProps)
{
// Spine events
WzSpineObject spineObj = (WzSpineObject) obj.MSTagSpine;
if (spineObj != null)
{
spineObj.state.Start += Start;
spineObj.state.End += End;
spineObj.state.Complete += Complete;
spineObj.state.Event += Event;
}

obj.MSTag = null;
obj.MSTagSpine = null; // cleanup
}
Expand Down Expand Up @@ -618,7 +642,7 @@ protected override void Draw(GameTime gameTime)
spriteBatch.Begin(
SpriteSortMode.Immediate, // spine :( needs to be drawn immediately to maintain the layer orders
//SpriteSortMode.Deferred,
BlendState.NonPremultiplied, null, null, null, null, Matrix.CreateScale(RenderObjectScaling));
BlendState.NonPremultiplied, null, null, null, null, this.matrixScale);
//skeletonMeshRenderer.Begin();

// Back Backgrounds
Expand Down Expand Up @@ -740,15 +764,16 @@ protected override void Draw(GameTime gameTime)
spriteBatch.DrawString(font_navigationKeysHelper,
string.Format("[Left] [Right] [Up] [Down] [Shift] for navigation.{0}[F5] for debug mode{1}[Alt+Enter] Full screen{2}[PrintSc] Screenshot",
Environment.NewLine, Environment.NewLine, Environment.NewLine),
new Vector2(20, RenderHeight - 140), Color.White);
new Vector2(20, Height - 140), Color.White);

if (!bSaveScreenshot && bShowDebugMode)
{
StringBuilder sb = new StringBuilder();
sb.Append("FPS: ").Append(frameRate).Append(Environment.NewLine);
sb.Append("Mouse : X ").Append(mouseXRelativeToMap).Append(", Y ").Append(mouseYRelativeToMap).Append(Environment.NewLine);
sb.Append("RMouse: X ").Append(mouseState.X).Append(", Y ").Append(mouseState.Y);
spriteBatch.DrawString(font_DebugValues, sb.ToString(), new Vector2(RenderWidth - 170, 10), Color.White);
spriteBatch.DrawString(font_DebugValues, sb.ToString(),
new Vector2(Width - 170, 10), Color.White); // use the original width to render text
}

// Cursor [this is in front of everything else]
Expand Down Expand Up @@ -878,7 +903,7 @@ private void DoScreenshot()
GraphicsDevice.GetBackBufferData(backBuffer);

//Copy to texture
using (Texture2D texture = new Texture2D(GraphicsDevice, RenderWidth, RenderHeight, false, SurfaceFormat.Color /*RGBA8888*/))
using (Texture2D texture = new Texture2D(GraphicsDevice, backBufferWidth, backBufferHeight, false, SurfaceFormat.Color /*RGBA8888*/))
{
texture.SetData(backBuffer);

Expand All @@ -891,7 +916,7 @@ private void DoScreenshot()

using (MemoryStream stream_png = new MemoryStream()) // memorystream for png
{
texture.SaveAsPng(stream_png, RenderWidth, RenderHeight); // save to png stream
texture.SaveAsPng(stream_png, backBufferWidth, backBufferHeight); // save to png stream

System.Drawing.Bitmap bitmap = new System.Drawing.Bitmap(stream_png);
ImageCodecInfo jpgEncoder = GetEncoder(ImageFormat.Jpeg);
Expand Down
10 changes: 6 additions & 4 deletions HaCreator/MapSimulator/MapSimulatorLoader.cs
Original file line number Diff line number Diff line change
Expand Up @@ -507,11 +507,12 @@ public static NpcItem CreateNpcFromProperty(TexturePool texturePool, NpcInstance
/// <param name="UIWZFile">UI.wz file directory</param>
/// <param name="mapBoard"></param>
/// <param name="device"></param>
/// <param name="UserScreenScaleFactor">The scale factor of the window (DPI)</param>
/// <param name="MapName">The map name. i.e The Hill North</param>
/// <param name="StreetName">The street name. i.e Hidden street</param>
/// <param name="bBigBang">Big bang update</param>
/// <returns></returns>
public static MinimapItem CreateMinimapFromProperty(WzDirectory UIWZFile, Board mapBoard, GraphicsDevice device, string MapName, string StreetName, WzDirectory SoundWZFile, bool bBigBang)
public static MinimapItem CreateMinimapFromProperty(WzDirectory UIWZFile, Board mapBoard, GraphicsDevice device, float UserScreenScaleFactor, string MapName, string StreetName, WzDirectory SoundWZFile, bool bBigBang)
{
if (mapBoard.MiniMap == null)
return null;
Expand Down Expand Up @@ -559,7 +560,7 @@ public static MinimapItem CreateMinimapFromProperty(WzDirectory UIWZFile, Board
int effective_width = miniMapImage.Width + e.Width + w.Width;
int effective_height = miniMapImage.Height + n.Height + s.Height;

using (System.Drawing.Font font = new System.Drawing.Font(GLOBAL_FONT, TOOLTIP_FONTSIZE))
using (System.Drawing.Font font = new System.Drawing.Font(GLOBAL_FONT, TOOLTIP_FONTSIZE / UserScreenScaleFactor))
{
// Get the width of the 'streetName' or 'mapName'
System.Drawing.Graphics graphics_dummy = System.Drawing.Graphics.FromImage(new System.Drawing.Bitmap(1, 1)); // dummy image just to get the Graphics object for measuring string
Expand Down Expand Up @@ -696,11 +697,12 @@ public static MinimapItem CreateMinimapFromProperty(WzDirectory UIWZFile, Board
/// Tooltip
/// </summary>
/// <param name="texturePool"></param>
/// <param name="UserScreenScaleFactor">The scale factor of the window (DPI)</param>
/// <param name="farmFrameParent"></param>
/// <param name="tooltip"></param>
/// <param name="device"></param>
/// <returns></returns>
public static TooltipItem CreateTooltipFromProperty(TexturePool texturePool, WzSubProperty farmFrameParent, ToolTipInstance tooltip, GraphicsDevice device)
public static TooltipItem CreateTooltipFromProperty(TexturePool texturePool, float UserScreenScaleFactor, WzSubProperty farmFrameParent, ToolTipInstance tooltip, GraphicsDevice device)
{
// Wz frames
System.Drawing.Bitmap c = ((WzCanvasProperty)farmFrameParent?["c"])?.GetLinkedWzCanvasBitmap();
Expand Down Expand Up @@ -730,7 +732,7 @@ public static TooltipItem CreateTooltipFromProperty(TexturePool texturePool, WzS
const int HEIGHT_PADDING = 6;

// Create
using (System.Drawing.Font font = new System.Drawing.Font(GLOBAL_FONT, TOOLTIP_FONTSIZE))
using (System.Drawing.Font font = new System.Drawing.Font(GLOBAL_FONT, TOOLTIP_FONTSIZE / UserScreenScaleFactor))
{
System.Drawing.Graphics graphics_dummy = System.Drawing.Graphics.FromImage(new System.Drawing.Bitmap(1, 1)); // dummy image just to get the Graphics object for measuring string
System.Drawing.SizeF tooltipSize = graphics_dummy.MeasureString(renderText, font);
Expand Down
27 changes: 17 additions & 10 deletions MapleLib/SquishPNGWrapper.cs
Original file line number Diff line number Diff line change
Expand Up @@ -55,55 +55,62 @@ public static bool CheckAndLoadLibrary()
if (!(assemblyArchitecture == ProcessorArchitecture.X86 || assemblyArchitecture == ProcessorArchitecture.Amd64))
{
_SquishLibLoadingState = SquishLibLoadingState.WrongProcessorArchitecture;

System.Diagnostics.Debug.WriteLine("squish.dll library not loaded. MSIL assembly detected.");
return false;
}
// Load library here
IntPtr apnglib = LoadLibrary("squish.dll");
if (apnglib != IntPtr.Zero)
IntPtr errorCode = LoadLibrary("squish.dll");
if (errorCode != IntPtr.Zero)
{
IntPtr FixFlagsPtr = GetProcAddress(apnglib, "_DLLEXPORT_FixFlags");
IntPtr FixFlagsPtr = GetProcAddress(errorCode, "_DLLEXPORT_FixFlags");
if (FixFlagsPtr != null)
FixFlags = (_DLLEXPORT_FixFlags)Marshal.GetDelegateForFunctionPointer(FixFlagsPtr, typeof(_DLLEXPORT_FixFlags));

IntPtr CompressPtr = GetProcAddress(apnglib, "_DLLEXPORT_Compress");
IntPtr CompressPtr = GetProcAddress(errorCode, "_DLLEXPORT_Compress");
if (CompressPtr != null)
Compress = (_DLLEXPORT_Compress)Marshal.GetDelegateForFunctionPointer(CompressPtr, typeof(_DLLEXPORT_Compress));

IntPtr CompressMaskedPtr = GetProcAddress(apnglib, "_DLLEXPORT_CompressMasked");
IntPtr CompressMaskedPtr = GetProcAddress(errorCode, "_DLLEXPORT_CompressMasked");
if (CompressMaskedPtr != null)
CompressMasked = (_DLLEXPORT_CompressMasked)Marshal.GetDelegateForFunctionPointer(CompressMaskedPtr, typeof(_DLLEXPORT_CompressMasked));

IntPtr DecompressPtr = GetProcAddress(apnglib, "_DLLEXPORT_Decompress");
IntPtr DecompressPtr = GetProcAddress(errorCode, "_DLLEXPORT_Decompress");
if (DecompressPtr != null)
Decompress = (_DLLEXPORT_Decompress)Marshal.GetDelegateForFunctionPointer(DecompressPtr, typeof(_DLLEXPORT_Decompress));

IntPtr StorageRequirementsPtr = GetProcAddress(apnglib, "_DLLEXPORT_GetStorageRequirements");
IntPtr StorageRequirementsPtr = GetProcAddress(errorCode, "_DLLEXPORT_GetStorageRequirements");
if (StorageRequirementsPtr != null)
GetStorageRequirements = (_DLLEXPORT_GetStorageRequirements)Marshal.GetDelegateForFunctionPointer(StorageRequirementsPtr, typeof(_DLLEXPORT_GetStorageRequirements));

IntPtr CompressImagePtr = GetProcAddress(apnglib, "_DLLEXPORT_CompressImage");
IntPtr CompressImagePtr = GetProcAddress(errorCode, "_DLLEXPORT_CompressImage");
if (CompressImagePtr != null)
CompressImage = (_DLLEXPORT_CompressImage)Marshal.GetDelegateForFunctionPointer(CompressImagePtr, typeof(_DLLEXPORT_CompressImage));

IntPtr DecompressImagePtr = GetProcAddress(apnglib, "_DLLEXPORT_DecompressImage");
IntPtr DecompressImagePtr = GetProcAddress(errorCode, "_DLLEXPORT_DecompressImage");
if (DecompressImagePtr != null)
DecompressImage = (_DLLEXPORT_DecompressImage)Marshal.GetDelegateForFunctionPointer(DecompressImagePtr, typeof(_DLLEXPORT_DecompressImage));


_SquishLibLoadingState = SquishLibLoadingState.Initialised; // flag as initialised

System.Diagnostics.Debug.WriteLine("Loaded squish.dll library.");

return true;
}
else
{
_SquishLibLoadingState = SquishLibLoadingState.UnknownError;

System.Diagnostics.Debug.WriteLine("Error loaded squish.dll library, code = " + errorCode);
return false;
//throw new Exception("squish.dll not found in the program directory.");
}
}
return false;
}

#region Exports
#region Imports
public static _DLLEXPORT_FixFlags FixFlags;
public static _DLLEXPORT_Compress Compress;
public static _DLLEXPORT_CompressMasked CompressMasked;
Expand Down
Loading

0 comments on commit c62ebb3

Please sign in to comment.