-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathpsv_voxel.pas
377 lines (338 loc) · 9.44 KB
/
psv_voxel.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
//------------------------------------------------------------------------------
//
// DD_VOXEL: DelphiDoom Voxel Editor
// Copyright (C) 2013-2021 by Jim Valavanis
//
// 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.
//
// DESCRIPTION:
// VoxelScript loader
//
//------------------------------------------------------------------------------
// E-Mail: jimmyvalavanis@yahoo.gr
// Site : https://sourceforge.net/projects/delphidoom-voxel-editor/
// https://sourceforge.net/projects/delphidoom/files/DDVoxels/
//------------------------------------------------------------------------------
unit psv_voxel;
interface
uses
SysUtils,
Classes,
voxels,
vxe_utils;
type
voxelcmd_t = record
cmd: integer;
x, y, z: byte;
Color: voxelitem_t;
end;
voxelcmd_p = ^voxelcmd_t;
voxelcmd_a = array[0..$FFF] of voxelcmd_t;
voxelcmd_pa = ^voxelcmd_a;
const
C_vxSet = 0;
C_vxClear = 1;
const
VDL_MAGIC = $2;
type
scriptlanguage_t = (sl_pascal, sl_voxelddscript, sl_clang);
TDDVoxelScriptLoader = class(TObject)
private
fNumCmds: integer;
fRealNumCmds: integer;
fCmds: voxelcmd_pa;
voxelbuf: voxelbuffer_p;
voxelsz: integer;
function Grow: voxelcmd_p;
protected
procedure AddCmd(const cmd: integer; const x, y, z: byte; const cl: LongWord);
public
constructor Create(const abuf: voxelbuffer_p; const asz: integer); virtual;
destructor Destroy; override;
procedure Clear;
function LoadFromScript(const aScript: string; const doCompile, doRun: boolean;
var Data: {$IFDEF UNICODE}AnsiString{$ELSE}string{$ENDIF}): boolean;
function AppendFromScript(const aScript: string; const doCompile, doRun: boolean;
var Data: {$IFDEF UNICODE}AnsiString{$ELSE}string{$ENDIF}): boolean;
function LoadFromStream(const aStream: TStream): boolean;
function AppendFromStream(const aStream: TStream): boolean;
function LoadFromFile(const aFileName: string): boolean;
function AppendFromFile(const aFileName: string): boolean;
procedure SaveToStream(const aStream: TStream);
procedure SaveToFile(const aFileName: string);
function RenderToBuffer: integer;
function VoxelItemAt(const x, y, z: byte): LongWord;
property NumCmds: integer read fNumCmds;
end;
procedure VDLS_SetVoxel(const x, y, z: byte; const value: LongWord);
procedure VDLS_ClearVoxel(const value: LongWord);
procedure VDLS_Clear;
function VDLS_GetVoxel(const x, y, z: byte): LongWord;
function VDLS_VoxelSize: Integer;
function VDLS_RGB(const r, g, b: byte): LongWord;
function VDLS_GetRValue(const c: LongWord): LongWord;
function VDLS_GetGValue(const c: LongWord): LongWord;
function VDLS_GetBValue(const c: LongWord): LongWord;
implementation
uses
psv_script,
psv_script_functions,
frm_Editor,
main,
psv_VXTextures,
psv_VXVoxels,
psv_temp;
var
currentvoxelloader: TDDVoxelScriptLoader;
constructor TDDVoxelScriptLoader.Create(const abuf: voxelbuffer_p; const asz: integer);
begin
fNumCmds := 0;
fRealNumCmds := 0;
fCmds := nil;
voxelbuf := abuf;
voxelsz := asz;
inherited Create;
end;
destructor TDDVoxelScriptLoader.Destroy;
begin
Clear;
inherited;
end;
function TDDVoxelScriptLoader.Grow: voxelcmd_p;
begin
Inc(fNumCmds);
if fNumCmds >= fRealNumCmds then
begin
fRealNumCmds := fRealNumCmds + 1024;
ReallocMem(fCmds, fRealNumCmds * SizeOf(voxelcmd_t));
end;
Result := @fCmds[fNumCmds - 1];
end;
procedure TDDVoxelScriptLoader.AddCmd(const cmd: integer; const x, y, z: byte;
const cl: LongWord);
var
pc: voxelcmd_p;
begin
pc := Grow;
pc.cmd := cmd;
pc.x := x;
pc.y := y;
pc.z := z;
pc.Color := cl;
end;
procedure TDDVoxelScriptLoader.Clear;
begin
ReallocMem(fCmds, 0);
fNumCmds := 0;
fRealNumCmds := 0;
end;
function TDDVoxelScriptLoader.LoadFromScript(const aScript: string; const doCompile, doRun: boolean;
var Data: {$IFDEF UNICODE}AnsiString{$ELSE}string{$ENDIF}): boolean;
begin
Clear;
Result := AppendFromScript(aScript, doCompile, doRun, Data);
end;
function TDDVoxelScriptLoader.AppendFromScript(const aScript: string; const doCompile, doRun: boolean;
var Data: {$IFDEF UNICODE}AnsiString{$ELSE}string{$ENDIF}): boolean;
begin
currentvoxelloader := Self;
VXT_ResetTextures;
VXT_ResetVoxels;
VXT_ClearSearchPaths;
VXT_AddSearchPath(ExtractFilePath(EditorForm.FileName));
VXT_AddSearchPath(ExtractFilePath(Form1.FileName));
VXT_AddSearchPath(ExtractFilePath(ParamStr(0)));
VXT_AddSearchPath(GetCurrentDir);
Result := VDL_CompileScript(aScript, doCompile, doRun, Data);
VXT_ResetTextures;
VXT_ResetVoxels;
VXT_ClearSearchPaths;
end;
function TDDVoxelScriptLoader.LoadFromStream(const aStream: TStream): boolean;
begin
Clear;
Result := AppendFromStream(aStream);
end;
function TDDVoxelScriptLoader.AppendFromStream(const aStream: TStream): boolean;
var
header: integer;
sz: integer;
begin
aStream.Read(header, SizeOf(integer));
if header <> VDL_MAGIC then
begin
Result := False;
Exit;
end;
aStream.Read(sz, SizeOf(integer));
if sz < 0 then
begin
Result := False;
Exit;
end;
fNumCmds := sz;
fRealNumCmds := sz;
ReallocMem(fCmds, fNumCmds * SizeOf(voxelcmd_t));
aStream.Read(fCmds^, fNumCmds * SizeOf(voxelcmd_t));
Result := True;
end;
function TDDVoxelScriptLoader.LoadFromFile(const aFileName: string): boolean;
var
fs: TFileStream;
begin
fs := TFileStream.Create(aFileName, fmOpenRead);
try
Result := LoadFromStream(fs);
finally
fs.Free;
end;
end;
function TDDVoxelScriptLoader.AppendFromFile(const aFileName: string): boolean;
var
fs: TFileStream;
begin
fs := TFileStream.Create(aFileName, fmOpenRead);
try
Result := AppendFromStream(fs);
finally
fs.Free;
end;
end;
procedure TDDVoxelScriptLoader.SaveToStream(const aStream: TStream);
var
header: integer;
sz: integer;
begin
header := VDL_MAGIC;
aStream.Write(header, SizeOf(integer));
sz := fNumCmds;
aStream.Write(sz, SizeOf(integer));
aStream.Write(fCmds^, sz * SizeOf(voxelcmd_t));
end;
procedure TDDVoxelScriptLoader.SaveToFile(const aFileName: string);
var
fs: TFileStream;
begin
fs := TFileStream.Create(aFileName, fmOpenReadWrite or fmCreate);
try
SaveToStream(fs);
finally
fs.Free;
end;
end;
function TDDVoxelScriptLoader.RenderToBuffer: integer;
var
cnt, i, j, k: integer;
pc: voxelcmd_p;
begin
Result := 0;
for cnt := 0 to fNumCmds - 1 do
begin
pc := @fCmds[cnt];
case pc.cmd of
C_vxSet:
if pc.x < voxelsz then
if pc.y < voxelsz then
if pc.z < voxelsz then
if voxelbuf[pc.x, pc.y, pc.z] <> pc.Color then
begin
voxelbuf[pc.x, pc.y, pc.z] := pc.Color;
Inc(Result);
end;
C_vxClear:
for i := 0 to voxelsz - 1 do
for j := 0 to voxelsz - 1 do
for k := 0 to voxelsz - 1 do
if voxelbuf[i, j, k] <> pc.Color then
begin
voxelbuf[i, j, k] := pc.Color;
Inc(Result);
end;
end;
end;
end;
function TDDVoxelScriptLoader.VoxelItemAt(const x, y, z: byte): LongWord;
begin
if (x >= voxelsz) or (y >= voxelsz) or (z >= voxelsz) then
Result := 0
else
Result := voxelbuf[x, y, z];
end;
//-------------------------- PascalScript Functions ----------------------------
procedure VDLS_SetVoxel(const x, y, z: byte; const value: LongWord);
begin
if currentvoxelloader = nil then
begin
printf('VDLS_SetVoxel(): No voxel loader available'#13#10);
Exit;
end;
currentvoxelloader.AddCmd(C_vxSet, x, y, z, value);
end;
procedure VDLS_ClearVoxel(const value: LongWord);
begin
if currentvoxelloader = nil then
begin
printf('VDLS_ClearVoxel(): No voxel loader available'#13#10);
Exit;
end;
currentvoxelloader.AddCmd(C_vxClear, 0, 0, 0, value);
end;
procedure VDLS_Clear;
begin
if currentvoxelloader = nil then
begin
printf('VDLS_Clear(): No voxel loader available'#13#10);
Exit;
end;
currentvoxelloader.AddCmd(C_vxClear, 0, 0, 0, 0);
end;
function VDLS_GetVoxel(const x, y, z: byte): LongWord;
begin
if currentvoxelloader = nil then
begin
printf('VDLS_Clear(): No voxel loader available'#13#10);
Result := 0;
Exit;
end;
Result := currentvoxelloader.VoxelItemAt(x, y, z);
end;
function VDLS_VoxelSize: Integer;
begin
if currentvoxelloader = nil then
begin
printf('VDLS_VoxelSize(): No voxel loader available'#13#10);
Result := 0;
Exit;
end;
Result := currentvoxelloader.voxelsz;
end;
function VDLS_RGB(const r, g, b: byte): LongWord;
begin
Result := (r or (g shl 8) or (b shl 16));
end;
function VDLS_GetRValue(const c: LongWord): LongWord;
begin
Result := Byte(c);
end;
function VDLS_GetGValue(const c: LongWord): LongWord;
begin
Result := Byte(c shr 8);
end;
function VDLS_GetBValue(const c: LongWord): LongWord;
begin
Result := Byte(c shr 16);
end;
end.