Changeset 65
- Timestamp:
- Jan 23, 2020, 12:21:53 AM (5 years ago)
- Location:
- trunk
- Files:
-
- 5 edited
Legend:
- Unmodified
- Added
- Removed
-
trunk/Forms/UFormMain.lfm
r64 r65 55 55 end 56 56 object TimerDraw: TTimer 57 Interval = 2057 Interval = 10 58 58 OnTimer = TimerDrawTimer 59 59 left = 138 -
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; -
trunk/Game2048.lpr
r54 r65 29 29 Application.Scaled := True; 30 30 Application.Initialize; 31 Application.CreateForm(TCore, Core); 31 32 Application.CreateForm(TFormMain, FormMain); 32 Application.CreateForm(TCore, Core);33 33 Application.Run; 34 34 end. -
trunk/UCore.pas
r51 r65 40 40 private 41 41 procedure GameChange(Sender: TObject); 42 procedure GamePaint(Sender: TObject); 42 43 procedure GameWin(Sender: TObject); 43 44 procedure GameOver(Sender: TObject); … … 80 81 Game.Board.Size := Point(4, 4); 81 82 Game.OnChange := GameChange; 83 Game.OnPaint := GamePaint; 82 84 Game.OnWin := GameWin; 83 85 Game.OnGameOver := GameOver; … … 179 181 procedure TCore.GameChange(Sender: TObject); 180 182 begin 181 FormMain.Redraw;182 183 UpdateInterface; 184 end; 185 186 procedure TCore.GamePaint(Sender: TObject); 187 begin 188 if Assigned(FormMain) then FormMain.Redraw; 183 189 end; 184 190 -
trunk/UGame.pas
r63 r65 104 104 FOnChange: TNotifyEvent; 105 105 FOnGameOver: TNotifyEvent; 106 FOnPaint: TNotifyEvent; 106 107 FOnWin: TNotifyEvent; 107 108 FRecordHistory: Boolean; … … 115 116 procedure SetScore(AValue: Integer); 116 117 procedure DoChange; 118 procedure DoPaint; 117 119 procedure RenderTile(Canvas: TCanvas; Tile: TTile; TileRect: TRect; WithText: Boolean); 118 120 procedure GameOver; … … 129 131 TopScore: Integer; 130 132 AnimationDuration: Integer; 133 AnimationTick: Integer; 131 134 WinTileValue: Integer; 132 135 UndoEnabled: Boolean; … … 155 158 property Running: Boolean read FRunning write FRunning; 156 159 property OnChange: TNotifyEvent read FOnChange write FOnChange; 160 property OnPaint: TNotifyEvent read FOnPaint write FOnPaint; 157 161 property OnWin: TNotifyEvent read FOnWin write FOnWin; 158 162 property OnGameOver: TNotifyEvent read FOnGameOver write FOnGameOver; … … 506 510 end; 507 511 512 procedure TGame.DoPaint; 513 begin 514 if Assigned(FOnPaint) then FOnPaint(Self); 515 end; 516 508 517 procedure TGame.GameOver; 509 518 begin … … 516 525 if FSkin = AValue then Exit; 517 526 FSkin := AValue; 518 Do Change;527 DoPaint; 519 528 end; 520 529 … … 595 604 AnimateTiles; 596 605 DoChange; 606 DoPaint; 597 607 end; 598 608 … … 763 773 Inc(P.Y, Area.Increment.Y); 764 774 end; 765 Do Change;775 DoPaint; 766 776 end; 767 777 FMoving := False; … … 945 955 Trunc(Part * DirectionDiff[Direction].Y * 100)); 946 956 end; 947 Do Change;948 Application.ProcessMessages;949 Sleep( 1);957 DoPaint; 958 //Application.ProcessMessages; 959 Sleep(AnimationTick); 950 960 until Time > EndTime; 951 961 … … 968 978 Board.Tiles[Y, X].Value := Board.Tiles[Y, X].NewValue; 969 979 end; 970 Do Change;980 DoPaint; 971 981 FMoving := False; 972 982 end; … … 1008 1018 Board.Tiles[Y, X].Shift := Point(Trunc(Part * 100), Trunc(Part * 100)); 1009 1019 end; 1010 Do Change;1011 Application.ProcessMessages;1012 Sleep( 1);1020 DoPaint; 1021 //Application.ProcessMessages; 1022 Sleep(AnimationTick); 1013 1023 until Time > EndTime; 1014 1024 … … 1019 1029 Board.Tiles[Y, X].Shift := Point(0, 0); 1020 1030 end; 1021 Do Change;1031 DoPaint; 1022 1032 FMoving := False; 1023 1033 end; … … 1155 1165 end; 1156 1166 DoChange; 1167 DoPaint; 1157 1168 end; 1158 1169 … … 1160 1171 begin 1161 1172 AnimationDuration := 30; 1173 AnimationTick := 10; // ms 1162 1174 WinTileValue := 11; // 2^11 = 2048 1163 1175 Board := TBoard.Create;
Note:
See TracChangeset
for help on using the changeset viewer.