From 53acf105e741e46655861a623f1876b61d6358d0 Mon Sep 17 00:00:00 2001 From: Andy De George Date: Sun, 7 Feb 2021 09:38:04 -0800 Subject: [PATCH] An old example of creating a monogame game and adding sadconsole to it --- monogame with sadconsole example.txt | 169 +++++++++++++++++++++++++++ 1 file changed, 169 insertions(+) create mode 100644 monogame with sadconsole example.txt diff --git a/monogame with sadconsole example.txt b/monogame with sadconsole example.txt new file mode 100644 index 000000000..6cb1d83b0 --- /dev/null +++ b/monogame with sadconsole example.txt @@ -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); + + } + } + } +} \ No newline at end of file