-
Notifications
You must be signed in to change notification settings - Fork 7
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
a714c3a
commit 55cbea5
Showing
7 changed files
with
186 additions
and
125 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
using System.Collections; | ||
namespace Blocktest.Rendering; | ||
|
||
public sealed class Camera { | ||
private readonly Color _backgroundColor; | ||
private readonly Vector2 _size; | ||
|
||
public readonly List<Renderable> RenderedComponents = new(); | ||
|
||
public Rectangle RenderLocation; | ||
public readonly RenderTarget2D RenderTarget; | ||
public Vector2 Position; | ||
|
||
public Camera(Vector2 position, Vector2 size, GraphicsDevice graphicsDevice, Color? backgroundColor = null) { | ||
Position = position; | ||
_size = size; | ||
_backgroundColor = backgroundColor ?? Color.CornflowerBlue; | ||
RenderTarget = new RenderTarget2D(graphicsDevice, (int)size.X, (int)size.Y, false, SurfaceFormat.Color, DepthFormat.None, 0, RenderTargetUsage.DiscardContents); | ||
} | ||
|
||
public void Draw(GraphicsDevice graphics, SpriteBatch spriteBatch) { | ||
graphics.SetRenderTarget(RenderTarget); | ||
graphics.Clear(_backgroundColor); | ||
|
||
spriteBatch.Begin(); | ||
|
||
foreach (Renderable component in RenderedComponents) { | ||
if (component.Appearance == null) { | ||
continue; | ||
} | ||
|
||
Vector2 worldPosition = component.Transform.Position; | ||
Vector2 cameraPosition = worldPosition - Position; | ||
|
||
if (worldPosition.X + component.Appearance.Bounds.Width < Position.X && | ||
worldPosition.X > Position.X + _size.X && | ||
worldPosition.Y + component.Appearance.Bounds.Height < Position.Y && | ||
worldPosition.Y > Position.Y + _size.Y) { | ||
continue; | ||
} | ||
|
||
Vector2 flippedPosition = new(cameraPosition.X, RenderTarget.Height - cameraPosition.Y - component.Appearance.Bounds.Height); | ||
|
||
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); | ||
} | ||
|
||
spriteBatch.End(); | ||
|
||
graphics.SetRenderTarget(null); | ||
} | ||
|
||
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); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
namespace Blocktest.Rendering; | ||
|
||
public sealed class Renderable { | ||
public readonly Transform Transform; | ||
public Drawable? Appearance; | ||
public Color RenderColor; | ||
|
||
public Renderable(Transform transform, Drawable? appearance = null, Color? renderColor = null) { | ||
Transform = transform; | ||
Appearance = appearance; | ||
RenderColor = renderColor ?? Color.White; | ||
} | ||
|
||
public void Draw(SpriteBatch spriteBatch, Vector2 cameraPosition) { | ||
if (Appearance == null) { | ||
return; | ||
} | ||
|
||
spriteBatch.Draw(Appearance.Texture, Transform.Position - cameraPosition, Appearance.Bounds, RenderColor, Transform.Rotation, Transform.Origin, Transform.Scale, | ||
SpriteEffects.None, 0); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
namespace Blocktest.Rendering; | ||
|
||
public sealed class Transform { | ||
public Vector2 Position; | ||
public float Rotation; | ||
public Vector2 Scale; | ||
|
||
public Transform(Vector2 position, Vector2? scale = null, float rotation = 0) { | ||
Position = position; | ||
Scale = scale ?? Vector2.One; | ||
Rotation = rotation; | ||
} | ||
|
||
public Vector2 Origin => new(Scale.X / 2, Scale.Y / 2); | ||
} |
Oops, something went wrong.