Skip to content

Commit 5f16ca7

Browse files
committed
Fix resolution independence.
1 parent 55ca91b commit 5f16ca7

1 file changed

Lines changed: 10 additions & 9 deletions

File tree

Platformer2D/Core/ScreenManagers/ScreenManager.cs

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ public class ScreenManager : DrawableGameComponent
3434
private bool traceEnabled;
3535

3636
internal const int BASE_BUFFER_WIDTH = 800;
37-
internal const int BASE_BUFFER_HEIGHT = 400;
37+
internal const int BASE_BUFFER_HEIGHT = 480;
3838

3939
private int backbufferWidth;
4040
/// <summary>Gets or sets the current backbuffer width.</summary>
@@ -68,7 +68,7 @@ public class ScreenManager : DrawableGameComponent
6868
/// </summary>
6969
public bool TraceEnabled { get => traceEnabled; set => traceEnabled = value; }
7070

71-
Rectangle safeArea = new Rectangle(0, 0, BASE_BUFFER_WIDTH, BASE_BUFFER_HEIGHT);
71+
private Rectangle safeArea = new Rectangle(0, 0, BASE_BUFFER_WIDTH, BASE_BUFFER_HEIGHT);
7272
/// <summary>
7373
/// Returns the portion of the screen where drawing is safely allowed.
7474
/// </summary>
@@ -271,12 +271,11 @@ public GameScreen[] GetScreens()
271271
/// <param name="alpha">The opacity level of the fade (0 = fully transparent, 1 = fully opaque).</param>
272272
public void FadeBackBufferToBlack(float alpha)
273273
{
274-
// Draw without transformation to cover the entire backbuffer
275-
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, null);
274+
spriteBatch.Begin(SpriteSortMode.Deferred, null, null, null, null, null, globalTransformation);
276275

277276
spriteBatch.Draw(blankTexture,
278-
new Rectangle(0, 0, backbufferWidth, backbufferHeight),
279-
Color.Black * alpha);
277+
new Rectangle(0, 0, (int)BaseScreenSize.X, (int)BaseScreenSize.Y),
278+
Color.Black * alpha);
280279

281280
spriteBatch.End();
282281
}
@@ -324,16 +323,18 @@ public void ScalePresentationArea()
324323
// Taller screen: scale by width
325324
scalingFactor = backbufferWidth / baseScreenSize.X;
326325

327-
// Don't center vertically - align to top
328-
verticalOffset = -30;
326+
// Centre things vertically.
327+
verticalOffset = (backbufferHeight - baseScreenSize.Y * scalingFactor) / 2;
329328
}
330329

331330
// Update the transformation matrix
332331
globalTransformation = Matrix.CreateScale(scalingFactor) *
333-
Matrix.CreateTranslation(horizontalOffset, verticalOffset, 0);
332+
Matrix.CreateTranslation(horizontalOffset, verticalOffset, 0);
334333

335334
// Update the inputTransformation with the Inverted globalTransformation
336335
inputState.UpdateInputTransformation(Matrix.Invert(globalTransformation));
337336

337+
// Debug info
338+
Debug.WriteLine($"Screen Size - Width[{backbufferWidth}] Height[{backbufferHeight}] ScalingFactor[{scalingFactor}]");
338339
}
339340
}

0 commit comments

Comments
 (0)