PongFE/PongFE/PongFEGame.cs

79 lines
2.4 KiB
C#
Raw Normal View History

2020-07-11 21:36:18 +00:00
using Encompass;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
2020-07-12 01:25:43 +00:00
using PongFE.Components;
using PongFE.Renderers;
2020-07-11 21:36:18 +00:00
namespace PongFE
{
class PongFEGame : Game
{
GraphicsDeviceManager graphics;
WorldBuilder WorldBuilder { get; } = new WorldBuilder();
World World { get; set; }
2020-07-12 01:25:43 +00:00
SpriteBatch SpriteBatch { get; set; }
Texture2D WhitePixel { get; set; }
RenderTarget2D PaddleTexture { get; set; }
2020-07-11 21:36:18 +00:00
public PongFEGame()
{
graphics = new GraphicsDeviceManager(this);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
graphics.PreferMultiSampling = true;
Content.RootDirectory = "Content";
Window.AllowUserResizing = true;
IsMouseVisible = true;
}
protected override void LoadContent()
{
2020-07-12 01:25:43 +00:00
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));
2020-07-11 21:36:18 +00:00
World = WorldBuilder.Build();
}
protected override void UnloadContent()
{
base.UnloadContent();
}
protected override void Update(GameTime gameTime)
{
World.Update(gameTime.ElapsedGameTime.TotalSeconds);
base.Update(gameTime);
}
protected override void Draw(GameTime gameTime)
{
2020-07-12 01:25:43 +00:00
GraphicsDevice.Clear(Color.Black);
2020-07-11 21:36:18 +00:00
2020-07-12 01:25:43 +00:00
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
2020-07-11 21:36:18 +00:00
World.Draw();
2020-07-12 01:25:43 +00:00
SpriteBatch.End();
2020-07-11 21:36:18 +00:00
base.Draw(gameTime);
}
}
}