-
Notifications
You must be signed in to change notification settings - Fork 0
/
sprites.pas
428 lines (374 loc) · 11.9 KB
/
sprites.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
unit Sprites;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, allegro;
TYPE
{ TSprite }
TSprite = class
public
SpriteBitmap,MaskBitmap,ActualMaskBitmap: AL_BITMAPptr;
x,y,vx,vy,ScaleFactor:Integer; //in some cases you may want to affect x/y directly so x/y are public.
magentatransparent:Boolean;
constructor Create(sprite,mask:AL_BITMAPptr);
Destructor Destroy();override;
procedure Update;
procedure UpdateMask;
procedure Draw(bitmap:AL_BITMAPptr);
procedure DrawMask(bitmap:AL_BITMAPptr);
function IsColliding(other:TSprite):Boolean;
procedure Put(bitmap:AL_BITMAPptr; _x,_y:Integer);
private
end;
{ TAnimatedSprite }
TAnimationType = (atLoop,atPingPong,atStop);
TAnimatedSprite = class(TSprite)
private
direction:Shortint;
AnimationSpritesheet :AL_BITMAPptr;
AnimationSpritesheetMask :AL_BITMAPptr;
_NeedUpdate:Boolean;
_CurrentFrame,framecounter:Integer;
public
AnimationType:TAnimationType;
DelayBetweenFrames:Integer; //in frames, 1s=60 frames.
FrameCount:Integer;
FrameWidth:Integer;
property CurrentFrame:Integer read _CurrentFrame;
property NeedUpdate: Boolean read _NeedUpdate;
constructor Create(spritesheet,spritesheetmask:AL_BITMAPptr; framenum:integer; animtype:TAnimationType);
constructor Create(spritesheet,spritesheetmask:AL_BITMAPptr; framenum:integer);
destructor Destroy;override;
procedure Update;
procedure UpdateFrame;
procedure Reset;
end;
{ TSpriteList }
TSpriteList = class(TList)
private
ffreeobjects : boolean;
Protected
Procedure Notify(Ptr: Pointer; Action: TListNotification); override;
function GetItem(Index: Integer): TSprite;
Procedure SetItem(Index: Integer; AObject: TSprite);
public
constructor create;
constructor create(freeobjects : boolean);
function Add(AObject: TSprite): Integer;
function Extract(Item: TSprite): TSprite;
function Remove(AObject: TSprite): Integer;
function IndexOf(AObject: TSprite): Integer;
Procedure Insert(Index: Integer; AObject: TSprite);
function First: TSprite;
Function Last: TSprite;
property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
property Items[Index: Integer]: TSprite read GetItem write SetItem; default;
end;
TAnimatedSpriteList = class(TList)
private
ffreeobjects : boolean;
Protected
Procedure Notify(Ptr: Pointer; Action: TListNotification); override;
function GetItem(Index: Integer): TAnimatedSprite;
Procedure SetItem(Index: Integer; AObject: TAnimatedSprite);
public
constructor create;
constructor create(freeobjects : boolean);
function Add(AObject: TAnimatedSprite): Integer;
function Extract(Item: TAnimatedSprite): TAnimatedSprite;
function Remove(AObject: TAnimatedSprite): Integer;
function IndexOf(AObject: TAnimatedSprite): Integer;
Procedure Insert(Index: Integer; AObject: TAnimatedSprite);
function First: TAnimatedSprite;
Function Last: TAnimatedSprite;
property OwnsObjects: Boolean read FFreeObjects write FFreeObjects;
property Items[Index: Integer]: TAnimatedSprite read GetItem write SetItem; default;
end;
implementation
uses masked_collision;
{ TSpriteList }
procedure TSpriteList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if FFreeObjects then
if (Action=lnDeleted) then
TSprite(Ptr).Free;
inherited Notify(Ptr,Action);
end;
function TSpriteList.GetItem(Index: Integer): TSprite;
begin
Result:=TSprite(Inherited Get(Index));
end;
procedure TSpriteList.SetItem(Index: Integer; AObject: TSprite);
begin
// Put will take care of deleting old one in Notify.
Put(Index,Pointer(AObject));
end;
constructor TSpriteList.create;
begin
inherited create;
ffreeobjects:=true;
end;
constructor TSpriteList.create(freeobjects: boolean);
begin
inherited create;
ffreeobjects:=freeobjects;
end;
function TSpriteList.Add(AObject: TSprite): Integer;
begin
Result:=Inherited Add(Pointer(AObject));
end;
function TSpriteList.Extract(Item: TSprite): TSprite;
begin
Result:=TSprite(Inherited Extract(Pointer(Item)));
end;
function TSpriteList.Remove(AObject: TSprite): Integer;
begin
Result:=Inherited Remove(Pointer(AObject));
end;
function TSpriteList.IndexOf(AObject: TSprite): Integer;
begin
Result:=Inherited indexOF(Pointer(AObject));
end;
procedure TSpriteList.Insert(Index: Integer; AObject: TSprite);
begin
Inherited Insert(Index,Pointer(AObject));
end;
function TSpriteList.First: TSprite;
begin
Result := TSprite(Inherited First);
end;
function TSpriteList.Last: TSprite;
begin
Result := TSprite(Inherited Last);
end;
//-----
procedure TAnimatedSpriteList.Notify(Ptr: Pointer; Action: TListNotification);
begin
if FFreeObjects then
if (Action=lnDeleted) then
TAnimatedSprite(Ptr).Free;
inherited Notify(Ptr,Action);
end;
function TAnimatedSpriteList.GetItem(Index: Integer): TAnimatedSprite;
begin
Result:=TAnimatedSprite(Inherited Get(Index));
end;
procedure TAnimatedSpriteList.SetItem(Index: Integer; AObject: TAnimatedSprite);
begin
// Put will take care of deleting old one in Notify.
Put(Index,Pointer(AObject));
end;
constructor TAnimatedSpriteList.create;
begin
inherited create;
ffreeobjects:=true;
end;
constructor TAnimatedSpriteList.create(freeobjects: boolean);
begin
inherited create;
ffreeobjects:=freeobjects;
end;
function TAnimatedSpriteList.Add(AObject: TAnimatedSprite): Integer;
begin
Result:=Inherited Add(Pointer(AObject));
end;
function TAnimatedSpriteList.Extract(Item: TAnimatedSprite): TAnimatedSprite;
begin
Result:=TAnimatedSprite(Inherited Extract(Pointer(Item)));
end;
function TAnimatedSpriteList.Remove(AObject: TAnimatedSprite): Integer;
begin
Result:=Inherited Remove(Pointer(AObject));
end;
function TAnimatedSpriteList.IndexOf(AObject: TAnimatedSprite): Integer;
begin
Result:=Inherited indexOF(Pointer(AObject));
end;
procedure TAnimatedSpriteList.Insert(Index: Integer; AObject: TAnimatedSprite);
begin
Inherited Insert(Index,Pointer(AObject));
end;
function TAnimatedSpriteList.First: TAnimatedSprite;
begin
Result := TAnimatedSprite(Inherited First);
end;
function TAnimatedSpriteList.Last: TAnimatedSprite;
begin
Result := TAnimatedSprite(Inherited Last);
end;
{ TAnimatedSprite }
constructor TAnimatedSprite.Create(spritesheet, spritesheetmask: AL_BITMAPptr;
framenum: integer; animtype: TAnimationType);
begin
Create(spritesheet,spritesheetmask,framenum);
AnimationType:=animtype;
end;
constructor TAnimatedSprite.Create(spritesheet,spritesheetmask: AL_BITMAPptr; framenum:integer);
var sprite,mask:AL_BITMAPptr;
begin
FrameCount:=framenum;
sprite:=al_create_bitmap(2,2);
mask:=al_create_bitmap(2,2);
inherited Create(sprite, mask);
AnimationSpritesheet:=spritesheet;
AnimationSpritesheetMask:=spritesheetmask;
direction:=1;
_CurrentFrame:=-1;
end;
destructor TAnimatedSprite.Destroy;
begin
if AnimationSpritesheet<> nil then al_destroy_bitmap(AnimationSpritesheet);
if AnimationSpritesheetMask<> nil then al_destroy_bitmap(AnimationSpritesheetMask);
inherited;
end;
procedure TAnimatedSprite.Update;
begin
inherited Update;
if _CurrentFrame=-1 then
begin
_CurrentFrame:=0;
framecounter:=1;
_NeedUpdate:=true;
exit;
end;
Inc(FrameCounter);
if ((framecounter>=DelayBetweenFrames)) then
begin
_CurrentFrame:=_CurrentFrame+direction;
framecounter:=1;
_NeedUpdate:=true;
end;
case AnimationType of
atLoop: begin
if _CurrentFrame>=FrameCount then
begin
direction:=1; //making sure we get right direction if we switch over from atPingPong;
_CurrentFrame:=0;
_NeedUpdate:=true;
end;
end;
atPingPong: begin
if ((_CurrentFrame>=FrameCount)) then
begin
direction:=-1;
_CurrentFrame:=FrameCount-2;
end else
if _CurrentFrame<=0 then
begin
direction:=1;
_CurrentFrame:=0;
end;
end;
atStop: begin
if ((_CurrentFrame>=FrameCount)) then
begin
direction:=1; //making sure we get right direction if we switch over from atPingPong;
_CurrentFrame:=FrameCount-1;
end
end;
end;
end;
procedure TAnimatedSprite.UpdateFrame;
var _x:Integer;
begin
_NeedUpdate:=false;
//preventing memory leaks as we will recreate those bitmaps
if self.SpriteBitmap<>nil then al_destroy_bitmap(self.SpriteBitmap);
if self.MaskBitmap<>nil then al_destroy_bitmap(self.MaskBitmap);
//recreating bitmaps
self.SpriteBitmap:=al_create_bitmap(self.FrameWidth,self.AnimationSpritesheet^.h);
self.MaskBitmap:=al_create_bitmap(self.FrameWidth,self.AnimationSpritesheetMask^.h);
//getting x of current frame
_x:=self.CurrentFrame*FrameWidth;
//blitting frames onto displayable bitmaps
al_blit(AnimationSpritesheet,SpriteBitmap,_x,0,0,0,self.FrameWidth,self.AnimationSpritesheet^.h);
al_blit(AnimationSpritesheetMask,MaskBitmap,_x,0,0,0,self.FrameWidth,self.AnimationSpritesheetMask^.h);
//updating "real" (i.e. scaled) collision mask, so things would collide properly
self.UpdateMask;
end;
procedure TAnimatedSprite.Reset;
begin
_CurrentFrame:=0;
_NeedUpdate:=true;
framecounter:=0;
end;
{ TSprite }
constructor TSprite.Create(sprite, mask: AL_BITMAPptr);
begin
inherited Create;
SpriteBitmap:=sprite;
MaskBitmap:=mask;
ScaleFactor:=1;
UpdateMask;
vx:=0;
vy:=0;
x:=0;
y:=0;
end;
destructor TSprite.Destroy;
begin
if SpriteBitmap<>MaskBitmap then
begin
if SpriteBitmap<> nil then begin al_destroy_bitmap(SpriteBitmap); SpriteBitmap:=nil;end;
if MaskBitmap<> nil then begin al_destroy_bitmap(MaskBitmap); MaskBitmap:=nil;end;
end else
if SpriteBitmap<> nil then begin al_destroy_bitmap(SpriteBitmap); SpriteBitmap:=nil;end;
if ActualMaskBitmap<> nil then begin al_destroy_bitmap(ActualMaskBitmap); ActualMaskBitmap:=nil;end;
inherited;
end;
procedure TSprite.Update;
begin
self.x:=x+vx;
self.y:=y+vy;
end;
procedure TSprite.UpdateMask;
begin
//this should be called after changing scale factor of sprite, so it would collide properly
if ActualMaskBitmap<>nil then
begin
al_destroy_bitmap(ActualMaskBitmap);
ActualMaskBitmap := nil;
end; //preventing memory leak
if MaskBitmap=nil then exit; //preventing segfault
if ScaleFactor=1 then
begin
ActualMaskBitmap:=al_create_bitmap(MaskBitmap^.w,MaskBitmap^.h);
al_blit(MaskBitmap,ActualMaskBitmap,0,0,0,0,MaskBitmap^.w,MaskBitmap^.h);
end else
begin
ActualMaskBitmap:=al_create_bitmap(MaskBitmap^.w*ScaleFactor,MaskBitmap^.h*ScaleFactor);
al_stretch_blit(MaskBitmap,ActualMaskBitmap,0,0,MaskBitmap^.w,MaskBitmap^.h,0,0,MaskBitmap^.w*scalefactor,MaskBitmap^.h*scalefactor);
end
end;
procedure TSprite.Draw(bitmap: AL_BITMAPptr);
begin
if ((self<>nil) and (bitmap<>nil) and (SpriteBitmap<>nil)) then
begin
if not magentatransparent then al_stretch_blit(SpriteBitmap,bitmap,0,0,SpriteBitmap^.w,SpriteBitmap^.h,x,y,SpriteBitmap^.w*scalefactor,SpriteBitmap^.h*scalefactor)
else al_masked_stretch_blit(SpriteBitmap,bitmap,0,0,SpriteBitmap^.w,SpriteBitmap^.h,x,y,SpriteBitmap^.w*scalefactor,SpriteBitmap^.h*scalefactor);
end;
end;
procedure TSprite.DrawMask(bitmap: AL_BITMAPptr);
begin
if ((bitmap<>nil) and (MaskBitmap<>nil)) then
if not magentatransparent then al_stretch_blit(MaskBitmap,bitmap,0,0,MaskBitmap^.w,MaskBitmap^.h,x,y,MaskBitmap^.w*scalefactor,MaskBitmap^.h*scalefactor)
else al_masked_stretch_blit(MaskBitmap,bitmap,0,0,MaskBitmap^.w,MaskBitmap^.h,x,y,MaskBitmap^.w*scalefactor,MaskBitmap^.h*scalefactor);
end;
function TSprite.IsColliding(other: TSprite): Boolean;
begin
Result:=false;
if ((ActualMaskBitmap=nil) or (other.ActualMaskBitmap=nil)) then exit; //prevents segfault.
Result:=collide(self,other);
end;
procedure TSprite.Put(bitmap: AL_BITMAPptr; _x, _y: Integer);
var oldx,oldy:Integer;
begin
Oldx:=x;
Oldy:=y;
x:=_x;
y:=_y;
Draw(bitmap);
x:=oldx;
y:=oldy;
end;
end.