DEADSOFTWARE

render: remove TAnimation
[d2df-sdl.git] / src / game / g_textures.pas
1 (* Copyright (C) Doom 2D: Forever Developers
2 *
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.
6 *
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.
11 *
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/>.
14 *)
15 {$INCLUDE ../shared/a_modes.inc}
16 unit g_textures;
18 interface
20 uses
21 SysUtils, Classes,
22 {$IFDEF USE_MEMPOOL}mempool,{$ENDIF}
23 g_base, r_graphics, MAPDEF, ImagingTypes, Imaging, ImagingUtility;
25 type
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;
30 end;
32 TLevelTextureArray = array of TLevelTexture;
34 TAnimationState = class{$IFDEF USE_MEMPOOL}(TPoolObject){$ENDIF}
35 private
36 mAlpha: Byte;
37 mBlending: Boolean;
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; // Ñìåíà êàäðîâ îáðàòíàÿ?
47 mLength: Integer;
49 public
50 constructor Create (aloop: Boolean; aspeed: Byte; len: Integer);
51 destructor Destroy (); override;
53 procedure reset ();
54 procedure update ();
55 procedure enable ();
56 procedure disable ();
57 procedure revert (r: Boolean);
59 procedure saveState (st: TStream);
60 procedure loadState (st: TStream);
62 function totalFrames (): Integer; inline;
64 public
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;
77 end;
79 implementation
81 uses
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);
86 begin
87 assert(len >= 0);
88 mLength := len;
90 mMinLength := 0;
91 mLoop := aloop;
92 mSpeed := aspeed;
93 mEnabled := true;
94 mCurrentFrame := 0;
95 mAlpha := 0;
96 mPlayed := false;
97 end;
99 destructor TAnimationState.Destroy;
100 begin
101 inherited;
102 end;
104 procedure TAnimationState.update;
105 begin
106 if (not mEnabled) then exit;
108 mCounter += 1;
110 if (mCounter >= mSpeed) then
111 begin
112 // Îæèäàíèå ìåæäó êàäðàìè çàêîí÷èëîñü
113 // Îáðàòíûé ïîðÿäîê êàäðîâ?
114 if mRevert then
115 begin
116 // Äîøëè äî êîíöà àíèìàöèè. Âîçìîæíî, æäåì åùå
117 if (mCurrentFrame = 0) then
118 begin
119 if (mLength * mSpeed + mCounter < mMinLength) then exit;
120 end;
122 mCurrentFrame -= 1;
123 mPlayed := (mCurrentFrame < 0);
125 // Ïîâòîðÿòü ëè àíèìàöèþ ïî êðóãó?
126 if mPlayed then
127 begin
128 if mLoop then
129 mCurrentFrame := mLength - 1
130 else
131 mCurrentFrame += 1
132 end;
134 mCounter := 0;
135 end
136 else
137 begin
138 // Ïðÿìîé ïîðÿäîê êàäðîâ
139 // Äîøëè äî êîíöà àíèìàöèè. Âîçìîæíî, æäåì åùå
140 if (mCurrentFrame = mLength - 1) then
141 begin
142 if (mLength * mSpeed + mCounter < mMinLength) then exit;
143 end;
145 mCurrentFrame += 1;
146 mPlayed := (mCurrentFrame > mLength - 1);
148 // Ïîâòîðÿòü ëè àíèìàöèþ ïî êðóãó?
149 if mPlayed then
150 begin
151 if mLoop then mCurrentFrame := 0 else mCurrentFrame -= 1;
152 end;
154 mCounter := 0;
155 end;
156 end;
157 end;
159 procedure TAnimationState.reset;
160 begin
161 if mRevert then
162 mCurrentFrame := mLength - 1
163 else
164 mCurrentFrame := 0;
165 mCounter := 0;
166 mPlayed := false
167 end;
169 procedure TAnimationState.disable;
170 begin
171 mEnabled := false
172 end;
174 procedure TAnimationState.enable;
175 begin
176 mEnabled := true
177 end;
179 procedure TAnimationState.revert (r: Boolean);
180 begin
181 mRevert := r;
182 reset
183 end;
185 function TAnimationState.totalFrames (): Integer; inline;
186 begin
187 result := mLength
188 end;
190 procedure TAnimationState.saveState (st: TStream);
191 begin
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));
198 // Òåêóùèé êàäð
199 utils.writeInt(st, LongInt(mCurrentFrame));
200 // Ïðîèãðàíà ëè àíèìàöèÿ öåëèêîì
201 utils.writeBool(st, mPlayed);
202 // Alpha-êàíàë âñåé òåêñòóðû
203 utils.writeInt(st, Byte(mAlpha));
204 // Ðàçìûòèå òåêñòóðû
205 utils.writeInt(st, Byte(mBlending));
206 // Âðåìÿ îæèäàíèÿ ìåæäó êàäðàìè
207 utils.writeInt(st, Byte(mSpeed));
208 // Çàöèêëåíà ëè àíèìàöèÿ
209 utils.writeBool(st, mLoop);
210 // Âêëþ÷åíà ëè
211 utils.writeBool(st, mEnabled);
212 // Îæèäàíèå ïîñëå ïðîèãðûâàíèÿ
213 utils.writeInt(st, Byte(mMinLength));
214 // Îáðàòíûé ëè ïîðÿäîê êàäðîâ
215 utils.writeBool(st, mRevert);
216 end;
219 procedure TAnimationState.loadState (st: TStream);
220 begin
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);
227 // Òåêóùèé êàäð
228 mCurrentFrame := utils.readLongInt(st);
229 // Ïðîèãðàíà ëè àíèìàöèÿ öåëèêîì
230 mPlayed := utils.readBool(st);
231 // Alpha-êàíàë âñåé òåêñòóðû
232 mAlpha := utils.readByte(st);
233 // Ðàçìûòèå òåêñòóðû
234 mBlending := utils.readBool(st);
235 // Âðåìÿ îæèäàíèÿ ìåæäó êàäðàìè
236 mSpeed := utils.readByte(st);
237 // Çàöèêëåíà ëè àíèìàöèÿ
238 mLoop := utils.readBool(st);
239 // Âêëþ÷åíà ëè
240 mEnabled := utils.readBool(st);
241 // Îæèäàíèå ïîñëå ïðîèãðûâàíèÿ
242 mMinLength := utils.readByte(st);
243 // Îáðàòíûé ëè ïîðÿäîê êàäðîâ
244 mRevert := utils.readBool(st);
245 end;
247 end.