code_snippets:psx_scaling
This is an old revision of the document!
Table of Contents
PSX Scaling
The code below should help you set up PlayStation 1 era visuals and should be commented enough.
The Goal
To implement the technique of rendering at a lower resolution and then scaling up to 720×480 in MonoGame, you can use the RenderTarget2D class.
Here's a step-by-step guide:
- Create a Render Target: Define a render target with your desired lower resolution (e.g., 640×480).
- Render Your Scene: Draw your game scene to the render target.
- Scale and Draw to Back Buffer: Draw the render target to the back buffer with scaling to fit the 720×480 resolution.
Fields
Fields:
private RenderTarget2D renderTarget;
Initialize()
Initialize()
{
// set backbuffer to PlayStation 1 Wide Screen 720x480
_graphics.PreferredBackBufferWidth = 720; // 320
_graphics.PreferredBackBufferHeight = 480; // 240
_graphics.ApplyChanges(); // !
base.Initialize();
}
LoadContent()
LoadContent()
{
// Define your lower resolution render target
renderTarget = new RenderTarget2D(GraphicsDevice, 640, 480);
}
Draw()
// ! means this is important
Draw()
{
// Render your scene to the render target
GraphicsDevice.SetRenderTarget(renderTarget); // !
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin(SpriteSortMode.Deferred,
BlendState.AlphaBlend, SamplerState.PointClamp); // pointclamp !
_spriteBatch.Draw(); // something
_spriteBatch.Draw(); // something
_spriteBatch.End(); // !
// Reset the render target to the backbuffer
GraphicsDevice.SetRenderTarget(null); // !
// Calculate the scaling factors
float scaleX = GraphicsDevice.Viewport.Width / 720f; // !
float scaleY = GraphicsDevice.Viewport.Height / 480f; // !
// Set up the viewport and scaling matrix
var viewport = new Viewport(0, 0, 720, 480); // !
var scaleMatrix = Matrix.CreateScale(scaleX, scaleY, 1.0f); // !
// Draw the render target to the backbuffer, scaling it to fill the screen
GraphicsDevice.Clear(Color.CornflowerBlue);
_spriteBatch.Begin(transformMatrix: scaleMatrix); // !
_spriteBatch.Draw(renderTarget, new Rectangle(0, 0, 720, 480), Color.White); // !
_spriteBatch.End(); // !
base.Draw(gameTime);
}
code_snippets/psx_scaling.1729537361.txt.gz · Last modified: 2024/10/21 19:02 by mrvalentine