-
Notifications
You must be signed in to change notification settings - Fork 2
/
waffle.pas
908 lines (758 loc) · 20 KB
/
waffle.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
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
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
unit waffle;
{$mode objfpc}{$H+}
interface
uses
Classes, fgl, fptimer, Math, SysUtils;
const
KEY_RETURN = 40;
KEY_ESC = 41;
KEY_SPACE = 44;
KEY_RIGHT = 79;
KEY_LEFT = 80;
KEY_DOWN = 81;
KEY_UP = 82;
{$I sdlconst.pp }
{$I sdlintf.pp}
type
TEventTimer = class;
TGameEntity = class;
TLayer = class;
TSprite = class;
TScene = class;
TWaffleGame = class;
TIntProc = procedure(arg: integer);
TGameProc = procedure(Game: TWaffleGame);
TProc = procedure;
TLayerList = specialize TFPGObjectList<TLayer>;
TEntityList = specialize TFPGObjectList<TGameEntity>;
TSpriteList = specialize TFPGObjectList<TSprite>;
TTexture = record
TextureData: PSDL_Texture;
TextureWidth, TextureHeight: integer;
end;
TGameEntity = class
private
FEntityName: string;
FParentLayer: TLayer;
FParentScene: TScene;
public
procedure Delete; virtual;
property ParentLayer: TLayer read FParentLayer write FParentLayer;
property ParentScene: TScene read FParentScene write FParentScene;
property EntitiyName: string read FEntityName write FEntityName;
end;
TEventTimer = class(TGameEntity)
private
FInterval: integer;
counter: integer;
public
constructor Create; virtual;
procedure OnTimer; virtual; abstract;
procedure Tick(dt: Float);
procedure Start;
procedure Stop;
property Interval: integer read FInterval write FInterval;
end;
TWaffleGame = class
FPSLimit: integer;
Quit: boolean;
ScreenHeight, ScreenWidth: integer;
Title: PChar;
private
CurrentScene: TScene;
FFullscreen: boolean;
procedure SetFullscreen(AValue: boolean);
public
OnKeyDown: TIntProc;
constructor Create(ATitle: PChar; AX, AY, AScreenWidth, AScreenHeight: integer);
procedure Cleanup;
procedure Exit;
procedure OnInit; virtual; abstract;
procedure SetScene(Scene: TScene);
procedure Start;
property Fullscreen: boolean read FFullscreen write SetFullscreen;
end;
TSprite = class(TGameEntity)
DstRect: TSDL_Rect;
private
FFlipHorizontal, FFlipVertical: boolean;
FOpacity: integer;
FPosX, FPosY: Float;
FShown: boolean;
FWidth, FHeight: Float;
FRotation: double;
CurrentFlip: integer;
Texture: TTexture;
function GetPosX: Float;
function GetPosY: Float;
procedure SetFlipHorizontal(AValue: boolean);
procedure SetFlipVertical(AValue: boolean);
procedure SetOpacity(AValue: SInt32);
procedure SetPosX(AValue: Float);
procedure SetPosY(AValue: Float);
procedure UpdateFlip;
public
constructor Create(APosX, APosY, AWidth, AHeight: Float);
virtual;
procedure Draw; virtual;
procedure OnUpdate(Game: TWaffleGame; dt: Float); virtual; abstract;
procedure SetTexture(ATexture: TTexture);
property FlipHorizontal: boolean read FFlipHorizontal write SetFlipHorizontal;
property FlipVertical: boolean read FFlipVertical write SetFlipVertical;
property Opacity: SInt32 read FOpacity write SetOpacity;
property Shown: boolean read FShown write FShown;
property PosX: Float read FPosX write SetPosX;
property PosY: Float read FPosY write SetPosY;
property Rotation: double read FRotation write FRotation;
property Width: Float read FWidth write FWidth;
property Height: Float read FHeight write FHeight;
end;
PFrameInfo = ^TFrameInfo;
TFrameInfo = record
CurrentFrame: pInt32;
TotalFrame: integer;
end;
TAnimatedSprite = class(TSprite)
FFrameChangeDelay: integer;
CurrFrame: integer;
FFrameCols, FFrameRows: integer;
SrcRect: TSDL_Rect;
InternalCounter: integer;
FrameInfo: TFrameInfo;
private
Counter: integer;
FrameNum: integer;
AnimationTimerID: integer;
Playing: boolean;
function GetFrameHeight: integer;
function GetFrameWidth: integer;
procedure UpdateFrame;
procedure SetFrameChangeDelay(AValue: integer);
procedure SetFrameCols(AValue: integer);
procedure SetFrameHeight(AValue: integer);
procedure SetFrameRows(AValue: integer);
procedure SetFrameWidth(AValue: integer);
procedure UpdateFrameNum;
public
constructor Create(APosX, APosY, AWidth, AHeight: Float); overload;
procedure Delete; override;
procedure Draw; override;
procedure OnUpdate(Game: TWaffleGame; dt: Float); override;
procedure PlayAnimation;
procedure StopAnimation;
property FrameChangeDelay: integer read FFrameChangeDelay write SetFrameChangeDelay;
property FrameCols: integer read FFrameCols write SetFrameCols;
property FrameRows: integer read FFrameRows write SetFrameRows;
property FrameWidth: integer read GetFrameWidth write SetFrameWidth;
property FrameHeight: integer read GetFrameHeight write SetFrameHeight;
end;
TText = class(TSprite)
private
Surface: PSDL_Surface;
FFont: PTTF_Font;
FFontColor: TSDL_Color;
FFontSize: integer;
FText: PChar;
public
constructor Create(APosX, APosY: Float; AFontSize: integer); overload;
procedure LoadFontFromFile(FileName: PChar);
procedure SetColor(r, g, b, a: integer);
procedure SetText(AText: PChar);
procedure UpdateTexture;
end;
TLayer = class
private
FParentScene: TScene;
FEntityList: TEntityList;
public
constructor Create;
procedure AddEntitiy(Entity: TGameEntity);
procedure Cleanup;
procedure Draw;
procedure OnUpdate(Game: TWaffleGame; dt: Float);
property EntityList: TEntityList read FEntityList;
property ParentScene: TScene read FParentScene write FParentScene;
end;
TParticle = class(TSprite)
vx, vy: Float;
life: Float;
public
constructor Create(APosX, APosY, AWidth, AHeight, AVx, AVy: Float);
procedure OnUpdate(Game: TWaffleGame; dt: Float); override;
end;
TParticleEmitter = class
PosX, PosY: float;
MinVx, MaxVx: float;
MinVy, MaxVy: float;
Width, Height: float;
ParentLayer: TLayer;
ParticleTexture: TTexture;
private
FNumParticle: integer;
FSpriteList: TSpriteList;
public
constructor Create(ATexture: TTexture; APosX, APosY, AWidth, AHeight: Float;
ANumParticle: integer);
constructor Delete;
procedure Draw;
procedure OnUpdate(Game: TWaffleGame; dt: Float);
property SpriteList: TSpriteList read FSpriteList write FSpriteList;
end;
TScene = class
GameRef: TWaffleGame;
OnInit: TProc;
private
FLayerList: TLayerList;
FSpriteList: TSpriteList;
FPaused: boolean;
BaseLayer: TLayer;
function GetScreenWidth: SInt32;
public
constructor Create;
procedure AddEntity(Entity: TGameEntity);
procedure AddLayer(layer: TLayer);
procedure Cleanup;
procedure OnDraw; virtual;
procedure OnMouseMotion(X, Y: integer); virtual;
procedure OnUpdate(Game: TWaffleGame; dt: Float); virtual;
procedure OnKeyDown(Key: integer); virtual;
property LayerList: TLayerList read FLayerList;
property Paused: boolean read FPaused write FPaused;
property ScreenWidth: SInt32 read GetScreenWidth;
property SpriteList: TSpriteList read FSpriteList;
end;
TDefaultScene = class(TScene)
procedure OnUpdate(Game: TWaffleGame; dt: Float); override;
end;
function CreateTextureFromFile(FileName: PChar; Smooth: boolean = True): TTexture;
function IsKeyDown(KeyCode: integer): boolean;
function SpriteRectsIntersect(s1, s2: TSprite): boolean;
procedure ClearScreen(r, g, b, a: integer);
procedure FillRect(Rect: PSDL_Rect; r, g, b, a: integer);
var
Surf: PSDL_Surface;
Tex: PSDL_Texture;
GameKeyboardState: PUInt8;
GameWindow: PSDL_Window;
GameRenderer: PSDL_Renderer;
implementation
function CreateTextureFromFile(FileName: PChar; Smooth: boolean): TTexture;
begin
if Smooth then
SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, 'linear');
Result.TextureData := SDL_CreateTextureFromSurface(GameRenderer, IMG_Load(FileName));
SDL_QueryTexture(Result.TextureData, nil, nil, @Result.TextureWidth,
@Result.TextureHeight);
end;
function IsKeyDown(KeyCode: integer): boolean;
begin
Exit(GameKeyboardState[KeyCode] = 1);
end;
function SpriteRectsIntersect(s1, s2: TSprite): boolean;
var
r1, r2: TSDL_Rect;
begin
r1 := s1.DstRect;
r2 := s2.DstRect;
Result := not ((r1.x > (r2.x + r2.w)) or (r1.x + r1.w < r2.x) or
(r1.y > r2.y + r2.h) or (r1.y + r1.h < r2.y));
end;
procedure ClearScreen(r, g, b, a: integer);
begin
SDL_SetRenderDrawColor(GameRenderer, r, g, b, a);
SDL_SetRenderDrawBlendMode(GameRenderer, $00000001);
SDL_RenderClear(GameRenderer);
end;
procedure FillRect(Rect: PSDL_Rect; r, g, b, a: integer);
begin
SDL_SetRenderDrawColor(GameRenderer, r, g, b, a);
SDL_SetRenderDrawBlendMode(GameRenderer, $00000001);
SDL_RenderFillRect(GameRenderer, Rect);
end;
{ TEventTimerOld }
function Callback(interval: UInt32; param: Pointer): UInt32; cdecl;
begin
if Assigned(param) then
TProc(param);
exit(interval);
end;
constructor TEventTimer.Create;
begin
counter := 0;
Interval := 2000;
end;
procedure TEventTimer.Tick(dt: Float);
begin
counter := counter + 1;
if counter > (1 / dt) * (Interval / 1000) then
begin
try
OnTimer;
finally
end;
counter := 0;
end;
end;
procedure TEventTimer.Start;
begin
end;
procedure TEventTimer.Stop;
begin
end;
{ TGameEntity }
procedure TGameEntity.Delete;
begin
//ParentLayer.SpriteList.Remove(TSprite(self));
ParentLayer.EntityList.Remove(self);
self := nil;
end;
{ TParticle }
constructor TParticle.Create(APosX, APosY, AWidth, AHeight, AVx, AVy: Float);
begin
inherited Create(APosX, APosY, AWidth, AHeight);
vx := AVx;
vy := AVy;
life := 0;
end;
procedure TParticle.OnUpdate(Game: TWaffleGame; dt: Float);
begin
life := life + 1;
PosX := PosX + vx * dt;
PosY := PosY + vy * dt;
Opacity := Opacity - 5;
if Opacity < 0 then
self.Delete;
if life > 100 then
self.Delete;
end;
{ TParticleEmitter }
constructor TParticleEmitter.Create(ATexture: TTexture;
APosX, APosY, AWidth, AHeight: Float; ANumParticle: integer);
begin
PosX := APosX;
PosY := APosY;
Width := AWidth;
Height := AHeight;
MinVx := -100;
MaxVx := 100;
MinVy := -100;
MaxVy := 100;
FNumParticle := ANumParticle;
FSpriteList := TSpriteList.Create();
ParticleTexture := ATexture;
end;
constructor TParticleEmitter.Delete;
var
s: TSprite;
begin
for s in SpriteList do
s.Delete;
end;
procedure TParticleEmitter.Draw;
begin
end;
function RandomRangeF(min, max: single): single;
begin
Result := min + Random * (max - min);
end;
procedure TParticleEmitter.OnUpdate(Game: TWaffleGame; dt: Float);
var
p: TParticle;
begin
p := TParticle.Create(PosX, PosY, Width, Height, RandomRangeF(MinVx, MaxVx),
RandomRangeF(MinVy, MaxVy));
p.SetTexture(ParticleTexture);
ParentLayer.AddEntitiy(p);
end;
{ TLayer }
constructor TLayer.Create;
begin
FEntityList := TEntityList.Create();
end;
procedure TLayer.AddEntitiy(Entity: TGameEntity);
begin
EntityList.Add(Entity);
Entity.ParentLayer := self;
end;
procedure TLayer.Cleanup;
var
e: TGameEntity;
begin
for e in EntityList do
e.Delete;
end;
procedure TLayer.Draw;
var
e: TGameEntity;
begin
for e in EntityList do
if e is TSprite then
TSprite(e).Draw;
end;
procedure TLayer.OnUpdate(Game: TWaffleGame; dt: Float);
var
e: TGameEntity;
begin
for e in EntityList do
begin
if e is TSprite then
if not (TMethod(@TSprite(e).OnUpdate).Code = Pointer(@system.AbstractError)) then
TSprite(e).OnUpdate(Game, dt);
if e is TEventTimer then
TEventTimer(e).Tick(dt);
end;
end;
{ TText }
constructor TText.Create(APosX, APosY: Float; AFontSize: integer);
begin
Create(APosX, APosY, 0, 0);
SetColor(255, 255, 255, 255);
FText := '';
FFontSize := AFontSize;
end;
procedure TText.LoadFontFromFile(FileName: PChar);
begin
FFont := TTF_OpenFont(FileName, FFontSize);
TTF_SetFontHinting(FFont, TTF_HINTING_NORMAL);
end;
procedure TText.SetColor(r, g, b, a: integer);
begin
FFontColor.r := r;
FFontColor.g := g;
FFontColor.b := b;
FFontColor.a := a;
UpdateTexture;
end;
procedure TText.SetText(AText: PChar);
begin
FText := AText;
UpdateTexture;
end;
procedure TText.UpdateTexture;
begin
Surface := TTF_RenderText_Solid(FFont, FText, FFontColor);
Texture.TextureData := SDL_CreateTextureFromSurface(GameRenderer, Surface);
TTF_SizeText(FFont, FText, @DstRect.w, @DstRect.h);
Width := DstRect.w;
Height := DstRect.h;
end;
{ TDefaultScene }
procedure TDefaultScene.OnUpdate(Game: TWaffleGame; dt: Float);
begin
end;
{ TAnimatedSprite }
function TAnimatedSprite.GetFrameHeight: integer;
begin
Exit(SrcRect.h);
end;
function TAnimatedSprite.GetFrameWidth: integer;
begin
Exit(SrcRect.w);
end;
procedure TAnimatedSprite.UpdateFrame;
begin
end;
procedure TAnimatedSprite.SetFrameChangeDelay(AValue: integer);
begin
FFrameChangeDelay := AValue;
end;
procedure TAnimatedSprite.SetFrameCols(AValue: integer);
begin
if FFrameCols = AValue then
Exit;
FFrameCols := AValue;
UpdateFrameNum;
end;
procedure TAnimatedSprite.SetFrameHeight(AValue: integer);
begin
SrcRect.h := AValue;
end;
procedure TAnimatedSprite.SetFrameRows(AValue: integer);
begin
if FFrameRows = AValue then
Exit;
FFrameRows := AValue;
UpdateFrameNum;
end;
procedure TAnimatedSprite.SetFrameWidth(AValue: integer);
begin
SrcRect.w := AValue;
end;
procedure TAnimatedSprite.UpdateFrameNum;
begin
FrameNum := FrameCols * FrameRows;
FrameInfo.TotalFrame := FrameNum;
end;
function foo(interval: UInt32; param: Pointer): UInt32; cdecl;
begin
PFrameInfo(param)^.CurrentFrame^ :=
(PFrameInfo(param)^.CurrentFrame^ + 1) mod (PFrameInfo(param)^.TotalFrame);
Exit(interval);
end;
constructor TAnimatedSprite.Create(APosX, APosY, AWidth, AHeight: Float);
begin
inherited;
Counter := 0;
CurrFrame := 0;
FrameChangeDelay := 1000;
FrameCols := 1;
FrameRows := 1;
FrameInfo.CurrentFrame := @CurrFrame;
FrameInfo.TotalFrame := FrameCols * FrameRows;
//Playing := False;
StopAnimation;
SrcRect.x := 0;
SrcRect.y := 0;
SrcRect.w := round(AWidth);
SrcRect.h := round(AHeight);
end;
procedure TAnimatedSprite.Delete;
begin
StopAnimation;
inherited Delete;
end;
procedure TAnimatedSprite.Draw;
begin
SDL_RenderCopyEx(GameRenderer, Texture.TextureData, @SrcRect,
@DstRect, self.Rotation,
nil, CurrentFlip);
end;
procedure TAnimatedSprite.OnUpdate(Game: TWaffleGame; dt: Float);
begin
SrcRect.x := (CurrFrame * (FrameWidth));
if playing then
begin
Counter := Counter + 1;
if Counter > (1 / dt) * (FrameChangeDelay / 1000) then
begin
CurrFrame := (CurrFrame + 1) mod (FrameNum);
Counter := 0;
end;
end;
end;
procedure TAnimatedSprite.PlayAnimation;
begin
if not Playing then
Playing := not Playing;
end;
procedure TAnimatedSprite.StopAnimation;
begin
if Playing then
Playing := not Playing;
end;
{ TScene }
function TScene.GetScreenWidth: SInt32;
begin
Exit(GameRef.ScreenWidth);
end;
constructor TScene.Create;
begin
FLayerList := TLayerList.Create();
FPaused := False;
FSpriteList := TSpriteList.Create();
BaseLayer := TLayer.Create;
AddLayer(BaseLayer);
end;
procedure TScene.AddEntity(Entity: TGameEntity);
begin
BaseLayer.AddEntitiy(Entity);
end;
procedure TScene.AddLayer(layer: TLayer);
begin
LayerList.Add(layer);
layer.ParentScene := self;
end;
procedure TScene.Cleanup;
var
s: TSprite;
l: TLayer;
begin
for s in SpriteList do
s.Delete;
for l in LayerList do
l.Cleanup;
FreeAndNil(self);
end;
procedure TScene.OnDraw;
var
l: TLayer;
begin
BaseLayer.Draw;
for l in LayerList do
l.Draw;
end;
procedure TScene.OnMouseMotion(X, Y: integer);
begin
end;
procedure TScene.OnUpdate(Game: TWaffleGame; dt: Float);
var
l: TLayer;
begin
if not Paused then
for l in LayerList do
l.OnUpdate(Game, dt);
end;
procedure TScene.OnKeyDown(Key: integer);
begin
end;
{ TSprite }
function TSprite.GetPosX: Float;
begin
Exit(DstRect.x);
end;
function TSprite.GetPosY: Float;
begin
Exit(DstRect.y);
end;
procedure TSprite.SetFlipHorizontal(AValue: boolean);
begin
FFlipHorizontal := AValue;
UpdateFlip;
end;
procedure TSprite.SetFlipVertical(AValue: boolean);
begin
FFlipVertical := AValue;
UpdateFlip;
end;
procedure TSprite.SetOpacity(AValue: SInt32);
begin
FOpacity := AValue;
end;
procedure TSprite.SetPosX(AValue: Float);
begin
FPosX := AValue;
DstRect.x := round(AValue);
end;
procedure TSprite.SetPosY(AValue: Float);
begin
FPosY := AValue;
DstRect.y := round(AValue);
end;
procedure TSprite.UpdateFlip;
begin
if (not FFlipHorizontal) and (not FFlipVertical) then
CurrentFlip := SDL_FLIP_NONE;
if FFlipHorizontal and not FFlipVertical then
CurrentFlip := SDL_FLIP_HORIZONTAL;
if (not FFlipHorizontal) and FFlipVertical then
CurrentFlip := SDL_FLIP_VERTICAL;
if FFlipHorizontal and FFlipVertical then
CurrentFlip := SDL_FLIP_HORIZONTAL or SDL_FLIP_VERTICAL;
end;
constructor TSprite.Create(APosX, APosY, AWidth, AHeight: Float);
begin
CurrentFlip := SDL_FLIP_NONE;
FlipHorizontal := False;
FlipVertical := False;
Shown := True;
PosX := APosX;
PosY := APosY;
Width := AWidth;
Height := AHeight;
DstRect.x := round(PosX);
DstRect.y := round(PosY);
DstRect.w := round(Width);
DstRect.h := round(Height);
Opacity := 255;
Rotation := 0;
end;
procedure TSprite.Draw;
begin
SDL_SetTextureAlphaMod(self.Texture.TextureData, Opacity);
SDL_RenderCopyEx(GameRenderer, self.Texture.TextureData, nil, @DstRect, self.Rotation,
nil, CurrentFlip);
end;
procedure TSprite.SetTexture(ATexture: TTexture);
begin
self.Texture := ATexture;
end;
{ TWaffleGame }
procedure DefaultSceneUpdate(Game: TWaffleGame; dt: Float);
begin
if IsKeyDown(KEY_ESC) then
Game.Exit;
end;
procedure TWaffleGame.SetFullscreen(AValue: boolean);
begin
FFullscreen := AValue;
if AValue then
SDL_SetWindowFullscreen(GameWindow, SDL_WINDOW_FULLSCREEN)
else
SDL_SetWindowFullscreen(GameWindow, 0);
end;
constructor TWaffleGame.Create(ATitle: PChar;
AX, AY, AScreenWidth, AScreenHeight: integer);
var
DefaultScene: TScene;
begin
DefaultScene := TDefaultScene.Create;
CurrentScene := DefaultScene;
FPSLimit := 60;
Quit := False;
ScreenHeight := AScreenHeight;
ScreenWidth := AScreenWidth;
Title := ATitle;
SDL_SetWindowSize(GameWindow, AScreenWidth, AScreenHeight);
SDL_SetWindowPosition(GameWindow, AX, AY);
SDL_SetWindowTitle(GameWindow, ATitle);
end;
procedure TWaffleGame.Cleanup;
begin
FreeAndNil(self);
end;
procedure TWaffleGame.Exit;
begin
self.Quit := True;
end;
procedure TWaffleGame.SetScene(Scene: TScene);
begin
CurrentScene := Scene;
Scene.GameRef := self;
end;
procedure TWaffleGame.Start;
var
StartingTick: UInt32;
TNow, TLast: UInt64;
dt: Float;
ev: PSDL_Event;
begin
if Assigned(CurrentScene.OnInit) then
CurrentScene.OnInit;
New(ev);
TNow := SDL_GetPerformanceCounter;
while not quit do
begin
GameKeyboardState := SDL_GetKeyboardState(nil);
SDL_PumpEvents;
StartingTick := SDL_GetTicks;
TLast := TNow;
TNow := SDL_GetPerformanceCounter;
dt := (TNow - TLast) / Float(SDL_GetPerformanceFrequency);
{ Event }
while SDL_PollEvent(ev) = 1 do
case ev^.type_ of
SDL_KEYDOWN: CurrentScene.OnKeyDown(ev^.key.keysym.scancode);
SDL_MOUSEMOTION: CurrentScene.OnMouseMotion(ev^.motion.x, ev^.motion.y);
end;
{ Update logic }
CurrentScene.OnUpdate(self, dt);
{ Drawing logic }
SDL_SetRenderDrawColor(GameRenderer, 0, 0, 0, SDL_ALPHA_OPAQUE);
SDL_RenderClear(GameRenderer);
CurrentScene.OnDraw;
SDL_RenderPresent(GameRenderer);
if (1000 div FPSLimit) > (SDL_GetTicks - StartingTick) then
SDL_Delay(1000 div FPSLimit - (SDL_GetTicks - StartingTick));
end;
SDL_Quit();
end;
initialization
{ TODO: add error handling }
SDL_Init(SDL_INIT_EVERYTHING);
TTF_Init();
GameWindow := SDL_CreateWindow('', 0, 0, 0, 0, SDL_WINDOW_SHOWN);
GameRenderer := SDL_CreateRenderer(GameWindow, -1, 0);
finalization
SDL_DestroyRenderer(GameRenderer);
SDL_DestroyWindow(GameWindow);
end.