Skip to content

Commit

Permalink
* Framerate calculation
Browse files Browse the repository at this point in the history
* 33% improvement on WindowsForms
  • Loading branch information
Alexandre Rocha Lima e Marcondes committed Feb 22, 2019
1 parent e424f16 commit 4bfd545
Show file tree
Hide file tree
Showing 6 changed files with 69 additions and 21 deletions.
15 changes: 8 additions & 7 deletions Chip8.Sharp/Chip8.Sharp.sln
Original file line number Diff line number Diff line change
Expand Up @@ -6,15 +6,13 @@ Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Chip8.Core", "Chip8.Core\Ch
EndProject
Project("{2150E333-8FDC-42A3-9474-1A3956D46DE8}") = "Solution Items", "Solution Items", "{05D2CF24-E03F-4D32-906B-641573A16429}"
ProjectSection(SolutionItems) = preProject
CHIP-8 Keyboard Mapping.md = CHIP-8 Keyboard Mapping.md
CHIP-8 OpCodes.md = CHIP-8 OpCodes.md
CHIP-8 Specs.md = CHIP-8 Specs.md
run-tests = run-tests
..\nunit3-xslt\html-report.xslt = ..\nunit3-xslt\html-report.xslt
..\nunit3-xslt\html-summary.xslt = ..\nunit3-xslt\html-summary.xslt
..\nunit3-xslt\README.md = ..\nunit3-xslt\README.md
run-tests = run-tests
..\nunit3-xslt\text-profile.xslt = ..\nunit3-xslt\text-profile.xslt
..\nunit3-xslt\text-report.xslt = ..\nunit3-xslt\text-report.xslt
..\nunit3-xslt\text-summary.xslt = ..\nunit3-xslt\text-summary.xslt
..\nunit3-xslt\text-profile.xslt = ..\nunit3-xslt\text-profile.xslt
EndProjectSection
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chip8.Tests", "Chip8.Tests\Chip8.Tests.csproj", "{ACF18D83-5BD5-4AA9-B794-711E069719B5}"
Expand All @@ -25,7 +23,7 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chip8.WindowsForms", "Chip8
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chip8.WPF", "Chip8.WPF\Chip8.WPF.csproj", "{415A9C97-30EA-49D5-AA8E-B50D472D18CD}"
EndProject
Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "Chip8.SDL", "Chip8.SDL\Chip8.SDL.csproj", "{A4862065-B576-49B3-89DC-A501AA17B905}"
Project("{9A19103F-16F7-4668-BE54-9A1E7A4F7556}") = "Chip8.SDL", "Chip8.SDL\Chip8.SDL.csproj", "{A4862065-B576-49B3-89DC-A501AA17B905}"
EndProject
Global
GlobalSection(Performance) = preSolution
Expand Down Expand Up @@ -65,6 +63,7 @@ Global
{ACF18D83-5BD5-4AA9-B794-711E069719B5}.Release|x86.ActiveCfg = Debug|Any CPU
{ACF18D83-5BD5-4AA9-B794-711E069719B5}.Release|x86.Build.0 = Debug|Any CPU
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Debug|Any CPU.ActiveCfg = Debug|x86
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Debug|Any CPU.Build.0 = Debug|x86
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Debug|x64.ActiveCfg = Debug|x64
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Debug|x64.Build.0 = Debug|x64
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Debug|x86.ActiveCfg = Debug|x86
Expand All @@ -73,7 +72,6 @@ Global
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Release|x64.ActiveCfg = Debug|x64
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Release|x64.Build.0 = Debug|x64
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Release|x86.ActiveCfg = Debug|x64
{CB188B88-CBFF-4D27-B5B2-335D7029C322}.Debug|Any CPU.Build.0 = Debug|x86
{AB9198E1-E2E9-4B1F-9FC3-8B7F32C986F6}.Debug|Any CPU.ActiveCfg = Debug|Any CPU
{AB9198E1-E2E9-4B1F-9FC3-8B7F32C986F6}.Debug|Any CPU.Build.0 = Debug|Any CPU
{AB9198E1-E2E9-4B1F-9FC3-8B7F32C986F6}.Debug|x64.ActiveCfg = Debug|Any CPU
Expand Down Expand Up @@ -117,4 +115,7 @@ Global
GlobalSection(ExtensibilityGlobals) = postSolution
SolutionGuid = {85F0FA54-6A6D-4D08-86A4-5BC480BF3DED}
EndGlobalSection
GlobalSection(Performance) = preSolution
HasPerformanceSessions = true
EndGlobalSection
EndGlobal
1 change: 1 addition & 0 deletions Chip8.Sharp/Chip8.WPF/MainWindow.xaml
Original file line number Diff line number Diff line change
Expand Up @@ -9,5 +9,6 @@
<Grid Margin="0,0,0,0">
<ComboBox Name="cbPrograms" HorizontalAlignment="Left" Height="25" Width="640" VerticalAlignment="Top" Margin="0,0,0,0" SelectionChanged="ComboBox_SelectionChanged" />
<Image Name="imgScreen" Margin="0,33,0,0" Height="480" Width="640" HorizontalAlignment="Left" VerticalAlignment="Top" Stretch="Uniform" />
<Label x:Name="frameRateStatusLabel" Content="0 FPS" HorizontalAlignment="Left" Margin="10,33,0,0" VerticalAlignment="Top" Foreground="Red"/>
</Grid>
</Window>
12 changes: 12 additions & 0 deletions Chip8.Sharp/Chip8.WPF/MainWindow.xaml.cs
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,10 @@ public partial class MainWindow : Window
readonly TimeSpan targetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 1000);
TimeSpan lastTime;

// frame rate handling
private DateTime frameDateTime;
private double averageDeltaTime;

private static readonly Color ambar = Color.FromArgb(0xFF, 0xFF, 0xB0, 0x00);
private static readonly Color lightAmbar = Color.FromArgb(0xFF, 0xFF, 0xCC, 0x00);
private static readonly Color green1 = Color.FromArgb(0xFF, 0x33, 0xFF, 0x00);
Expand Down Expand Up @@ -392,6 +396,14 @@ void OnDraw(byte[] graphics)
{
Draw(graphics);
imgScreen.InvalidateVisual();

// frame rate
DateTime currentDateTime = DateTime.Now;
double currentDeltaTime = (currentDateTime - frameDateTime).TotalSeconds;
frameDateTime = currentDateTime;
averageDeltaTime = averageDeltaTime * 0.9 + currentDeltaTime * 0.1;
int frameRate = (int)(1.0 / averageDeltaTime);
frameRateStatusLabel.Content = frameRate + " FPS";
});
}

Expand Down
27 changes: 22 additions & 5 deletions Chip8.Sharp/Chip8.WindowsForms/MainForm.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 25 additions & 8 deletions Chip8.Sharp/Chip8.WindowsForms/MainForm.cs
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,10 @@ public partial class MainForm : Form
readonly TimeSpan targetElapsedTime = TimeSpan.FromTicks(TimeSpan.TicksPerSecond / 1000);
TimeSpan lastTime;

// frame rate
private DateTime frameDateTime;
private double averageDeltaTime;

private static readonly Color ambar = Color.FromArgb(0xFF, 0xFF, 0xB0, 0x00);
private static readonly Color lightAmbar = Color.FromArgb(0xFF, 0xFF, 0xCC, 0x00);
private static readonly Color green1 = Color.FromArgb(0xFF, 0x33, 0xFF, 0x00);
Expand Down Expand Up @@ -387,12 +391,28 @@ void OnDraw(byte[] graphics)
{
Draw(graphics);
pbScreen.Refresh();

// frame rate
DateTime currentDateTime = DateTime.Now;
double currentDeltaTime = (currentDateTime - frameDateTime).TotalSeconds;
frameDateTime = currentDateTime;
averageDeltaTime = averageDeltaTime * 0.9 + currentDeltaTime * 0.1;
int frameRate = (int)(1.0 / averageDeltaTime);
frameRateStatusLabel.Text = frameRate + " FPS";
}));
}
else
{
Draw(graphics);
pbScreen.Refresh();

// frame rate
DateTime currentDateTime = DateTime.Now;
double currentDeltaTime = (currentDateTime - frameDateTime).TotalSeconds;
frameDateTime = currentDateTime;
averageDeltaTime = averageDeltaTime * 0.9 + currentDeltaTime * 0.1;
int frameRate = (int)(1.0 / averageDeltaTime);
frameRateStatusLabel.Text = frameRate + " FPS";
}
}

Expand All @@ -403,18 +423,15 @@ void Draw(byte[] graphics)

unsafe
{
byte* pointer = (byte*)bits.Scan0;
int* pointer = (int*)bits.Scan0;

foreach (byte pixel in graphics)
for (int i = 0; i < graphics.Length; i++)
{
byte pixel = graphics[i];
var color = (pixel > 0) ? appleIIcGreen : Color.Black;

pointer[0] = color.B;
pointer[1] = color.G;
pointer[2] = color.R;
pointer[3] = 255; // Alpha

pointer += 4; // 4 bytes per pixel
*pointer = color.ToArgb();
pointer++; // 4 bytes (1 int) per pixel
}
}

Expand Down
2 changes: 1 addition & 1 deletion SDL2-CS

0 comments on commit 4bfd545

Please sign in to comment.