-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfmMain.pas
More file actions
737 lines (653 loc) · 18.1 KB
/
fmMain.pas
File metadata and controls
737 lines (653 loc) · 18.1 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
unit fmMain;
interface
uses
Classes, Forms, Windows, SysUtils, Dialogs, Controls, StdCtrls, ExtCtrls,
ComCtrls, AppEvnts, MMSystem, Menus, Buttons, ActnList, unVM, unStopwatch,
unSound;
const
c_ProgramName = 'BytePusheD';
c_ProgramVersion = '1.0';
c_ProgramYear = '2025';
c_ProgramCopyright = 'Dan Peroff';
c_SoundBufferSize = 8 * c_BytePusherSoundBufSize;
c_MaxSoundBuffers = 10;
c_DefaultSoundValue = 10; // %
type
TScreenBitmapInfo = packed record
bmiHeader: TBitmapInfoHeader;
bmiColors: array[Byte] of TRGBQuad;
end;
PScreenPixels = ^TScreenPixels;
TScreenPixels = array[0..c_BytePusherScrHeight - 1,
0..c_BytePusherScrWidth - 1] of Byte;
TStatusItem = (siState = 0, siFPS, siCalcTime, siRenderTime, siDrawingTime,
siSoundTime);
TMainForm = class(TForm)
pbScreen: TPaintBox;
odSnapshot: TOpenDialog;
stbStatus: TStatusBar;
tmrBenchmarks: TTimer;
AppEvents: TApplicationEvents;
MainMenu: TMainMenu;
miFile: TMenuItem;
miOpen: TMenuItem;
miSeparator1: TMenuItem;
miExit: TMenuItem;
miRun: TMenuItem;
miDoRun: TMenuItem;
miNextFrame: TMenuItem;
miPause: TMenuItem;
miReset: TMenuItem;
miOptions: TMenuItem;
miVolume: TMenuItem;
miBenchmarks: TMenuItem;
miHelp: TMenuItem;
miAbout: TMenuItem;
ActionList: TActionList;
acOpen: TAction;
acExit: TAction;
acRun: TAction;
acNextFrame: TAction;
acPause: TAction;
acReset: TAction;
acBenchmarks: TAction;
acAbout: TAction;
pnlKeyboard: TPanel;
pnlScreen: TPanel;
procedure pbScreenPaint(Sender: TObject);
procedure FormCreate(Sender: TObject);
procedure FormDestroy(Sender: TObject);
procedure tmrBenchmarksTimer(Sender: TObject);
procedure AppEventsIdle(Sender: TObject; var Done: Boolean);
procedure acOpenExecute(Sender: TObject);
procedure acExitExecute(Sender: TObject);
procedure acRunExecute(Sender: TObject);
procedure acNextFrameExecute(Sender: TObject);
procedure acPauseExecute(Sender: TObject);
procedure acResetExecute(Sender: TObject);
procedure acAboutExecute(Sender: TObject);
procedure acBenchmarksExecute(Sender: TObject);
procedure pnlScreenResize(Sender: TObject);
private
FVM: TBytePusherVM;
FVMKeyStates: TBytePusherKeyStates;
FScreenBitmapInfo: TScreenBitmapInfo;
FScreenPixels: PScreenPixels;
FSoundStreamer: TSoundStreamer;
FSoundBuffer: TSoundBuffer;
FSoundPos: Cardinal;
FIsSoundEnabled: Boolean;
FSoundVolume: Double; // 0.0 .. 1.0
FIsSnapshotLoaded: Boolean;
FLoadedSnapshotPath: string;
FIsRunning: Boolean;
FTimerFreq: Int64;
FFramePeriod: Int64;
FFrameTimer: Int64;
FFrameCount: Integer;
FPrevFrameTime: Int64;
FPrevBenchmarksTime: Int64;
FFrameCalcTime: TStopwatch; // for benchmarks
FFrameRenderTime: TStopwatch; // for benchmarks
FFrameDrawCount: Integer; // for benchmarks
FFrameDrawingTime: TStopwatch; // for benchmarks
FSoundSystemTime: TStopwatch; // for benchmarks
procedure CreateScreen;
procedure CreateKeyboard;
procedure CreateSoundStreamer;
procedure CreateBenchmarkTimers;
procedure FreeBenchmarkTimers;
procedure CreateVolumeMenu;
procedure AutoLoadFile;
procedure UpdateScreen;
procedure DrawScreen(ADC: HDC; ADstX, ADstY, ADstWidth, ADstHeight: Integer);
procedure DoVMFrame;
procedure PlaySound;
procedure FreeSoundBuffer;
procedure SetIsRunning(AIsRunning: Boolean);
procedure LoadSnapshot(const AFileName: string; ARun: Boolean);
procedure SetStatus(AItem: TStatusItem; const AFormat: string;
const AArgs: array of const);
function UpdateFrameIfNeeded: Boolean;
procedure UpdateActions; reintroduce;
procedure UpdateStatus;
function IsBenchmarkingActive: Boolean; inline;
procedure UpdateBenchmarks;
procedure ResetBenchmarks;
procedure SoundVolumeItemClick(Sender: TObject);
procedure VMKeyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
procedure VMKeyMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
public
end;
var
MainForm: TMainForm;
implementation
{$R *.dfm}
uses
Math;
procedure TMainForm.acAboutExecute(Sender: TObject);
var
lcText: string;
begin
{ "About..." }
lcText := Format('%s v%s'#13#13'Copyright (c) %s %s',
[c_ProgramName, c_ProgramVersion, c_ProgramYear, c_ProgramCopyright]);
Application.MessageBox(PChar(lcText), 'About', MB_ICONINFORMATION or MB_OK);
end;
procedure TMainForm.acBenchmarksExecute(Sender: TObject);
begin
{ "Show benchmarks" }
if IsBenchmarkingActive then
begin
tmrBenchmarks.Enabled := True;
ResetBenchmarks;
end
else
tmrBenchmarks.Enabled := False;
UpdateBenchmarks;
end;
procedure TMainForm.acExitExecute(Sender: TObject);
begin
{ "Exit" }
Close;
end;
procedure TMainForm.acNextFrameExecute(Sender: TObject);
begin
{ "Next frame" }
DoVMFrame;
end;
procedure TMainForm.acOpenExecute(Sender: TObject);
begin
{ "Open..." }
if odSnapshot.Execute then
LoadSnapshot(odSnapshot.FileName, True);
end;
procedure TMainForm.acPauseExecute(Sender: TObject);
begin
{ "Pause" }
SetIsRunning(False);
UpdateActions;
UpdateStatus;
UpdateBenchmarks;
end;
procedure TMainForm.acResetExecute(Sender: TObject);
begin
{ "Reset" }
LoadSnapshot(FLoadedSnapshotPath, FIsRunning);
UpdateScreen;
end;
procedure TMainForm.acRunExecute(Sender: TObject);
begin
{ "Run" }
SetIsRunning(True);
UpdateActions;
UpdateStatus;
UpdateBenchmarks;
end;
procedure TMainForm.AppEventsIdle(Sender: TObject; var Done: Boolean);
begin
if not FIsRunning then
Exit;
if not UpdateFrameIfNeeded then
Sleep(1);
Done := False;
end;
procedure TMainForm.AutoLoadFile;
var
lcFileToLoad: string;
begin
{$IFDEF DEBUG}
lcFileToLoad := 'Snapshots\Sprites.BytePusher';
{$ENDIF}
if ParamCount > 0 then
lcFileToLoad := ParamStr(1);
if (lcFileToLoad <> '') and FileExists(lcFileToLoad) then
LoadSnapshot(lcFileToLoad, True);
end;
procedure TMainForm.CreateBenchmarkTimers;
begin
FFrameCalcTime := TStopwatch.Create;
FFrameRenderTime := TStopwatch.Create;
FFrameDrawingTime := TStopwatch.Create;
FSoundSystemTime := TStopwatch.Create;
end;
procedure TMainForm.CreateKeyboard;
const
c_TopSpace = 25;
c_KeySize = 50;
c_FontSize = 14;
var
lcRows, lcCols, lcSideSpace, i, j: Integer;
lcKey: TBytePusherKey;
begin
lcRows := Length(c_BytePusherKeysLayout);
lcCols := Length(c_BytePusherKeysLayout[0]);
lcSideSpace := (pnlKeyboard.Width - lcCols * c_KeySize) div 2;
for i := 0 to lcRows - 1 do
for j := 0 to lcCols - 1 do
begin
lcKey := c_BytePusherKeysLayout[i, j];
with TSpeedButton.Create(pnlKeyboard) do
begin
Parent := pnlKeyboard;
Caption := c_BytePusherKeyNames[lcKey];
Tag := Ord(lcKey);
Font.Size := c_FontSize;
Left := lcSideSpace + c_KeySize * j;
Top := c_TopSpace + c_KeySize * i;
Width := c_KeySize;
Height := c_KeySize;
OnMouseDown := VMKeyMouseDown;
OnMouseUp := VMKeyMouseUp;
end;
end;
end;
procedure TMainForm.CreateScreen;
var
lcColor: Integer;
lcR, lcG, lcB: Byte;
begin
FillChar(FScreenBitmapInfo, SizeOf(FScreenBitmapInfo), 0);
with FScreenBitmapInfo.bmiHeader do
begin
biSize := SizeOf(TBitmapInfoHeader);
biWidth := c_BytePusherScrWidth;
biHeight := -c_BytePusherScrHeight; // the first line is on top
biPlanes := 1;
biBitCount := 8; // 8-bit indices in the color table
biCompression := BI_RGB;
end;
// create BytePusher's color table
for lcColor := 0 to 255 do
begin
if lcColor < 216 then
begin
lcR := lcColor div 36;
lcG := (lcColor - lcR * 36) div 6;
lcB := lcColor mod 6;
end
else begin
lcR := 0;
lcG := 0;
lcB := 0;
end;
with FScreenBitmapInfo.bmiColors[lcColor] do
begin
rgbRed := lcR * $33;
rgbGreen := lcG * $33;
rgbBlue := lcB * $33;
// rgbReserved already zeroed
end;
end;
New(FScreenPixels);
FillChar(FScreenPixels^, SizeOf(FScreenPixels^), 0);
end;
procedure TMainForm.CreateSoundStreamer;
var
lcParams: TSoundStreamerParams;
begin
with lcParams do
begin
Channels := c_BytePusherSoundChannels;
SamplesPerSec := c_BytePusherSamplesPerSec;
BitsPerSample := c_BytePusherBitsPerSample;
MaxBuffers := c_MaxSoundBuffers;
end;
FSoundStreamer := TSoundStreamer.Create(lcParams);
end;
procedure TMainForm.CreateVolumeMenu;
function CreateItem(AVolume: Integer): TMenuItem;
begin
Result := TMenuItem.Create(MainMenu);
miVolume.Add(Result);
with Result do
begin
case AVolume of
0: Caption := '&Mute';
100: Caption := '1&00%';
else
Caption := Format('&%d%%', [AVolume]);
end;
Tag := AVolume;
RadioItem := True;
AutoCheck := True;
OnClick := SoundVolumeItemClick;
end;
end; // CreateItem
var
i, lcVolume: Integer;
lcItem, lcActiveItem: TMenuItem;
begin
lcActiveItem := nil; // anti-warning
for i := 10 downto 0 do
begin
lcVolume := i * 10; // 0..100%
lcItem := CreateItem(lcVolume);
if lcVolume = c_DefaultSoundValue then
lcActiveItem := lcItem;
end;
lcActiveItem.Checked := True;
SoundVolumeItemClick(lcActiveItem);
end;
procedure TMainForm.DoVMFrame;
begin
FVM.SetKeyStates(FVMKeyStates);
if IsBenchmarkingActive then
FFrameCalcTime.Start;
try
FVM.CalcNextFrame;
finally
if IsBenchmarkingActive then
FFrameCalcTime.Stop;
end;
UpdateScreen;
PlaySound;
if IsBenchmarkingActive then
Inc(FFrameCount);
end;
procedure TMainForm.DrawScreen(ADC: HDC; ADstX, ADstY, ADstWidth,
ADstHeight: Integer);
begin
if IsBenchmarkingActive then
FFrameDrawingTime.Start;
try
StretchDIBits(ADC, ADstX, ADstY, ADstWidth, ADstHeight,
0, 0, c_BytePusherScrWidth, c_BytePusherScrHeight,
FScreenPixels, PBitmapInfo(@FScreenBitmapInfo)^, DIB_RGB_COLORS, SRCCOPY);
finally
if IsBenchmarkingActive then
begin
FFrameDrawingTime.Stop;
Inc(FFrameDrawCount);
end;
end;
end;
procedure TMainForm.FormCreate(Sender: TObject);
begin
Caption := c_ProgramName;
timeBeginPeriod(1); // for more accurate delay on Sleep(1)
QueryPerformanceFrequency(FTimerFreq);
FFramePeriod := FTimerFreq div c_BytePusherFPS; // 1 / c_BytePusherFPS * FTimerFreq
FVM := TBytePusherVM.Create;
CreateScreen;
CreateKeyboard;
CreateSoundStreamer;
CreateVolumeMenu;
CreateBenchmarkTimers;
SetIsRunning(False);
UpdateActions;
UpdateStatus;
{$IFDEF DEBUG}
acBenchmarks.Checked := True;
{$ENDIF}
UpdateBenchmarks;
AutoLoadFile;
end;
procedure TMainForm.FormDestroy(Sender: TObject);
begin
FreeBenchmarkTimers;
FSoundStreamer.Free;
Dispose(FScreenPixels);
FVM.Free;
timeEndPeriod(1);
end;
procedure TMainForm.FreeBenchmarkTimers;
begin
FSoundSystemTime.Free;
FFrameDrawingTime.Free;
FFrameRenderTime.Free;
FFrameCalcTime.Free;
end;
procedure TMainForm.FreeSoundBuffer;
begin
if FSoundBuffer <> nil then
begin
FSoundStreamer.CancelBuffer(FSoundBuffer);
FSoundBuffer := nil;
end;
end;
function TMainForm.IsBenchmarkingActive: Boolean;
begin
Result := FIsRunning and acBenchmarks.Checked;
end;
procedure TMainForm.LoadSnapshot(const AFileName: string; ARun: Boolean);
begin
FVM.LoadSnapshot(AFileName);
FIsSnapshotLoaded := True;
FLoadedSnapshotPath := AFileName;
Caption := Format('%s - %s', [ExtractFileName(FLoadedSnapshotPath), c_ProgramName]);
SetIsRunning(ARun);
FSoundStreamer.StopPlaying;
FreeSoundBuffer;
UpdateActions;
UpdateStatus;
UpdateBenchmarks;
end;
procedure TMainForm.pbScreenPaint(Sender: TObject);
begin
DrawScreen(pbScreen.Canvas.Handle, 0, 0, pbScreen.ClientWidth,
pbScreen.ClientHeight);
end;
procedure TMainForm.PlaySound;
var
lcVMSound: PByte;
i: Integer;
lcSample: Byte;
begin
if IsBenchmarkingActive then
FSoundSystemTime.Start;
try
if not FSoundStreamer.IsActive then
begin
FreeSoundBuffer;
Exit;
end;
if FSoundBuffer = nil then
begin
Assert((c_SoundBufferSize mod c_BytePusherSoundBufSize) = 0);
FSoundBuffer := FSoundStreamer.GetBuffer(c_SoundBufferSize);
if FSoundBuffer = nil then
Exit; // max buffers allocated, no free one
FSoundPos := 0;
end;
lcVMSound := FVM.GetSoundBuf;
Assert((FSoundBuffer.Size - FSoundPos) >= c_BytePusherSoundBufSize);
Assert((FSoundVolume >= 0.0) and (FSoundVolume <= 1.0));
if FIsSoundEnabled then
begin
// volume > 0
for i := 0 to c_BytePusherSoundBufSize - 1 do
begin
lcSample := Round(ShortInt(lcVMSound[i]) * FSoundVolume) + $80 {signed sample -> unsigned};
FSoundBuffer.Data[FSoundPos] := lcSample;
Inc(FSoundPos);
end;
end
else begin
// volume = 0, little optimization
FillChar(FSoundBuffer.Data[FSoundPos], c_BytePusherSoundBufSize, $80 {silence} );
Inc(FSoundPos, c_BytePusherSoundBufSize);
end;
if FSoundPos = FSoundBuffer.Size then
begin
if FIsRunning and FIsSoundEnabled then
begin
FSoundStreamer.PlayBuffer(FSoundBuffer);
FSoundBuffer := nil;
end
else
FSoundPos := 0; // in "next frame" mode don't play sound, just clear buffer
end;
finally
if IsBenchmarkingActive then
FSoundSystemTime.Stop;
end;
end;
procedure TMainForm.pnlScreenResize(Sender: TObject);
var
lcScrSize: Integer;
begin
lcScrSize := Min(pnlScreen.Width, pnlScreen.Height);
pbScreen.Left := (pnlScreen.Width - lcScrSize) div 2;
pbScreen.Top := (pnlScreen.Height - lcScrSize) div 2;
pbScreen.Width := lcScrSize;
pbScreen.Height := lcScrSize;
end;
procedure TMainForm.ResetBenchmarks;
begin
FFrameCount := 0;
FFrameDrawCount := 0;
FFrameCalcTime.Reset;
FFrameRenderTime.Reset;
FFrameDrawingTime.Reset;
FSoundSystemTime.Reset;
QueryPerformanceCounter(FPrevBenchmarksTime);
end;
procedure TMainForm.SetIsRunning(AIsRunning: Boolean);
begin
FIsRunning := AIsRunning;
tmrBenchmarks.Enabled := IsBenchmarkingActive;
if FIsRunning then
begin
FFrameTimer := 0;
QueryPerformanceCounter(FPrevFrameTime);
ResetBenchmarks;
end
else
FSoundStreamer.StopPlaying;
end;
procedure TMainForm.SetStatus(AItem: TStatusItem; const AFormat: string;
const AArgs: array of const);
begin
stbStatus.Panels[Ord(AItem)].Text := Format(AFormat, AArgs);
end;
procedure TMainForm.SoundVolumeItemClick(Sender: TObject);
var
lcVolume: Integer;
begin
lcVolume := (Sender as TMenuItem).Tag;
Assert((lcVolume >= 0) and (lcVolume <= 100));
FIsSoundEnabled := lcVolume > 0;
FSoundVolume := lcVolume / 100;
if not FIsSoundEnabled then
begin
FSoundStreamer.StopPlaying;
FreeSoundBuffer;
end;
end;
procedure TMainForm.tmrBenchmarksTimer(Sender: TObject);
begin
UpdateBenchmarks;
end;
procedure TMainForm.UpdateActions;
begin
acRun.Enabled := FIsSnapshotLoaded and not FIsRunning;
acNextFrame.Enabled := FIsSnapshotLoaded and not FIsRunning;
acPause.Enabled := FIsSnapshotLoaded and FIsRunning;
acReset.Enabled := FIsSnapshotLoaded;
end;
procedure TMainForm.UpdateBenchmarks;
var
lcCurTime: Int64;
lcFPS, lcCalcTime, lcRenderTime, lcDrawingTime, lcSoundTime: Double;
begin
if not IsBenchmarkingActive then
begin
SetStatus(siFPS, '', []);
SetStatus(siCalcTime, '', []);
SetStatus(siRenderTime, '', []);
SetStatus(siDrawingTime, '', []);
SetStatus(siSoundTime, '', []);
Exit;
end;
QueryPerformanceCounter(lcCurTime);
lcFPS := FFrameCount / ((lcCurTime - FPrevBenchmarksTime) / FTimerFreq);
SetStatus(siFPS, 'FPS: %d', [Trunc(lcFPS)]);
if FFrameCount > 0 then
begin
lcCalcTime := FFrameCalcTime.ElapsedMillisecondsF / FFrameCount;
lcRenderTime := FFrameRenderTime.ElapsedMillisecondsF / FFrameCount;
end
else begin
lcCalcTime := 0.0;
lcRenderTime := 0.0;
end;
SetStatus(siCalcTime, 'VM: %.2f ms', [lcCalcTime]);
SetStatus(siRenderTime, 'Render: %.2f ms', [lcRenderTime]);
if FFrameDrawCount > 0 then
lcDrawingTime := FFrameDrawingTime.ElapsedMillisecondsF / FFrameDrawCount
else
lcDrawingTime := 0.0;
SetStatus(siDrawingTime, 'Draw: %.2f ms', [lcDrawingTime]);
if FFrameCount > 0 then
lcSoundTime := FSoundSystemTime.ElapsedMillisecondsF / FFrameCount
else
lcSoundTime := 0.0;
SetStatus(siSoundTime, 'Sound: %.2f ms', [lcSoundTime]);
ResetBenchmarks;
end;
function TMainForm.UpdateFrameIfNeeded: Boolean;
var
lcCurTime, lcDeltaTime: Int64;
begin
QueryPerformanceCounter(lcCurTime);
lcDeltaTime := lcCurTime - FPrevFrameTime;
Inc(FFrameTimer, lcDeltaTime);
if FFrameTimer >= FFramePeriod then
begin
FFrameTimer := FFrameTimer mod FFramePeriod;
DoVMFrame;
Result := True;
end
else
Result := False;
FPrevFrameTime := lcCurTime;
end;
procedure TMainForm.UpdateScreen;
var
lcScreenBuf: PByte;
begin
if IsBenchmarkingActive then
FFrameRenderTime.Start;
try
lcScreenBuf := FVM.GetScreenBuf;
// Remember: all scanlines in the DIB (FScreenPixels) must be 4-byte aligned.
// Since we have 256 bytes per line, this requirement is already met.
Assert(((c_BytePusherScrWidth * SizeOf(Byte)) mod 4) = 0);
Assert(SizeOf(FScreenPixels^) = c_BytePusherScrBufSize);
Move(lcScreenBuf^, FScreenPixels^, c_BytePusherScrBufSize);
finally
if IsBenchmarkingActive then
FFrameRenderTime.Stop;
end;
pbScreen.Invalidate;
end;
procedure TMainForm.UpdateStatus;
begin
if not FIsSnapshotLoaded then
SetStatus(siState, 'Snapshot not loaded', [])
else
if FIsRunning then
SetStatus(siState, 'Running', [])
else
SetStatus(siState, 'Paused', []);
end;
procedure TMainForm.VMKeyMouseDown(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
lcKey: TBytePusherKey;
begin
lcKey := TBytePusherKey((Sender as TSpeedButton).Tag);
FVMKeyStates[lcKey] := True;
end;
procedure TMainForm.VMKeyMouseUp(Sender: TObject; Button: TMouseButton;
Shift: TShiftState; X, Y: Integer);
var
lcKey: TBytePusherKey;
begin
lcKey := TBytePusherKey((Sender as TSpeedButton).Tag);
FVMKeyStates[lcKey] := False;
end;
end.