Skip to content

Commit

Permalink
Working on fixing MBC controllers
Browse files Browse the repository at this point in the history
  • Loading branch information
qkmaxware committed Jan 14, 2024
1 parent eca3f20 commit fb7a603
Show file tree
Hide file tree
Showing 26 changed files with 609 additions and 232 deletions.
6 changes: 6 additions & 0 deletions BlazorBoy.sln
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gameboy.Player.Cli", "Gameb
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gameboy.Player.Blazor", "Gameboy.Player.Blazor\BlazorBoy.Player.csproj", "{C05C9750-5DA4-467F-AE0D-B14330885D87}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Gameboy.Database", "Gameboy.Database\Gameboy.Database.csproj", "{D85FB21E-22FE-4C1D-B8D3-2104E2F7CF44}"
EndProject
Global
GlobalSection(SolutionConfigurationPlatforms) = preSolution
Debug|Any CPU = Debug|Any CPU
Expand Down Expand Up @@ -42,5 +44,9 @@ Global
{C05C9750-5DA4-467F-AE0D-B14330885D87}.Debug|Any CPU.Build.0 = Debug|Any CPU
{C05C9750-5DA4-467F-AE0D-B14330885D87}.Release|Any CPU.ActiveCfg = Release|Any CPU
{C05C9750-5DA4-467F-AE0D-B14330885D87}.Release|Any CPU.Build.0 = Release|Any CPU
{D85FB21E-22FE-4C1D-B8D3-2104E2F7CF44}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{D85FB21E-22FE-4C1D-B8D3-2104E2F7CF44}.Debug|Any CPU.Build.0 = Debug|Any CPU
{D85FB21E-22FE-4C1D-B8D3-2104E2F7CF44}.Release|Any CPU.ActiveCfg = Release|Any CPU
{D85FB21E-22FE-4C1D-B8D3-2104E2F7CF44}.Release|Any CPU.Build.0 = Release|Any CPU
EndGlobalSection
EndGlobal
60 changes: 60 additions & 0 deletions Gameboy.Database/GameDatabase.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System.Collections;
using System.Reflection;
using System.Text.Json.Serialization;

namespace Qkmaxware.Emulators.Gameboy;

public class GameInfo {
[JsonPropertyName("title")]
public string? CartTitle {get; set;}
[JsonPropertyName("name")]
public string? Name {get; set;}
[JsonPropertyName("boxart")]
public string? BoxArtUrl {get; set;}
[JsonPropertyName("description")]
public string? Description {get; set;}
[JsonPropertyName("genres")]
public string[]? Genres {get; set;}
[JsonPropertyName("released")]
public int ReleaseYear {get; set;}
[JsonPropertyName("developer")]
public string? DeveloperName {get; set;}
[JsonPropertyName("publisher")]
public string? PublisherName {get; set;}
}

public class GameDatabase : IEnumerable<GameInfo> {

private List<GameInfo> all = new List<GameInfo>();

private GameDatabase() {
var assembly = typeof(GameDatabase).GetTypeInfo().Assembly;
foreach (var name in assembly.GetManifestResourceNames()) {
if (name == ("Gameboy.Database.database.json")) {
Stream? resource = assembly.GetManifestResourceStream(name);
if (resource is null)
continue;

var records = System.Text.Json.JsonSerializer.Deserialize<List<GameInfo>>(resource);
if (records is null)
break;
this.all = records;
}
}
}

private static GameDatabase? instance;
public static GameDatabase Instance() {
if (instance is null)
instance = new GameDatabase();
return instance;
}

public IEnumerator<GameInfo> GetEnumerator() {
return all.GetEnumerator();
}

IEnumerator IEnumerable.GetEnumerator() {
return all.GetEnumerator();
}
}
13 changes: 13 additions & 0 deletions Gameboy.Database/Gameboy.Database.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<TargetFramework>net6.0</TargetFramework>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<EmbeddedResource Include="database.json" />
</ItemGroup>

</Project>
28 changes: 28 additions & 0 deletions Gameboy.Database/database.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
[
{
"title": "TETRIS",
"name": "Tetris",
"boxart": "https://upload.wikimedia.org/wikipedia/en/thumb/4/4a/Tetris_Boxshot.jpg/220px-Tetris_Boxshot.jpg",
"description": "A portable version of Alexey Pajitnov's original tetris for the Nintendo Game Boy.",
"genres": [
"Puzzle"
],
"released": 1989,
"developer": "Nintendo R&D1",
"publisher": "Nintendo"
},
{
"title": "POKEMON BLUE",
"name": "Pokémon Blue",
"boxart": "https://archives.bulbagarden.net/media/upload/thumb/5/5a/Blue_EN_boxart.png/250px-Blue_EN_boxart.png",
"description": "Along with Pokémon Red, Pokémon Blue was one of the first installments of the Pokémon series.",
"genres": [
"Role-playing",
"Adventure",
"Turn-based strategy"
],
"released": 1996,
"developer": "Game Freak",
"publisher": "Nintendo"
}
]
1 change: 1 addition & 0 deletions Gameboy.Player.Blazor/BlazorBoy.Player.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@

<ItemGroup>
<ProjectReference Include="..\Gameboy\Gameboy.csproj" />
<ProjectReference Include="..\Gameboy.Database\Gameboy.Database.csproj" />
</ItemGroup>

</Project>
39 changes: 34 additions & 5 deletions Gameboy.Player.Blazor/Shared/CartridgeInfo.razor
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,32 @@
@using Qkmaxware.Emulators.Gameboy.Hardware

@if (Header is not null) {
<div>
<table>
<thead>
<tr>
<th>Title</th> <th>@Header.title</th>
<th class="w3-center">@(dbInfo?.Name ?? Header.title)</th>
</tr>
</thead>
</table>
@if (dbInfo?.BoxArtUrl is not null) {
<img src="@dbInfo.BoxArtUrl" alt="@dbInfo.BoxArtUrl" title="@dbInfo.BoxArtUrl">
}
<table>
<tbody>
@if (dbInfo is not null) {
<tr>
<td>Region</td> <td>@Header.region</td>
<td>Developer</td> <td>@dbInfo.DeveloperName</td>
</tr>
<tr>
<td>Manufacturer</td> <td>@Header.manufacturerCode</td>
<td>Publisher</td> <td>@dbInfo.PublisherName</td>
</tr>
<tr>
<td>Licencee</td> <td>@Header.licencee</td>
<td>Released</td> <td>@dbInfo.ReleaseYear</td>
</tr>
}
<tr>
<td>Region</td> <td>@Header.region</td>
</tr>
<tr>
<td>Cart Type</td> <td>@Header.cartType.MBC</td>
Expand All @@ -29,10 +40,28 @@
</tr>
</tbody>
</table>
</div>
}

@code {
[Parameter] public Cartridge? Cart {get; set;}

private Cartridge? cart;
private GameInfo? dbInfo;
#pragma warning disable BL0007
[Parameter] public Cartridge? Cart{
get => cart;
set {
if (value != cart) {
this.cart = value;
if (value is not null) {
this.dbInfo = GameDatabase.Instance().Where(x => x.CartTitle == value.Info.title).FirstOrDefault();
} else {
this.dbInfo = null;
}
}
}
}
#pragma warning restore BL0007

public CartridgeHeader? Header => Cart?.Info;
}
11 changes: 10 additions & 1 deletion Gameboy.Player.Blazor/Shared/CartridgeInfo.razor.css
Original file line number Diff line number Diff line change
@@ -1,11 +1,20 @@
table {
div {
background-color: rgba(0, 0, 0, 0.5);
color: white;
padding: 12px;
border-radius: 12px;
width: 100%;
}
table {
width: 100%;
}

table th {
text-align: left;
}

img {
width: 100%;
max-height: 240px;
object-fit: contain;
}
23 changes: 23 additions & 0 deletions Gameboy.Player.Godot/FpsCounter.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
using Godot;
using System;

namespace Qkmaxware.Emulators.Gameboy.Player;

public partial class FpsCounter : Label {
[Export] public TextureRenderer Renderer {get; set;}
private int fps;

// Called every frame. 'delta' is the elapsed time since the previous frame.
public override void _Process(double delta) {
if (Renderer is not null && Renderer.IsPlaying) {
var fps = (int)Math.Round(Godot.Engine.GetFramesPerSecond());
if (this.fps != fps) {
// New string only if the FPS changed
this.Text = "FPS: " + fps;
this.fps = fps;
}
} else {
this.Text = string.Empty;
}
}
}
21 changes: 20 additions & 1 deletion Gameboy.Player.Godot/Gameboy.tscn
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
[gd_scene load_steps=7 format=3 uid="uid://bytv8v3jd4m0l"]
[gd_scene load_steps=8 format=3 uid="uid://bytv8v3jd4m0l"]

[ext_resource type="Script" path="res://TextureRenderer.cs" id="1_hfp5s"]
[ext_resource type="Texture2D" uid="uid://dfkwdfgk0dp8n" path="res://icon.svg" id="1_ptiq5"]
[ext_resource type="Texture2D" uid="uid://cip0keh15srys" path="res://cart.png" id="3_gvrte"]
[ext_resource type="Texture2D" uid="uid://byxxvldruom6j" path="res://play.png" id="4_lwet3"]
[ext_resource type="Texture2D" uid="uid://cm332so38crqs" path="res://pause.png" id="5_5clxm"]
[ext_resource type="Script" path="res://FpsCounter.cs" id="5_rtd4o"]
[ext_resource type="Texture2D" uid="uid://d0dpxbrvr2bo7" path="res://stop.png" id="6_cecoo"]

[node name="Gameboy" type="Control"]
Expand Down Expand Up @@ -61,6 +62,24 @@ focus_mode = 0
icon = ExtResource("4_lwet3")
expand_icon = true

[node name="FPS" type="Label" parent="Top Right/Play" node_paths=PackedStringArray("Renderer")]
layout_mode = 1
anchors_preset = 7
anchor_left = 0.5
anchor_top = 1.0
anchor_right = 0.5
anchor_bottom = 1.0
offset_left = -22.5
offset_top = -3.0
offset_right = 22.5
offset_bottom = 20.0
grow_horizontal = 2
grow_vertical = 0
text = "FPS: 00"
horizontal_alignment = 1
script = ExtResource("5_rtd4o")
Renderer = NodePath("../../../LCD")

[node name="Pause" type="Button" parent="Top Right"]
layout_mode = 2
size_flags_horizontal = 3
Expand Down
9 changes: 7 additions & 2 deletions Gameboy.Player.Godot/TextureRenderer.cs
Original file line number Diff line number Diff line change
Expand Up @@ -6,12 +6,15 @@
using System.Linq;
using System.IO;

namespace Qkmaxware.Emulators.Gameboy.Player;

public partial class TextureRenderer : TextureRect {

public enum RendererState {
Stopped, Paused, Playing
}
private RendererState State {get; set;} = RendererState.Stopped;
public bool IsPlaying => State == RendererState.Playing;

[Export]
[ExportGroup("Colour Pallet/Background")]
Expand Down Expand Up @@ -47,11 +50,13 @@ public enum RendererState {

private Image pixels;
private ImageTexture texture;
private LcdBitmap intro;
private LcdBitmap white;

// Called when the node enters the scene tree for the first time.
public override void _Ready() {
var intro = new LcdBitmap(Gpu.LCD_WIDTH, Gpu.LCD_HEIGHT);
this.intro = intro;
intro.Fill(ColourPallet.BackgroundWhite);

white = new LcdBitmap(Gpu.LCD_WIDTH, Gpu.LCD_HEIGHT);
Expand Down Expand Up @@ -153,8 +158,8 @@ public void Stop() {
if (this.gb is not null && this.gb.IsCartridgeLoaded()) {
this.State = RendererState.Stopped;
this.gb.Reset();
if (white is not null) {
Redraw(white);
if (intro is not null) {
Redraw(intro);
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion Gameboy.Player.Godot/icon.svg.import
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,6 @@ process/hdr_as_srgb=false
process/hdr_clamp_exposure=false
process/size_limit=0
detect_3d/compress_to=1
svg/scale=2.0
svg/scale=8.0
editor/scale_with_editor_scale=false
editor/convert_colors_with_editor_theme=false
1 change: 1 addition & 0 deletions Gameboy.Player.Godot/project.godot
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ config_version=5
config/name="Gameboy.Player.Godot"
run/main_scene="res://Gameboy.tscn"
config/features=PackedStringArray("4.2", "C#", "GL Compatibility")
run/max_fps=60
boot_splash/bg_color=Color(0.141176, 0.141176, 0.141176, 1)
config/icon="res://icon.svg"

Expand Down
12 changes: 12 additions & 0 deletions Gameboy.Test/DatabaseTest.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
using System.Reflection;
using Microsoft.VisualStudio.TestTools.UnitTesting;

namespace Qkmaxware.Emulators.Gameboy.Test;

[TestClass]
public class GameDatabaseTest {
[TestMethod]
public void TestGameDatabaseNonEmpty() {
Assert.AreEqual(true, GameDatabase.Instance().Any());
}
}
3 changes: 2 additions & 1 deletion Gameboy.Test/Gameboy.Test.csproj
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,10 @@

<ItemGroup>
<ProjectReference Include="..\Gameboy\Gameboy.csproj" />
<ProjectReference Include="..\Gameboy.Database\Gameboy.Database.csproj" />
</ItemGroup>

<ItemGroup>
<ItemGroup>
<EmbeddedResource Include="Roms/instructions/*.gb" />
<EmbeddedResource Include="Data/*.json" />
<EmbeddedResource Include="Data/opcode_tests/*.json" />
Expand Down
Loading

0 comments on commit fb7a603

Please sign in to comment.