forked from boriswinner/raycaster
-
Notifications
You must be signed in to change notification settings - Fork 1
/
utexture.pas
79 lines (62 loc) · 1.93 KB
/
utexture.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
unit utexture;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils, SDL2;
type TTexture = record
RawTexture : PSDL_Texture;
Width, Height : Int32;
Transparent, Solid : boolean;
RenderTarget : PSDL_Renderer;
end;
type PTexture = ^TTexture;
function LoadTexture(_RenderTarget : PSDL_Renderer; FileName: string; _Transparent, _Solid: boolean) : TTexture;
procedure DestroyTexture(TextureToDestroy : PTexture);
function TextureExists(Target : PTexture) : boolean; inline;
var
TexturePath: string;
implementation
//LoadTexture(RenderTarget, FileName, Transparent, Solid);
//RenderTarget - use 'renderer' variable;
//FileName - I think you got it;
//Transparent - shows if texture supports transparency or not;
//Solid - shows ability to walk through walls with this texture.
function LoadTexture(_RenderTarget : PSDL_Renderer; FileName: string; _Transparent, _Solid: boolean) : TTexture;
var
bmp : PSDL_Surface;
begin
Result.Width:=0;
Result.Height:=0;
bmp := SDL_LoadBMP(PAnsiChar(TexturePath + FileName));
if bmp = nil then
exit;
if _Transparent then
begin
SDL_SetColorKey(bmp, 1, SDL_MapRGB(bmp^.format, 255, 0, 255)); //magenta is transparent
end;
Result.Transparent := _Transparent;
Result.RawTexture := SDL_CreateTextureFromSurface(_RenderTarget, bmp);
if Result.RawTexture = nil then
exit;
SDL_FreeSurface(bmp);
SDL_QueryTexture(Result.RawTexture, nil, nil, @Result.Width, @Result.Height);
Result.RenderTarget := _RenderTarget;
Result.Solid := _Solid;
end;
procedure DestroyTexture(TextureToDestroy : PTexture);
begin
with TextureToDestroy^ do
begin
RenderTarget := nil;
SDL_DestroyTexture(RawTexture);
RawTexture := nil;
Width := 0; Height := 0;
end;
end;
function TextureExists(Target : PTexture) : boolean; inline;
begin
Result := (Target^.Height <> 0) and (Target^.Width <> 0);
end;
initialization
TexturePath := './res/textures/';
end.