Skip to content

Commit

Permalink
Makes camera much less blurry, adds pixel perfect mode
Browse files Browse the repository at this point in the history
  • Loading branch information
MarkSuckerberg committed Sep 26, 2023
1 parent 55cbea5 commit 81d1214
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 28 deletions.
4 changes: 2 additions & 2 deletions Blocktest/Code/Block System/Tilemap.cs
Original file line number Diff line number Diff line change
Expand Up @@ -118,10 +118,10 @@ public class Tile
/// </summary>
/// <param name="newBlock">The type of block the new tile should be.</param>
/// <param name="position">The position in a tilemap the tile will be.</param>
public Tile(Block newBlock, Vector2Int position)
public Tile(Block newBlock, Vector2Int position, Layer layer = Layer.ForegroundBlocks)
{
SourceBlock = newBlock;
Renderable = new Renderable(new Transform(new Vector2(Globals.gridSize.X * position.X, Globals.gridSize.Y * position.Y)), SourceBlock.blockSprite);
Renderable = new Renderable(new Transform(new Vector2(Globals.gridSize.X * position.X, Globals.gridSize.Y * position.Y)), layer, SourceBlock.blockSprite);
}

/// <summary>
Expand Down
4 changes: 3 additions & 1 deletion Blocktest/Code/BuildSystem.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
using Blocktest.Rendering;

namespace Blocktest
{
public static class BuildSystem
Expand Down Expand Up @@ -45,7 +47,7 @@ public static void BreakBlockCell(bool foreground, Vector2Int tilePosition)
/// <param name="tilePosition">The position of the placed block. (Grid coords)</param>
public static void PlaceBlockCell(Block toPlace, bool foreground, Vector2Int tilePosition)
{
Tile newTile = new(toPlace, tilePosition);
Tile newTile = new(toPlace, tilePosition, foreground ? Layer.ForegroundBlocks : Layer.BackgroundBlocks);
toPlace.OnPlace(tilePosition, foreground);

if (foreground) {
Expand Down
9 changes: 6 additions & 3 deletions Blocktest/Code/Rendering/Camera.cs
Original file line number Diff line number Diff line change
Expand Up @@ -43,17 +43,20 @@ public void Draw(GraphicsDevice graphics, SpriteBatch spriteBatch) {

Rectangle positionBounds = new((int)flippedPosition.X, (int)flippedPosition.Y, (int)(component.Appearance.Bounds.Width * component.Transform.Scale.X),
(int)(component.Appearance.Bounds.Height * component.Transform.Scale.Y));

spriteBatch.Draw(component.Appearance.Texture, positionBounds, component.Appearance.Bounds,
component.RenderColor, component.Transform.Rotation, component.Transform.Origin, SpriteEffects.None, 0);
component.RenderColor, component.Transform.Rotation, component.Transform.Origin, SpriteEffects.None, (float)component.Layer / EnumCount);
}

spriteBatch.End();

graphics.SetRenderTarget(null);
}

private static readonly int EnumCount = Enum.GetValues(typeof(Layer)).Length;

public Vector2 CameraToWorldPos(Vector2 mouseState) {
return new(mouseState.X / RenderLocation.Width * RenderTarget.Width + Position.X, Position.Y + RenderTarget.Height - mouseState.Y / RenderLocation.Height * RenderTarget.Height);
return new((mouseState.X - RenderLocation.X) / RenderLocation.Width * RenderTarget.Width + Position.X, Position.Y + RenderTarget.Height -
(mouseState.Y - RenderLocation.Y) / RenderLocation.Height * RenderTarget.Height);
}
}
14 changes: 12 additions & 2 deletions Blocktest/Code/Rendering/Renderable.cs
Original file line number Diff line number Diff line change
@@ -1,12 +1,22 @@
namespace Blocktest.Rendering;
namespace Blocktest.Rendering;

public enum Layer {
Top = 0,
Player = 1,
Default = 2,
ForegroundBlocks = 3,
BackgroundBlocks = 4
}

public sealed class Renderable {
public readonly Transform Transform;
public Drawable? Appearance;
public Color RenderColor;
public Layer Layer;

public Renderable(Transform transform, Drawable? appearance = null, Color? renderColor = null) {
public Renderable(Transform transform, Layer layer = Layer.Default, Drawable? appearance = null, Color? renderColor = null) {
Transform = transform;
Layer = layer;
Appearance = appearance;
RenderColor = renderColor ?? Color.White;
}
Expand Down
6 changes: 3 additions & 3 deletions Blocktest/Code/Rendering/Transform.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,12 +4,12 @@ public sealed class Transform {
public Vector2 Position;
public float Rotation;
public Vector2 Scale;
public Vector2 Origin;

public Transform(Vector2 position, Vector2? scale = null, float rotation = 0) {
public Transform(Vector2 position, Vector2? scale = null, float rotation = 0, Vector2? origin = null) {
Position = position;
Scale = scale ?? Vector2.One;
Rotation = rotation;
Origin = origin ?? Vector2.Zero;
}

public Vector2 Origin => new(Scale.X / 2, Scale.Y / 2);
}
57 changes: 40 additions & 17 deletions Blocktest/Code/Scenes/GameScene.cs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ public class GameScene : Scene {
private Camera _camera;

public void Update(GameTime gameTime) {
//for block placement
MouseState mouseState = Mouse.GetState();
KeyboardState keyState = Keyboard.GetState();

Expand Down Expand Up @@ -47,24 +46,29 @@ public void Update(GameTime gameTime) {

latchBlockSelect = true;
}

var moveValue = 2.5f;
if (keyState.IsKeyDown(Keys.LeftShift) || keyState.IsKeyDown(Keys.RightShift)) {
moveValue *= 4;
}

if (keyState.IsKeyDown(Keys.A)) {
_camera.Position.X -= 2f;
_camera.Position.X -= moveValue;
} else if (keyState.IsKeyDown(Keys.D)) {
_camera.Position.X += 2f;
_camera.Position.X += moveValue;
}

if (keyState.IsKeyDown(Keys.W)) {
_camera.Position.Y += 2f;
_camera.Position.Y += moveValue;
} else if (keyState.IsKeyDown(Keys.S)) {
_camera.Position.Y -= 2f;
_camera.Position.Y -= moveValue;
}

if (!_camera.RenderLocation.Contains(mouseState.Position)) {
return;
}

Vector2 mousePos = _camera.CameraToWorldPos(new(mouseState.X, mouseState.Y));
var mousePos = _camera.CameraToWorldPos(new(mouseState.X, mouseState.Y));
//build and destroy mode
if (buildMode)
{
Expand Down Expand Up @@ -98,7 +102,33 @@ public void Draw(GameTime gameTime, GraphicsDevice graphicsDevice) {


_camera.Draw(graphicsDevice, _spriteBatch);

const bool pixelPerfect = false;

var destinationRectangle = pixelPerfect ? GetPixelPerfectRect() : GetFitRect();
_camera.RenderLocation = destinationRectangle;

graphicsDevice.Clear(Color.DarkGray);

_spriteBatch.Begin(samplerState: pixelPerfect ? SamplerState.PointClamp : null);
_spriteBatch.Draw(_camera.RenderTarget, destinationRectangle, Color.White);
_spriteBatch.End();
}

private Rectangle GetPixelPerfectRect() {
int multiplier = int.Min(_game.GraphicsDevice.Viewport.Height / _camera.RenderTarget.Height,
_game.GraphicsDevice.Viewport.Width / _camera.RenderTarget.Width);

int width = _camera.RenderTarget.Width * multiplier;
int height = _camera.RenderTarget.Height * multiplier;

int x = (_game.GraphicsDevice.Viewport.Width - width) / 2;
int y = (_game.GraphicsDevice.Viewport.Height - height) / 2;

return new Rectangle(x, y, width, height);
}

private Rectangle GetFitRect() {
float aspectRatio = (float)_game.GraphicsDevice.Viewport.Width / _game.GraphicsDevice.Viewport.Height;
float renderTargetAspectRatio = (float)_camera.RenderTarget.Width / _camera.RenderTarget.Height;

Expand All @@ -111,26 +141,19 @@ public void Draw(GameTime gameTime, GraphicsDevice graphicsDevice) {
width = _game.GraphicsDevice.Viewport.Width;
height = (int)(_game.GraphicsDevice.Viewport.Width / renderTargetAspectRatio);
}

int x = (_game.GraphicsDevice.Viewport.Width - width) / 2;
int y = (_game.GraphicsDevice.Viewport.Height - height) / 2;

Rectangle destinationRectangle = new(x, y, width, height);
_camera.RenderLocation = destinationRectangle;

graphicsDevice.Clear(Color.DarkGray);

_spriteBatch.Begin();
_spriteBatch.Draw(_camera.RenderTarget, destinationRectangle, Color.White);
_spriteBatch.End();

return new Rectangle(x, y, width, height);
}



public GameScene(BlocktestGame game) {
_spriteBatch = new SpriteBatch(game.GraphicsDevice);
_game = game;
_camera = new Camera(new Vector2(0, 0), new Vector2(534, 300), _game.GraphicsDevice);
_camera = new Camera(new Vector2(0, 0), new Vector2(512, 256), _game.GraphicsDevice);

Globals.BackgroundTilemap = new Tilemap(Globals.maxX, Globals.maxY, _camera);
Globals.ForegroundTilemap = new Tilemap(Globals.maxX, Globals.maxY, _camera);
Expand Down

0 comments on commit 81d1214

Please sign in to comment.