1 (* Copyright (C) Doom 2D: Forever Developers
3 * This program is free software: you can redistribute it and/or modify
4 * it under the terms of the GNU General Public License as published by
5 * the Free Software Foundation, version 3 of the License ONLY.
7 * This program is distributed in the hope that it will be useful,
8 * but WITHOUT ANY WARRANTY; without even the implied warranty of
9 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
10 * GNU General Public License for more details.
12 * You should have received a copy of the GNU General Public License
13 * along with this program. If not, see <http://www.gnu.org/licenses/>.
15 {$INCLUDE ../shared/a_modes.inc}
22 {$IFDEF USE_MEMPOOL}mempool
,{$ENDIF}
23 g_base
, r_graphics
, MAPDEF
, ImagingTypes
, Imaging
, ImagingUtility
;
26 TLevelTexture
= record
27 TextureName
: AnsiString; // as stored in wad
28 FullName
: AnsiString; // full path to texture // !!! merge it with TextureName
29 framesCount
, speed
: Byte;
32 TLevelTextureArray
= array of TLevelTexture
;
34 TAnimationState
= class{$IFDEF USE_MEMPOOL}(TPoolObject
){$ENDIF}
38 mCounter
: Byte; // Ñ÷åò÷èê îæèäàíèÿ ìåæäó êàäðàìè
39 mSpeed
: Byte; // Âðåìÿ îæèäàíèÿ ìåæäó êàäðàìè
40 mCurrentFrame
: Integer; // Òåêóùèé êàäð (íà÷èíàÿ ñ 0)
41 mLoop
: Boolean; // Ïåðåõîäèòü íà ïåðâûé êàäð ïîñëå ïîñëåäíåãî?
42 mEnabled
: Boolean; // Ðàáîòà ðàçðåøåíà?
43 mPlayed
: Boolean; // Ïðîèãðàíà âñÿ õîòÿ áû ðàç?
44 mMinLength
: Byte; // Îæèäàíèå ïîñëå ïðîèãðûâàíèÿ
45 mRevert
: Boolean; // Ñìåíà êàäðîâ îáðàòíàÿ?
50 constructor Create (aloop
: Boolean; aspeed
: Byte; len
: Integer);
51 destructor Destroy (); override;
57 procedure revert (r
: Boolean);
59 procedure saveState (st
: TStream
);
60 procedure loadState (st
: TStream
);
62 function totalFrames (): Integer; inline;
65 property played
: Boolean read mPlayed
;
66 property enabled
: Boolean read mEnabled
;
67 property isReverse
: Boolean read mRevert
;
68 property loop
: Boolean read mLoop write mLoop
;
69 property speed
: Byte read mSpeed write mSpeed
;
70 property minLength
: Byte read mMinLength write mMinLength
;
71 property currentFrame
: Integer read mCurrentFrame write mCurrentFrame
;
72 property currentCounter
: Byte read mCounter write mCounter
;
73 property counter
: Byte read mCounter
;
74 property blending
: Boolean read mBlending write mBlending
;
75 property alpha
: Byte read mAlpha write mAlpha
;
76 property length
: Integer read mLength
;
82 g_game
, e_log
, g_basic
, g_console
, wadreader
, r_animations
,
83 g_language
, utils
, xstreams
;
85 constructor TAnimationState
.Create (aloop
: Boolean; aspeed
: Byte; len
: Integer);
99 destructor TAnimationState
.Destroy
;
104 procedure TAnimationState
.update
;
106 if (not mEnabled
) then exit
;
110 if (mCounter
>= mSpeed
) then
112 // Îæèäàíèå ìåæäó êàäðàìè çàêîí÷èëîñü
113 // Îáðàòíûé ïîðÿäîê êàäðîâ?
116 // Äîøëè äî êîíöà àíèìàöèè. Âîçìîæíî, æäåì åùå
117 if (mCurrentFrame
= 0) then
119 if (mLength
* mSpeed
+ mCounter
< mMinLength
) then exit
;
123 mPlayed
:= (mCurrentFrame
< 0);
125 // Ïîâòîðÿòü ëè àíèìàöèþ ïî êðóãó?
129 mCurrentFrame
:= mLength
- 1
138 // Ïðÿìîé ïîðÿäîê êàäðîâ
139 // Äîøëè äî êîíöà àíèìàöèè. Âîçìîæíî, æäåì åùå
140 if (mCurrentFrame
= mLength
- 1) then
142 if (mLength
* mSpeed
+ mCounter
< mMinLength
) then exit
;
146 mPlayed
:= (mCurrentFrame
> mLength
- 1);
148 // Ïîâòîðÿòü ëè àíèìàöèþ ïî êðóãó?
151 if mLoop
then mCurrentFrame
:= 0 else mCurrentFrame
-= 1;
159 procedure TAnimationState
.reset
;
162 mCurrentFrame
:= mLength
- 1
169 procedure TAnimationState
.disable
;
174 procedure TAnimationState
.enable
;
179 procedure TAnimationState
.revert (r
: Boolean);
185 function TAnimationState
.totalFrames (): Integer; inline;
190 procedure TAnimationState
.saveState (st
: TStream
);
192 if (st
= nil) then exit
;
194 utils
.writeSign(st
, 'ANIM');
195 utils
.writeInt(st
, Byte(0)); // version
196 // Ñ÷åò÷èê îæèäàíèÿ ìåæäó êàäðàìè
197 utils
.writeInt(st
, Byte(mCounter
));
199 utils
.writeInt(st
, LongInt(mCurrentFrame
));
200 // Ïðîèãðàíà ëè àíèìàöèÿ öåëèêîì
201 utils
.writeBool(st
, mPlayed
);
202 // Alpha-êàíàë âñåé òåêñòóðû
203 utils
.writeInt(st
, Byte(mAlpha
));
205 utils
.writeInt(st
, Byte(mBlending
));
206 // Âðåìÿ îæèäàíèÿ ìåæäó êàäðàìè
207 utils
.writeInt(st
, Byte(mSpeed
));
208 // Çàöèêëåíà ëè àíèìàöèÿ
209 utils
.writeBool(st
, mLoop
);
211 utils
.writeBool(st
, mEnabled
);
212 // Îæèäàíèå ïîñëå ïðîèãðûâàíèÿ
213 utils
.writeInt(st
, Byte(mMinLength
));
214 // Îáðàòíûé ëè ïîðÿäîê êàäðîâ
215 utils
.writeBool(st
, mRevert
);
219 procedure TAnimationState
.loadState (st
: TStream
);
221 if (st
= nil) then exit
;
223 if not utils
.checkSign(st
, 'ANIM') then raise XStreamError
.Create('animation chunk expected');
224 if (utils
.readByte(st
) <> 0) then raise XStreamError
.Create('invalid animation chunk version');
225 // Ñ÷åò÷èê îæèäàíèÿ ìåæäó êàäðàìè
226 mCounter
:= utils
.readByte(st
);
228 mCurrentFrame
:= utils
.readLongInt(st
);
229 // Ïðîèãðàíà ëè àíèìàöèÿ öåëèêîì
230 mPlayed
:= utils
.readBool(st
);
231 // Alpha-êàíàë âñåé òåêñòóðû
232 mAlpha
:= utils
.readByte(st
);
234 mBlending
:= utils
.readBool(st
);
235 // Âðåìÿ îæèäàíèÿ ìåæäó êàäðàìè
236 mSpeed
:= utils
.readByte(st
);
237 // Çàöèêëåíà ëè àíèìàöèÿ
238 mLoop
:= utils
.readBool(st
);
240 mEnabled
:= utils
.readBool(st
);
241 // Îæèäàíèå ïîñëå ïðîèãðûâàíèÿ
242 mMinLength
:= utils
.readByte(st
);
243 // Îáðàòíûé ëè ïîðÿäîê êàäðîâ
244 mRevert
:= utils
.readBool(st
);