-
Notifications
You must be signed in to change notification settings - Fork 4
/
wraylib.pas
337 lines (295 loc) · 10 KB
/
wraylib.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
unit wraylib;
{$mode objfpc}{$H+}
interface
uses
Classes, SysUtils,LazFileUtils,rmcore,rmcodegen,rmxgfcore,rwxgf,rmconfig,rwpng;
type
raylibImageHeadRec = packed Record
width : smallint;
height : smallint;
mipmaps : smallint;
format : smallint;
end;
const
RGBSize = 3;
RGBASize = 4;
procedure WriteRayLibCodeToBuffer(var F : Text; x,y,x2,y2, Lan,format : integer;ImageName : string);
procedure WriteRayLibCodeToFile(filename : string; x,y,x2,y2, Lan,format : integer);
procedure ResExportRayLibToBuffer(var F : File; x,y,x2,y2,format : integer);
function ResRayLibImageSize(width,height ,format : integer) : longint;
function RayLibImageSize(width,height ,format : integer) : longint;
function GetRayLibFormatValue(format : integer) : string;
implementation
function RayLibImageSize(width,height ,format : integer) : longint;
var
Size : longint;
begin
if format = RGBSize then Size:=3*width*height else Size:=4*width*height;
RayLibImageSize:=Size;
end;
function ResRayLibImageSize(width,height ,format : integer) : longint;
begin
ResRayLibImageSize:=sizeof(raylibimageHeadRec)+RayLibImageSize(width,height ,format);
end;
function GetRayLibFormatDesc(format : integer) : string;
begin
GetRayLibFormatDesc:='';
Case format of 1:GetRayLibFormatDesc:='RGBA - Fuchsia';
2:GetRayLibFormatDesc:='RGBA - Index 0';
3:GetRayLibFormatDesc:='RGB';
4:GetRayLibFormatDesc:='RGBA - Custom';
end;
end;
function GetRayLibFormatValue(format : integer) : string;
begin
GetRayLibFormatValue:='';
Case format of 1,2,4:GetRayLibFormatValue:='7'; // PIXELFORMAT_UNCOMPRESSED_R8G8B8A8
3:GetRayLibFormatValue:='4'; // PIXELFORMAT_UNCOMPRESSED_R8G8B8
end;
end;
procedure ExportPascalHeader(var mc : codegenrec;width,height,imageId,format : integer;ImageName : string);
var
size : longint;
begin
size:=RayLibImageSize(width,height,format);
MWSetValuesTotal(mc,size);
MWSetValuesPerLine(mc,20);
MWSetLan(mc,PascalLan);
MWSetValueFormat(mc,ValueFormatHex);
Writeln(mc.FTextPtr^,'(* RGB/RGBA Pascal Image Code Created By Raster Master *)');
Writeln(mc.FTextPtr^,'(* Size = ',size,' Format = ',GetRayLibFormatDesc(format),' Width=',width,' Height=',height,' *)');
Writeln(mc.FTextPtr^,' ',ImageName,'_Size = ',size,';');
Writeln(mc.FTextPtr^,' ',ImageName,'_Format = ', GetRayLibFormatValue(format),';');
Writeln(mc.FTextPtr^,' ',ImageName,'_Width = ',width,';');
Writeln(mc.FTextPtr^,' ',ImageName,'_Height = ',height,';');
Writeln(mc.FTextPtr^,' ',ImageName,'_Id = ',imageId,';');
Writeln(mc.FTextPtr^,' ',ImageName,' : array[0..',size-1,'] of byte = (');
end;
procedure ExportCHeader(var mc : codegenrec;width,height,imageId,format : integer;ImageName : string);
var
size : longint;
begin
size:=RayLibImageSize(width,height,format);
MWSetValuesTotal(mc,size);
MWSetValuesPerLine(mc,20);
MWSetLan(mc,CLan);
MWSetValueFormat(mc,ValueFormatHex);
Writeln(mc.FTextPtr^,'/* RGB/RGBA C Image Code Created By Raster Master */');
Writeln(mc.FTextPtr^,'/* Size = ',size,' Format = ',GetRayLibFormatDesc(format),' Width=',width,' Height=',height,' */');
Writeln(mc.FTextPtr^,'#define ',ImageName,'_Size ',size);
Writeln(mc.FTextPtr^,'#define ',ImageName,'_Format ',GetRayLibFormatValue(format));
Writeln(mc.FTextPtr^,'#define ',ImageName,'_Width ',width);
Writeln(mc.FTextPtr^,'#define ',ImageName,'_Height ',height);
Writeln(mc.FTextPtr^,'#define ',ImageName,'_Id ',imageId);
Writeln(mc.FTextPtr^,'static unsigned char ',Imagename, '[',size,'] = {');
end;
procedure ExportBasicHeader(var mc : codegenrec;width,height,imageId,format : integer;ImageName : string);
var
size : longint;
begin
size:=RayLibImageSize(width,height,format);
MWSetValuesTotal(mc,size);
MWSetValuesPerLine(mc,20);
MWSetLan(mc,BasicLan);
MWSetValueFormat(mc,ValueFormatHex);
Writeln(mc.FTextPtr^,#39,' RGB/RGBA Basic Image Code Created By Raster Master');
Writeln(mc.FTextPtr^,#39,' Size = ',size,' Format = ',GetRayLibFormatDesc(format),' Width=',width,' Height=',height);
end;
procedure ExportQBJSHeader(var mc : codegenrec;width,height,imageId,format : integer;ImageName : string);
var
size : longint;
begin
size:=RayLibImageSize(width,height,format);
MWSetValuesTotal(mc,size);
MWSetValuesPerLine(mc,20);
MWSetLan(mc,QBJSBasicLan);
MWSetValueFormat(mc,ValueFormatHex);
Writeln(mc.FTextPtr^,#39,' RGB/RGBA QBJS Image Code Created By Raster Master');
Writeln(mc.FTextPtr^,#39,' Size = ',size,' Format = ',GetRayLibFormatDesc(format),' Width=',width,' Height=',height);
end;
procedure WriteRayLibCodeToBuffer(var F : Text; x,y,x2,y2, Lan,format : integer;ImageName : string);
var
mc : codegenrec;
width,height,imageId : integer;
i,j : integer;
PixelIndex : integer;
cr : TRMColorRec;
alpha : integer;
PngRGBA : PngRGBASettingsRec;
begin
rmconfigbase.GetProps(PngRGBA);
MWInit(mc,F);
width:=x2-x+1;
height:=y2-y+1;
imageId:=GetThumbIndex;
case Lan of FPLan:ExportPascalHeader(mc,width,height,imageId,format,ImageName);
gccLan:ExportCHeader(mc,width,height,imageId,format,ImageName);
FBLan,QB64Lan:ExportBasicHeader(mc,width,height,imageId,format,ImageName);
QBJSLan:ExportQBJSHeader(mc,width,height,imageId,format,ImageName);
end;
for j:=y to y2 do
begin
for i:=x to x2 do
begin
PixelIndex:=GetPixel(i,j);
GetColor(PixelIndex,cr);
alpha:=255;
if (format = 1) then
begin
if (cr.r = 255) and (cr.b=255) and (cr.g=0) then //if fuschia
begin
alpha:=0; // Alpha 0 = transparent
end;
MWWriteByte(mc,cr.r);
MWWriteByte(mc,cr.g);
MWWriteByte(mc,cr.b);
MWWriteByte(mc,alpha);
end
else if (format = 2) then
begin
if PixelIndex = 0 then
begin
alpha:=0; // transparent
end;
MWWriteByte(mc,cr.r);
MWWriteByte(mc,cr.g);
MWWriteByte(mc,cr.b);
MWWriteByte(mc,alpha);
end
else if (format = 4) then //custom
begin
if (PngRGBA.UseColorIndex) and (PngRGBA.ColorIndex=PixelIndex) then
begin
alpha:=0; // Alpha 0 = transparent
end;
if (PngRGBA.UseFuschia) and (cr.r = 255) and (cr.g=0) and (cr.b=255) then //use fuschia
begin
alpha:=0; // Alpha 0 = transparent
end;
if (PngRGBA.UseCustom) and (cr.r = PngRGBA.R) and (cr.b=PngRGBA.B) and (cr.g=PngRGBA.G) then //if custom RGB
begin
alpha:=PngRGBA.A; // set custom Alpha value
end;
MWWriteByte(mc,cr.r);
MWWriteByte(mc,cr.g);
MWWriteByte(mc,cr.b);
MWWriteByte(mc,alpha);
end
else //must be format 3 - RGB
begin
MWWriteByte(mc,cr.r);
MWWriteByte(mc,cr.g);
MWWriteByte(mc,cr.b);
end;
end;
end;
Case Lan of gccLan:Writeln(F,'};');
FPLan:Writeln(F,');');
else Writeln(f);
End;
end;
procedure WriteRayLibCodeToFile(filename : string; x,y,x2,y2, Lan,format : integer);
var
F : Text;
ImageName : string;
begin
SetCoreActive;
{$I-}
Assign(F,Filename);
Rewrite(F);
{$I+}
if IORESULT<>0 then exit;
Imagename:=ExtractFileName(ExtractFileNameWithoutExt(filename));
WriteRayLibCodeToBuffer(F,x,y,x2,y2,Lan,format,ImageName);
{$I-}
close(F);
{$I+}
end;
procedure ResExportRayLibToBuffer(var F : File; x,y,x2,y2, format : integer);
var
rimage : raylibImageHeadRec;
i,j : integer;
PixelIndex : integer;
cr : TRMColorRec;
alpha : integer;
PixelLine : array[0..2047] of Byte; // a line of RGB or RGBA pixels
PCounter : integer;
PngRGBA : PngRGBASettingsRec;
begin
rmconfigbase.GetProps(PngRGBA);
rimage.width:=x2-x+1;
rimage.height:=y2-y+1;
rimage.mipmaps:=1;
rimage.format:=7; //rgba
if format = 3 then rimage.format:=4; //rgb
{$I-}
BlockWrite(F,rimage,sizeof(rimage));
{$I+}
if IORESULT <> 0 then exit;
for j:=y to y2 do
begin
PCounter:=0;
for i:=x to x2 do
begin
PixelIndex:=GetPixel(i,j);
GetColor(PixelIndex,cr);
alpha:=255;
if (format = 1) then
begin
if (cr.r = 255) and (cr.b=255) and (cr.g=0) then //if fuschia
begin
alpha:=0; // Alpha 0 = transparent
end;
PixelLine[PCounter]:=cr.r;
PixelLine[PCounter+1]:=cr.g;
PixelLine[PCounter+2]:=cr.b;
PixelLine[PCounter+3]:=alpha;
inc(PCounter,4);
end
else if (format = 2) then
begin
if PixelIndex = 0 then
begin
alpha:=0; // transparent
end;
PixelLine[PCounter]:=cr.r;
PixelLine[PCounter+1]:=cr.g;
PixelLine[PCounter+2]:=cr.b;
PixelLine[PCounter+3]:=alpha;
inc(PCounter,4);
end
else if (format = 4) then //custom
begin
if (PngRGBA.UseColorIndex) and (PngRGBA.ColorIndex=PixelIndex) then
begin
alpha:=0; // Alpha 0 = transparent
end;
if (PngRGBA.UseFuschia) and (cr.r = 255) and (cr.g=0) and (cr.b=255) then //use fuschia
begin
alpha:=0; // Alpha 0 = transparent
end;
if (PngRGBA.UseCustom) and (cr.r = PngRGBA.R) and (cr.b=PngRGBA.B) and (cr.g=PngRGBA.G) then //if custom RGB
begin
alpha:=PngRGBA.A; // set custom Alpha value
end;
PixelLine[PCounter]:=cr.r;
PixelLine[PCounter+1]:=cr.g;
PixelLine[PCounter+2]:=cr.b;
PixelLine[PCounter+3]:=alpha;
inc(PCounter,4);
end
else //must be format 3 - RGB
begin
PixelLine[PCounter]:=cr.r;
PixelLine[PCounter+1]:=cr.g;
PixelLine[PCounter+2]:=cr.b;
inc(PCounter,3);
end;
end;
{$I-}
Blockwrite(F,PixelLine,PCounter);
{$I+}
if IORESULT<>0 then exit;
end;
end;
end.