-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathppp01.pas
More file actions
411 lines (351 loc) · 10.5 KB
/
ppp01.pas
File metadata and controls
411 lines (351 loc) · 10.5 KB
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
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
{**************************************************************************
Copyright (C) 2015-2018 Parallel Realities
This program is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
***************************************************************************
converted from "C" to "Pascal" by Ulrich 2022
***************************************************************************
* changed all PChar to string Types for better string handling!
* Procedural Parameters for Delegate Draw/Logic
* without momory holes; tested with: fpc -Criot -gl -gh ppp01.pas
***************************************************************************}
PROGRAM ppp01;
{$COPERATORS OFF} {$mode FPC} {$H+}
USES SDL2, SDL2_Image, sysutils;
CONST SCREEN_WIDTH = 1280; { size of the grafic window }
SCREEN_HEIGHT = 720; { size of the grafic window }
MAX_TILES = 7;
TILE_SIZE = 64;
MAP_WIDTH = 40;
MAP_HEIGHT = 20;
MAP_RENDER_WIDTH = 20;
MAP_RENDER_HEIGHT = 12;
MAX_KEYBOARD_KEYS = 350;
MAX_SND_CHANNELS = 16;
Map_Path = 'data/map01.dat';
TYPE { "T" short for "TYPE" }
TDelegating = Procedure;
TDelegate = RECORD
logic, draw : TDelegating;
end;
PTexture = ^TTexture;
TTexture = RECORD
name : string;
texture : PSDL_Texture;
next : PTexture;
end;
TApp = RECORD
Window : PSDL_Window;
Renderer : PSDL_Renderer;
Keyboard : ARRAY[0..MAX_KEYBOARD_KEYS] OF integer;
TextureHead, TextureTail : PTexture;
Delegate : TDelegate;
end;
TStage = RECORD
map : ARRAY[0..PRED(MAP_WIDTH), 0..PRED(MAP_HEIGHT)] of integer;
end;
VAR app : TApp;
stage : TStage;
event : TSDL_EVENT;
exitLoop : Boolean;
gTicks : UInt32;
gRemainder : double;
tiles : ARRAY[1..MAX_TILES] of PSDL_Texture;
// ***************** UTIL *****************
procedure errorMessage(Message1 : string);
begin
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR,'Error Box',PChar(Message1),NIL);
HALT(1);
end;
procedure pathTest;
begin
if NOT FileExists(Map_Path) then ErrorMessage(Map_Path + ' not found!');
end;
// ***************** DRAW *****************
procedure blit(texture : PSDL_Texture; x, y, center : integer);
VAR dest : TSDL_Rect;
begin
dest.x := x;
dest.y := y;
SDL_QueryTexture(texture, NIL, NIL, @dest.w, @dest.h);
if center <> 0 then
begin
dest.x := dest.x - dest.w DIV 2;
dest.y := dest.y - dest.h DIV 2;
end;
SDL_RenderCopy(app.Renderer, texture, NIL, @dest);
end;
procedure blitRect(texture : PSDL_Texture; src : PSDL_Rect; x, y : integer);
VAR dest : TSDL_Rect;
begin
dest.x := x;
dest.y := y;
dest.w := src^.w;
dest.h := src^.h;
SDL_RenderCopy(app.Renderer, texture, src, @dest);
end;
procedure prepareScene;
begin
SDL_SetRenderDrawColor(app.Renderer, 128, 192, 255, 255);
SDL_RenderClear(app.Renderer);
end;
procedure presentScene;
begin
SDL_RenderPresent(app.Renderer);
end;
// **************** TEXTURE ***************
procedure addTextureToCache(LName : string; LTexture : PSDL_Texture);
VAR cache : PTexture;
begin
NEW(cache);
app.TextureTail^.next := cache;
app.TextureTail := cache;
cache^.name := LName;
cache^.texture := LTexture;
cache^.next := NIL;
end;
function getTexture(name : string) : PSDL_Texture;
VAR tf : PTexture;
begin
getTexture := NIL;
tf := app.TextureHead^.next;
while (tf <> NIL) do
begin
if tf^.name = name then
getTexture := tf^.texture;
tf := tf^.next;
end;
end;
function loadTexture(pfad : string) : PSDL_Texture;
VAR tg : PSDL_Texture;
begin
tg := getTexture(pfad);
if tg = NIL then
begin
tg := IMG_LoadTexture(app.Renderer, PChar(pfad));
if tg = NIL then
errorMessage(SDL_GetError());
addTextureToCache(pfad, tg);
end;
loadTexture := tg;
end;
procedure loadTiles;
VAR i : integer;
filename : string;
begin
for i := 1 to MAX_TILES do
begin
filename := 'gfx/tile' + IntToStr(i) + '.png';
tiles[i] := loadTexture(filename);
end;
end;
procedure initStage;
begin
NEW(app.TextureHead);
app.TextureHead^.name := '';
app.TextureHead^.texture := NIL;
app.TextureHead^.next := NIL;
app.TextureTail := app.TextureHead;
gTicks := SDL_GetTicks;
gRemainder := 0;
end;
// ***************** MAP *****************
procedure drawMap;
VAR x, y, n : integer;
begin
for y := 0 to PRED(MAP_RENDER_HEIGHT) do
begin
for x := 0 to PRED(MAP_RENDER_WIDTH) do
begin
n := stage.map[x,y];
if (n > 0) then
begin
blit(tiles[n], x * TILE_SIZE, y * TILE_SIZE, 0);
end;
end;
end;
end;
procedure loadMap(filename : string);
VAR i, x, y, le : integer;
FileIn : text;
line : string;
a : string[10];
begin
assign (FileIn, filename);
{$i-}; reset(FileIn); {$i+};
if IOresult = 0 then
begin
for y := 0 to PRED(MAP_HEIGHT) do
begin
x := 0; // first tile of the line
a := ''; // new string / number
readln(FileIn,line);
le := length(line);
for i := 1 to le do // parse through the line
begin
if line[i] <> ' ' then // if line[i] is a number and not space
begin
a := a + line[i]; // add number to the other numbers
if i = le then // end of line, so add the last number!
begin
stage.map[x,y] := StrToInt(a); // write it to stage.map as last number
end;
end
else
begin
stage.map[x,y] := StrToInt(a); // write number regular
INC(x); // next tile
a := ''; // new string / number
end;
end;
end;
close(FileIn);
end
else errorMessage(filename + ' not found!');
end;
procedure initMap;
begin
FillChar(stage.map, sizeof(stage.map), 0);
loadTiles;
loadMap(Map_Path);
end;
// ***************** STAGE *****************
procedure draw_Game;
begin
//SDL_SetRenderDrawColor(app.renderer, 128, 192, 255, 255);
//SDL_RenderFillRect(app.renderer, NIL);
drawMap;
end;
procedure logic_Game;
begin
end;
// *************** INIT SDL ***************
procedure initSDL;
VAR rendererFlags, windowFlags : integer;
begin
rendererFlags := {SDL_RENDERER_PRESENTVSYNC OR} SDL_RENDERER_ACCELERATED;
windowFlags := 0;
if SDL_Init(SDL_INIT_VIDEO) < 0 then
errorMessage(SDL_GetError());
app.Window := SDL_CreateWindow('Pete''s Pizza Party 1', SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, windowFlags);
if app.Window = NIL then
errorMessage(SDL_GetError());
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 'linear');
app.Renderer := SDL_CreateRenderer(app.Window, -1, rendererFlags);
if app.Renderer = NIL then
errorMessage(SDL_GetError());
IMG_INIT(IMG_INIT_PNG OR IMG_INIT_JPG);
SDL_ShowCursor(0);
end;
procedure destroyTexture;
VAR t, a : PTexture;
begin
a := app.TextureHead^.next;
while (a <> NIL) do
begin
t := a^.next;
DISPOSE(a);
a := t;
end;
DISPOSE(app.TextureHead);
end;
procedure cleanUp;
begin
destroyTexture;
if ExitCode <> 0 then WriteLn('CleanUp complete!');
end;
procedure atExit;
VAR i : byte;
begin
for i := 1 to MAX_TILES do
SDL_DestroyTexture(Tiles[i]);
if ExitCode <> 0 then cleanUp;
SDL_DestroyRenderer(app.Renderer);
SDL_DestroyWindow(app.Window);
IMG_Quit; { Quits the SDL_Image }
SDL_Quit; { Quits the SDL }
if Exitcode <> 0 then WriteLn(SDL_GetError());
SDL_ShowCursor(1);
end;
// ***************** INPUT *****************
procedure doKeyDown;
begin
if event.key.repeat_ = 0 then
begin
CASE event.key.keysym.sym of
SDL_KEYDOWN : begin
if ((event.key.repeat_ = 0) AND (event.key.keysym.scancode < MAX_KEYBOARD_KEYS)) then
app.Keyboard[event.key.keysym.scancode] := 1;
if (app.Keyboard[SDL_ScanCode_ESCAPE]) = 1 then exitLoop := TRUE;
end; { SDL_Keydown }
end; { CASE }
end; { IF }
end;
procedure doKeyUp;
begin
if event.key.repeat_ = 0 then
begin
CASE event.key.keysym.sym of
SDL_KEYUP : begin
if ((event.key.repeat_ = 0) AND (event.key.keysym.scancode < MAX_KEYBOARD_KEYS)) then
app.Keyboard[event.key.keysym.scancode] := 0;
end; { SDL_Keyup }
end; { CASE }
end; { IF }
end;
procedure doInput;
begin
while SDL_PollEvent(@event) = 1 do
begin
CASE event.Type_ of
SDL_QUITEV: exitLoop := TRUE; { close Window }
SDL_MOUSEBUTTONDOWN: exitLoop := TRUE; { if Mousebutton pressed }
SDL_KEYDOWN: doKeyDown;
SDL_KEYUP: doKeyUp;
end; { CASE event }
end; { SDL_PollEvent }
end;
// ************* CAPFRAMERATE *************
procedure CapFrameRate(VAR remainder : double; VAR Ticks : UInt32);
VAR wait, FrameTime : longint;
begin
wait := 16 + TRUNC(remainder);
remainder := remainder - TRUNC(remainder);
frameTime := SDL_GetTicks - Ticks;
DEC(wait, frameTime);
if (wait < 1) then wait := 1;
SDL_Delay(wait);
remainder := remainder + 0.667;
Ticks := SDL_GetTicks;
end;
// ***************** MAIN *****************
begin
pathTest;
initSDL;
addExitProc(@atExit);
initStage;
initMap;
exitLoop := FALSE;
app.Delegate.Logic := @logic_Game;
app.Delegate.Draw := @draw_Game;
while exitLoop = FALSE do
begin
prepareScene;
doInput;
app.delegate.Logic;
app.delegate.Draw;
presentScene;
CapFrameRate(gRemainder, gTicks);
end;
cleanUp;
atExit;
end.