Skip to content

Commit

Permalink
Upgrade Highbyte.DotNet6502.SadConsoleHost (and examples) from SadCon…
Browse files Browse the repository at this point in the history
…sole v8 to v9
  • Loading branch information
highbyte authored Nov 14, 2021
1 parent c48647b commit 301f18a
Show file tree
Hide file tree
Showing 10 changed files with 62 additions and 51 deletions.
10 changes: 9 additions & 1 deletion Examples/SadConsoleTest/Fonts/C64.font
Original file line number Diff line number Diff line change
@@ -1 +1,9 @@
{"FilePath":"Yayo_c64.png","GlyphHeight":16,"GlyphPadding":1,"GlyphWidth":16,"Name":"C64","SolidGlyphIndex":219}
{
"$type": "SadConsole.SadFont, SadConsole",
"FilePath": "Yayo_c64.png",
"GlyphHeight": 16,
"GlyphPadding": 1,
"GlyphWidth": 16,
"Name": "C64",
"SolidGlyphIndex": 219
}
16 changes: 7 additions & 9 deletions Examples/SadConsoleTest/SadConsoleTest.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
<PackageReference Include="Microsoft.Extensions.Configuration.Binder" Version="6.0.0" />
<PackageReference Include="Microsoft.Extensions.Configuration.Json" Version="6.0.0" />
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="SadConsole" Version="8.99.3" />
<PackageReference Include="SadConsole" Version="9.1.1" />
</ItemGroup>

<ItemGroup>
Expand All @@ -28,12 +28,10 @@
<None Update="appsettings_snake.json">
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None>
<None Update="Fonts\C64.font">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
<None Update="Fonts\Yayo_c64.png">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>

</ItemGroup>
<ItemGroup>
<None Update="Fonts\*.*">
<CopyToOutputDirectory>Always</CopyToOutputDirectory>
</None>
</ItemGroup>
</Project>
2 changes: 1 addition & 1 deletion Examples/SadConsoleTest/appsettings_scroll.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

"SadConsoleConfig": {
"WindowTitle": "SadConsole displaying screen generated by machine code running in Highbyte.DotNet6502 emulator.",
"Font": "Fonts/C64.font", // Leave blank for default font. C64 font copied from https://github.com/Thraka/SadConsole
"Font": "Fonts/C64.font", // Leave blank for default font. C64 font copied from https://github.com/Thraka/SadConsole
"FontScale": 2
},

Expand Down
2 changes: 1 addition & 1 deletion Highbyte.DotNet6502.SadConsoleHost/ColorMaps.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using SadRogue.Primitives;
using System.Collections.Generic;
using Microsoft.Xna.Framework;

namespace Highbyte.DotNet6502.SadConsoleHost
{
Expand Down
3 changes: 2 additions & 1 deletion Highbyte.DotNet6502.SadConsoleHost/EmulatorMemoryConfig.cs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SadRogue.Primitives;
using System;
using System.Collections.Generic;

Expand Down Expand Up @@ -98,7 +99,7 @@ public class EmulatorScreenConfig
public byte DefaultBgColor { get; set; }
public byte DefaultBorderColor { get; set; }

public Dictionary<byte,Microsoft.Xna.Framework.Color> ColorMap { get; set; }
public Dictionary<byte,Color> ColorMap { get; set; }
public bool UseAscIICharacters { get; set; }

/// <summary>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@
</ItemGroup>

<ItemGroup>
<PackageReference Include="MonoGame.Framework.DesktopGL" Version="3.8.0.1641" />
<PackageReference Include="SadConsole" Version="8.99.3" />
<PackageReference Include="SadConsole.Host.MonoGame" Version="9.1.1" />
</ItemGroup>
</Project>
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
using SadConsole;
using System;

namespace Highbyte.DotNet6502.SadConsoleHost
Expand All @@ -23,7 +24,7 @@ public void CaptureInput()

private void CaptureKeyboard()
{
var keyboard = SadConsole.Global.KeyboardState;
var keyboard = GameHost.Instance.Keyboard;

if(keyboard.KeysPressed.Count > 0)
_emulatorMem[_emulatorInputConfig.KeyPressedAddress] = (byte)keyboard.KeysPressed[0].Character;
Expand Down
4 changes: 2 additions & 2 deletions Highbyte.DotNet6502.SadConsoleHost/SadConsoleEmulatorLoop.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
using System;
using Microsoft.Xna.Framework;
using SadConsole;

namespace Highbyte.DotNet6502.SadConsoleHost
{
Expand Down Expand Up @@ -29,7 +29,7 @@ public SadConsoleEmulatorLoop(
/// This method is called once every frame, and is responsible for letting the SadConsole engine interact with the emulator
/// </summary>
/// <param name="gameTime"></param>
public void SadConsoleUpdate(GameTime gameTime)
public void SadConsoleUpdate(GameHost gameHost)
{
_frameCounter++;
if(_frameCounter >= _updateEmulatorEveryXFrame)
Expand Down
45 changes: 19 additions & 26 deletions Highbyte.DotNet6502.SadConsoleHost/SadConsoleMain.cs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
using Microsoft.Xna.Framework;

using SadConsole;
namespace Highbyte.DotNet6502.SadConsoleHost
{
public class SadConsoleMain
Expand All @@ -23,17 +22,23 @@ public SadConsoleMain(

public void Run()
{
Settings.WindowTitle = _sadConsoleConfig.WindowTitle;

// Setup the SadConsole engine and create the main window.
// If font is null or empty, the default SadConsole font will be used.
SadConsole.Game.Create(_sadConsoleConfig.Font,
SadConsole.Game.Create(
(_emulatorScreenConfig.Cols + (_emulatorScreenConfig.BorderCols*2)) * _sadConsoleConfig.FontScale,
(_emulatorScreenConfig.Rows + (_emulatorScreenConfig.BorderRows*2)) * _sadConsoleConfig.FontScale);
(_emulatorScreenConfig.Rows + (_emulatorScreenConfig.BorderRows*2)) * _sadConsoleConfig.FontScale,
_sadConsoleConfig.Font
);

//SadConsole.Game.Instance.DefaultFontSize = IFont.Sizes.One;

// Hook the start event so we can add consoles to the system.
SadConsole.Game.OnInitialize = InitSadConsole;
SadConsole.Game.Instance.OnStart = InitSadConsole;

// Hook the update event that happens each frame
SadConsole.Game.OnUpdate = UpdateSadConsole;
SadConsole.Game.Instance.FrameUpdate += UpdateSadConsole;

// Hook the "after render"
//SadConsole.Game.OnDraw = Screen.DrawFrame;
Expand All @@ -47,37 +52,25 @@ public void Run()
/// Runs every frame
/// </summary>
/// <param name="gameTime"></param>
private void UpdateSadConsole(GameTime gameTime)
private void UpdateSadConsole(object sender, GameHost e)
{
_sadConsoleEmulatorLoop.SadConsoleUpdate(gameTime);
_sadConsoleEmulatorLoop.SadConsoleUpdate(e);
}

/// <summary>
/// Runs when SadConsole engine starts up
/// </summary>
private void InitSadConsole()
{
// TODO: Better way to map numeric scale value to SadConsole.Font.FontSizes enum?
var fontSize = _sadConsoleConfig.FontScale switch
{
1 => SadConsole.Font.FontSizes.One,
2 => SadConsole.Font.FontSizes.Two,
3 => SadConsole.Font.FontSizes.Three,
_ => SadConsole.Font.FontSizes.One,
};
SadConsole.Global.FontDefault = SadConsole.Global.FontDefault.Master.GetFont(fontSize);

// Create a SadConsole screen
_sadConsoleScreen = new SadConsoleScreen(_emulatorScreenConfig);

// Set SadConsole engine current screen to our screen
SadConsole.Global.CurrentScreen = _sadConsoleScreen;
_sadConsoleScreen = new SadConsoleScreen(_emulatorScreenConfig, _sadConsoleConfig);

// Start with focus on screen console
SadConsole.Global.FocusedConsoles.Set(_sadConsoleScreen.ScreenConsole);
SadConsole.Game.Instance.Screen = _sadConsoleScreen;
SadConsole.Game.Instance.DestroyDefaultStartingConsole();

// Set main window title
SadConsole.Game.Instance.Window.Title = _sadConsoleConfig.WindowTitle;
// Start with focus on main console on current screen
_sadConsoleScreen.IsFocused = true;
_sadConsoleScreen.ScreenConsole.IsFocused = true;
}
}
}
25 changes: 18 additions & 7 deletions Highbyte.DotNet6502.SadConsoleHost/SadConsoleScreen.cs
Original file line number Diff line number Diff line change
@@ -1,29 +1,40 @@
using SadConsole;
using SadRogue.Primitives;

namespace Highbyte.DotNet6502.SadConsoleHost
{
/// <summary>
/// A SadConsole "container" console that will contain our screen where we render to
/// </summary>
public class SadConsoleScreen: ContainerConsole
public class SadConsoleScreen: ScreenObject
{
public Console ScreenConsole { get ;}

public SadConsoleScreen(EmulatorScreenConfig emulatorScreenConfig)
public SadConsoleScreen(EmulatorScreenConfig emulatorScreenConfig, SadConsoleConfig sadConsoleConfig)
{
ScreenConsole = CreateScreenConsole(emulatorScreenConfig);
ScreenConsole = CreateScreenConsole(emulatorScreenConfig, sadConsoleConfig);
}

private Console CreateScreenConsole(EmulatorScreenConfig emulatorScreenConfig)
private Console CreateScreenConsole(EmulatorScreenConfig emulatorScreenConfig, SadConsoleConfig sadConsoleConfig)
{
// Setup screen
var screen = new Console(emulatorScreenConfig.Cols + (emulatorScreenConfig.BorderCols * 2), emulatorScreenConfig.Rows + (emulatorScreenConfig.BorderRows * 2))
{
DefaultForeground = Microsoft.Xna.Framework.Color.White,
DefaultBackground = Microsoft.Xna.Framework.Color.Black
DefaultForeground = Color.White,
DefaultBackground = Color.Black
};
//screen.Position = new Point(BorderWidth, BorderHeight);

// TODO: Better way to map numeric scale value to SadConsole.Font.FontSizes enum?
var fontSize = sadConsoleConfig.FontScale switch
{
1 => IFont.Sizes.One,
2 => IFont.Sizes.Two,
3 => IFont.Sizes.Three,
_ => IFont.Sizes.One,
};
screen.FontSize = screen.Font.GetFontSize(fontSize);

screen.Clear();
screen.Cursor.IsEnabled = false;
screen.Cursor.IsVisible = false;
Expand All @@ -32,7 +43,7 @@ private Console CreateScreenConsole(EmulatorScreenConfig emulatorScreenConfig)
return screen;
}

public void DrawCharacter(int x, int y, int sadConsoleCharCode, Microsoft.Xna.Framework.Color fgColor, Microsoft.Xna.Framework.Color bgColor)
public void DrawCharacter(int x, int y, int sadConsoleCharCode, Color fgColor, Color bgColor)
{
ScreenConsole.SetGlyph(x, y, sadConsoleCharCode, fgColor, bgColor);
}
Expand Down

0 comments on commit 301f18a

Please sign in to comment.