PongFE/PongFE/PongFEGame.cs

79 lines
2.4 KiB
C#

using Encompass;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Graphics;
using PongFE.Components;
using PongFE.Renderers;
namespace PongFE
{
class PongFEGame : Game
{
GraphicsDeviceManager graphics;
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);
graphics.PreferredBackBufferWidth = 1280;
graphics.PreferredBackBufferHeight = 720;
graphics.PreferMultiSampling = true;
Content.RootDirectory = "Content";
Window.AllowUserResizing = true;
IsMouseVisible = true;
}
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();
}
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)
{
GraphicsDevice.Clear(Color.Black);
SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied);
World.Draw();
SpriteBatch.End();
base.Draw(gameTime);
}
}
}