From f1cb6fe9eca26b2f64b973d95a50c88337361392 Mon Sep 17 00:00:00 2001 From: Evan Hemsley Date: Sat, 11 Jul 2020 18:25:43 -0700 Subject: [PATCH] initial paddle drawing --- .gitignore | 2 ++ .gitmodules | 3 ++ MoonTools.Structs | 1 + PongFE/Components/PositionComponent.cs | 15 ++++++++++ PongFE/Components/Texture2DComponent.cs | 17 +++++++++++ PongFE/Extensions/Position2DExtensions.cs | 12 ++++++++ PongFE/PongFE.Core.csproj | 1 + PongFE/PongFE.Framework.csproj | 1 + PongFE/PongFEGame.cs | 28 +++++++++++++++++- PongFE/Program.cs | 2 +- PongFE/Renderers/Texture2DRenderer.cs | 35 +++++++++++++++++++++++ 11 files changed, 115 insertions(+), 2 deletions(-) create mode 160000 MoonTools.Structs create mode 100644 PongFE/Components/PositionComponent.cs create mode 100644 PongFE/Components/Texture2DComponent.cs create mode 100644 PongFE/Extensions/Position2DExtensions.cs create mode 100644 PongFE/Renderers/Texture2DRenderer.cs diff --git a/.gitignore b/.gitignore index e191009..0678386 100644 --- a/.gitignore +++ b/.gitignore @@ -3,6 +3,8 @@ ## ## Get latest from https://github.com/github/gitignore/blob/master/VisualStudio.gitignore +fnalibs/ + # User-specific files *.rsuser *.suo diff --git a/.gitmodules b/.gitmodules index dfa0e5d..2059675 100644 --- a/.gitmodules +++ b/.gitmodules @@ -4,3 +4,6 @@ [submodule "encompass-cs"] path = encompass-cs url = https://gitea.moonside.games/MoonsideGames/encompass-cs.git +[submodule "MoonTools.Structs"] + path = MoonTools.Structs + url = git@gitea.moonside.games:MoonsideGames/MoonTools.Structs.git diff --git a/MoonTools.Structs b/MoonTools.Structs new file mode 160000 index 0000000..a3a5803 --- /dev/null +++ b/MoonTools.Structs @@ -0,0 +1 @@ +Subproject commit a3a5803961d5f172a2376f7b0451ba21bd5842b6 diff --git a/PongFE/Components/PositionComponent.cs b/PongFE/Components/PositionComponent.cs new file mode 100644 index 0000000..f5cd870 --- /dev/null +++ b/PongFE/Components/PositionComponent.cs @@ -0,0 +1,15 @@ +using Encompass; +using MoonTools.Structs; + +namespace PongFE.Components +{ + public struct PositionComponent : IComponent + { + public Position2D Position { get; } + + public PositionComponent(Position2D position) + { + Position = position; + } + } +} diff --git a/PongFE/Components/Texture2DComponent.cs b/PongFE/Components/Texture2DComponent.cs new file mode 100644 index 0000000..99f3fad --- /dev/null +++ b/PongFE/Components/Texture2DComponent.cs @@ -0,0 +1,17 @@ +using Encompass; +using Microsoft.Xna.Framework.Graphics; + +namespace PongFE.Components +{ + public struct Texture2DComponent : IComponent, IDrawableComponent + { + public Texture2D Texture { get; } + public int Layer { get; } + + public Texture2DComponent(Texture2D texture, int layer) + { + Texture = texture; + Layer = layer; + } + } +} diff --git a/PongFE/Extensions/Position2DExtensions.cs b/PongFE/Extensions/Position2DExtensions.cs new file mode 100644 index 0000000..29a3331 --- /dev/null +++ b/PongFE/Extensions/Position2DExtensions.cs @@ -0,0 +1,12 @@ +using MoonTools.Structs; + +namespace PongFE.Extensions +{ + public static class Position2DExtensions + { + public static Microsoft.Xna.Framework.Vector2 ToXNAVector(this Position2D position) + { + return new Microsoft.Xna.Framework.Vector2(position.X, position.Y); + } + } +} diff --git a/PongFE/PongFE.Core.csproj b/PongFE/PongFE.Core.csproj index 046271a..5929c87 100644 --- a/PongFE/PongFE.Core.csproj +++ b/PongFE/PongFE.Core.csproj @@ -25,6 +25,7 @@ + diff --git a/PongFE/PongFE.Framework.csproj b/PongFE/PongFE.Framework.csproj index a36a048..96aac9b 100644 --- a/PongFE/PongFE.Framework.csproj +++ b/PongFE/PongFE.Framework.csproj @@ -28,6 +28,7 @@ + diff --git a/PongFE/PongFEGame.cs b/PongFE/PongFEGame.cs index 8308a19..e17cc71 100644 --- a/PongFE/PongFEGame.cs +++ b/PongFE/PongFEGame.cs @@ -1,6 +1,8 @@ using Encompass; using Microsoft.Xna.Framework; using Microsoft.Xna.Framework.Graphics; +using PongFE.Components; +using PongFE.Renderers; namespace PongFE { @@ -11,6 +13,10 @@ namespace PongFE WorldBuilder WorldBuilder { get; } = new WorldBuilder(); World World { get; set; } + SpriteBatch SpriteBatch { get; set; } + Texture2D WhitePixel { get; set; } + RenderTarget2D PaddleTexture { get; set; } + public PongFEGame() { graphics = new GraphicsDeviceManager(this); @@ -25,6 +31,24 @@ namespace PongFE protected override void LoadContent() { + SpriteBatch = new SpriteBatch(GraphicsDevice); + + WhitePixel = new Texture2D(GraphicsDevice, 1, 1); + WhitePixel.SetData(new Color[] { Color.White }); + + PaddleTexture = new RenderTarget2D(GraphicsDevice, 20, 80); + GraphicsDevice.SetRenderTarget(PaddleTexture); + SpriteBatch.Begin(); + SpriteBatch.Draw(WhitePixel, new Rectangle(0, 0, 20, 80), Color.White); + SpriteBatch.End(); + GraphicsDevice.SetRenderTarget(null); + + WorldBuilder.AddOrderedRenderer(new Texture2DRenderer(SpriteBatch)); + + var paddle = WorldBuilder.CreateEntity(); + WorldBuilder.SetComponent(paddle, new PositionComponent(new MoonTools.Structs.Position2D(5, 5))); + WorldBuilder.SetComponent(paddle, new Texture2DComponent(PaddleTexture, 0)); + World = WorldBuilder.Build(); } @@ -42,9 +66,11 @@ namespace PongFE protected override void Draw(GameTime gameTime) { - GraphicsDevice.Clear(Color.CornflowerBlue); + GraphicsDevice.Clear(Color.Black); + SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied); World.Draw(); + SpriteBatch.End(); base.Draw(gameTime); } diff --git a/PongFE/Program.cs b/PongFE/Program.cs index 3b36742..053f583 100644 --- a/PongFE/Program.cs +++ b/PongFE/Program.cs @@ -28,7 +28,7 @@ namespace PongFE } // https://github.com/FNA-XNA/FNA/wiki/7:-FNA-Environment-Variables#fna_graphics_enable_highdpi - // NOTE: from documentation: + // NOTE: from documentation: // Lastly, when packaging for macOS, be sure this is in your app bundle's Info.plist: // NSHighResolutionCapable // True diff --git a/PongFE/Renderers/Texture2DRenderer.cs b/PongFE/Renderers/Texture2DRenderer.cs new file mode 100644 index 0000000..5faf5bf --- /dev/null +++ b/PongFE/Renderers/Texture2DRenderer.cs @@ -0,0 +1,35 @@ +using Encompass; +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using PongFE.Components; +using PongFE.Extensions; + +namespace PongFE.Renderers +{ + public class Texture2DRenderer : OrderedRenderer + { + private readonly SpriteBatch _spriteBatch; + + public Texture2DRenderer(SpriteBatch spriteBatch) + { + _spriteBatch = spriteBatch; + } + + public override void Render(Entity entity, in Texture2DComponent textureComponent) + { + ref readonly var positionComponent = ref GetComponent(entity); + + _spriteBatch.Draw( + textureComponent.Texture, + positionComponent.Position.Truncated().ToXNAVector(), + null, + Color.White, + 0, + Vector2.Zero, + Vector2.One, + SpriteEffects.None, + 0 + ); + } + } +}