Subversion Repositories plumbers

Rev

Rev 17 | Rev 20 | Go to most recent revision | Blame | Compare with Previous | Last modification | View Log | RSS feed

  1. unit Main;
  2.  
  3. // BUG: If you drag the window, the dia show will stop playing, but the sound continues! This makes everything out of sync.
  4. // TODO: When the windows is only resized a little bit (A few pixels), the window should not centered
  5. //       ... Calc the width and height of ALL pictures, and then size the form to the biggest value?
  6. //       ... or hard code the resolution in the INI file?
  7. // Idea: Ini Parameter if fullscreen is applied or not
  8. // Idea: Savestates, speedup, pause, Use Space bar to go to the next decision point.
  9.  
  10. // -----------------------------------------------------------------------------
  11.  
  12. // HOTSPOT_RELATIVE_ORIGIN is a new behavior which is not compatible with the original engine.
  13. // With HOTSPOT_RELATIVE_ORIGIN enabled, the coordinates will be relative to the picture
  14. // The original game has the origin at the top left corner of the screen.
  15. // This is a problem because the game as well as the scene editor does not know the
  16. // desired resolution, as it is automatically determined.
  17. // If we would hardcode the desired canvas (640x480) in <ExeName>.ini, then
  18. // it would work, but then, the scene Editor can not know the desired resolution...
  19. {$DEFINE HOTSPOT_RELATIVE_ORIGIN}
  20.  
  21. interface
  22.  
  23. uses
  24.   Windows, Messages, SysUtils, Classes, Graphics, Controls, Forms,
  25.   Dialogs, ExtCtrls, StdCtrls, Game, MPlayer, SyncObjs;
  26.  
  27. type
  28.   TMainForm = class(TForm)
  29.     Image1: TImage;
  30.     Panel1: TPanel;
  31.     StartupTimer: TTimer;
  32.     MediaPlayer1: TMediaPlayer;
  33.     Panel2: TPanel;
  34.     procedure FormCreate(Sender: TObject);
  35.     procedure FormKeyDown(Sender: TObject; var Key: Word; Shift: TShiftState);
  36.     procedure StartupTimerTimer(Sender: TObject);
  37.     procedure ControlClick(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  38.     procedure FormDestroy(Sender: TObject);
  39.   private
  40.     FHotspots: array[0..2] of THotspot;
  41.     FullscreenMode: boolean;
  42.     procedure cbPictureShow(ASender: TGame; AFilename: string; AType: TPictureType);
  43.     procedure cbAsyncSound(ASender: TGame; AFilename: string);
  44.     procedure cbExit(ASender: TGame);
  45.     function cbWait(ASender: TGame; AMilliseconds: integer): boolean;
  46.     procedure cbSetHotspot(ASender: TGame; AIndex: THotspotIndex; AHotspot: THotspot);
  47.     procedure cbClearHotspots(ASender: TGame);
  48.     procedure ClickEvent(X, Y: Integer);
  49.   private
  50.     FCancelSceneRequest: boolean;
  51.     csCancelSceneRequest: TCriticalSection;
  52.   public
  53.     game: TGame;
  54.   end;
  55.  
  56. var
  57.   MainForm: TMainForm;
  58.  
  59. implementation
  60.  
  61. {$R *.dfm}
  62.  
  63. uses
  64.   MMSystem, IniFiles, Math, GameBinStruct;
  65.  
  66. function AddThouSeps(const S: string): string;
  67. var
  68.   LS, L2, I, N: Integer;
  69.   Temp: string;
  70. begin
  71.   // http://www.delphigroups.info/2/11/471892.html
  72.   result := S ;
  73.   LS := Length(S);
  74.   N := 1 ;
  75.   if LS > 1 then
  76.   begin
  77.     if S [1] = '-' then  // check for negative value
  78.     begin
  79.       N := 2;
  80.       LS := LS - 1;
  81.     end;
  82.   end;
  83.   if LS <= 3 then exit;
  84.   L2 := (LS - 1) div 3;
  85.   Temp := '';
  86.   for I := 1 to L2 do
  87.   begin
  88.     Temp := {$IF not Declared(ThousandSeparator)}FormatSettings.{$IFEND}ThousandSeparator + Copy (S, LS - 3 * I + 1, 3) + Temp;
  89.   end;
  90.   Result := Copy (S, N, (LS - 1) mod 3 + 1) + Temp;
  91.   if N > 1 then Result := '-' + Result;
  92. end;
  93.  
  94. { TMainForm }
  95.  
  96. procedure TMainForm.cbPictureShow(ASender: TGame; AFilename: string; AType: TPictureType);
  97. resourcestring
  98.   S_YOUR_SCORE = 'Your score is: %s';
  99. begin
  100.   {$IFDEF DEBUG}
  101.   Caption := AFileName;
  102.   {$ENDIF}
  103.  
  104.   if FileExists(AFilename) then
  105.   begin
  106.     Image1.Visible := false;
  107.     try
  108.       Image1.Picture.LoadFromFile(AFilename);
  109.       Image1.Autosize := true;
  110.     finally
  111.       // This speeds up the picture loading on very old computers
  112.       Image1.Visible := true;
  113.     end;
  114.  
  115.     // Make form bigger if necessary
  116.     if Image1.Width > ClientWidth then
  117.     begin
  118.       ClientWidth := Min(Image1.Width, Screen.Width);
  119.       if (ClientWidth >= Screen.Width) then FullscreenMode := true;
  120.       Position := poScreenCenter;
  121.     end;
  122.     if Image1.Height > ClientHeight then
  123.     begin
  124.       ClientHeight := Min(Image1.Height, Screen.Height);
  125.       if (ClientHeight >= Screen.Height) then FullscreenMode := true;
  126.       Position := poScreenCenter;
  127.     end;
  128.  
  129.     // Center image
  130.     Image1.Left := ClientWidth div 2 - Image1.Width div 2;
  131.     Image1.Top := ClientHeight div 2 - Image1.Height div 2;
  132.   end
  133.   else
  134.   begin
  135.     ShowMessageFmt('File not found: %s', [AFileName]);
  136.     Image1.Picture := nil;
  137.   end;
  138.  
  139.   if FullScreenMode then
  140.   begin
  141.     BorderStyle := bsNone;
  142.     FormStyle := fsStayOnTop;
  143.     Case AType of
  144.       ptDia: Screen.Cursor := -1;
  145.       ptDecision: Screen.Cursor := 0;
  146.     End;
  147.   end;
  148.  
  149.   Panel1.Caption := Format(S_YOUR_SCORE, [AddThouSeps(IntToStr(ASender.Score))]);
  150.   Panel1.Left := 8;
  151.   Panel1.Top := Min(ClientHeight, Screen.Height) - Panel1.Height - 8;
  152.   Panel1.Visible := AType = ptDecision;
  153. end;
  154.  
  155. procedure TMainForm.cbAsyncSound(ASender: TGame; AFilename: string);
  156. begin
  157.   PlaySound(nil, hinstance, 0);
  158.   if FileExists(AFilename) then
  159.   begin
  160.     PlaySound(PChar(AFilename), hinstance, SND_FILENAME or SND_ASYNC);
  161.   end;
  162. end;
  163.  
  164. procedure TMainForm.cbSetHotspot(ASender: TGame; AIndex: THotspotIndex; AHotspot: THotspot);
  165. begin
  166.   FHotspots[AIndex] := AHotspot;
  167. end;
  168.  
  169. procedure TMainForm.cbClearHotspots(ASender: TGame);
  170. var
  171.   i: Integer;
  172. begin
  173.   for i := Low(FHotspots) to High(FHotspots) - 1 do
  174.   begin
  175.     FHotspots[i].lpAction := nil;
  176.   end;
  177. end;
  178.  
  179. procedure TMainForm.cbExit(ASender: TGame);
  180. begin
  181.   Application.Terminate;
  182. end;
  183.  
  184. function TMainForm.cbWait(ASender: TGame; AMilliseconds: integer): boolean;
  185. var
  186.   FirstTickCount: DWord;
  187. begin
  188.   //Cursor := crHourglass;
  189.   try
  190.     result := false; // don't cancel
  191.     FirstTickCount := GetTickCount; // TODO: Attention, GetTickCount can overflow
  192.     while not Application.Terminated and ((GetTickCount - FirstTickCount) < AMilliseconds) do
  193.     begin
  194.       csCancelSceneRequest.Acquire;
  195.       try
  196.         if FCancelSceneRequest then
  197.         begin
  198.           FCancelSceneRequest := false;
  199.           result := true; // cancel
  200.           exit;
  201.         end;
  202.       finally
  203.         csCancelSceneRequest.Release;
  204.       end;
  205.       Application.ProcessMessages;
  206.       Sleep(0);
  207.     end;
  208.   finally
  209.     //Cursor := crDefault;
  210.   end;
  211. end;
  212.  
  213. procedure TMainForm.FormCreate(Sender: TObject);
  214. var
  215.   ini: TMemIniFile;
  216.   iniFilename: string;
  217. begin
  218.   csCancelSceneRequest := TCriticalSection.Create;
  219.   iniFilename := ChangeFileExt(ExtractFileName(ParamStr(0)), '.ini');
  220.  
  221.   DoubleBuffered := true;
  222.  
  223.   if FileExists(iniFilename) then
  224.   begin
  225.     ini := TMemIniFile.Create(iniFilename);
  226.     try
  227.       Caption := ini.ReadString('Config', 'Title', '');
  228.     finally
  229.       FreeAndNil(ini);
  230.     end;
  231.   end;
  232.  
  233.   try
  234.     StartupTimer.Enabled := true;
  235.   except
  236.     Application.Terminate;
  237.   end;
  238. end;
  239.  
  240. procedure TMainForm.FormDestroy(Sender: TObject);
  241. begin
  242.   FreeAndNil(Game);
  243.  
  244.   // Without this, some audio drivers could crash if you press ESC to end the game.
  245.   // (VPC 2007 with Win95; cpsman.dll crashes sometimes)
  246.   PlaySound(nil, hinstance, 0);
  247.   FreeAndNil(csCancelSceneRequest);
  248.   if Assigned(Game) then FreeAndNil(Game);
  249. end;
  250.  
  251. procedure TMainForm.FormKeyDown(Sender: TObject; var Key: Word;
  252.   Shift: TShiftState);
  253. begin
  254.   if Key = VK_SPACE then
  255.   begin
  256.     if MediaPlayer1.Mode = mpPlaying then MediaPlayer1.Stop;
  257.   end;
  258.   if KEY = VK_RETURN then
  259.   begin
  260.     if MediaPlayer1.Mode = mpPlaying then
  261.     begin
  262.       MediaPlayer1.Position := MediaPlayer1.EndPos;
  263.     end;
  264.     csCancelSceneRequest.Acquire;
  265.     try
  266.       FCancelSceneRequest := true;
  267.     finally
  268.       csCancelSceneRequest.Release;
  269.     end;
  270.   end;
  271.   if Key = VK_ESCAPE then Close;
  272. end;
  273.  
  274. procedure TMainForm.ClickEvent(X, Y: Integer);
  275. var
  276.   i: integer;
  277.   ac: TActionDef;
  278. begin
  279.   // Debug: Go to prev decision by clicking on the top left edge
  280.   if (X < 20) and (Y < 20) then
  281.   begin
  282.     // TODO: Also allow to go back multiple steps
  283.     ac.scoreDelta := 0;
  284.     ac.nextSceneID := SCENEID_PREVDECISION;
  285.     ac.sceneSegment := 0;
  286.     Game.PerformAction(@ac);
  287.     Exit;
  288.   end;
  289.  
  290.   // If hotspots are overlaying, the lowest action will be chosen (same behavior as original game)
  291.   for i := Low(FHotspots) to High(FHotspots) do
  292.   begin
  293.     if Assigned(FHotspots[i].lpAction) and
  294.        (X >= FHotspots[i].cHotspotTopLeft.X) and
  295.        (Y >= FHotspots[i].cHotspotTopLeft.Y) and
  296.        (X <= FHotspots[i].cHotspotBottomRight.X) and
  297.        (Y <= FHotspots[i].cHotspotBottomRight.Y) then
  298.     begin
  299.       FHotspots[i].Game.PerformAction(FHotspots[i].lpAction);
  300.       Exit;
  301.     end;
  302.   end;
  303. end;
  304.  
  305. procedure TMainForm.ControlClick(Sender: TObject; Button: TMouseButton; Shift: TShiftState; X, Y: Integer);
  306. begin
  307.   {$IFDEF HOTSPOT_RELATIVE_ORIGIN}
  308.   ClickEvent(X, Y);
  309.   {$ELSE}
  310.   ClickEvent(X+TControl(Sender).Left, Y+TControl(Sender).Top);
  311.   {$ENDIF}
  312. end;
  313.  
  314. procedure TMainForm.StartupTimerTimer(Sender: TObject);
  315. begin
  316.   StartupTimer.Enabled := false;
  317.  
  318.   if FileExists('INTRO.AVI') then
  319.   begin
  320.     MediaPlayer1.FileName := 'INTRO.AVI';
  321.     MediaPlayer1.Open;
  322.  
  323.     Panel2.Visible := true;
  324.     Panel2.Top := 0;
  325.     Panel2.Left := 0;
  326.     Panel2.Width  := MediaPlayer1.DisplayRect.Right;
  327.     Panel2.Height := MediaPlayer1.DisplayRect.Bottom;
  328.  
  329.     ClientWidth := Panel2.Width;
  330.     if (ClientWidth >= Screen.Width) then FullscreenMode := true;
  331.     ClientHeight := Panel2.Height;
  332.     if (ClientHeight >= Screen.Height) then FullscreenMode := true;
  333.     Position := poScreenCenter;
  334.  
  335.     if FullScreenMode then
  336.     begin
  337.       BorderStyle := bsNone;
  338.       FormStyle := fsStayOnTop;
  339.       Screen.Cursor := -1;
  340.     end;
  341.  
  342.     // For some reason, "Position := poScreenCenter" causes the video handle to break!
  343.     // we need to close+open it again!
  344.     MediaPlayer1.Close;
  345.     MediaPlayer1.Open;
  346.  
  347.     MediaPlayer1.Play;
  348.     while MediaPlayer1.Mode <> mpStopped do
  349.     begin
  350.       Sleep(100);
  351.       Application.ProcessMessages;
  352.       if Application.Terminated then break;
  353.     end;
  354.  
  355.     MediaPlayer1.Close;
  356.     Panel2.Visible := false;
  357.     Screen.Cursor := 0;
  358.   end;
  359.  
  360.   try
  361.     Game := TGame.Create('.');
  362.     Game.PictureShowCallback := cbPictureShow;
  363.     Game.AsyncSoundCallback := cbAsyncSound;
  364.     Game.ExitCallback := cbExit;
  365.     Game.WaitCallback := cbWait;
  366.     Game.SetHotspotCallback := cbSetHotspot;
  367.     Game.ClearHotspotsCallback := cbClearHotspots;
  368.     Game.Run;
  369.   except
  370.     on E: Exception do
  371.     begin
  372.       MessageDlg(E.Message, mtError, [mbOK], 0);
  373.       Close;
  374.     end;
  375.   end;
  376. end;
  377.  
  378. end.
  379.