-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathppp03.pas
More file actions
622 lines (526 loc) · 15.8 KB
/
ppp03.pas
File metadata and controls
622 lines (526 loc) · 15.8 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
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
{**************************************************************************
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 ppp03.pas
***************************************************************************}
PROGRAM ppp03;
{$COPERATORS OFF} {$mode FPC} {$H+}
USES SDL2, SDL2_Image, Math, sysutils, cTypes;
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;
PLAYER_MOVE_SPEED = 6;
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;
PEntity = ^TEntity;
TEntity = RECORD
x, y, dx, dy : double;
w, h : cint;
isOnGround : Boolean;
texture : PSDL_Texture;
next : PEntity;
end;
TStage = RECORD
camera : TSDL_Point;
map : ARRAY[0..PRED(MAP_WIDTH), 0..PRED(MAP_HEIGHT)] of integer;
EntityHead, EntityTail : PEntity;
end;
VAR app : TApp;
stage : TStage;
event : TSDL_EVENT;
exitLoop : Boolean;
gTicks : UInt32;
gRemainder : double;
tiles : ARRAY[1..MAX_TILES] of PSDL_Texture;
pete : ARRAY[0..1] of PSDL_Texture;
player : PEntity;
// ***************** 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;
procedure InitEntity(e : PEntity);
begin
e^.x := 0.0; e^.y := 0.0; e^.dx := 0.0; e^.dy := 0.0; e^.w := 0; e^.h := 0;
e^.isOnGround := FALSE; e^.texture := NIL; e^.next := NIL;
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 doPlayer;
begin
player^.dx := 0;
if ((app.Keyboard[SDL_SCANCODE_A] = 1) OR (app.Keyboard[SDL_SCANCODE_LEFT] = 1)) then
begin
player^.dx := player^.dx - PLAYER_MOVE_SPEED;
player^.texture := pete[1];
end;
if ((app.Keyboard[SDL_SCANCODE_D] = 1) OR (app.Keyboard[SDL_SCANCODE_RIGHT] = 1)) then
begin
player^.dx := player^.dx + PLAYER_MOVE_SPEED;
player^.texture := pete[0];
end;
if ((app.Keyboard[SDL_SCANCODE_I] = 1) AND (player^.isOnGround = TRUE)) then
player^.dy := -20;
if (app.Keyboard[SDL_SCANCODE_SPACE] = 1) then
begin
player^.x := 0;
player^.y := 0;
app.Keyboard[SDL_SCANCODE_SPACE] := 0;
end;
end;
procedure initPlayer;
begin
NEW(player);
initEntity(player);
stage.EntityTail^.next := player;
stage.EntityTail := player;
pete[0] := loadTexture('gfx/pete01.png');
pete[1] := loadTexture('gfx/pete02.png');
player^.texture := pete[0];
SDL_QueryTexture(player^.texture, NIL, NIL, @player^.w, @player^.h);
end;
// ***************** MAP *****************
procedure drawMap;
VAR x, y, n, x1, x2, y1, y2, mx, my : integer;
begin
x1 := (stage.camera.x MOD TILE_SIZE) * (-1);
if (x1 = 0) then x2 := x1 + MAP_RENDER_WIDTH * TILE_SIZE
else x2 := x1 + TILE_SIZE + MAP_RENDER_WIDTH * TILE_SIZE;
y1 := (stage.camera.y MOD TILE_SIZE) * (-1);
if (y1 = 0) then y2 := y1 + MAP_RENDER_HEIGHT * TILE_SIZE
else y2 := y1 + TILE_SIZE + MAP_RENDER_HEIGHT * TILE_SIZE;
mx := stage.camera.x DIV TILE_SIZE;
my := stage.camera.y DIV TILE_SIZE;
y := y1;
while y <= y2 do
begin
x := x1;
while x <= x2 do
begin
if ((mx >= 0) AND (my >= 0) AND (mx < MAP_WIDTH) AND (my < MAP_HEIGHT)) then
begin
n := stage.map[mx,my];
if (n > 0) then
blit(tiles[n], x, y, 0);
end;
INC(mx);
INC(x, TILE_SIZE);
end;
mx := stage.camera.x DIV TILE_SIZE;
INC(my);
INC(y, TILE_SIZE);
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;
// *************** ENTITIES ***************
function isInsideMap(x, y : integer) : Boolean;
begin
if ((x >= 0) AND (y >= 0) AND (x < MAP_WIDTH) AND (y < MAP_HEIGHT))
then isInsideMap := TRUE //1
else isInsideMap := FALSE; //0
end;
procedure moveToWorld(e : PEntity; dx, dy : double);
VAR mx, my, hit, adj : integer;
begin
if (dx <> 0) then
begin
e^.x := e^.x + dx;
//ORG C-Code: mx = dx > 0 ? (e->x + e->w) : e->x;
if dx > 0 then mx := TRUNC(e^.x + e^.w)
else mx := TRUNC(e^.x);
mx := mx DIV TILE_SIZE;
my := TRUNC(e^.y) DIV TILE_SIZE;
hit := 0;
if ((NOT isInsideMap(mx, my)) OR (stage.map[mx][my] <> 0)) then
hit := 1;
my := (TRUNC(e^.y + e^.h) - 1) DIV TILE_SIZE;
if ((NOT isInsideMap(mx, my)) OR (stage.map[mx][my] <> 0)) then
hit := 1;
if hit = 1 then
begin
//ORG C-Code: adj = dx > 0 ? -e^.w : TILE_SIZE
if dx > 0 then adj := -e^.w
else adj := TILE_SIZE;
e^.x := (mx * TILE_SIZE) + adj;
e^.dx := 0;
end;
end;
if (dy <> 0) then
begin
e^.y := e^.y + dy;
//ORG C-Code: my = dy > 0 ? (e^.y + e^.h) : e^.y;
if dy > 0 then my := TRUNC(e^.y + e^.h)
else my := TRUNC(e^.y);
my := my DIV TILE_SIZE;
mx := TRUNC(e^.x) DIV TILE_SIZE;
hit := 0;
if ((NOT isInsideMap(mx, my)) OR (stage.map[mx][my] <> 0)) then
hit := 1;
mx := (TRUNC(e^.x + e^.w) - 1) DIV TILE_SIZE;
if ((NOT isInsideMap(mx, my)) OR (stage.map[mx][my] <> 0)) then
hit := 1;
if (hit = 1) then
begin
//ORG C-Code: adj = dy > 0 ? -e^.h : TILE_SIZE;
if dy > 0 then adj := -e^.h
else adj := TILE_SIZE;
e^.y := (my * TILE_SIZE) + adj;
e^.dy := 0;
//e^.isOnGround = dy > 0;
if dy > 0 then e^.isOnGround := TRUE;
end;
end;
end;
procedure move(e : PEntity);
begin
e^.dy := e^.dy + 1.5;
e^.dy := MAX(MIN(e^.dy, 18), -999);
e^.isOnGround := FALSE;
moveToWorld(e, e^.dx, 0);
moveToWorld(e, 0, e^.dy);
e^.x := MIN(MAX(e^.x, 0), MAP_WIDTH * TILE_SIZE);
e^.y := MIN(MAX(e^.y, 0), MAP_HEIGHT * TILE_SIZE);
end;
procedure doEntities;
VAR e : PEntity;
begin
e := stage.EntityHead^.next;
while e <> NIL do
begin
//selv := e;
move(e);
e := e^.next;
end;
end;
procedure drawEntities;
VAR e : PEntity;
begin
e := stage.EntityHead^.next;
while e <> NIL do
begin
blit(e^.texture, ROUND(e^.x - stage.camera.x), ROUND(e^.y - stage.camera.y), 0);
e := e^.next;
end;
end;
procedure initEntities;
begin
end;
// **************** CAMERA ****************
procedure doCamera;
begin
stage.camera.x := ROUND(player^.x + (player^.w DIV 2));
stage.camera.y := ROUND(player^.y + (player^.h DIV 2));
stage.camera.x := stage.camera.x - (SCREEN_WIDTH DIV 2);
stage.camera.y := stage.camera.y - (SCREEN_HEIGHT DIV 2);
stage.camera.x := MIN(MAX(stage.camera.x, 0), (MAP_WIDTH * TILE_SIZE) - SCREEN_WIDTH);
stage.camera.y := MIN(MAX(stage.camera.y, 0), (MAP_HEIGHT * TILE_SIZE) - SCREEN_HEIGHT);
end;
// ***************** STAGE *****************
procedure draw_Game;
begin
drawMap;
drawEntities;
end;
procedure logic_Game;
begin
doPlayer;
doEntities;
doCamera;
end;
procedure initStage;
begin
NEW(app.TextureHead);
app.TextureHead^.name := '';
app.TextureHead^.texture := NIL;
app.TextureHead^.next := NIL;
app.TextureTail := app.TextureHead;
NEW(stage.EntityHead);
stage.EntityHead^.next := NIL;
stage.EntityTail := stage.EntityHead;
gTicks := SDL_GetTicks;
gRemainder := 0;
initPlayer;
initMap;
app.Delegate.Logic := @logic_Game;
app.Delegate.Draw := @draw_Game;
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 3', 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 destroyEntity;
VAR t, ent : PEntity;
begin
ent := stage.EntityHead^.next;
while (ent <> NIL) do
begin
t := ent^.next;
DISPOSE(ent);
ent := t;
end;
DISPOSE(stage.EntityHead);
end;
procedure cleanUp;
begin
destroyTexture;
destroyEntity;
if ExitCode <> 0 then WriteLn('CleanUp complete!');
end;
procedure initGame;
begin
end;
procedure atExit;
VAR i : integer;
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 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: 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 }
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 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);
initGame;
initStage;
exitLoop := FALSE;
while exitLoop = FALSE do
begin
prepareScene;
doInput;
app.delegate.Logic;
app.delegate.Draw;
presentScene;
CapFrameRate(gRemainder, gTicks);
end;
cleanUp;
atExit;
end.