This repository has been archived by the owner on Aug 21, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
Copy patheditor_graphics.pas
1507 lines (1207 loc) · 34.2 KB
/
editor_graphics.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
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{ This file is a part of Map editor for VCMI project
Copyright (C) 2013-2017 Alexander Shishkin [email protected]
This source 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 code 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.
A copy of the GNU General Public License is available on the World Wide Web at
<http://www.gnu.org/copyleft/gpl.html>. You can also obtain it by writing to the Free Software Foundation, Inc., 59
Temple Place - Suite 330, Boston, MA 02111-1307, USA.
}
unit editor_graphics;
{$I compilersetup.inc}
interface
uses
Classes, SysUtils, Math, gvector, fgl, Gl, Graphics, IntfGraphics, GraphType, editor_types, editor_consts,
editor_utils, filesystem_base, editor_gl, vcmi_json, editor_classes, vcmi.image_loaders, LazLoggerBase, LazUTF8,
fpjson;
type
TSprites = specialize gvector.TVector<TGLSprite>;
TGraphicsLoadMode = (LoadFisrt, LoadRest, LoadComplete);
TGraphicsLoadFlag = (None, First, Complete);
{ TIconList }
TIconList = class
strict private
type
TImages = specialize TFPGObjectList<TLazIntfImage>;
var
FImages: TImages;
public
constructor Create;
destructor Destroy; override;
end;
{ TAnimation }
TAnimation = class
private
FLoaded: TGraphicsLoadFlag;
FPaletteID: GLuint;
FResourceID: AnsiString;
//typ: UInt32;
FWidth: UInt32;
FHeight: UInt32;
entries: TSprites;
function GetFrameCount: Integer; inline;
procedure SetFrameCount(AValue: Integer);
procedure UnbindPalette;
public
constructor Create;
destructor Destroy; override;
procedure RenderBorder(AState: TLocalState; TileX,TileY: Integer);
procedure RenderIcon(AState: TLocalState; const SpriteIndex: UInt8; dim:integer; color: TPlayer = TPlayer.none);
//for hero flags
procedure RenderOverlayIcon(AState: TLocalState; dim:integer; h: integer);
procedure RenderF(AState: TLocalState; const SpriteIndex: UInt8; flags:UInt8);
procedure RenderO(AState: TLocalState; const SpriteIndex: UInt8; X,Y: Integer; color: TPlayer = TPlayer.none);
property FrameCount: Integer read GetFrameCount write SetFrameCount;
property Width: UInt32 read FWidth;
property Height: UInt32 read FHeight;
function GetSpriteHeight(const SpriteIndex: UInt8): UInt32;
property ResourceID: AnsiString read FResourceID write FResourceID;
procedure FixLoadMode(var AMode: TGraphicsLoadMode);
procedure UpdateLoadFlag(AMode: TGraphicsLoadMode);
procedure UnBindTextures;
property Loaded: TGraphicsLoadFlag read FLoaded write FLoaded;
procedure SetPalette(constref APalette: TRGBAPalette);
end;
{ TAnimationMap }
TAnimationMap = class (specialize TObjectMap<string,TAnimation>)
public
constructor Create;
end;
{ TAnimationLoadTask }
TAnimationLoadTask = class
private
FMode: TGraphicsLoadMode;
FTarget: TAnimation;
public
constructor Create(ATarget: TAnimation; AMode: TGraphicsLoadMode);
property Target: TAnimation read FTarget;
property Mode: TGraphicsLoadMode read FMode;
end;
{ TAnimationLoadQueue }
TAnimationLoadQueue = class(specialize TFPGObjectList<TAnimationLoadTask>)
public
constructor Create;
end;
{ TAnimationLoader }
TAnimationLoader = class abstract(TFSConsumer)
strict private
FCurrentPath: AnsiString;
FMode: TGraphicsLoadMode;
FCurrent: TAnimation;
strict protected
FFrameCount: Integer;
FTextureIDs: array of GLuint;
function DoTryLoad: Boolean; virtual; abstract;
function ConvertCurrentPath(): AnsiString;
procedure GenerateTextureIds();
public
function TryLoad: Boolean;
property Mode: TGraphicsLoadMode read FMode write FMode;
property Current: TAnimation read FCurrent write FCurrent;
property CurrentPath: AnsiString read FCurrentPath write FCurrentPath;
end;
{ TDefFormatLoader }
TDefFormatLoader = class (TAnimationLoader, IResource)
strict private
const
INITIAL_BUFFER_SIZE = 32768;
var
FBuffer: packed array of byte; //indexed bitmap
FDefBuffer: packed array of byte;
palette: TRGBAPalette;
procedure IncreaseBuffer(ANewSize: SizeInt);
procedure IncreaseDefBuffer(ANewSize: SizeInt);
procedure LoadSprite(AStream: TStream; const SpriteIndex: UInt8; ATextureID: GLuint; offset: Uint32);
strict protected
function DoTryLoad: Boolean; override;
public
procedure LoadFromStream(AFileName: AnsiString; AStream: TStream); //IResource
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
{ TAnimationSequence }
TAnimationSequence = class(TCollectionItem)
private
FFrames: TStrings;
FGroup: Integer;
procedure SetGroup(AValue: Integer);
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
published
property Group: Integer read FGroup write SetGroup default 0;
property Frames: TStrings read FFrames;
end;
TAnimationSequences = specialize TGArrayCollection<TAnimationSequence>;
{ TAnimationFrame }
TAnimationFrame = class(TCollectionItem)
private
FFile: AnsiString;
FFrame: Integer;
FGroup: Integer;
public
constructor Create(ACollection: TCollection); override;
destructor Destroy; override;
published
property Group: Integer read FGroup write FGroup default 0;
property Frame: Integer read FFrame write FFrame default 0;
property &File: AnsiString read FFile write FFile;
end;
TAnimationOverrides = specialize TGArrayCollection<TAnimationFrame>;
{ TJsonAnimationHeader }
TJsonAnimationHeader = class(TPersistent)
private
FBasepath: AnsiString;
FImages: TAnimationOverrides;
FSequences: TAnimationSequences;
procedure SetBasepath(AValue: AnsiString);
public
constructor Create;
destructor Destroy; override;
procedure Clear;
function FindSequence(AGroup: Integer):TAnimationSequence;
published
property Basepath: AnsiString read FBasepath write SetBasepath;
property Sequences: TAnimationSequences read FSequences;
property Images: TAnimationOverrides read FImages;
end;
{ TJsonFormatLoader }
TJsonFormatLoader = class(TAnimationLoader)
strict private
FHeader:TJsonAnimationHeader;
procedure LoadFrame(idx: Integer; path: AnsiString);
function LoadSequence(): Boolean;
procedure UpdateAnimationSize(ASpriteWidth, ASpriteHeight: integer);
//if json header is not present try to load single frame from raster image
function TryLoadSingle: Boolean;
strict protected
function DoTryLoad: Boolean; override;
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
end;
{ TGraphicsStorage }
TGraphicsStorage = class (TFSConsumer)
strict private
FDefLoader: TDefFormatLoader;
FJsonLoader: TJsonFormatLoader;
FHeroFlagAnimations: array[TPlayerColor] of TAnimation;
FData: TAnimationMap;
FLoadMode: TGraphicsLoadMode;
FQueue: TAnimationLoadQueue;
FBlocked: Boolean;
procedure SetBlocked(AValue: Boolean);
procedure DoLoadGraphics(const AResourceName:string; ADef: TAnimation; ALoadMode: TGraphicsLoadMode);
procedure SetLoadMode(AValue: TGraphicsLoadMode);
procedure AddTask(ATarget: TAnimation; AMode: TGraphicsLoadMode);
procedure DoTask(ATask: TAnimationLoadTask; AProgress: IProgressCallback);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure PreLoad;
function HasTasks(): Boolean;
property Blocked: Boolean read FBlocked write SetBlocked;
property LoadMode: TGraphicsLoadMode read FLoadMode write SetLoadMode;
//load all expect first
procedure Load(AAnimation: TAnimation);
procedure LoadQueued(AState: TLocalState; AProgress: IProgressCallback);
function GetGraphics(const AResourceName:string): TAnimation;
function GetHeroFlagAnimation(APlayer: TPlayer): TAnimation;
end;
{ TGraphicsManager }
TGraphicsManager = class (TFSConsumer)
strict private
FBlocked: Boolean;
FAnimations: TGraphicsStorage;
FIcons: TGraphicsStorage;
procedure SetBlocked(AValue: Boolean);
public
constructor Create(AOwner: TComponent); override;
destructor Destroy; override;
procedure PreLoad;
//if true blocks all actual assets operations
property Blocked: Boolean read FBlocked write SetBlocked;
function HasTasks(): Boolean;
property Icons: TGraphicsStorage read FIcons;
property Animations: TGraphicsStorage read FAnimations;
end;
{ TGraphicsConsumer }
TGraphicsConsumer = class abstract (TFSConsumer)
private
FGraphicsManager: TGraphicsManager;
procedure SetGraphicsManager(AValue: TGraphicsManager);
public
constructor Create(AOwner: TComponent); override;
property GraphicsManager:TGraphicsManager read FGraphicsManager write SetGraphicsManager;
end;
implementation
type
TH3Palette = packed array [0..255] of TH3DefColor;
TH3DefHeader = packed record
typ: UInt32;
width: UInt32;
height: UInt32;
blockCount: UInt32;
palette: TH3Palette;
end;
TH3DefEntryBlockHeader = packed record
unknown1: UInt32;
totalInBlock: UInt32;
unknown2: UInt32;
unknown3: UInt32;
// char *names[length];
// uint32_t offsets[length];
//folowed by sprites
end;
TH3SpriteHeader = packed record
prSize: Int32;
defType2: Int32;
FullWidth: Int32;
FullHeight: Int32;
SpriteWidth: Int32;
SpriteHeight: Int32;
LeftMargin: Int32;
TopMargin: Int32;
end;
{$IF FALSE}
const
H3_SPECIAL_COLORS: array[0..7] of TH3DefColor = (
{0} (r: 0; g: 255; b:255),
{1} (r: 255; g: 150; b:255),
{2} (r: 255; g: 100; b:255),
{3} (r: 255; g: 50; b:255),
{4} (r: 255; g: 0; b:255),
{5} (r: 255; g: 255; b:0),
{6} (r: 180; g: 0; b:255),
{7} (r: 0; g: 255; b:0));
{$ENDIF}
const
STANDARD_COLORS: array[0..7] of TRBGAColor = (
(r: 0; g: 0; b:0; a: 0), //Transparency
(r: 0; g: 0; b:0; a: 73),//Shadow border (75% transparent)
(r: 0; g: 0; b:0; a: 93), //shadow
(r: 0; g: 0; b:0; a: 112), //shadow
(r: 0; g: 0; b:0; a: 131), //shadow 50%
(r: 255; g: 255; b:0; a: 0), //player color | Selection highlight (creatures)
(r: 0; g: 0; b:0; a: 128), //Selection + shadow body
(r: 0; g: 0; b:0; a: 64)); // Selection + shadow border
PLAYER_COLOR_INDEX = 5;
type
TSpecialColorUsage = set of byte;
{$IF FALSE}
TDefType = (
Spell = $40,
Sprite = $41,
Creature = $42,
MapObject = $43,
Hero = $44,
Terrain = $45,
Cursor = $46,
Gui = $47,
SpriteFrame = $48,
CombatHero = $49
);
{$ENDIF}
TSpecialColorOptions = array[0..9] of TSpecialColorUsage;
const
SpecialColorOptions : TSpecialColorOptions =
(
[0],
[0,1,2,3,4,5,6,7],
[0,1,4,5,6,7],
[0,1,4],
[0,1,4],
[0,1,2,3,4],
[0],
[0,1,4],
[0,1,2,3,4,5,6,7],
[0,1,4]
);
function CompareDefs(const d1,d2: TAnimation): integer;
begin
Result := PtrInt(d1) - PtrInt(d2);
end;
{ TAnimationLoadTask }
constructor TAnimationLoadTask.Create(ATarget: TAnimation; AMode: TGraphicsLoadMode);
begin
FTarget := ATarget;
FMode:=AMode;
end;
{ TGraphicsStorage }
procedure TGraphicsStorage.SetBlocked(AValue: Boolean);
begin
FBlocked:=AValue;
if FBlocked then
begin
FQueue.Clear;
end;
end;
procedure TGraphicsStorage.DoLoadGraphics(const AResourceName: string; ADef: TAnimation; ALoadMode: TGraphicsLoadMode);
var
found: Boolean;
begin
if Blocked or (ADef.Loaded = TGraphicsLoadFlag.Complete) then
begin
exit;
end;
ADef.FixLoadMode(ALoadMode);
FDefLoader.Current := ADef;
FDefLoader.Mode := ALoadMode;
FDefLoader.CurrentPath := AResourceName;
found := FDefLoader.TryLoad();
FJsonLoader.Current := ADef;
FJsonLoader.Mode:=ALoadMode;
FJsonLoader.CurrentPath:=AResourceName;
if FJsonLoader.TryLoad() then
begin
found := true;
end;
if not found then
begin
DebugLn('Animation not found: '+AResourceName);
ResourceLoader.LoadResource(FDefLoader, TResourceType.Animation, 'SPRITES/DEFAULT');
end;
ADef.UpdateLoadFlag(ALoadMode);
end;
procedure TGraphicsStorage.SetLoadMode(AValue: TGraphicsLoadMode);
begin
FLoadMode:=AValue;
end;
procedure TGraphicsStorage.AddTask(ATarget: TAnimation; AMode: TGraphicsLoadMode);
begin
if ATarget.Loaded <> TGraphicsLoadFlag.Complete then
begin
FQueue.Add(TAnimationLoadTask.Create(ATarget, AMode));
end;
end;
procedure TGraphicsStorage.DoTask(ATask: TAnimationLoadTask; AProgress: IProgressCallback);
var
ALoadMode: TGraphicsLoadMode;
ATarget: TAnimation;
found: Boolean;
begin
ALoadMode := ATask.Mode;
ATarget := ATask.Target;
ATarget.FixLoadMode(ALoadMode);
FDefLoader.Current := ATarget;
FDefLoader.Mode := ALoadMode;
FDefLoader.CurrentPath := ATarget.ResourceID;
found := FDefLoader.TryLoad();
FJsonLoader.Current := ATarget;
FJsonLoader.Mode:=ALoadMode;
FJsonLoader.CurrentPath:=ATarget.ResourceID;
if FJsonLoader.TryLoad() then
begin
found := true;
end;
if not found then
begin
AProgress.AddError('Animation not found: '+ATarget.ResourceID);
DebugLn('Animation not found: '+ATarget.ResourceID);
ResourceLoader.LoadResource(FDefLoader, TResourceType.Animation, 'SPRITES/DEFAULT');
end;
ATarget.UpdateLoadFlag(ALoadMode);
AProgress.Advance(1);
end;
function TGraphicsStorage.GetGraphics(const AResourceName: string): TAnimation;
var
res_index: Integer;
begin
res_index := FData.IndexOf(AResourceName);
if res_index >= 0 then
begin
Result := FData.Data[res_index];
end
else begin
Result := TAnimation.Create;
FData.Add(AResourceName,Result);
Result.ResourceID := AResourceName;
AddTask(Result, LoadMode);
end;
end;
function TGraphicsStorage.GetHeroFlagAnimation(APlayer: TPlayer): TAnimation;
begin
Result := FHeroFlagAnimations[APlayer];
end;
constructor TGraphicsStorage.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FDefLoader := TDefFormatLoader.Create(Self);
FJsonLoader := TJsonFormatLoader.Create(Self);
FData := TAnimationMap.Create;
FQueue := TAnimationLoadQueue.Create;
end;
destructor TGraphicsStorage.Destroy;
begin
FQueue.Free;
FData.Free;
inherited Destroy;
end;
procedure TGraphicsStorage.PreLoad;
const
FMT = 'AF0%dE';
var
i: TPlayer;
begin
for i in TPlayerColor do
begin
FHeroFlagAnimations[i] := GetGraphics(Format(FMT,[Integer(i)]));
Load(FHeroFlagAnimations[i]);
end;
end;
function TGraphicsStorage.HasTasks(): Boolean;
begin
Result := FQueue.Count <> 0;
end;
procedure TGraphicsStorage.Load(AAnimation: TAnimation);
begin
if AAnimation.Loaded <> TGraphicsLoadFlag.Complete then
AddTask(AAnimation, TGraphicsLoadMode.LoadRest);
end;
procedure TGraphicsStorage.LoadQueued(AState: TLocalState; AProgress: IProgressCallback);
var
ATask: TAnimationLoadTask;
begin
if Blocked then
begin
FQueue.Clear;
exit;
end;
if not AState.StartFrame then
begin
exit;
end;
AProgress.SetMax(FQueue.Count);
for ATask in FQueue do
DoTask(ATask, AProgress);
FQueue.Clear;
end;
{ TAnimationLoadQueue }
constructor TAnimationLoadQueue.Create;
begin
inherited Create(True);
end;
{ TIconList }
constructor TIconList.Create;
begin
FImages := TImages.Create(true);
end;
destructor TIconList.Destroy;
begin
FImages.Free;
inherited Destroy;
end;
{ TAnimationFrame }
constructor TAnimationFrame.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
end;
destructor TAnimationFrame.Destroy;
begin
inherited Destroy;
end;
{ TAnimationSequence }
procedure TAnimationSequence.SetGroup(AValue: Integer);
begin
FGroup:=AValue;
end;
constructor TAnimationSequence.Create(ACollection: TCollection);
begin
inherited Create(ACollection);
FFrames := TStringList.Create;
end;
destructor TAnimationSequence.Destroy;
begin
FFrames.Free;
inherited Destroy;
end;
{ TJsonAnimationHeader }
procedure TJsonAnimationHeader.SetBasepath(AValue: AnsiString);
begin
FBasepath:=AValue;
end;
constructor TJsonAnimationHeader.Create;
begin
FSequences := TAnimationSequences.Create;
FImages := TAnimationOverrides.Create;
end;
destructor TJsonAnimationHeader.Destroy;
begin
FImages.Free;
FSequences.Free;
inherited Destroy;
end;
procedure TJsonAnimationHeader.Clear;
begin
FBasepath:='';
FSequences.Clear;
FImages.Clear;
end;
function TJsonAnimationHeader.FindSequence(AGroup: Integer): TAnimationSequence;
var
i: Integer;
begin
Result := nil;
for i := 0 to Sequences.Count - 1 do
begin
if Sequences.Items[i].Group = AGroup then
begin
Result := Sequences.Items[i];
Exit;
end;
end;
end;
{ TAnimationLoader }
function TAnimationLoader.ConvertCurrentPath: AnsiString;
begin
Result := 'SPRITES/'+ CurrentPath;
end;
procedure TAnimationLoader.GenerateTextureIds;
begin
SetLength(FTextureIDs, FFrameCount);
case Mode of
TGraphicsLoadMode.LoadFisrt:
begin
glGenTextures(1, @FTextureIDs[0]);
end;
TGraphicsLoadMode.LoadRest:
begin
if FFrameCount > 1 then
begin
glGenTextures(FFrameCount-1, @FTextureIDs[1]);
end;
end;
TGraphicsLoadMode.LoadComplete:
begin
glGenTextures(FFrameCount, @FTextureIDs[0]);
end;
end;;
end;
function TAnimationLoader.TryLoad: Boolean;
begin
Assert(Assigned(Current),'TDefFormatLoader.TryLoad: nil Current');
Result := DoTryLoad();
end;
{ TJsonFormatLoader }
procedure TJsonFormatLoader.LoadFrame(idx: Integer; path: AnsiString);
var
PEntry: PGLSprite;
AImage:TIntfImageResource;
begin
PEntry:=Current.entries.Mutable[idx];
AImage := TIntfImageResource.Create(path);
try
AImage.Load(ResourceLoader);
PEntry^.Height:=AImage.Data.Height;
PEntry^.LeftMargin:=0;
PEntry^.SpriteHeight:=AImage.Data.Height;
PEntry^.SpriteWidth:=AImage.Data.Width;
PEntry^.TopMargin:=0;
PEntry^.Width:=AImage.Data.Width;
PEntry^.PaletteID:=0;
PEntry^.TextureID:=FTextureIDs[idx];
UpdateAnimationSize(PEntry^.SpriteWidth, PEntry^.SpriteHeight);
BindUncompressedRGBA(PEntry^.TextureID, PEntry^.SpriteWidth, PEntry^.SpriteHeight, AImage.Data.PixelData^);
finally
AImage.Free;
end;
end;
function TJsonFormatLoader.LoadSequence: Boolean;
var
sequence: TAnimationSequence;
framePath, realPath: String;
idx: Integer;
begin
sequence := FHeader.FindSequence(0);
Result := Assigned(sequence);
if Result then
begin
Current.UnBindTextures;
idx := 0;
FFrameCount := sequence.Frames.Count;
Current.FrameCount := FFrameCount;
GenerateTextureIds();
for framePath in sequence.Frames do
begin
realPath := 'SPRITES/'+UTF8Trim(FHeader.Basepath)+UTF8Trim(framePath);
Result := ResourceLoader.ExistsResource(TResourceType.Image, realPath);
if not Result then
begin
DebugLn('Frame not found: '+realPath);
Exit;
end;
LoadFrame(idx, realPath);
inc(idx);
end;
end;
end;
procedure TJsonFormatLoader.UpdateAnimationSize(ASpriteWidth, ASpriteHeight: integer);
begin
//TODO: align to tile size (with margins)
Current.FWidth := ASpriteWidth;
Current.FHeight := ASpriteHeight;
end;
function TJsonFormatLoader.TryLoadSingle: Boolean;
var
path: AnsiString;
begin
path := ConvertCurrentPath();
Result := false;
if ResourceLoader.ExistsResource(TResourceType.Image, path) then
begin
Current.UnBindTextures;
FFrameCount := 1;
Current.FrameCount := FFrameCount;
GenerateTextureIds();
LoadFrame(0, path);
Result := true;
end;
end;
constructor TJsonFormatLoader.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
FHeader := TJsonAnimationHeader.Create;
end;
destructor TJsonFormatLoader.Destroy;
begin
FHeader.Free;
inherited;
end;
function TJsonFormatLoader.DoTryLoad: Boolean;
var
FHeaderJson: TJsonResource;
begin
FHeader.Clear;
Result := false;
FHeaderJson := TJsonResource.Create(ConvertCurrentPath());
try
Result := FHeaderJson.TryLoad(ResourceLoader);
if Result then
begin
FHeaderJson.DestreamTo(FHeader);
//TODO: patch single frames
Result := LoadSequence();
end
else
Result := TryLoadSingle();
finally
FHeaderJson.Free;
end;
end;
{ TDefFormatLoader }
constructor TDefFormatLoader.Create(AOwner: TComponent);
begin
inherited Create(AOwner);
SetLength(FBuffer, INITIAL_BUFFER_SIZE);
end;
destructor TDefFormatLoader.Destroy;
begin
inherited;
end;
function TDefFormatLoader.DoTryLoad: Boolean;
begin
Result := ResourceLoader.TryLoadResource(Self, TResourceType.Animation, ConvertCurrentPath());
end;
procedure TDefFormatLoader.IncreaseBuffer(ANewSize: SizeInt);
begin
if ANewSize > Length(FBuffer) then
begin
ANewSize := (ANewSize div INITIAL_BUFFER_SIZE + 1)*INITIAL_BUFFER_SIZE;
SetLength(FBuffer, ANewSize);
end;
end;
procedure TDefFormatLoader.IncreaseDefBuffer(ANewSize: SizeInt);
begin
if ANewSize > Length(FDefBuffer) then
begin
ANewSize := (ANewSize div INITIAL_BUFFER_SIZE + 1)*INITIAL_BUFFER_SIZE;
SetLength(FDefBuffer, ANewSize);
end;
end;
procedure TDefFormatLoader.LoadSprite(AStream: TStream; const SpriteIndex: UInt8;
ATextureID: GLuint; offset: Uint32);
var
ftcp: Int32;
procedure Skip(Count: Int32); inline;
begin
ftcp +=Count;
end;
procedure SkipIfPositive(Count: Int32); inline;
begin
if Count > 0 then
Skip(Count);
end;
var
h: TH3SpriteHeader;
RightMargin, BottomMargin: Int32;
BaseOffset: Int32;
BaseOffsetor: Int32;
PEntry: PGLSprite;
procedure ReadType0;
var
row: Int32;
begin
for row:=0 to h.SpriteHeight - 1 do
begin
SkipIfPositive(h.LeftMargin);
AStream.Read(FBuffer[ftcp+row * h.SpriteWidth],h.SpriteWidth);
SkipIfPositive(RightMargin);
end;
end;
procedure ReadType1;
var
row: Int32;
TotalRowLength : Int32;
SegmentLength: Int32;
SegmentType: UInt8;
begin
for row := 0 to h.SpriteHeight - 1 do
begin
AStream.Seek(BaseOffsetor + SizeOf(UInt32)*row, soFromBeginning);
AStream.Read(BaseOffset,SizeOf(BaseOffset));
AStream.Seek(BaseOffsetor + BaseOffset, soFromBeginning);
SkipIfPositive(h.LeftMargin);
TotalRowLength := 0;
repeat
SegmentType := AStream.ReadByte;
SegmentLength := AStream.ReadByte + 1;
if SegmentType = $FF then
begin
AStream.Read(FBuffer[ftcp],SegmentLength);
BaseOffset += SegmentLength;
end
else begin
FillChar(FBuffer[ftcp],SegmentLength,SegmentType);
end;
Skip(SegmentLength);
TotalRowLength += SegmentLength;
until TotalRowLength>=(h.SpriteWidth);
SkipIfPositive(RightMargin);
end;
end;
procedure ReadType2();
var
row: Integer;
TotalRowLength: Integer;
SegmentType, code, value: Byte;
begin
BaseOffset := BaseOffsetor + AStream.ReadWord();
AStream.Seek(BaseOffset,soBeginning);
for row := 0 to h.SpriteHeight - 1 do
begin