-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTILESETS.PAS
517 lines (469 loc) · 11.6 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
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
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
{$I COMPILER.INC}
unit Tilesets;
interface
uses
AplObj,
AplTypes,
Streams,
Compress,
Lists,
Common,
Errors;
const
TilesetMajorVersion = 1;
TilesetMinorVersion = 0;
TilesetSignature = 'TIL';
DefaultImageListCapacity = 16;
type
PTileset = ^TTileset;
PImageList = ^TImageList;
TTilesetCompression = (tcNone, tcLzw);
TTilesetHeader = record
FileTypeSignature: string[3];
MajorVersion: integer;
MinorVersion: integer;
TileWidth: integer;
TileHeight: integer;
TileCount: integer;
Name: string[50];
Compression: TTilesetCompression;
Reserved: array[0..189] of byte;
end;
TTileset = object(TExceptionObject)
private
public
Tiles: PImageList;
Name: string;
Filename: string;
Compression: TTilesetCompression;
function TileWidth: integer;
function TileHeight: integer;
constructor Create(AWidth, AHeight: integer);
constructor LoadFromFile(const AFilename: string);
procedure LoadFromStream(AStream: PStream);
procedure SaveToStream(AStream: PStream);
procedure SaveToFile(const AFilename: string);
procedure Init; virtual;
destructor Free; virtual;
end;
TImageList = object(TExceptionObject)
private
FStream: PMemoryStream;
FWidth: word;
FHeight: word;
FCapacity: integer;
procedure Grow;
public
Count: integer;
constructor Create(AWidth, AHeight: word);
constructor CreateCapacity(AWidth, AHeight: word; ACapacity: integer);
destructor Free; virtual;
function Stream: PMemoryStream;
function NewImage: longint;
function Width: word;
function Height: word;
function PositionOf(AIndex: integer): longint;
procedure NewImageAt(AIndex: integer);
procedure SetCapacity(ACapacity: integer);
procedure Init; virtual;
procedure Swap(AIndex1, AIndex2: integer); virtual;
procedure Move(AIndex, ANewIndex: integer); virtual;
procedure Delete(AIndex: integer); virtual;
procedure Clear; virtual;
end;
implementation
uses
Lzw;
constructor TTileset.Create(AWidth, AHeight: integer);
begin
inherited Create;
Tiles := New(PImageList, Create(AWidth, AHeight));
if not Assigned(Tiles) then
Raise(ecNotEnoughMemory);
end;
function TTileset.TileWidth: integer;
begin
if not Assigned(Tiles) then
TileWidth := 0
else
TileWidth := Tiles^.Width;
end;
function TTileset.TileHeight: integer;
begin
if not Assigned(Tiles) then
TileHeight := 0
else
TileHeight := Tiles^.Height;
end;
procedure TTileset.Init;
begin
inherited Init;
Tiles := nil;
Name := '';
Filename := '';
Compression := tcLzw;
end;
destructor TTileset.Free;
begin
FreeAndNil(Tiles);
inherited Free;
end;
procedure TTileset.LoadFromStream(AStream: PStream);
var
header: TTilesetHeader;
lzw: TLzw;
count: integer;
begin
AStream^.Read(@header, SizeOf(header));
if AStream^.HasException then begin
Raise(AStream^.Exception^.Code);
exit;
end;
if header.FileTypeSignature <> TilesetSignature then begin
Raise(ecFileInvalidFormat);
exit;
end;
if (header.MajorVersion > TilesetMajorVersion) or
((header.MajorVersion = TilesetMajorVersion) and
(header.MinorVersion > TilesetMinorVersion)) then begin
Raise(ecFileInvalidVersion);
exit;
end;
Tiles := New(PImageList, Create(header.TileWidth, header.TileHeight));
if (header.TileCount = 0) or (header.TileWidth <= 0) or (header.TileHeight <= 0) then
exit;
if not Assigned(Tiles) then begin
Raise(ecNotEnoughMemory);
exit;
end;
if header.Compression = tcLzw then begin
lzw.Create;
lzw.DecompressStream(AStream, Tiles^.FStream);
AStream^.Seek(0, soFromBeginning);
if lzw.HasException then begin
Raise(lzw.Exception^.Code);
FreeAndNil(Tiles);
end
else
Tiles^.Count := header.TileCount;
lzw.Free;
end
else begin
Tiles^.FStream^.ReadFromStream(AStream, header.TileWidth * header.TileHeight * header.TileCount);
if Tiles^.FStream^.HasException then begin
Raise(Tiles^.FStream^.Exception^.Code);
FreeAndNil(Tiles);
exit;
end;
AStream^.Seek(0, soFromBeginning);
Tiles^.Count := header.TileCount;
end;
end;
constructor TTileset.LoadFromFile(const AFilename: string);
var
stream: PFileStream;
begin
inherited Create;
stream := New(PFileStream, Create(AFilename, fmRead));
if not Assigned(stream) then begin;
Raise(ecNotEnoughMemory);
exit;
end;
LoadFromStream(stream);
if stream^.HasException then
Raise(stream^.Exception^.Code)
else
Filename := AFilename;
FreeAndNil(stream);
end;
procedure TTileset.SaveToStream(AStream: PStream);
var
header: TTilesetHeader;
lzw: TLzw;
count: integer;
begin
count := 0;
if Assigned(Tiles) then
count := Tiles^.Count;
FillChar(header, SizeOf(header), 0);
header.FileTypeSignature := TilesetSignature;
header.MajorVersion := TilesetMajorVersion;
header.MinorVersion := TilesetMinorVersion;
header.TileWidth := TileWidth;
header.TileHeight := TileHeight;
header.TileCount := count;
header.Name := name;
header.Compression := Compression;
AStream^.Write(@header, SizeOf(header));
if count = 0 then
exit;
if Compression = tcLzw then begin
lzw.Create;
lzw.CompressStream(Tiles^.FStream, AStream, Tiles^.FWidth * Tiles^.FHeight * count);
if lzw.HasException then
Raise(lzw.Exception^.Code);
lzw.Free;
end
else begin
Tiles^.FStream^.WriteToStream(AStream, Tiles^.FWidth * Tiles^.FHeight * count);
if Tiles^.FStream^.HasException then begin
Raise(Tiles^.FStream^.Exception^.Code);
Tiles^.FStream^.ClearException;
end;
end;
end;
procedure TTileset.SaveToFile(const AFilename: string);
var
stream: PFileStream;
begin
stream := New(PFileStream, Create(AFilename, fmWrite));
if not Assigned(stream) then begin;
Raise(ecNotEnoughMemory);
exit;
end;
SaveToStream(stream);
if stream^.HasException then
Raise(stream^.Exception^.Code);
Filename := AFilename;
FreeAndNil(stream);
end;
constructor TImageList.Create(AWidth, AHeight: word);
begin
inherited Create;
CreateCapacity(AWidth, AHeight, DefaultImageListCapacity);
end;
constructor TImageList.CreateCapacity(AWidth, AHeight: word; ACapacity: integer);
begin
inherited Create;
FStream := nil;
FWidth := AWidth;
FHeight := AHeight;
FCapacity := 0;
SetCapacity(ACapacity);
end;
procedure TImageList.Init;
begin
inherited Init;
FWidth := 0;
FHeight := 0;
Count := 0;
FStream := nil;
FCapacity := 0;
end;
function TImageList.Width: word;
begin
Width := FWidth;
end;
function TImageList.Height: word;
begin
Height := FHeight;
end;
destructor TImageList.Free;
begin
FreeAndNil(FStream);
inherited Free;
end;
function TImageList.Stream: PMemoryStream;
begin
Stream := FStream;
end;
procedure TImageList.SetCapacity(ACapacity: integer);
var
newStream: PMemoryStream;
max: word;
dataSize: longint;
oldSize: longint;
position: longint;
error: TException;
begin
if FCapacity = ACapacity then
exit;
if ACapacity > ListBufferSize then begin
Raise(ecCollectionCapacityOverflow);
exit;
end;
dataSize := FWidth * FHeight * ACapacity;
if not Assigned(FStream) then begin
FStream := New(PMemoryStream, Create(dataSize));
if (FStream = nil) or (FStream^.HasException) then begin
Raise(ecNotEnoughMemory);
FreeAndNil(FStream);
exit;
end;
FStream^.Fill(dataSize, 0);
FStream^.Seek(0, soFromBeginning);
FCapacity := ACapacity;
exit;
end;
newStream := New(PMemoryStream, Create(dataSize));
if (newStream = nil) or (newStream^.HasException) then begin
Raise(ecNotEnoughMemory);
FreeAndNil(newStream);
exit;
end;
oldSize := FWidth * FHeight * FCapacity;
if ACapacity > FCapacity then begin
position := FStream^.Position;
FStream^.Seek(0, soFromBeginning);
newStream^.Seek(0, soFromBeginning);
FStream^.WriteToStream(newStream, oldSize);
FreeAndNil(FStream);
newStream^.Seek(position, soFromBeginning);
end;
FStream := newStream;
FCapacity := ACapacity;
if Count > FCapacity then
Count := FCapacity;
end;
procedure TImageList.Grow;
var
newCapacity: integer;
begin
if FCapacity >= 64 then
newCapacity := (FCapacity * 3) div 2
else if FCapacity >= 8 then
newCapacity := FCapacity + 16
else
newCapacity := FCapacity + 4;
SetCapacity(newCapacity);
end;
function TImageList.NewImage: longint;
begin
if Count + 1 > FCapacity then begin
Grow;
if HasException then
exit;
end;
Inc(Count);
NewImage := Count - 1;
end;
procedure TImageList.NewImageAt(AIndex: integer);
var
newIndex: integer;
begin
newIndex := NewImage;
Move(newIndex, AIndex);
end;
function TImageList.PositionOf(AIndex: integer): longint;
begin
PositionOf := FWidth * FHeight * AIndex;
end;
procedure TImageList.Swap(AIndex1, AIndex2: integer);
var
ptr1, ptr2: PByte;
pos1, pos2: longint;
size: integer;
begin
if (Count < 2) or (AIndex1 < 0) or (AIndex2 < 0)
or (AIndex1 > Count - 1) or (AIndex2 > Count - 1) or
(AIndex1 = AIndex2) then
exit;
size := FWidth * FHeight;
GetMem(ptr1, size);
if not Assigned(ptr1) then begin
Raise(ecNotEnoughMemory);
exit;
end;
GetMem(ptr2, size);
if not Assigned(ptr2) then begin
FreeMem(ptr1, size);
Raise(ecNotEnoughMemory);
exit;
end;
pos1 := PositionOf(AIndex1);
pos2 := PositionOf(AIndex2);
FStream^.Seek(pos1, soFromBeginning);
FStream^.Read(ptr1, size);
FStream^.Seek(pos2, soFromBeginning);
FStream^.Read(ptr2, size);
FStream^.Seek(pos1, soFromBeginning);
FStream^.Write(ptr2, size);
FStream^.Seek(pos2, soFromBeginning);
FStream^.Write(ptr1, size);
FreeMem(ptr1, size);
FreeMem(ptr2, size);
end;
procedure TImageList.Move(AIndex, ANewIndex: integer);
var
pos: longint;
size: integer;
ptr, newPtr: PByte;
index: integer;
begin
if AIndex = ANewIndex then
exit;
if (AIndex < 0) or (AIndex > Count - 1) then
exit;
if (ANewIndex < 0) or (ANewIndex > Count - 1) then
exit;
size := FWidth * FHeight;
GetMem(ptr, size);
if not Assigned(ptr) then begin
Raise(ecNotEnoughMemory);
exit;
end;
GetMem(newPtr, size);
if not Assigned(newPtr) then begin
FreeMem(ptr, size);
Raise(ecNotEnoughMemory);
exit;
end;
pos := PositionOf(AIndex);
FStream^.Seek(pos, soFromBeginning);
FStream^.Read(ptr, size);
Delete(AIndex);
for index := Count - 1 downto ANewIndex do begin
pos := PositionOf(index);
FStream^.Seek(pos, soFromBeginning);
FStream^.Read(newPtr, size);
pos := PositionOf(index + 1);
FStream^.Write(newPtr, size);
end;
pos := PositionOf(AIndex);
FStream^.Seek(pos, soFromBeginning);
FStream^.Write(Ptr, 0);
FreeMem(ptr, size);
FreeMem(newPtr, size);
end;
procedure TImageList.Delete(AIndex: integer);
var
pos: longint;
size: integer;
ptr: PByte;
index: integer;
begin
if (AIndex < 0) or (AIndex > Count - 1) then
exit;
pos := PositionOf(AIndex);
size := FWidth * FHeight;
if AIndex = Count - 1 then begin
FStream^.Seek(pos, soFromBeginning);
FStream^.Fill(size, 0);
Dec(Count);
exit;
end;
GetMem(ptr, size);
if not Assigned(ptr) then begin
Raise(ecNotEnoughMemory);
exit;
end;
for index := AIndex + 1 to Count - 1 do begin
pos := PositionOf(index);
FStream^.Seek(pos, soFromBeginning);
FStream^.Read(ptr, size);
pos := PositionOf(index - 1);
FStream^.Write(ptr, size);
end;
pos := PositionOf(Count - 1);
FStream^.Seek(pos, soFromBeginning);
FStream^.Fill(size, 0);
Dec(Count);
FreeMem(ptr, size);
end;
procedure TImageList.Clear;
begin
FreeAndNil(FStream);
Count := 0;
FCapacity := 0;
end;
end.