-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #256 from Thraka/develop
An old example of creating a monogame game and adding sadconsole to it
- Loading branch information
Showing
1 changed file
with
169 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,169 @@ | ||
using Microsoft.Xna.Framework; | ||
using Microsoft.Xna.Framework.Graphics; | ||
using MonoGame.Extended.Screens; | ||
using System; | ||
using System.Collections.Generic; | ||
using System.Linq; | ||
using System.Text; | ||
using System.Threading.Tasks; | ||
|
||
namespace ConsoleApp8 | ||
{ | ||
public class SadConsoleScreen : GameScreen | ||
{ | ||
// How many columns/rows the screen for sadcosnole will display at normal font size | ||
public const int WidthCellCount = 80; | ||
public const int HeightCellCount = 25; | ||
|
||
SpriteBatch spriteBatch; | ||
|
||
public SadConsoleScreen(Game game) | ||
: base(game) | ||
{ | ||
// Basics used for rendering | ||
SadConsole.Global.GraphicsDevice = GraphicsDevice; | ||
SadConsole.Global.SpriteBatch = new Microsoft.Xna.Framework.Graphics.SpriteBatch(GraphicsDevice); | ||
|
||
// Load the default font. This is an internal method so use reflection | ||
// SadConsole.Global.LoadEmbeddedFont(); | ||
if (SadConsole.Global.Fonts.Count == 0) | ||
typeof(SadConsole.Global).InvokeMember("LoadEmbeddedFont", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.NonPublic, null, null, null); | ||
|
||
SadConsole.Global.RenderWidth = WidthCellCount * SadConsole.Global.FontDefault.Size.X; | ||
SadConsole.Global.RenderHeight = HeightCellCount * SadConsole.Global.FontDefault.Size.Y; | ||
SadConsole.Global.RenderOutput = new RenderTarget2D(GraphicsDevice, SadConsole.Global.RenderWidth, SadConsole.Global.RenderHeight); | ||
SadConsole.Global.RenderRect = new Rectangle(0, 0, SadConsole.Global.RenderWidth, SadConsole.Global.RenderHeight); | ||
SadConsole.Global.RenderScale = new Vector2(1); | ||
|
||
spriteBatch = new SpriteBatch(GraphicsDevice); | ||
} | ||
|
||
public override void Initialize() | ||
{ | ||
//var startingConsole = new TestConsole(); | ||
var startingConsole = new SadConsole.Console(10, 10); | ||
startingConsole.FillWithRandomGarbage(); | ||
SadConsole.Global.CurrentScreen = startingConsole; | ||
|
||
base.Initialize(); | ||
} | ||
|
||
public override void LoadContent() | ||
{ | ||
base.LoadContent(); | ||
} | ||
|
||
public override void Draw(GameTime gameTime) | ||
{ | ||
if (SadConsole.Settings.DoDraw) | ||
{ | ||
SadConsole.Global.GameTimeRender = gameTime; | ||
SadConsole.Global.GameTimeElapsedRender = gameTime.ElapsedGameTime.TotalSeconds; | ||
|
||
// Clear draw calls for next run | ||
SadConsole.Global.DrawCalls.Clear(); | ||
|
||
// Make sure all items in the screen are drawn. (Build a list of draw calls) | ||
SadConsole.Global.CurrentScreen?.Draw(gameTime.ElapsedGameTime); | ||
|
||
// Comment this out as it's global logic used by a standalone sadconsole game. You really can just add your own logic here. Or uncomment it and use it. | ||
//SadConsole.Game.OnDraw?.Invoke(gameTime); | ||
|
||
// Render to the global output texture | ||
GraphicsDevice.SetRenderTarget(SadConsole.Global.RenderOutput); | ||
GraphicsDevice.Clear(SadConsole.Settings.ClearColor); | ||
|
||
// Render each draw call | ||
SadConsole.Global.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.DepthRead, RasterizerState.CullNone); | ||
foreach (SadConsole.DrawCalls.IDrawCall call in SadConsole.Global.DrawCalls) | ||
{ | ||
call.Draw(); | ||
} | ||
|
||
SadConsole.Global.SpriteBatch.End(); | ||
GraphicsDevice.SetRenderTarget(null); | ||
|
||
// If we're going to draw to the screen, do it. | ||
if (SadConsole.Settings.DoFinalDraw) | ||
{ | ||
// This is the original draw to screen code from SadConsole | ||
//Global.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.DepthRead, RasterizerState.CullNone); | ||
//Global.SpriteBatch.Draw(Global.RenderOutput, Global.RenderRect, Color.White); | ||
//Global.SpriteBatch.End(); | ||
|
||
// This is the code you wrote but modified to draw the output texture from SadConsole. | ||
GraphicsDevice.Clear(Color.Black); | ||
|
||
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend, | ||
SamplerState.LinearClamp, DepthStencilState.Default, | ||
RasterizerState.CullNone); | ||
|
||
spriteBatch.Draw(SadConsole.Global.RenderOutput, new Rectangle(0, 0, 300, 100), Color.Red); | ||
|
||
spriteBatch.End(); | ||
} | ||
} | ||
|
||
|
||
|
||
} | ||
|
||
public override void Update(GameTime gameTime) | ||
{ | ||
if (SadConsole.Settings.DoUpdate) | ||
{ | ||
SadConsole.Global.GameTimeUpdate = gameTime; | ||
SadConsole.Global.GameTimeElapsedUpdate = gameTime.ElapsedGameTime.TotalSeconds; | ||
|
||
if (Game.IsActive) | ||
{ | ||
if (SadConsole.Settings.Input.DoKeyboard) | ||
{ | ||
SadConsole.Global.KeyboardState.Update(gameTime); | ||
SadConsole.Global.KeyboardState.Process(); | ||
} | ||
|
||
if (SadConsole.Settings.Input.DoMouse) | ||
{ | ||
SadConsole.Global.MouseState.Update(gameTime); | ||
SadConsole.Global.MouseState.Process(); | ||
} | ||
} | ||
|
||
SadConsole.Global.CurrentScreen?.Update(gameTime.ElapsedGameTime); | ||
|
||
// Comment this out as it's global logic used by a standalone sadconsole game. You really can just add your own logic here. Or uncomment it and use it. | ||
//SadConsole.Game.OnUpdate?.Invoke(gameTime); | ||
} | ||
} | ||
|
||
class Program | ||
{ | ||
static void Main(string[] args) | ||
{ | ||
new MonoGameGame().Run(); | ||
} | ||
} | ||
|
||
// My test game to make this code work. you didn't provide this so I came up with this :) | ||
class MonoGameGame : Microsoft.Xna.Framework.Game | ||
{ | ||
public GraphicsDeviceManager GraphicsDeviceManager; | ||
|
||
public MonoGameGame() | ||
{ | ||
GraphicsDeviceManager = new GraphicsDeviceManager(this); | ||
} | ||
|
||
protected override void Initialize() | ||
{ | ||
base.Initialize(); | ||
|
||
ScreenManager manager = new ScreenManager(); | ||
manager.LoadScreen(new SadConsoleScreen(this)); | ||
Components.Add(manager); | ||
|
||
} | ||
} | ||
} | ||
} |