-
Notifications
You must be signed in to change notification settings - Fork 0
/
tilesets.pas
80 lines (63 loc) · 1.96 KB
/
tilesets.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
unit Tilesets;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,allegro,sprites;
type
{ TTileset }
TTileset = class
public
TileSize,TilesetCols,TilesetRows:Integer;
TilesetSprite:TSprite;
constructor Create();
constructor Create(Sprite:TSprite);
destructor Destroy();override;
function GetTileSprite(TileID:Integer):TSprite;
private
end;
const
GROUNDID=0;
INVINCIBLEBLOCKID=1;
BLOCKID=2;
SPIKEID=3;
AIRID=255;
implementation
{ TTileset }
constructor TTileset.Create;
begin
inherited;
end;
constructor TTileset.Create(Sprite: TSprite);
begin
inherited Create;
TilesetSprite:=Sprite;
end;
destructor TTileset.Destroy;
begin
inherited;
if TilesetSprite<>nil then TilesetSprite.Destroy(); //taking care of tileset sprite.
end;
function TTileset.GetTileSprite(TileID: Integer): TSprite;
var x,y:Integer;
begin
if TileID>TilesetRows*TilesetCols then begin
//we can't get tileID bigger than last tile in tileset, so we failing there.
Result:=TSprite.Create(al_create_bitmap(TileSize,TileSize),al_create_bitmap(TileSize,TileSize));
al_clear_to_color(Result.SpriteBitmap,al_makecol(255,0,255));
al_clear_to_color(Result.MaskBitmap,al_makecol(255,0,255));
al_textout_ex(Result.SpriteBitmap,al_font,'?',0,0,al_makecol(0,0,0),al_makecol(255,0,255));
exit;
end;
Result:=TSprite.Create(al_create_bitmap(TileSize,TileSize),al_create_bitmap(TileSize,TileSize));
//getting x/y in TILES of tile with selected ID (thanks imcold from Pascal Game
//Development forums for code!)
x:= TileID mod TilesetCols;
y:= TileID div TilesetCols;
//and now changing it into values in pixels to cut tile into sprite properly
x:= x*TileSize;
y:= y*TileSize;
//now the fun part - cutting tileset's sprite
al_blit(TilesetSprite.SpriteBitmap,Result.SpriteBitmap,x,y,0,0,TileSize,TileSize); //sprite
al_blit(TilesetSprite.MaskBitmap,Result.MaskBitmap,x,y,0,0,TileSize,TileSize); //collision mask
end;
end.