Split draw and game loop
added some comments
This commit is contained in:
+29
-18
@@ -8,23 +8,29 @@ namespace Raster3D.Engine
|
||||
{
|
||||
public class Engine : Game
|
||||
{
|
||||
// init the GDM and SpriteBatch systems
|
||||
private GraphicsDeviceManager _graphics;
|
||||
private SpriteBatch _spriteBatch;
|
||||
|
||||
public Pixel[] pixels;
|
||||
public Renderer renderer = new Renderer();
|
||||
public IRunnable TargetApplication;
|
||||
|
||||
public CGameLoop loop;
|
||||
// init some internal engine values
|
||||
public Pixel[] pixels; // the pixel array, RenderObjects directly modify this
|
||||
public Renderer renderer = new Renderer(); // The rendering pipeline, while XNA has its own, we are using a "subset" of that to directly render pixels
|
||||
public IRunnable TargetApplication; // the reference to the target application, passed into the constructor
|
||||
|
||||
public CGameLoop gameLoop; // the GameLoop, which is ran in the update
|
||||
public CDrawLoop drawLoop; // this is the Drawing loop, this is where you would put all of your rendering code
|
||||
|
||||
|
||||
public Engine(IRunnable Application, CGameLoop EngineLoop)
|
||||
|
||||
public Engine(IRunnable Application, CGameLoop EngineLoop, CDrawLoop DrawLoop)
|
||||
{
|
||||
_graphics = new GraphicsDeviceManager(this);
|
||||
Content.RootDirectory = "Content";
|
||||
_graphics = new GraphicsDeviceManager(this); // init GDM
|
||||
Content.RootDirectory = "Content"; // set content dir
|
||||
IsMouseVisible = true;
|
||||
TargetApplication = Application;
|
||||
loop = EngineLoop;
|
||||
gameLoop = EngineLoop;
|
||||
drawLoop = DrawLoop;
|
||||
}
|
||||
|
||||
|
||||
@@ -33,20 +39,25 @@ namespace Raster3D.Engine
|
||||
{
|
||||
// Allocate pixel array based on current viewport size
|
||||
pixels = new Pixel[GraphicsDevice.Viewport.Width * GraphicsDevice.Viewport.Height];
|
||||
Window.Title = TargetApplication.WindowTitle;
|
||||
Window.Title = TargetApplication.WindowTitle; // set the window title
|
||||
base.Initialize();
|
||||
|
||||
}
|
||||
|
||||
protected override void LoadContent()
|
||||
{
|
||||
// initaialixze the spritebatch
|
||||
_spriteBatch = new SpriteBatch(GraphicsDevice);
|
||||
}
|
||||
|
||||
protected override void Update(GameTime gameTime)
|
||||
{
|
||||
if (GamePad.GetState(PlayerIndex.One).Buttons.Back == ButtonState.Pressed || Keyboard.GetState().IsKeyDown(Keys.Escape))
|
||||
Exit();
|
||||
// exit function
|
||||
|
||||
if (gameLoop != null)
|
||||
{
|
||||
gameLoop.Update(this, gameTime); // call the gameloop update
|
||||
}
|
||||
|
||||
base.Update(gameTime);
|
||||
}
|
||||
@@ -54,18 +65,18 @@ namespace Raster3D.Engine
|
||||
protected override void Draw(GameTime gameTime)
|
||||
{
|
||||
|
||||
if (loop != null)
|
||||
if (drawLoop != null)
|
||||
{
|
||||
loop.exec(this);
|
||||
drawLoop.Draw(this, gameTime);
|
||||
}
|
||||
|
||||
|
||||
GraphicsDevice.Clear(Color.Black);
|
||||
_spriteBatch.Begin();
|
||||
renderer.RenderFrame(pixels, _spriteBatch, GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height);
|
||||
_spriteBatch.End();
|
||||
GraphicsDevice.Clear(Color.Black); // Clear the GPU buffer
|
||||
_spriteBatch.Begin(); // start spriteBatch
|
||||
renderer.RenderFrame(pixels, _spriteBatch, GraphicsDevice, GraphicsDevice.Viewport.Width, GraphicsDevice.Viewport.Height); // render teh frame
|
||||
_spriteBatch.End(); // end the batch
|
||||
|
||||
base.Draw(gameTime);
|
||||
base.Draw(gameTime); // call XNA internal draw call
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,17 @@
|
||||
using Microsoft.Xna.Framework;
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
|
||||
namespace Raster3D.Engine.Impl
|
||||
{
|
||||
public class CDrawLoop
|
||||
{
|
||||
public void Draw(Engine engine, GameTime time)
|
||||
{
|
||||
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -3,12 +3,13 @@ using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Microsoft.Xna.Framework;
|
||||
using Raster3D.Engine;
|
||||
|
||||
namespace Raster3D.Engine.Impl
|
||||
{
|
||||
public abstract class CGameLoop
|
||||
{
|
||||
public abstract void exec(Engine engine);
|
||||
public abstract void Update(Engine engine, GameTime time);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,78 +0,0 @@
|
||||
using System;
|
||||
using System.Collections.Generic;
|
||||
using System.Linq;
|
||||
using System.Text;
|
||||
using System.Threading.Tasks;
|
||||
using Raster3D.Engine.Graphics;
|
||||
|
||||
namespace Raster3D.Engine
|
||||
{
|
||||
public class OObjects
|
||||
{
|
||||
public void Line(Vec2D pos1, Vec2D pos2, rgb clr, Engine engine)
|
||||
{
|
||||
int x0 = (int)pos1.X;
|
||||
int y0 = (int)pos1.Y;
|
||||
int x1 = (int)pos2.X;
|
||||
int y1 = (int)pos2.Y;
|
||||
|
||||
int dx = Math.Abs(x1 - x0);
|
||||
int dy = Math.Abs(y1 - y0);
|
||||
|
||||
int sx = x0 < x1 ? 1 : -1;
|
||||
int sy = y0 < y1 ? 1 : -1;
|
||||
|
||||
int err = dx - dy;
|
||||
|
||||
int screenWidth = engine.GraphicsDevice.Viewport.Width;
|
||||
int screenHeight = engine.GraphicsDevice.Viewport.Height;
|
||||
|
||||
while (true)
|
||||
{
|
||||
if (x0 >= 0 && x0 < screenWidth && y0 >= 0 && y0 < screenHeight)
|
||||
{
|
||||
int index = (y0 * screenWidth) + x0;
|
||||
engine.pixels[index] = new Pixel(x0, y0, clr);
|
||||
}
|
||||
|
||||
if (x0 == x1 && y0 == y1) break;
|
||||
|
||||
int e2 = 2 * err;
|
||||
|
||||
if (e2 > -dy)
|
||||
{
|
||||
err -= dy;
|
||||
x0 += sx;
|
||||
}
|
||||
|
||||
if (e2 < dx)
|
||||
{
|
||||
err += dx;
|
||||
y0 += sy;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Triangle(Vec2D[] positions, rgb[] colors, Engine engine)
|
||||
{
|
||||
// positions should have a length of 3 and exactly 3
|
||||
// colors has 3 points
|
||||
|
||||
// this just draws an array of lines
|
||||
|
||||
// line 1
|
||||
Line(positions[0], positions[1], colors[0], engine); // pos1 - pos2
|
||||
|
||||
// line 2
|
||||
Line(positions[1], positions[2], colors[1], engine); // pos2 - pos3
|
||||
|
||||
// line 3
|
||||
Line(positions[2], positions[0], colors[2], engine); // pos3 - back to pos1
|
||||
}
|
||||
|
||||
public void Square(Vec2D[] points, rgb[] colors, Engine engine)
|
||||
{
|
||||
// TODO: implemen t this
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user