-
Notifications
You must be signed in to change notification settings - Fork 6
/
unit1.pas
370 lines (331 loc) · 11.9 KB
/
unit1.pas
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
(******************************************************************************)
(* Sandtris 27.06.2023 *)
(* *)
(* Version : 0.01 *)
(* *)
(* Author : Uwe Schächterle (Corpsman) *)
(* *)
(* Support : www.Corpsman.de *)
(* *)
(* Description : This is a mix between sandflow simulation and the Tetris *)
(* classical game. *)
(* inspired by: https://www.youtube.com/shorts/aaCWkot8mIU *)
(* *)
(* License : See the file license.md, located under: *)
(* https://github.com/PascalCorpsman/Software_Licenses/blob/main/license.md *)
(* for details about the license. *)
(* *)
(* It is not allowed to change or remove this text from any *)
(* source file of the project. *)
(* *)
(* Warranty : There is no warranty, neither in correctness of the *)
(* implementation, nor anything other that could happen *)
(* or go wrong, use at your own risk. *)
(* *)
(* Known Issues: none *)
(* *)
(* History : 0.01 - Initial version *)
(* *)
(******************************************************************************)
Unit Unit1;
{$MODE objfpc}{$H+}
{$DEFINE DebuggMode}
Interface
Uses
Classes, SysUtils, Forms, Controls, Graphics, Dialogs, ExtCtrls, StdCtrls,
Buttons, OpenGLContext,
(*
* Kommt ein Linkerfehler wegen OpenGL dann: sudo apt-get install freeglut3-dev
*)
dglOpenGL // http://wiki.delphigl.com/index.php/dglOpenGL.pas
//, uopengl_graphikengine // Die OpenGLGraphikengine ist eine Eigenproduktion von www.Corpsman.de, und kann getrennt geladen werden.
, usandtris
, upieces
, uHighscoreEngine
;
Type
{ TForm1 }
TForm1 = Class(TForm)
Button1: TButton;
ImageList1: TImageList;
Label1: TLabel;
Label3: TLabel;
OpenGLControl1: TOpenGLControl;
PaintBox1: TPaintBox;
SpeedButton1: TSpeedButton;
Timer1: TTimer;
Timer2: TTimer;
Procedure Button1Click(Sender: TObject);
Procedure FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
Procedure FormCreate(Sender: TObject);
Procedure FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState);
Procedure FormKeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState);
Procedure OpenGLControl1MakeCurrent(Sender: TObject; Var Allow: boolean);
Procedure OpenGLControl1Paint(Sender: TObject);
Procedure OpenGLControl1Resize(Sender: TObject);
Procedure SpeedButton1Click(Sender: TObject);
Procedure Timer1Timer(Sender: TObject);
Procedure Timer2Timer(Sender: TObject);
private
SandTris: TSandTris;
Highscore: THighscoreEngine;
Procedure Go2d();
Procedure Exit2d();
Procedure OnEndGame(sender: TObject);
Procedure OnStartGame(sender: TObject);
Procedure OnNextPreviewPiece(sender: TObject);
Procedure OnPoints(sender: TObject);
Procedure Init();
public
End;
Var
Form1: TForm1;
Initialized: Boolean = false; // Wenn True dann ist OpenGL initialisiert
Implementation
{$R *.lfm}
Uses LCLType, Unit2;
{ TForm1 }
Procedure TForm1.Go2d;
Begin
glMatrixMode(GL_PROJECTION);
glPushMatrix(); // Store The Projection Matrix
glLoadIdentity(); // Reset The Projection Matrix
// glOrtho(0, 640, 0, 480, -1, 1); // Set Up An Ortho Screen
glOrtho(0, WorldWidth, worldheight, 0, -1, 1); // Set Up An Ortho Screen
glMatrixMode(GL_MODELVIEW);
glPushMatrix(); // Store old Modelview Matrix
glLoadIdentity(); // Reset The Modelview Matrix
glPointSize(Scale96ToForm(4));
End;
Procedure TForm1.Exit2d;
Begin
glMatrixMode(GL_PROJECTION);
glPopMatrix(); // Restore old Projection Matrix
glMatrixMode(GL_MODELVIEW);
glPopMatrix(); // Restore old Projection Matrix
End;
Procedure TForm1.Timer2Timer(Sender: TObject);
Var
s: String;
Begin
// We do this only one time
timer2.enabled := false;
// TODO: den Text dieses Dialogs sieht man nicht weil wir noch mitten im OpenGL onpaint sind ..
//showmessage('You reached: ' + IntToStr(SandTris.Points));
s := InputBox('Results', 'You reached: ' + IntToStr(SandTris.Points) + ' points, please enter your name for highscores.', 'Anonymos');
If s <> '' Then Begin
Highscore.Add(s, SandTris.Points);
Highscore.Save;
SpeedButton1Click(Nil);
End;
Button1.Visible := true;
Init();
Button1.SetFocus;
End;
Procedure TForm1.OnEndGame(sender: TObject);
Begin
// This is not elegant but easy
// and needed to decouple the OnRender routine from the showmessage routine
// which is needed on Linux systems to be able to actually display a messages
// Content
timer2.enabled := true;
End;
Procedure TForm1.OnStartGame(sender: TObject);
Begin
label1.caption := '0';
button1.visible := false;
End;
Procedure TForm1.OnNextPreviewPiece(sender: TObject);
Procedure SetPixel(p: TPoint; c: TColor);
Begin
PaintBox1.Canvas.Brush.Color := c;
PaintBox1.Canvas.pen.Color := c;
PaintBox1.Canvas.Rectangle(Scale96ToForm(p.x * 4), Scale96ToForm(p.Y * 4), Scale96ToForm(p.x * 4 + 4), Scale96ToForm(p.Y * 4 + 4));
{$IFDEF Linux}
PaintBox1.Canvas.Pixels[Scale96ToForm(p.x * 4 + 4 - 1), Scale96ToForm(p.Y * 4 + 4 - 1)] := c;
{$ENDIF}
End;
Var
i: Integer;
Begin
// 1. Das nächste Teil, welches erstellt werden soll erstellen
// 2. Dieses Teil in der Preview anzeigen
PaintBox1.Canvas.Brush.Color := clBlack;
PaintBox1.Canvas.Rectangle(-1, -1, PaintBox1.Width + 1, PaintBox1.Height + 1);
For i := 0 To high(SandTris.NextPiece.Pixels) Do Begin
SetPixel(
point(SandTris.NextPiece.Pixels[i].Location.x, SandTris.NextPiece.Pixels[i].Location.y + PieceHeight)
, SandTris.NextPiece.Pixels[i].Color);
End;
End;
Procedure TForm1.OnPoints(sender: TObject);
Begin
label1.Caption := inttostr(SandTris.Points);
End;
Procedure TForm1.Init;
Begin
Label1.caption := 'Please start';
SandTris.Init;
button1.Visible := true;
PaintBox1.Canvas.Brush.Color := clBlack;
PaintBox1.Canvas.Rectangle(-1, -1, PaintBox1.Width + 1, PaintBox1.Height + 1);
End;
Procedure TForm1.OpenGLControl1Paint(Sender: TObject);
Begin
If Not Initialized Then Exit;
// Render Szene
glClearColor(0.0, 0.0, 0.0, 0.0);
glClear(GL_COLOR_BUFFER_BIT Or GL_DEPTH_BUFFER_BIT);
glLoadIdentity();
go2d;
SandTris.Render();
exit2d;
OpenGLControl1.SwapBuffers;
End;
Procedure TForm1.OpenGLControl1Resize(Sender: TObject);
Begin
If Initialized Then Begin
glMatrixMode(GL_PROJECTION);
glLoadIdentity();
glViewport(0, 0, OpenGLControl1.Width, OpenGLControl1.Height);
gluPerspective(45.0, OpenGLControl1.Width / OpenGLControl1.Height, 0.1, 100.0);
glMatrixMode(GL_MODELVIEW);
End;
End;
Procedure TForm1.SpeedButton1Click(Sender: TObject);
Var
List: TItemList;
Begin
// Show Highscore
list := Highscore.Show(false);
form2.LoadHighscore(List);
form2.ShowModal;
End;
Procedure TForm1.Timer1Timer(Sender: TObject);
{$IFDEF DebuggMode}
Var
i: Cardinal;
p: Pchar;
{$ENDIF}
Begin
If Initialized Then Begin
OpenGLControl1.Invalidate;
{$IFDEF DebuggMode}
i := glGetError();
If i <> 0 Then Begin
Timer1.Enabled := false;
p := gluErrorString(i);
showmessage('OpenGL Error (' + inttostr(i) + ') occured.' + LineEnding + LineEnding +
'OpenGL Message : "' + p + '"' + LineEnding + LineEnding +
'Applikation will be terminated.');
close;
End;
{$ENDIF}
End;
End;
Var
allowcnt: Integer = 0;
Procedure TForm1.OpenGLControl1MakeCurrent(Sender: TObject; Var Allow: boolean);
Begin
If allowcnt > 2 Then Begin
exit;
End;
inc(allowcnt);
// Sollen Dialoge beim Starten ausgeführt werden ist hier der Richtige Zeitpunkt
If allowcnt = 1 Then Begin
// Init dglOpenGL.pas , Teil 2
ReadExtensions; // Anstatt der Extentions kann auch nur der Core geladen werden. ReadOpenGLCore;
ReadImplementationProperties;
End;
If allowcnt = 2 Then Begin // Dieses If Sorgt mit dem obigen dafür, dass der Code nur 1 mal ausgeführt wird.
// Der Anwendung erlauben zu Rendern.
Initialized := True;
OpenGLControl1Resize(Nil);
SandTris := TSandTris.Create();
SandTris.OnStartGameEvent := @OnStartGame;
SandTris.OnEndGameEvent := @OnEndGame;
SandTris.OnNextPreviewPieceEvent := @OnNextPreviewPiece;
SandTris.OnPointsEvent := @OnPoints;
Init();
End;
Form1.Invalidate;
End;
Procedure TForm1.FormCreate(Sender: TObject);
Begin
(*
* History: 0.01 = Initial version
* 0.02 = Fix DPI Scaling
* 0.03 = improve texture of straight block
* 0.04 = Add Simple Highscore Engine
*
* Known Bugs: - On Linux the Highscreen is not readable
* - The very first previewed piece is not shown (Linux and Windows)
*)
Caption := 'Sandtris ver. 0.04';
Randomize;
Color := clBlack;
Constraints.MinHeight := Height;
Constraints.MaxHeight := Height;
Constraints.MinWidth := Width;
Constraints.MaxWidth := Width;
Highscore := THighscoreEngine.Create('Highscores.dat', 'This should be a good password, not such a lame text.', 10);
Highscore.InsertAlways := true;
// Init dglOpenGL.pas , Teil 1
If Not InitOpenGl Then Begin
showmessage('Error, could not init dglOpenGL.pas');
Halt;
End;
(*
60 - FPS entsprechen
0.01666666 ms
Ist Interval auf 16 hängt das gesamte system, bei 17 nicht.
Generell sollte die Interval Zahl also dynamisch zum Rechenaufwand, mindestens aber immer 17 sein.
*)
Timer1.Interval := 17;
OpenGLControl1.Width := Scale96ToForm(WorldWidth * 4);
OpenGLControl1.Height := Scale96ToForm(WorldHeight * 4);
End;
Procedure TForm1.FormKeyDown(Sender: TObject; Var Key: Word; Shift: TShiftState
);
Begin
If key = VK_ESCAPE Then close; // Boss Key
If key = VK_LEFT Then Begin
SandTris.SetKey(-1, true);
End;
If key = VK_RIGHT Then Begin
SandTris.SetKey(1, true);
End;
If key = VK_SPACE Then Begin
SandTris.DropDown();
End;
If key = vK_P Then Begin
SandTris.TogglePause;
End;
If key = VK_UP Then Begin
SandTris.Turn(1);
End;
If key = VK_DOWN Then Begin
SandTris.Turn(-1);
End;
End;
Procedure TForm1.FormKeyUp(Sender: TObject; Var Key: Word; Shift: TShiftState);
Begin
If key = VK_LEFT Then Begin
SandTris.SetKey(-1, false);
End;
If key = VK_RIGHT Then Begin
SandTris.SetKey(1, false);
End;
End;
Procedure TForm1.FormCloseQuery(Sender: TObject; Var CanClose: Boolean);
Begin
Initialized := false;
Highscore.free;
SandTris.Free;
End;
Procedure TForm1.Button1Click(Sender: TObject);
Begin
// Start New Game
SandTris.Start;
End;
End.