From c0fe129729f801d2d4fb28899cc5e22ceacff36b Mon Sep 17 00:00:00 2001 From: Alessandro Proto Date: Sun, 22 Jan 2023 21:11:14 +0100 Subject: [PATCH] Changed how cursor behaves, added Overlay event --- Capy64/Capy64.cs | 30 +++++++++++-------- Capy64/Eventing/EventEmitter.cs | 8 ++++++ Capy64/Eventing/Events/OverlayEvent.cs | 11 +++++++ Capy64/IGame.cs | 3 +- Capy64/LuaRuntime/Libraries/Term.cs | 40 +++++++++++--------------- 5 files changed, 54 insertions(+), 38 deletions(-) create mode 100644 Capy64/Eventing/Events/OverlayEvent.cs diff --git a/Capy64/Capy64.cs b/Capy64/Capy64.cs index 18a0a26..0ddee2f 100644 --- a/Capy64/Capy64.cs +++ b/Capy64/Capy64.cs @@ -23,7 +23,7 @@ public class Capy64 : Game, IGame Environment.SpecialFolder.ApplicationData, Environment.SpecialFolderOption.Create), "Capy64"); - public Game Game => this; + public Capy64 Game => this; public IList NativePlugins { get; private set; } public IList Plugins { get; private set; } public int Width { get; set; } = 400; @@ -39,11 +39,12 @@ public class Capy64 : Game, IGame Left = 0, Right = 0, }; + public SpriteBatch SpriteBatch; + private readonly InputManager _inputManager; private RenderTarget2D renderTarget; private readonly GraphicsDeviceManager _graphics; - private SpriteBatch _spriteBatch; private IServiceProvider _serviceProvider; private ulong _totalTicks = 0; @@ -139,36 +140,41 @@ public class Capy64 : Game, IGame protected override void LoadContent() { - _spriteBatch = new SpriteBatch(GraphicsDevice); + SpriteBatch = new SpriteBatch(GraphicsDevice); } protected override void Update(GameTime gameTime) { Drawing.Begin(); + // Register user input + _inputManager.Update(IsActive); + EventEmitter.RaiseTick(new() { GameTime = gameTime, TotalTicks = _totalTicks }); - // resume here - Drawing.End(); - // Register user input - _inputManager.Update(IsActive); + _totalTicks++; base.Update(gameTime); - _totalTicks++; } protected override void Draw(GameTime gameTime) { - _spriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp); - //GraphicsDevice.Clear(Color.Blue); - _spriteBatch.Draw(renderTarget, new(Borders.Left, Borders.Top), null, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 0); - _spriteBatch.End(); + SpriteBatch.Begin(SpriteSortMode.Deferred, BlendState.AlphaBlend, SamplerState.PointClamp); + SpriteBatch.Draw(renderTarget, new(Borders.Left, Borders.Top), null, Color.White, 0f, Vector2.Zero, Scale, SpriteEffects.None, 0); + + EventEmitter.RaiseOverlay(new() + { + GameTime = gameTime, + TotalTicks = _totalTicks, + }); + + SpriteBatch.End(); base.Draw(gameTime); } diff --git a/Capy64/Eventing/EventEmitter.cs b/Capy64/Eventing/EventEmitter.cs index d50a459..8047111 100644 --- a/Capy64/Eventing/EventEmitter.cs +++ b/Capy64/Eventing/EventEmitter.cs @@ -22,6 +22,7 @@ public class EventEmitter public event EventHandler OnTick; public event EventHandler OnInit; public event EventHandler OnScreenSizeChange; + public event EventHandler OnOverlay; public void RaiseMouseMove(MouseMoveEvent ev) @@ -106,4 +107,11 @@ public class EventEmitter OnScreenSizeChange(this, EventArgs.Empty); } } + + public void RaiseOverlay(OverlayEvent ev) + { + if(OnOverlay is not null) { + OnOverlay(this, ev); + } + } } diff --git a/Capy64/Eventing/Events/OverlayEvent.cs b/Capy64/Eventing/Events/OverlayEvent.cs new file mode 100644 index 0000000..74af33b --- /dev/null +++ b/Capy64/Eventing/Events/OverlayEvent.cs @@ -0,0 +1,11 @@ +using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using System; + +namespace Capy64.Eventing.Events; + +public class OverlayEvent : EventArgs +{ + public GameTime GameTime { get; set; } + public ulong TotalTicks { get; set; } +} diff --git a/Capy64/IGame.cs b/Capy64/IGame.cs index 61748bb..0103b07 100644 --- a/Capy64/IGame.cs +++ b/Capy64/IGame.cs @@ -10,8 +10,7 @@ namespace Capy64; public interface IGame { - - Game Game { get; } + Capy64 Game { get; } IList NativePlugins { get; } IList Plugins { get; } GameWindow Window { get; } diff --git a/Capy64/LuaRuntime/Libraries/Term.cs b/Capy64/LuaRuntime/Libraries/Term.cs index d1e5662..442b402 100644 --- a/Capy64/LuaRuntime/Libraries/Term.cs +++ b/Capy64/LuaRuntime/Libraries/Term.cs @@ -2,7 +2,11 @@ using Capy64.Eventing.Events; using KeraLua; using Microsoft.Xna.Framework; +using Microsoft.Xna.Framework.Graphics; +using MonoGame.Extended; using System; +using static Capy64.Utils; +using static System.Formats.Asn1.AsnWriter; namespace Capy64.LuaRuntime.Libraries; @@ -34,6 +38,7 @@ internal class Term : IPlugin private static IGame _game; private static bool cursorState = false; private static bool enableCursor = true; + private static Texture2D cursorTexture; public Term(IGame game) { _game = game; @@ -42,9 +47,14 @@ internal class Term : IPlugin ForegroundColor = Color.White; BackgroundColor = Color.Black; + cursorTexture = new(_game.Game.GraphicsDevice, 1, CharHeight); + var textureData = new Color[CharHeight]; + Array.Fill(textureData, Color.White); + cursorTexture.SetData(textureData); + UpdateSize(); - _game.EventEmitter.OnTick += OnTick; + _game.EventEmitter.OnOverlay += OnOverlay; } private LuaRegister[] TermLib = new LuaRegister[] @@ -227,13 +237,13 @@ internal class Term : IPlugin Console.WriteLine("|\n \\{0}", new string('-', Width)); } - private void OnTick(object sender, TickEvent e) + private void OnOverlay(object sender, OverlayEvent e) { if (((int)e.TotalTicks % CursorDelay) == 0) { cursorState = !cursorState; - UpdateCursor(); } + UpdateCursor(); } private static void UpdateCursor() @@ -244,28 +254,12 @@ internal class Term : IPlugin if (_cursorPosition.X < 0 || _cursorPosition.Y < 0 || _cursorPosition.X >= Width || _cursorPosition.Y >= Height) return; - var ch = CharGrid[(int)_cursorPosition.X + ((int)_cursorPosition.Y * Width)] ?? - new Char - { - Character = ' ', - Foreground = ForegroundColor, - Background = BackgroundColor, - }; - - Color fg, bg; - if (cursorState) { - fg = ch.Background; - bg = ch.Foreground; + var realpos = ToRealPos(CursorPosition - Vector2.One); + var charpos = realpos * _game.Scale + CharOffset; + _game.Game.SpriteBatch.Draw(cursorTexture, charpos, null, ForegroundColor, 0f, Vector2.Zero, _game.Scale, SpriteEffects.None, 0); } - else - { - fg = ch.Foreground; - bg = ch.Background; - } - - PlotChar(_cursorPosition, ch.Character, fg, bg, false); } private static void ClearGrid() @@ -297,10 +291,8 @@ internal class Term : IPlugin public static void SetCursorPosition(int x, int y) { - RedrawPos(_cursorPosition); cursorState = true; _cursorPosition = new(x - 1, y - 1); - UpdateCursor(); } private static int L_GetPos(IntPtr state)