-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathun.Texture.pas
More file actions
260 lines (217 loc) · 7.37 KB
/
un.Texture.pas
File metadata and controls
260 lines (217 loc) · 7.37 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
{*****************************************************************************
Copyright (C) 2026 NormalUser
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.
*****************************************************************************}
unit un.Texture;
{$Mode objfpc} {$H+}
{$COPERATORS OFF}
// ******************** interface *************************
interface
USES SDL2,
un.Structs;
procedure digitalisiere_Eagle(width, hight : Integer; Rect : TSDL_Rect);
procedure draw_Background;
procedure init_Background;
procedure init_Atlas;
// ******************** implementation ********************
implementation
USES SDL2_image,
jsontools,
sysutils,
un.Utils,
un.Defs,
un.Draw;
procedure digitalisiere_Eagle(width, hight : Integer; Rect : TSDL_Rect);
VAR Image : PSDL_Surface;
pixel : PUInt32;
Col : UInt32;
x,y : Integer;
begin
Image := SDL_CreateRGBSurface(0, width, hight, 32, $FF000000, $00FF0000, $0000FF00, $000000FF);
SDL_RenderReadPixels(app.Renderer, @Rect, SDL_PIXELFORMAT_RGBA8888, Image^.pixels, Image^.pitch);
pixel := PUInt32(Image^.pixels); { assign the Startpointer of the Image to pixel }
setLength(EagleArr, width, hight);
for y := 0 to (pred(hight)) do
begin
for x := 0 to (pred(width)) do
begin
Col := UInt32(pixel[y * Image^.w + x]);
if Col = $FF then EagleArr[x,y] := 0 { depends on the background color !!!!! }
else EagleArr[x,y] := 1;
end;
end;
SDL_FreeSurface(Image);
end;
function HashCode(Value : String) : UInt32; { DJB hash function }
VAR i, x : UInt32;
begin
Result := 0;
for i := 1 to Length(Value) do
begin
Result := (Result SHL 4) + ORD(Value[i]);
x := Result AND $F0000000;
if (x <> 0) then
Result := Result XOR (x SHR 24);
Result := Result AND (NOT x);
end;
HashCode := Result;
end;
function getAtlasImage(filename : String) : PAtlasImage;
VAR a : PAtlasImage;
i : UInt32;
begin
i := HashCode(filename) MOD NUMATLASBUCKETS;
a := atlases[i]^.next;
getAtlasImage := NIL;
while (a <> NIL) do
begin
if a^.fnam = filename then
getAtlasImage := a;
a := a^.next;
end;
end;
procedure init_Texture;
VAR i : Byte;
fname : String;
begin
for i := 1 to Max_Exp do { load explosion }
begin
fname := 'gfx/exp' + intToStr(i) + '.png';
Explo[i].Textur := getAtlasImage(PChar(fname));
end;
game.Logo.Textur := getAtlasImage('gfx/logo.png'); { load background }
game.logo.Rect.w := game.logo.textur^.Rec.w + 180; { enlarge !! }
game.logo.Rect.h := game.logo.textur^.Rec.h + 40; { enlarge !! }
game.logo.Rect.x := xSizeHalf - game.logo.Rect.w DIV 2;
game.logo.Rect.y := 20;
game.Magigames.Textur := getAtlasImage('gfx/magigames_steel.gif'); { load background }
game.Magigames.Rect.w := game.Magigames.textur^.Rec.w * 2; { enlarge !! }
game.Magigames.Rect.h := game.Magigames.textur^.Rec.h * 2; { enlarge !! }
game.thruster_m.Textur := getAtlasImage('gfx/thruster1.png');
game.thruster_l.Textur := getAtlasImage('gfx/thruster_left.png');
game.thruster_r.Textur := getAtlasImage('gfx/thruster_right.png');
game.crosshairs.Textur := getAtlasImage('gfx/crosshairs.png');
game.miniship1.Textur := getAtlasImage('gfx/minieagle.png');
game.lander1.Textur := getAtlasImage('gfx/eagle.png');
game.miniship2.Textur := getAtlasImage('gfx/minirocket.png');
game.lander2.Textur := getAtlasImage('gfx/rocket.png');
game.miniship.Textur := game.miniship1.Textur;
Eagle.Textur := game.lander1.Textur;
Eagle.Rect.x := 0;
Eagle.Rect.y := 0;
Eagle.Rect.w := eagle.textur^.rec.w;
Eagle.Rect.h := eagle.textur^.rec.h;
blitAtlasImage(eagle.Textur, Eagle.Rect.x, Eagle.Rect.y, 0);
digitalisiere_Eagle(Eagle.Rect.w, Eagle.Rect.h, Eagle.Rect);
end;
procedure draw_Background;
begin
SDL_RenderCopy(app.Renderer, game.TBack.Textur, NIL, @game.TBack.Rect);
end;
procedure load_Background_Names;
VAR Info : TSearchRec;
begin
Anz_Back := 1;
backPicNam[Anz_Back] := 'gfx/red_plain.jpg'; { emergency - background pic }
If FindFirst ('images/*.jpg', faAnyFile, Info) = 0 then { if there are pictures then... }
begin
Anz_Back := 0;
Repeat { load all pictures }
INC(Anz_Back);
With Info do
begin
if Anz_Back <= Max_Back then
begin
backPicNam[Anz_Back] := 'images/' + Name;
end;
end;
Until FindNext(info) <> 0;
FindClose(Info);
end;
end;
function loadTexture(Pfad : String) : PSDL_Texture;
//VAR Fmt : PChar;
begin
loadTexture := IMG_LoadTexture(app.Renderer, PChar(Pfad));
if loadTexture = NIL then errorMessage(SDL_GetError());
//Fmt := 'Loading %s'#13;
//SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, Fmt, [PChar(Pfad)]);
end;
procedure init_Background;
begin
load_Background_Names;
game.Background.Textur := LoadTexture('gfx/red_plain.jpg'); { load background }
init_Texture;
end;
procedure initAtlasImage(VAR e : PAtlasImage);
begin
e^.FNam := ''; e^.Rot := 0; e^.Tex := NIL; e^.next := NIL;
end;
procedure loadAtlasTexture;
begin
atlasTex := IMG_LoadTexture(app.Renderer, Texture_Path);
if atlasTex = NIL then
errorMessage(SDL_GetError());
end;
procedure loadAtlasData;
VAR i, x, y, w, h, r : Integer;
a, AtlasNew : PAtlasImage;
N, C : TJsonNode;
filename : String;
begin
if FileExists(Atlas_Path) then
begin
{ Get the JSON data }
N := TJsonNode.Create;
N.LoadFromFile(Atlas_Path);
for c in n do
begin
filename := c.Find('filename').AsString;
x := c.Find('x').AsInteger;
y := c.Find('y').AsInteger;
w := c.Find('w').AsInteger;
h := c.Find('h').AsInteger;
r := c.Find('rotated').AsInteger;
i := HashCode(filename) MOD NUMATLASBUCKETS;
a := atlases[i]; { must be created and initialized before! }
while (a^.next <> NIL) do
begin a := a^.next; end;
NEW(AtlasNEW);
initAtlasImage(AtlasNEW);
AtlasNEW^.Fnam := filename;
AtlasNEW^.Rec.x := x;
AtlasNEW^.Rec.y := y;
AtlasNEW^.Rec.w := w;
AtlasNEW^.Rec.h := h;
AtlasNEW^.Rot := r;
AtlasNEW^.Tex := atlasTex;
AtlasNEW^.next := NIL;
a^.next := atlasNEW;
end;
N.free;
end
else
errorMessage('Atlas-Json not found!');
end;
procedure init_Atlas;
VAR i : Integer;
begin
for i := 0 to NUMATLASBUCKETS do
begin
NEW(atlases[i]);
initAtlasImage(atlases[i]); { create and initialize PAtlasImage }
end;
loadAtlasTexture;
loadAtlasData;
end;
end.