Changeset 65 for trunk/Forms/UFormMain.pas
- Timestamp:
- Jan 23, 2020, 12:21:53 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.pas
r64 r65 7 7 uses 8 8 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; 10 11 11 12 type 13 TFormMain = class; 14 15 { TMoveThread } 16 17 TMoveThread = class(TThread) 18 FormMain: TFormMain; 19 procedure Execute; override; 20 end; 12 21 13 22 { TFormMain } … … 39 48 procedure FormShow(Sender: TObject); 40 49 procedure TimerDrawTimer(Sender: TObject); 50 procedure EraseBackground(DC: HDC); override; 41 51 private 52 DrawDuration: TDateTime; 42 53 MouseStart: TPoint; 43 54 MouseDown: Boolean; 44 55 RedrawPending: Boolean; 45 56 MoveBuffer: array of TMoveDirection; 57 MoveBufferLock: TCriticalSection; 58 MoveThread: TMoveThread; 46 59 procedure AddToMoveBuffer(Direction: TMoveDirection); 47 60 procedure ProcessMoveBuffer; … … 60 73 uses 61 74 UCore; 75 76 { TMoveThread } 77 78 procedure TMoveThread.Execute; 79 begin 80 while not Terminated do begin 81 FormMain.ProcessMoveBuffer; 82 Sleep(10); 83 end; 84 end; 62 85 63 86 { TFormMain } … … 72 95 40: AddToMoveBuffer(drDown); 73 96 end; 74 ProcessMoveBuffer;97 //ProcessMoveBuffer; 75 98 end; 76 99 end; … … 111 134 else if (Angle > 135) and (Angle <= 225) then AddToMoveBuffer(drLeft) 112 135 else if (Angle > 225) and (Angle <= 315) then AddToMoveBuffer(drUp); 113 ProcessMoveBuffer;136 //ProcessMoveBuffer; 114 137 end; 115 138 end; … … 123 146 124 147 procedure TFormMain.FormPaint(Sender: TObject); 125 begin 148 var 149 TimeStart: TDateTime; 150 begin 151 TimeStart := Now; 126 152 Core.Game.Render(Canvas, Point(Width, Height - MainMenu1.Height)); 153 DrawDuration := Now - TimeStart; 127 154 end; 128 155 … … 130 157 begin 131 158 Core.PersistentForm1.Save(Self); 159 ControlStyle := [csOpaque]; 132 160 end; 133 161 134 162 procedure TFormMain.FormCreate(Sender: TObject); 135 163 begin 164 MoveBufferLock := TCriticalSection.Create; 165 MoveThread := TMoveThread.Create(True); 166 MoveThread.FormMain := Self; 167 MoveThread.FreeOnTerminate := False; 168 MoveThread.Start; 136 169 end; 137 170 138 171 procedure TFormMain.FormDestroy(Sender: TObject); 139 172 begin 173 FreeAndNil(MoveThread); 174 FreeAndNil(MoveBuffer); 140 175 end; 141 176 … … 156 191 RedrawPending := False; 157 192 Repaint; 158 end; 193 Caption := FloatToStr(Round(DrawDuration / OneMillisecond)); 194 end; 195 end; 196 197 procedure TFormMain.EraseBackground(DC: HDC); 198 begin 199 // Do nothing 159 200 end; 160 201 161 202 procedure TFormMain.AddToMoveBuffer(Direction: TMoveDirection); 162 203 begin 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; 165 211 end; 166 212 … … 168 214 begin 169 215 if not Core.Game.Moving then begin 216 MoveBufferLock.Acquire; 170 217 while Length(MoveBuffer) > 0 do begin 218 MoveBufferLock.Release; 171 219 Core.Game.MoveAllAndUpdate(MoveBuffer[0], True); 220 MoveBufferLock.Acquire; 172 221 if Length(MoveBuffer) > 1 then 173 222 Move(MoveBuffer[1], MoveBuffer[0], (Length(MoveBuffer) - 1) * SizeOf(TMoveDirection)); … … 175 224 SetLength(MoveBuffer, Length(MoveBuffer) - 1); 176 225 end; 177 end else begin226 MoveBufferLock.Release; 178 227 end; 179 228 end;
Note:
See TracChangeset
for help on using the changeset viewer.