10 TLevelTexture
= record
15 False: (TextureID
: DWORD
;);
16 True: (FramesID
: DWORD
;
21 TLevelTextureArray
= Array of TLevelTexture
;
23 TAnimation
= class(TObject
)
28 FCounter
: Byte; // Ñ÷åò÷èê îæèäàíèÿ ìåæäó êàäðàìè
29 FSpeed
: Byte; // Âðåìÿ îæèäàíèÿ ìåæäó êàäðàìè
30 FCurrentFrame
: Integer; // Òåêóùèé êàäð (íà÷èíàÿ ñ 0)
31 FLoop
: Boolean; // Ïåðåõîäèòü íà ïåðâûé êàäð ïîñëå ïîñëåäíåãî?
32 FEnabled
: Boolean; // Ðàáîòà ðàçðåøåíà?
33 FPlayed
: Boolean; // Ïðîèãðàíà âñÿ õîòÿ áû ðàç?
36 FMinLength
: Byte; // Îæèäàíèå ïîñëå ïðîèãðûâàíèÿ
37 FRevert
: Boolean; // Ñìåíà êàäðîâ îáðàòíàÿ?
40 constructor Create(FramesID
: DWORD
; Loop
: Boolean; Speed
: Byte);
41 destructor Destroy(); override;
42 procedure Draw(X
, Y
: Integer; Mirror
: TMirrorType
);
43 procedure DrawEx(X
, Y
: Integer; Mirror
: TMirrorType
; RPoint
: TPoint
;
49 procedure Revert(r
: Boolean);
50 procedure SaveState(Var Mem
: TBinMemoryWriter
);
51 procedure LoadState(Var Mem
: TBinMemoryReader
);
52 function TotalFrames(): Integer;
54 property Played
: Boolean read FPlayed
;
55 property Enabled
: Boolean read FEnabled
;
56 property IsReverse
: Boolean read FRevert
;
57 property Loop
: Boolean read FLoop write FLoop
;
58 property Speed
: Byte read FSpeed write FSpeed
;
59 property MinLength
: Byte read FMinLength write FMinLength
;
60 property CurrentFrame
: Integer read FCurrentFrame write FCurrentFrame
;
61 property CurrentCounter
: Byte read FCounter write FCounter
;
62 property Counter
: Byte read FCounter
;
63 property Blending
: Boolean read FBlending write FBlending
;
64 property Alpha
: Byte read FAlpha write FAlpha
;
65 property FramesID
: DWORD read ID
;
66 property Width
: Word read FWidth
;
67 property Height
: Word read FHeight
;
70 function g_Texture_CreateWAD(var ID
: DWORD
; Resource
: String): Boolean;
71 function g_Texture_CreateFile(var ID
: DWORD
; FileName
: String): Boolean;
72 function g_Texture_CreateWADEx(TextureName
: ShortString; Resource
: String): Boolean;
73 function g_Texture_CreateFileEx(TextureName
: ShortString; FileName
: String): Boolean;
74 function g_Texture_Get(TextureName
: ShortString; var ID
: DWORD
): Boolean;
75 procedure g_Texture_Delete(TextureName
: ShortString);
76 procedure g_Texture_DeleteAll();
78 function g_Frames_CreateWAD(ID
: PDWORD
; Name
: ShortString; Resource
: String;
79 FWidth
, FHeight
, FCount
: Word; BackAnimation
: Boolean = False): Boolean;
80 function g_Frames_CreateFile(ID
: PDWORD
; Name
: ShortString; FileName
: String;
81 FWidth
, FHeight
, FCount
: Word; BackAnimation
: Boolean = False): Boolean;
82 function g_Frames_CreateMemory(ID
: PDWORD
; Name
: ShortString; pData
: Pointer; dataSize
: LongInt;
83 FWidth
, FHeight
, FCount
: Word; BackAnimation
: Boolean = False): Boolean;
84 //function g_Frames_CreateRevert(ID: PDWORD; Name: ShortString; Frames: string): Boolean;
85 function g_Frames_Get(var ID
: DWORD
; FramesName
: ShortString): Boolean;
86 function g_Frames_GetTexture(var ID
: DWORD
; FramesName
: ShortString; Frame
: Word): Boolean;
87 function g_Frames_Exists(FramesName
: String): Boolean;
88 procedure g_Frames_DeleteByName(FramesName
: ShortString);
89 procedure g_Frames_DeleteByID(ID
: DWORD
);
90 procedure g_Frames_DeleteAll();
92 procedure DumpTextureNames();
97 g_game
, e_log
, g_basic
, SysUtils
, g_console
, wadreader
,
108 TexturesID
: Array of DWORD
;
115 TexturesArray
: Array of _TTexture
= nil;
116 FramesArray
: Array of TFrames
= nil;
119 ANIM_SIGNATURE
= $4D494E41; // 'ANIM'
121 function FindTexture(): DWORD
;
125 if TexturesArray
<> nil then
126 for i
:= 0 to High(TexturesArray
) do
127 if TexturesArray
[i
].Name
= '' then
133 if TexturesArray
= nil then
135 SetLength(TexturesArray
, 8);
140 Result
:= High(TexturesArray
) + 1;
141 SetLength(TexturesArray
, Length(TexturesArray
) + 8);
145 function g_Texture_CreateWAD(var ID
: DWORD
; Resource
: String): Boolean;
149 TextureData
: Pointer;
150 ResourceLength
: Integer;
153 FileName
:= g_ExtractWadName(Resource
);
155 WAD
:= TWADFile
.Create
;
156 WAD
.ReadFile(FileName
);
158 if WAD
.GetResource(g_ExtractFilePathName(Resource
), TextureData
, ResourceLength
) then
160 if e_CreateTextureMem(TextureData
, ResourceLength
, ID
) then
163 FreeMem(TextureData
);
167 e_WriteLog(Format('Error loading texture %s', [Resource
]), MSG_WARNING
);
168 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
173 function g_Texture_CreateFile(var ID
: DWORD
; FileName
: String): Boolean;
176 if not e_CreateTexture(FileName
, ID
) then
178 e_WriteLog(Format('Error loading texture %s', [FileName
]), MSG_WARNING
);
183 function g_Texture_CreateWADEx(TextureName
: ShortString; Resource
: String): Boolean;
187 TextureData
: Pointer;
189 ResourceLength
: Integer;
191 FileName
:= g_ExtractWadName(Resource
);
193 find_id
:= FindTexture();
195 WAD
:= TWADFile
.Create
;
196 WAD
.ReadFile(FileName
);
198 if WAD
.GetResource(g_ExtractFilePathName(Resource
), TextureData
, ResourceLength
) then
200 Result
:= e_CreateTextureMem(TextureData
, ResourceLength
, TexturesArray
[find_id
].ID
);
203 e_GetTextureSize(TexturesArray
[find_id
].ID
, @TexturesArray
[find_id
].Width
,
204 @TexturesArray
[find_id
].Height
);
205 TexturesArray
[find_id
].Name
:= LowerCase(TextureName
);
208 FreeMem(TextureData
);
212 e_WriteLog(Format('Error loading texture %s', [Resource
]), MSG_WARNING
);
213 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
219 function g_Texture_CreateFileEx(TextureName
: ShortString; FileName
: String): Boolean;
223 find_id
:= FindTexture
;
225 Result
:= e_CreateTexture(FileName
, TexturesArray
[find_id
].ID
);
228 TexturesArray
[find_id
].Name
:= LowerCase(TextureName
);
229 e_GetTextureSize(TexturesArray
[find_id
].ID
, @TexturesArray
[find_id
].Width
,
230 @TexturesArray
[find_id
].Height
);
232 else e_WriteLog(Format('Error loading texture %s', [FileName
]), MSG_WARNING
);
235 function g_Texture_Get(TextureName
: ShortString; var ID
: DWORD
): Boolean;
241 if TexturesArray
= nil then Exit
;
243 if TextureName
= '' then Exit
;
245 TextureName
:= LowerCase(TextureName
);
247 for a
:= 0 to High(TexturesArray
) do
248 if TexturesArray
[a
].Name
= TextureName
then
250 ID
:= TexturesArray
[a
].ID
;
255 //if not Result then g_ConsoleAdd('Texture '+TextureName+' not found');
258 procedure g_Texture_Delete(TextureName
: ShortString);
262 if TexturesArray
= nil then Exit
;
264 TextureName
:= LowerCase(TextureName
);
266 for a
:= 0 to High(TexturesArray
) do
267 if TexturesArray
[a
].Name
= TextureName
then
269 e_DeleteTexture(TexturesArray
[a
].ID
);
270 TexturesArray
[a
].Name
:= '';
271 TexturesArray
[a
].ID
:= 0;
272 TexturesArray
[a
].Width
:= 0;
273 TexturesArray
[a
].Height
:= 0;
277 procedure g_Texture_DeleteAll();
281 if TexturesArray
= nil then Exit
;
283 for a
:= 0 to High(TexturesArray
) do
284 if TexturesArray
[a
].Name
<> '' then
285 e_DeleteTexture(TexturesArray
[a
].ID
);
287 TexturesArray
:= nil;
290 function FindFrame(): DWORD
;
294 if FramesArray
<> nil then
295 for i
:= 0 to High(FramesArray
) do
296 if FramesArray
[i
].TexturesID
= nil then
302 if FramesArray
= nil then
304 SetLength(FramesArray
, 64);
309 Result
:= High(FramesArray
) + 1;
310 SetLength(FramesArray
, Length(FramesArray
) + 64);
314 function g_Frames_CreateFile(ID
: PDWORD
; Name
: ShortString; FileName
: String;
315 FWidth
, FHeight
, FCount
: Word; BackAnimation
: Boolean = False): Boolean;
322 find_id
:= FindFrame
;
324 if FCount
<= 2 then BackAnimation
:= False;
326 if BackAnimation
then SetLength(FramesArray
[find_id
].TexturesID
, FCount
+FCount
-2)
327 else SetLength(FramesArray
[find_id
].TexturesID
, FCount
);
329 for a
:= 0 to FCount
-1 do
330 if not e_CreateTextureEx(FileName
, FramesArray
[find_id
].TexturesID
[a
],
331 a
*FWidth
, 0, FWidth
, FHeight
) then Exit
;
333 if BackAnimation
then
334 for a
:= 1 to FCount
-2 do
335 FramesArray
[find_id
].TexturesID
[FCount
+FCount
-2-a
] := FramesArray
[find_id
].TexturesID
[a
];
337 FramesArray
[find_id
].FrameWidth
:= FWidth
;
338 FramesArray
[find_id
].FrameHeight
:= FHeight
;
340 FramesArray
[find_id
].Name
:= LowerCase(Name
)
342 FramesArray
[find_id
].Name
:= '<noname>';
344 if ID
<> nil then ID
^ := find_id
;
349 function CreateFramesMem(pData
: Pointer; dataSize
: LongInt; ID
: PDWORD
; Name
: ShortString;
350 FWidth
, FHeight
, FCount
: Word; BackAnimation
: Boolean = False): Boolean;
357 find_id
:= FindFrame();
359 if FCount
<= 2 then BackAnimation
:= False;
361 if BackAnimation
then SetLength(FramesArray
[find_id
].TexturesID
, FCount
+FCount
-2)
362 else SetLength(FramesArray
[find_id
].TexturesID
, FCount
);
364 for a
:= 0 to FCount
-1 do
365 if not e_CreateTextureMemEx(pData
, dataSize
, FramesArray
[find_id
].TexturesID
[a
],
366 a
*FWidth
, 0, FWidth
, FHeight
) then
372 if BackAnimation
then
373 for a
:= 1 to FCount
-2 do
374 FramesArray
[find_id
].TexturesID
[FCount
+FCount
-2-a
] := FramesArray
[find_id
].TexturesID
[a
];
376 FramesArray
[find_id
].FrameWidth
:= FWidth
;
377 FramesArray
[find_id
].FrameHeight
:= FHeight
;
379 FramesArray
[find_id
].Name
:= LowerCase(Name
)
381 FramesArray
[find_id
].Name
:= '<noname>';
383 if ID
<> nil then ID
^ := find_id
;
388 function g_Frames_CreateWAD(ID
: PDWORD
; Name
: ShortString; Resource
: string;
389 FWidth
, FHeight
, FCount
: Word; BackAnimation
: Boolean = False): Boolean;
393 TextureData
: Pointer;
394 ResourceLength
: Integer;
398 FileName
:= g_ExtractWadName(Resource
);
400 WAD
:= TWADFile
.Create();
401 WAD
.ReadFile(FileName
);
403 if not WAD
.GetResource(g_ExtractFilePathName(Resource
), TextureData
, ResourceLength
) then
406 e_WriteLog(Format('Error loading texture %s', [Resource
]), MSG_WARNING
);
407 //e_WriteLog(Format('WAD Reader error: %s', [WAD.GetLastErrorStr]), MSG_WARNING);
411 if not CreateFramesMem(TextureData
, ResourceLength
, ID
, Name
, FWidth
, FHeight
, FCount
, BackAnimation
) then
422 function g_Frames_CreateMemory(ID
: PDWORD
; Name
: ShortString; pData
: Pointer; dataSize
: LongInt;
423 FWidth
, FHeight
, FCount
: Word; BackAnimation
: Boolean = False): Boolean;
425 Result
:= CreateFramesMem(pData
, dataSize
, ID
, Name
, FWidth
, FHeight
, FCount
, BackAnimation
);
428 {function g_Frames_CreateRevert(ID: PDWORD; Name: ShortString; Frames: string): Boolean;
435 if not g_Frames_Get(b, Frames) then Exit;
437 find_id := FindFrame();
439 FramesArray[find_id].Name := Name;
440 FramesArray[find_id].FrameWidth := FramesArray[b].FrameWidth;
441 FramesArray[find_id].FrameHeight := FramesArray[b].FrameHeight;
443 c := High(FramesArray[find_id].TexturesID);
446 FramesArray[find_id].TexturesID[a] := FramesArray[b].TexturesID[c-a];
451 procedure g_Frames_DeleteByName(FramesName
: ShortString);
456 if FramesArray
= nil then Exit
;
458 FramesName
:= LowerCase(FramesName
);
460 for a
:= 0 to High(FramesArray
) do
461 if FramesArray
[a
].Name
= FramesName
then
463 if FramesArray
[a
].TexturesID
<> nil then
464 for b
:= 0 to High(FramesArray
[a
].TexturesID
) do
465 e_DeleteTexture(FramesArray
[a
].TexturesID
[b
]);
466 FramesArray
[a
].TexturesID
:= nil;
467 FramesArray
[a
].Name
:= '';
468 FramesArray
[a
].FrameWidth
:= 0;
469 FramesArray
[a
].FrameHeight
:= 0;
473 procedure g_Frames_DeleteByID(ID
: DWORD
);
477 if FramesArray
= nil then Exit
;
479 if FramesArray
[ID
].TexturesID
<> nil then
480 for b
:= 0 to High(FramesArray
[ID
].TexturesID
) do
481 e_DeleteTexture(FramesArray
[ID
].TexturesID
[b
]);
482 FramesArray
[ID
].TexturesID
:= nil;
483 FramesArray
[ID
].Name
:= '';
484 FramesArray
[ID
].FrameWidth
:= 0;
485 FramesArray
[ID
].FrameHeight
:= 0;
488 procedure g_Frames_DeleteAll
;
493 if FramesArray
= nil then Exit
;
495 for a
:= 0 to High(FramesArray
) do
496 if FramesArray
[a
].TexturesID
<> nil then
498 for b
:= 0 to High(FramesArray
[a
].TexturesID
) do
499 e_DeleteTexture(FramesArray
[a
].TexturesID
[b
]);
500 FramesArray
[a
].TexturesID
:= nil;
501 FramesArray
[a
].Name
:= '';
502 FramesArray
[a
].FrameWidth
:= 0;
503 FramesArray
[a
].FrameHeight
:= 0;
509 function g_Frames_Get(var ID
: DWORD
; FramesName
: ShortString): Boolean;
515 if FramesArray
= nil then
518 FramesName
:= LowerCase(FramesName
);
520 for a
:= 0 to High(FramesArray
) do
521 if FramesArray
[a
].Name
= FramesName
then
529 g_FatalError(Format(_lc
[I_GAME_ERROR_FRAMES
], [FramesName
]));
532 function g_Frames_GetTexture(var ID
: DWORD
; FramesName
: ShortString; Frame
: Word): Boolean;
538 if FramesArray
= nil then
541 FramesName
:= LowerCase(FramesName
);
543 for a
:= 0 to High(FramesArray
) do
544 if FramesArray
[a
].Name
= FramesName
then
545 if Frame
<= High(FramesArray
[a
].TexturesID
) then
547 ID
:= FramesArray
[a
].TexturesID
[Frame
];
553 g_FatalError(Format(_lc
[I_GAME_ERROR_FRAMES
], [FramesName
]));
556 function g_Frames_Exists(FramesName
: string): Boolean;
562 if FramesArray
= nil then Exit
;
564 FramesName
:= LowerCase(FramesName
);
566 for a
:= 0 to High(FramesArray
) do
567 if FramesArray
[a
].Name
= FramesName
then
574 procedure DumpTextureNames();
578 e_WriteLog('BEGIN Textures:', MSG_NOTIFY
);
579 for i
:= 0 to High(TexturesArray
) do
580 e_WriteLog(' '+IntToStr(i
)+'. '+TexturesArray
[i
].Name
, MSG_NOTIFY
);
581 e_WriteLog('END Textures.', MSG_NOTIFY
);
583 e_WriteLog('BEGIN Frames:', MSG_NOTIFY
);
584 for i
:= 0 to High(FramesArray
) do
585 e_WriteLog(' '+IntToStr(i
)+'. '+FramesArray
[i
].Name
, MSG_NOTIFY
);
586 e_WriteLog('END Frames.', MSG_NOTIFY
);
591 constructor TAnimation
.Create(FramesID
: DWORD
; Loop
: Boolean; Speed
: Byte);
602 FWidth
:= FramesArray
[ID
].FrameWidth
;
603 FHeight
:= FramesArray
[ID
].FrameHeight
;
606 destructor TAnimation
.Destroy
;
611 procedure TAnimation
.Draw(X
, Y
: Integer; Mirror
: TMirrorType
);
616 e_DrawAdv(FramesArray
[ID
].TexturesID
[FCurrentFrame
], X
, Y
, FAlpha
,
617 True, FBlending
, 0, nil, Mirror
);
618 //e_DrawQuad(X, Y, X+FramesArray[ID].FrameWidth-1, Y+FramesArray[ID].FrameHeight-1, 0, 255, 0);
621 procedure TAnimation
.Update();
626 FCounter
:= FCounter
+ 1;
628 if FCounter
>= FSpeed
then
629 begin // Îæèäàíèå ìåæäó êàäðàìè çàêîí÷èëîñü
631 begin // Îáðàòíûé ïîðÿäîê êàäðîâ
632 // Äîøëè äî êîíöà àíèìàöèè. Âîçìîæíî, æäåì åùå:
633 if FCurrentFrame
= 0 then
634 if Length(FramesArray
[ID
].TexturesID
) * FSpeed
+
635 FCounter
< FMinLength
then
638 FCurrentFrame
:= FCurrentFrame
- 1;
639 FPlayed
:= FCurrentFrame
< 0;
641 // Ïîâòîðÿòü ëè àíèìàöèþ ïî êðóãó:
644 FCurrentFrame
:= High(FramesArray
[ID
].TexturesID
)
646 FCurrentFrame
:= FCurrentFrame
+ 1;
651 begin // Ïðÿìîé ïîðÿäîê êàäðîâ
652 // Äîøëè äî êîíöà àíèìàöèè. Âîçìîæíî, æäåì åùå:
653 if FCurrentFrame
= High(FramesArray
[ID
].TexturesID
) then
654 if Length(FramesArray
[ID
].TexturesID
) * FSpeed
+
655 FCounter
< FMinLength
then
658 FCurrentFrame
:= FCurrentFrame
+ 1;
659 FPlayed
:= (FCurrentFrame
> High(FramesArray
[ID
].TexturesID
));
661 // Ïîâòîðÿòü ëè àíèìàöèþ ïî êðóãó:
666 FCurrentFrame
:= FCurrentFrame
- 1;
673 procedure TAnimation
.Reset();
676 FCurrentFrame
:= High(FramesArray
[ID
].TexturesID
)
684 procedure TAnimation
.Disable
;
689 procedure TAnimation
.Enable
;
694 procedure TAnimation
.DrawEx(X
, Y
: Integer; Mirror
: TMirrorType
; RPoint
: TPoint
;
700 e_DrawAdv(FramesArray
[ID
].TexturesID
[FCurrentFrame
], X
, Y
, FAlpha
,
701 True, FBlending
, Angle
, @RPoint
, Mirror
);
704 function TAnimation
.TotalFrames(): Integer;
706 Result
:= Length(FramesArray
[ID
].TexturesID
);
709 procedure TAnimation
.Revert(r
: Boolean);
715 procedure TAnimation
.SaveState(Var Mem
: TBinMemoryWriter
);
722 // Ñèãíàòóðà àíèìàöèè:
723 sig
:= ANIM_SIGNATURE
; // 'ANIM'
725 // Ñ÷åò÷èê îæèäàíèÿ ìåæäó êàäðàìè:
726 Mem
.WriteByte(FCounter
);
728 Mem
.WriteInt(FCurrentFrame
);
729 // Ïðîèãðàíà ëè àíèìàöèÿ öåëèêîì:
730 Mem
.WriteBoolean(FPlayed
);
731 // Alpha-êàíàë âñåé òåêñòóðû:
732 Mem
.WriteByte(FAlpha
);
733 // Ðàçìûòèå òåêñòóðû:
734 Mem
.WriteBoolean(FBlending
);
735 // Âðåìÿ îæèäàíèÿ ìåæäó êàäðàìè:
736 Mem
.WriteByte(FSpeed
);
737 // Çàöèêëåíà ëè àíèìàöèÿ:
738 Mem
.WriteBoolean(FLoop
);
740 Mem
.WriteBoolean(FEnabled
);
741 // Îæèäàíèå ïîñëå ïðîèãðûâàíèÿ:
742 Mem
.WriteByte(FMinLength
);
743 // Îáðàòíûé ëè ïîðÿäîê êàäðîâ:
744 Mem
.WriteBoolean(FRevert
);
747 procedure TAnimation
.LoadState(Var Mem
: TBinMemoryReader
);
754 // Ñèãíàòóðà àíèìàöèè:
756 if sig
<> ANIM_SIGNATURE
then // 'ANIM'
758 raise EBinSizeError
.Create('TAnimation.LoadState: Wrong Animation Signature');
760 // Ñ÷åò÷èê îæèäàíèÿ ìåæäó êàäðàìè:
761 Mem
.ReadByte(FCounter
);
763 Mem
.ReadInt(FCurrentFrame
);
764 // Ïðîèãðàíà ëè àíèìàöèÿ öåëèêîì:
765 Mem
.ReadBoolean(FPlayed
);
766 // Alpha-êàíàë âñåé òåêñòóðû:
767 Mem
.ReadByte(FAlpha
);
768 // Ðàçìûòèå òåêñòóðû:
769 Mem
.ReadBoolean(FBlending
);
770 // Âðåìÿ îæèäàíèÿ ìåæäó êàäðàìè:
771 Mem
.ReadByte(FSpeed
);
772 // Çàöèêëåíà ëè àíèìàöèÿ:
773 Mem
.ReadBoolean(FLoop
);
775 Mem
.ReadBoolean(FEnabled
);
776 // Îæèäàíèå ïîñëå ïðîèãðûâàíèÿ:
777 Mem
.ReadByte(FMinLength
);
778 // Îáðàòíûé ëè ïîðÿäîê êàäðîâ:
779 Mem
.ReadBoolean(FRevert
);