{===============================================================}
{ }
{ File: unt_Game.pas }
{ Created By: Name Goes Here }
{ Modified By: N/A }
{ Creation Date: N/A }
{ Last Modified On: N/A }
{ }
{ Description: Simple template for a game in Delphi. }
{ }
{---------------------------------------------------------------}
{ Copyright © XXXX-XXXX All Rights Reserved }
{===============================================================}
UNIT unt_Game;
{==============================================================================}
INTERFACE
USES Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms, Dialogs;
{==============================================================================}
{Global DataType / Constant / Variable Declarations}
{------------------------------------------------------------------------------}
TYPE
TGameState = (gsDemo, gsPlaying, gsIntermission, gsPaused, gsGameOver);
Tfrm_Game = class(TForm)
procedure FormCreate(Sender: TObject);
procedure OnIdleHandler(Sender: TObject; var Done: Boolean);
procedure FormClose(Sender: TObject; var Action: TCloseAction);
private
{ Private declarations }
public
{ Public declarations }
end;
CONST
Throttle = trunc(1000 / 30); {30fps Throttle}
VAR
frm_Game: Tfrm_Game;
GameRunning: Boolean;
GameState: TGameState;
{Function / Procedure ProtoTypes}
{------------------------------------------------------------------------------}
{None needed yet} {mainly used for forward delcarations}
{==============================================================================}
IMPLEMENTATION {$R *.DFM}
{==============================================================================}
{Game Event Handlers}
{------------------------------------------------------------------------------}
procedure Pause(MilliSeconds: Integer);
var
CurrentTime: LongInt;
PauseTime: LongInt;
begin
CurrentTime := GetTickCount;
PauseTime := CurrentTime + MilliSeconds;
while CurrentTime < (PauseTime) do begin
Application.ProcessMessages;
CurrentTime := GetTickCount;
end;
end;
procedure doDemo();
begin
//Display automated demonstration/intro here
Application.ProcessMessages;
end;
procedure doPlaying();
begin
//Perform all gameplay here
Application.ProcessMessages;
end;
procedure doIntermission();
begin
//Perform inter-level loads etc. here
Application.ProcessMessages;
end;
procedure doPause();
begin
//Pause gameplay here
Application.ProcessMessages;
end;
procedure doGameOver();
begin
//Display game over screen and cleanup here
Application.ProcessMessages;
end;
procedure GameLoop();
var
TimeIndex: LongInt;
begin
while GameRunning do begin
//Record time before processing
TimeIndex := LongInt(GetTickCount);
//Process game
case GameState of
gsDemo: doDemo;
gsPlaying: doPlaying;
gsIntermission: doIntermission;
gsPaused: doPause;
gsGameOver: doGameOver;
end;
//Calculate time it took to process
TimeIndex := LongInt(GetTickCount) - TimeIndex;
//Pause if it was too quick
if (TimeIndex < Throttle) then begin
Pause(Throttle - TimeIndex);
end;
Application.ProcessMessages;
end;
end;
{Form Event Handlers}
{------------------------------------------------------------------------------}
procedure Tfrm_Game.OnIdleHandler(Sender: TObject; var Done: Boolean);
begin
GameLoop;
end;
procedure Tfrm_Game.FormCreate(Sender: TObject);
begin
GameState := gsDemo;
GameRunning := True;
Application.OnIdle := OnIdleHandler;
end;
procedure Tfrm_Game.FormClose(Sender: TObject; var Action: TCloseAction);
begin
GameRunning := False;
end;
{===============================================================}
{ Copyright © XXXX-XXXX All Rights Reserved }
{===============================================================}
END.