Changeset 65 for trunk/Forms


Ignore:
Timestamp:
Jan 23, 2020, 12:21:53 AM (5 years ago)
Author:
chronos
Message:
  • Added: Separated TGame paint and state change events.
  • Modified: Animate tiles movements in background thread.
  • Modified: Initialize TCore as first application form.
Location:
trunk/Forms
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/Forms/UFormMain.lfm

    r64 r65  
    5555  end
    5656  object TimerDraw: TTimer
    57     Interval = 20
     57    Interval = 10
    5858    OnTimer = TimerDrawTimer
    5959    left = 138
  • trunk/Forms/UFormMain.pas

    r64 r65  
    77uses
    88  Classes, SysUtils, FileUtil, Forms, Controls, Graphics, Dialogs, Menus, Math,
    9   ActnList, ExtCtrls, StdCtrls, UGame, UPersistentForm, UApplicationInfo;
     9  ActnList, ExtCtrls, StdCtrls, UGame, UPersistentForm, UApplicationInfo,
     10  LCLType, Syncobjs, DateUtils;
    1011
    1112type
     13  TFormMain = class;
     14
     15  { TMoveThread }
     16
     17  TMoveThread = class(TThread)
     18    FormMain: TFormMain;
     19    procedure Execute; override;
     20  end;
    1221
    1322  { TFormMain }
     
    3948    procedure FormShow(Sender: TObject);
    4049    procedure TimerDrawTimer(Sender: TObject);
     50    procedure EraseBackground(DC: HDC); override;
    4151  private
     52    DrawDuration: TDateTime;
    4253    MouseStart: TPoint;
    4354    MouseDown: Boolean;
    4455    RedrawPending: Boolean;
    4556    MoveBuffer: array of TMoveDirection;
     57    MoveBufferLock: TCriticalSection;
     58    MoveThread: TMoveThread;
    4659    procedure AddToMoveBuffer(Direction: TMoveDirection);
    4760    procedure ProcessMoveBuffer;
     
    6073uses
    6174  UCore;
     75
     76{ TMoveThread }
     77
     78procedure TMoveThread.Execute;
     79begin
     80  while not Terminated do begin
     81    FormMain.ProcessMoveBuffer;
     82    Sleep(10);
     83  end;
     84end;
    6285
    6386{ TFormMain }
     
    7295      40: AddToMoveBuffer(drDown);
    7396    end;
    74     ProcessMoveBuffer;
     97    //ProcessMoveBuffer;
    7598  end;
    7699end;
     
    111134      else if (Angle > 135) and (Angle <= 225) then AddToMoveBuffer(drLeft)
    112135      else if (Angle > 225) and (Angle <= 315) then AddToMoveBuffer(drUp);
    113       ProcessMoveBuffer;
     136      //ProcessMoveBuffer;
    114137    end;
    115138  end;
     
    123146
    124147procedure TFormMain.FormPaint(Sender: TObject);
    125 begin
     148var
     149  TimeStart: TDateTime;
     150begin
     151  TimeStart := Now;
    126152  Core.Game.Render(Canvas, Point(Width, Height - MainMenu1.Height));
     153  DrawDuration := Now - TimeStart;
    127154end;
    128155
     
    130157begin
    131158  Core.PersistentForm1.Save(Self);
     159  ControlStyle := [csOpaque];
    132160end;
    133161
    134162procedure TFormMain.FormCreate(Sender: TObject);
    135163begin
     164  MoveBufferLock := TCriticalSection.Create;
     165  MoveThread := TMoveThread.Create(True);
     166  MoveThread.FormMain := Self;
     167  MoveThread.FreeOnTerminate := False;
     168  MoveThread.Start;
    136169end;
    137170
    138171procedure TFormMain.FormDestroy(Sender: TObject);
    139172begin
     173  FreeAndNil(MoveThread);
     174  FreeAndNil(MoveBuffer);
    140175end;
    141176
     
    156191    RedrawPending := False;
    157192    Repaint;
    158   end;
     193    Caption := FloatToStr(Round(DrawDuration / OneMillisecond));
     194  end;
     195end;
     196
     197procedure TFormMain.EraseBackground(DC: HDC);
     198begin
     199  // Do nothing
    159200end;
    160201
    161202procedure TFormMain.AddToMoveBuffer(Direction: TMoveDirection);
    162203begin
    163   SetLength(MoveBuffer, Length(MoveBuffer) + 1);
    164   MoveBuffer[Length(MoveBuffer) - 1] := Direction;
     204  MoveBufferLock.Acquire;
     205  try
     206    SetLength(MoveBuffer, Length(MoveBuffer) + 1);
     207    MoveBuffer[Length(MoveBuffer) - 1] := Direction;
     208  finally
     209    MoveBufferLock.Release;
     210  end;
    165211end;
    166212
     
    168214begin
    169215  if not Core.Game.Moving then begin
     216    MoveBufferLock.Acquire;
    170217    while Length(MoveBuffer) > 0 do begin
     218      MoveBufferLock.Release;
    171219      Core.Game.MoveAllAndUpdate(MoveBuffer[0], True);
     220      MoveBufferLock.Acquire;
    172221      if Length(MoveBuffer) > 1 then
    173222        Move(MoveBuffer[1], MoveBuffer[0], (Length(MoveBuffer) - 1) * SizeOf(TMoveDirection));
     
    175224        SetLength(MoveBuffer, Length(MoveBuffer) - 1);
    176225    end;
    177   end else begin
     226    MoveBufferLock.Release;
    178227  end;
    179228end;
Note: See TracChangeset for help on using the changeset viewer.