Added window scaling
This commit is contained in:
@@ -53,7 +53,7 @@ namespace Desktop
|
||||
|
||||
this.Text = $"Parsons Master System - {_currentRomName}";
|
||||
|
||||
// Safe to initialize hardware and files here!
|
||||
this.BackColor = Color.Black;
|
||||
_machine = new SmsMachine();
|
||||
_audioPlayer = new NAudioPlayer();
|
||||
_machine.AudioProcessor.AudioDevice = _audioPlayer;
|
||||
@@ -72,27 +72,37 @@ namespace Desktop
|
||||
}
|
||||
protected override void OnPaint(PaintEventArgs e)
|
||||
{
|
||||
// Always call the base method so Windows can draw your MenuStrip!
|
||||
base.OnPaint(e);
|
||||
|
||||
// THE FIX: We MUST ensure the designer has actually built the menu strip before asking for its height!
|
||||
if (_screenBitmap != null && menuStrip1 != null)
|
||||
{
|
||||
// 1. Set the rendering mode for perfect, chunky retro pixels!
|
||||
// 1. Maintain perfect, chunky retro pixels
|
||||
e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.NearestNeighbor;
|
||||
e.Graphics.PixelOffsetMode = System.Drawing.Drawing2D.PixelOffsetMode.Half;
|
||||
|
||||
// 2. Calculate the drawing area.
|
||||
// We start drawing BELOW the MenuStrip so it doesn't get covered up.
|
||||
// 2. Calculate the actual usable window space
|
||||
int topOffset = menuStrip1.Height;
|
||||
Rectangle renderArea = new Rectangle(
|
||||
0,
|
||||
topOffset,
|
||||
this.ClientSize.Width,
|
||||
this.ClientSize.Height - topOffset
|
||||
);
|
||||
int availableWidth = this.ClientSize.Width;
|
||||
int availableHeight = this.ClientSize.Height - topOffset;
|
||||
|
||||
// 3. Blast the bitmap directly onto the graphics card buffer!
|
||||
// 3. Calculate the maximum scale factor that fits perfectly
|
||||
float scaleX = (float)availableWidth / 256f;
|
||||
float scaleY = (float)availableHeight / 192f;
|
||||
|
||||
// Pick the smaller scale so the image never bleeds off the edge
|
||||
float scale = Math.Min(scaleX, scaleY);
|
||||
|
||||
// 4. Calculate the new physical pixel dimensions
|
||||
int newWidth = (int)(256 * scale);
|
||||
int newHeight = (int)(192 * scale);
|
||||
|
||||
// 5. Center the image (Letterboxing / Pillarboxing)
|
||||
int offsetX = (availableWidth - newWidth) / 2;
|
||||
int offsetY = topOffset + ((availableHeight - newHeight) / 2);
|
||||
|
||||
Rectangle renderArea = new Rectangle(offsetX, offsetY, newWidth, newHeight);
|
||||
|
||||
// 6. Draw the scaled image
|
||||
e.Graphics.DrawImage(_screenBitmap, renderArea);
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user