Skip to content

Commit 105c224

Browse files
authored
Merge pull request #256 from Thraka/develop
An old example of creating a monogame game and adding sadconsole to it
2 parents ec2e117 + 53acf10 commit 105c224

File tree

1 file changed

+169
-0
lines changed

1 file changed

+169
-0
lines changed
Lines changed: 169 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,169 @@
1+
using Microsoft.Xna.Framework;
2+
using Microsoft.Xna.Framework.Graphics;
3+
using MonoGame.Extended.Screens;
4+
using System;
5+
using System.Collections.Generic;
6+
using System.Linq;
7+
using System.Text;
8+
using System.Threading.Tasks;
9+
10+
namespace ConsoleApp8
11+
{
12+
public class SadConsoleScreen : GameScreen
13+
{
14+
// How many columns/rows the screen for sadcosnole will display at normal font size
15+
public const int WidthCellCount = 80;
16+
public const int HeightCellCount = 25;
17+
18+
SpriteBatch spriteBatch;
19+
20+
public SadConsoleScreen(Game game)
21+
: base(game)
22+
{
23+
// Basics used for rendering
24+
SadConsole.Global.GraphicsDevice = GraphicsDevice;
25+
SadConsole.Global.SpriteBatch = new Microsoft.Xna.Framework.Graphics.SpriteBatch(GraphicsDevice);
26+
27+
// Load the default font. This is an internal method so use reflection
28+
// SadConsole.Global.LoadEmbeddedFont();
29+
if (SadConsole.Global.Fonts.Count == 0)
30+
typeof(SadConsole.Global).InvokeMember("LoadEmbeddedFont", System.Reflection.BindingFlags.Static | System.Reflection.BindingFlags.InvokeMethod | System.Reflection.BindingFlags.NonPublic, null, null, null);
31+
32+
SadConsole.Global.RenderWidth = WidthCellCount * SadConsole.Global.FontDefault.Size.X;
33+
SadConsole.Global.RenderHeight = HeightCellCount * SadConsole.Global.FontDefault.Size.Y;
34+
SadConsole.Global.RenderOutput = new RenderTarget2D(GraphicsDevice, SadConsole.Global.RenderWidth, SadConsole.Global.RenderHeight);
35+
SadConsole.Global.RenderRect = new Rectangle(0, 0, SadConsole.Global.RenderWidth, SadConsole.Global.RenderHeight);
36+
SadConsole.Global.RenderScale = new Vector2(1);
37+
38+
spriteBatch = new SpriteBatch(GraphicsDevice);
39+
}
40+
41+
public override void Initialize()
42+
{
43+
//var startingConsole = new TestConsole();
44+
var startingConsole = new SadConsole.Console(10, 10);
45+
startingConsole.FillWithRandomGarbage();
46+
SadConsole.Global.CurrentScreen = startingConsole;
47+
48+
base.Initialize();
49+
}
50+
51+
public override void LoadContent()
52+
{
53+
base.LoadContent();
54+
}
55+
56+
public override void Draw(GameTime gameTime)
57+
{
58+
if (SadConsole.Settings.DoDraw)
59+
{
60+
SadConsole.Global.GameTimeRender = gameTime;
61+
SadConsole.Global.GameTimeElapsedRender = gameTime.ElapsedGameTime.TotalSeconds;
62+
63+
// Clear draw calls for next run
64+
SadConsole.Global.DrawCalls.Clear();
65+
66+
// Make sure all items in the screen are drawn. (Build a list of draw calls)
67+
SadConsole.Global.CurrentScreen?.Draw(gameTime.ElapsedGameTime);
68+
69+
// 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.
70+
//SadConsole.Game.OnDraw?.Invoke(gameTime);
71+
72+
// Render to the global output texture
73+
GraphicsDevice.SetRenderTarget(SadConsole.Global.RenderOutput);
74+
GraphicsDevice.Clear(SadConsole.Settings.ClearColor);
75+
76+
// Render each draw call
77+
SadConsole.Global.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.DepthRead, RasterizerState.CullNone);
78+
foreach (SadConsole.DrawCalls.IDrawCall call in SadConsole.Global.DrawCalls)
79+
{
80+
call.Draw();
81+
}
82+
83+
SadConsole.Global.SpriteBatch.End();
84+
GraphicsDevice.SetRenderTarget(null);
85+
86+
// If we're going to draw to the screen, do it.
87+
if (SadConsole.Settings.DoFinalDraw)
88+
{
89+
// This is the original draw to screen code from SadConsole
90+
//Global.SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.NonPremultiplied, SamplerState.PointClamp, DepthStencilState.DepthRead, RasterizerState.CullNone);
91+
//Global.SpriteBatch.Draw(Global.RenderOutput, Global.RenderRect, Color.White);
92+
//Global.SpriteBatch.End();
93+
94+
// This is the code you wrote but modified to draw the output texture from SadConsole.
95+
GraphicsDevice.Clear(Color.Black);
96+
97+
spriteBatch.Begin(SpriteSortMode.Immediate, BlendState.AlphaBlend,
98+
SamplerState.LinearClamp, DepthStencilState.Default,
99+
RasterizerState.CullNone);
100+
101+
spriteBatch.Draw(SadConsole.Global.RenderOutput, new Rectangle(0, 0, 300, 100), Color.Red);
102+
103+
spriteBatch.End();
104+
}
105+
}
106+
107+
108+
109+
}
110+
111+
public override void Update(GameTime gameTime)
112+
{
113+
if (SadConsole.Settings.DoUpdate)
114+
{
115+
SadConsole.Global.GameTimeUpdate = gameTime;
116+
SadConsole.Global.GameTimeElapsedUpdate = gameTime.ElapsedGameTime.TotalSeconds;
117+
118+
if (Game.IsActive)
119+
{
120+
if (SadConsole.Settings.Input.DoKeyboard)
121+
{
122+
SadConsole.Global.KeyboardState.Update(gameTime);
123+
SadConsole.Global.KeyboardState.Process();
124+
}
125+
126+
if (SadConsole.Settings.Input.DoMouse)
127+
{
128+
SadConsole.Global.MouseState.Update(gameTime);
129+
SadConsole.Global.MouseState.Process();
130+
}
131+
}
132+
133+
SadConsole.Global.CurrentScreen?.Update(gameTime.ElapsedGameTime);
134+
135+
// 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.
136+
//SadConsole.Game.OnUpdate?.Invoke(gameTime);
137+
}
138+
}
139+
140+
class Program
141+
{
142+
static void Main(string[] args)
143+
{
144+
new MonoGameGame().Run();
145+
}
146+
}
147+
148+
// My test game to make this code work. you didn't provide this so I came up with this :)
149+
class MonoGameGame : Microsoft.Xna.Framework.Game
150+
{
151+
public GraphicsDeviceManager GraphicsDeviceManager;
152+
153+
public MonoGameGame()
154+
{
155+
GraphicsDeviceManager = new GraphicsDeviceManager(this);
156+
}
157+
158+
protected override void Initialize()
159+
{
160+
base.Initialize();
161+
162+
ScreenManager manager = new ScreenManager();
163+
manager.LoadScreen(new SadConsoleScreen(this));
164+
Components.Add(manager);
165+
166+
}
167+
}
168+
}
169+
}

0 commit comments

Comments
 (0)