DEADSOFTWARE

render: draw flag anims using TAnimState
[d2df-sdl.git] / src / game / opengl / r_map.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 r_map;
18 interface
20 uses g_panel, MAPDEF; // TPanel, TDFColor
22 procedure r_Map_Initialize;
23 procedure r_Map_Finalize;
25 procedure r_Map_Load;
26 procedure r_Map_Free;
28 procedure r_Map_LoadTextures;
29 procedure r_Map_FreeTextures;
31 procedure r_Map_Update;
33 procedure r_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor); // unaccelerated
34 procedure r_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
35 procedure r_Map_DrawPanelShadowVolumes (lightX: Integer; lightY: Integer; radius: Integer);
36 procedure r_Map_DrawFlags;
38 procedure r_Panel_Draw (constref p: TPanel; hasAmbient: Boolean; constref ambColor: TDFColor);
39 procedure r_Panel_DrawShadowVolume (constref p: TPanel; lightX, lightY: Integer; radius: Integer);
41 implementation
43 uses
44 {$INCLUDE ../nogl/noGLuses.inc}
45 SysUtils, Classes, Math, e_log, wadreader, CONFIG, utils, g_language,
46 r_graphics, r_animations, r_textures, g_textures,
47 g_base, g_basic, g_game, g_options,
48 g_map
49 ;
51 var
52 RenTextures: array of record
53 ID: DWORD;
54 Width, Height: WORD;
55 Anim: Boolean;
56 end;
57 FlagFrames: array [FLAG_RED..FLAG_BLUE] of DWORD;
58 FlagAnim: TAnimState;
60 procedure r_Map_Initialize;
61 begin
62 FlagAnim := TAnimState.Create(True, 8, 5);
63 end;
65 procedure r_Map_Finalize;
66 begin
67 FlagAnim.Invalidate;
68 end;
70 procedure r_Map_Load;
71 begin
72 g_Frames_CreateWAD(@FlagFrames[FLAG_RED], 'FRAMES_FLAG_RED', GameWAD + ':TEXTURES\FLAGRED', 64, 64, 5, False);
73 g_Frames_CreateWAD(@FlagFrames[FLAG_BLUE], 'FRAMES_FLAG_BLUE', GameWAD + ':TEXTURES\FLAGBLUE', 64, 64, 5, False);
74 end;
76 procedure r_Map_Free;
77 begin
78 g_Frames_DeleteByName('FRAMES_FLAG_RED');
79 g_Frames_DeleteByName('FRAMES_FLAG_BLUE');
80 end;
82 procedure r_Map_LoadTextures;
83 const
84 log = True;
85 var
86 i, n: Integer;
87 WadName, ResName: String;
88 WAD, WADZ: TWADFile;
89 ResData, ReszData: Pointer;
90 ResLen, ReszLen: Integer;
91 cfg: TConfig;
92 TextureResource: String;
93 Width, Height: Integer;
94 FramesCount: Integer;
95 BackAnim: Boolean;
96 begin
97 if Textures <> nil then
98 begin
99 n := Length(Textures);
100 SetLength(RenTextures, n);
101 for i := 0 to n - 1 do
102 begin
103 // e_LogWritefln('r_Map_LoadTextures: -> [%s] :: [%s]', [Textures[i].FullName, Textures[i].TextureName]);
104 RenTextures[i].ID := LongWord(TEXTURE_NONE);
105 RenTextures[i].Width := 0;
106 RenTextures[i].Height := 0;
107 RenTextures[i].Anim := False;
108 case Textures[i].TextureName of
109 TEXTURE_NAME_WATER: RenTextures[i].ID := LongWord(TEXTURE_SPECIAL_WATER);
110 TEXTURE_NAME_ACID1: RenTextures[i].ID := LongWord(TEXTURE_SPECIAL_ACID1);
111 TEXTURE_NAME_ACID2: RenTextures[i].ID := LongWord(TEXTURE_SPECIAL_ACID2);
112 else
113 WadName := g_ExtractWadName(Textures[i].FullName);
114 ResName := g_ExtractFilePathName(Textures[i].FullName);
115 WAD := TWADFile.Create();
116 if WAD.ReadFile(WadName) then
117 begin
118 if WAD.GetResource(ResName, ResData, ResLen, log) then
119 begin
120 if IsWadData(ResData, ResLen) then
121 begin
122 WADz := TWADFile.Create();
123 if WADz.ReadMemory(ResData, ResLen) then
124 begin
125 if WADz.GetResource('TEXT/ANIM', ReszData, ReszLen) then
126 begin
127 cfg := TConfig.CreateMem(ReszData, ReszLen);
128 FreeMem(ReszData);
129 if cfg <> nil then
130 begin
131 TextureResource := cfg.ReadStr('', 'resource', '');
132 Width := cfg.ReadInt('', 'framewidth', 0);
133 Height := cfg.ReadInt('', 'frameheight', 0);
134 FramesCount := cfg.ReadInt('', 'framecount', 0);
135 // Speed := cfg.ReadInt('', 'waitcount', 0);
136 BackAnim := cfg.ReadBool('', 'backanimation', False);
137 RenTextures[i].Width := Width;
138 RenTextures[i].Height := Height;
139 if TextureResource <> '' then
140 begin
141 if WADz.GetResource('TEXTURES/' + TextureResource, ReszData, ReszLen) then
142 begin
143 if g_Frames_CreateMemory(@RenTextures[i].ID, '', ReszData, ReszLen, Width, Height, FramesCount, BackAnim) then
144 RenTextures[i].Anim := True
145 else
146 e_LogWritefln('r_Map_LoadTextures: failed to create frames object (%s)', [Textures[i].FullName]);
147 FreeMem(ReszData)
148 end
149 else
150 e_LogWritefln('r_Map_LoadTextures: failed to open animation resources (%s)', [Textures[i].FullName])
151 end
152 else
153 e_LogWritefln('r_Map_LoadTextures: failed to animation has no texture resource string (%s)', [Textures[i].FullName]);
154 cfg.Free
155 end
156 else
157 e_LogWritefln('r_Map_LoadTextures: failed to parse animation description (%s)', [Textures[i].FullName])
158 end
159 else
160 e_LogWritefln('r_Map_LoadTextures: failed to open animation description (%s)', [Textures[i].FullName])
161 end
162 else
163 e_LogWritefln('r_Map_LoadTextures: failed to open animation (%s)', [Textures[i].FullName]);
164 WADz.Free
165 end
166 else
167 begin
168 if e_CreateTextureMem(ResData, ResLen, RenTextures[i].ID) then
169 e_GetTextureSize(RenTextures[i].ID, @RenTextures[i].Width, @RenTextures[i].Height)
170 else
171 e_LogWritefln('r_Map_LoadTextures: failed to create texture (%s)', [Textures[i].FullName])
172 end;
173 FreeMem(ResData);
174 end
175 else
176 e_LogWritefln('r_Map_LoadTextures: failed to open (%s)', [Textures[i].FullName])
177 end
178 else
179 e_LogWritefln('r_Map_LoadTextures: failed to open %s', [WadName]);
180 WAD.Free;
181 end
182 end
183 end
184 end;
186 procedure r_Map_FreeTextures;
187 begin
188 // TODO
189 end;
191 procedure dplClear ();
192 begin
193 if (gDrawPanelList = nil) then gDrawPanelList := TBinHeapPanelDraw.Create() else gDrawPanelList.clear();
194 end;
196 // old algo
197 procedure r_Map_DrawPanels (PanelType: Word; hasAmbient: Boolean; constref ambColor: TDFColor);
199 procedure DrawPanels (constref panels: TPanelArray; drawDoors: Boolean=False);
200 var
201 idx: Integer;
202 begin
203 if (panels <> nil) then
204 begin
205 // alas, no visible set
206 for idx := 0 to High(panels) do
207 begin
208 if not (drawDoors xor panels[idx].Door) then
209 r_Panel_Draw(panels[idx], hasAmbient, ambColor);
210 end;
211 end;
212 end;
214 begin
215 case PanelType of
216 PANEL_WALL: DrawPanels(gWalls);
217 PANEL_CLOSEDOOR: DrawPanels(gWalls, True);
218 PANEL_BACK: DrawPanels(gRenderBackgrounds);
219 PANEL_FORE: DrawPanels(gRenderForegrounds);
220 PANEL_WATER: DrawPanels(gWater);
221 PANEL_ACID1: DrawPanels(gAcid1);
222 PANEL_ACID2: DrawPanels(gAcid2);
223 PANEL_STEP: DrawPanels(gSteps);
224 end;
225 end;
227 // new algo
228 procedure r_Map_CollectDrawPanels (x0, y0, wdt, hgt: Integer);
229 var
230 mwit: PPanel;
231 it: TPanelGrid.Iter;
232 begin
233 dplClear();
234 it := mapGrid.forEachInAABB(x0, y0, wdt, hgt, GridDrawableMask);
235 for mwit in it do if (((mwit^.tag and GridTagDoor) <> 0) = mwit^.Door) then gDrawPanelList.insert(mwit^);
236 it.release();
237 // list will be rendered in `g_game.DrawPlayer()`
238 end;
240 procedure r_Map_DrawPanelShadowVolumes (lightX: Integer; lightY: Integer; radius: Integer);
241 var
242 mwit: PPanel;
243 it: TPanelGrid.Iter;
244 begin
245 it := mapGrid.forEachInAABB(lightX-radius, lightY-radius, radius*2, radius*2, (GridTagWall or GridTagDoor));
246 for mwit in it do r_Panel_DrawShadowVolume(mwit^, lightX, lightY, radius);
247 it.release();
248 end;
250 procedure r_Map_DrawFlags;
251 var i, dx, tx, ty: Integer; Mirror: TMirrorType; f: PFlag;
252 begin
253 if gGameSettings.GameMode = GM_CTF then
254 begin
255 for i := FLAG_RED to FLAG_BLUE do
256 begin
257 f := @gFlags[i];
258 if not (f.State in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
259 begin
260 f.Obj.lerp(gLerpFactor, tx, ty);
261 if f.Direction = TDirection.D_LEFT then
262 Mirror := TMirrorType.Horizontal
263 else
264 Mirror := TMirrorType.None;
265 dx := IfThen(f.Direction = TDirection.D_LEFT, -1, +1);
266 r_AnimState_Draw(FlagFrames[i], FlagAnim, tx + dx, ty + 1, 0, Mirror, False);
267 if g_debug_Frames then
268 e_DrawQuad(tx + f.Obj.Rect.X, ty + f.Obj.Rect.Y, tx + f.Obj.Rect.X + f.Obj.Rect.Width - 1, ty + f.Obj.Rect.Y + f.Obj.Rect.Height - 1, 0, 255, 0)
269 end
270 end
271 end
272 end;
274 procedure Panel_Lerp (p: TPanel; t: Single; out tX, tY, tW, tH: Integer);
275 begin
276 if p.movingActive then
277 begin
278 tX := nlerp(p.OldX, p.X, t);
279 tY := nlerp(p.OldY, p.Y, t);
280 tW := nlerp(p.OldWidth, p.Width, t);
281 tH := nlerp(p.OldHeight, p.Height, t);
282 end
283 else
284 begin
285 tX := p.X;
286 tY := p.Y;
287 tW := p.Width;
288 tH := p.Height;
289 end;
290 end;
292 procedure r_Panel_Draw (constref p: TPanel; hasAmbient: Boolean; constref ambColor: TDFColor);
293 var tx, ty, tw, th, xx, yy: Integer; NoTextureID, TextureID, FramesID: DWORD; NW, NH: Word; Texture: Cardinal; IsAnim: Boolean; w, h: Integer;
294 begin
295 if {p.Enabled and} (p.FCurTexture >= 0) and (p.Width > 0) and (p.Height > 0) and (p.Alpha < 255) {and g_Collide(X, Y, Width, Height, sX, sY, sWidth, sHeight)} then
296 begin
297 Panel_Lerp(p, gLerpFactor, tx, ty, tw, th);
298 Texture := p.TextureIDs[p.FCurTexture].Texture;
299 IsAnim := RenTextures[Texture].Anim;
300 if IsAnim then
301 begin
302 if p.TextureIDs[p.FCurTexture].AnTex <> nil then
303 begin
304 FramesID := RenTextures[Texture].ID;
305 w := RenTextures[Texture].Width;
306 h := RenTextures[Texture].Height;
307 for xx := 0 to tw div w - 1 do
308 for yy := 0 to th div h - 1 do
309 r_AnimationState_Draw(FramesID, p.TextureIDs[p.FCurTexture].AnTex, tx + xx * w, ty + yy * h, p.Alpha, TMirrorType.None, p.Blending);
310 end
311 end
312 else
313 begin
314 TextureID := RenTextures[Texture].ID;
315 w := RenTextures[Texture].Width;
316 h := RenTextures[Texture].Height;
317 case TextureID of
318 LongWord(TEXTURE_SPECIAL_WATER): e_DrawFillQuad(tx, ty, tx + tw - 1, ty + th - 1, 0, 0, 255, 0, TBlending.Filter);
319 LongWord(TEXTURE_SPECIAL_ACID1): e_DrawFillQuad(tx, ty, tx + tw - 1, ty + th - 1, 0, 230, 0, 0, TBlending.Filter);
320 LongWord(TEXTURE_SPECIAL_ACID2): e_DrawFillQuad(tx, ty, tx + tw - 1, ty + th - 1, 230, 0, 0, 0, TBlending.Filter);
321 LongWord(TEXTURE_NONE):
322 if g_Texture_Get('NOTEXTURE', NoTextureID) then
323 begin
324 e_GetTextureSize(NoTextureID, @NW, @NH);
325 e_DrawFill(NoTextureID, tx, ty, tw div NW, th div NH, 0, False, False);
326 end
327 else
328 begin
329 xx := tx + (tw div 2);
330 yy := ty + (th div 2);
331 e_DrawFillQuad(tx, ty, xx, yy, 255, 0, 255, 0);
332 e_DrawFillQuad(xx, ty, tx + tw - 1, yy, 255, 255, 0, 0);
333 e_DrawFillQuad(tx, yy, xx, ty + th - 1, 255, 255, 0, 0);
334 e_DrawFillQuad(xx, yy, tx + tw - 1, ty + th - 1, 255, 0, 255, 0);
335 end;
336 else
337 if not p.movingActive then
338 e_DrawFill(TextureID, tx, ty, tw div w, th div h, p.Alpha, True, p.Blending, hasAmbient)
339 else
340 e_DrawFillX(TextureID, tx, ty, tw, th, p.Alpha, True, p.Blending, g_dbg_scale, hasAmbient);
341 if hasAmbient then
342 e_AmbientQuad(tx, ty, tw, th, ambColor.r, ambColor.g, ambColor.b, ambColor.a);
343 end
344 end
345 end
346 end;
348 procedure r_Panel_DrawShadowVolume (constref p: TPanel; lightX, lightY: Integer; radius: Integer);
349 var tx, ty, tw, th: Integer; Texture: Cardinal;
351 procedure extrude (x: Integer; y: Integer);
352 begin
353 glVertex2i(x + (x - lightX) * 500, y + (y - lightY) * 500);
354 //e_WriteLog(Format(' : (%d,%d)', [x + (x - lightX) * 300, y + (y - lightY) * 300]), MSG_WARNING);
355 end;
357 procedure drawLine (x0: Integer; y0: Integer; x1: Integer; y1: Integer);
358 begin
359 // does this side facing the light?
360 if ((x1 - x0) * (lightY - y0) - (lightX - x0) * (y1 - y0) >= 0) then exit;
361 //e_WriteLog(Format('lightpan: (%d,%d)-(%d,%d)', [x0, y0, x1, y1]), MSG_WARNING);
362 // this edge is facing the light, extrude and draw it
363 glVertex2i(x0, y0);
364 glVertex2i(x1, y1);
365 extrude(x1, y1);
366 extrude(x0, y0);
367 end;
369 begin
370 if radius < 4 then exit;
371 if p.Enabled and (p.FCurTexture >= 0) and (p.Width > 0) and (p.Height > 0) and (p.Alpha < 255) {and g_Collide(X, Y, tw, th, sX, sY, sWidth, sHeight)} then
372 begin
373 Panel_Lerp(p, gLerpFactor, tx, ty, tw, th);
374 Texture := p.TextureIDs[p.FCurTexture].Texture;
375 if not RenTextures[Texture].Anim then
376 begin
377 case RenTextures[Texture].ID of
378 LongWord(TEXTURE_SPECIAL_WATER): exit;
379 LongWord(TEXTURE_SPECIAL_ACID1): exit;
380 LongWord(TEXTURE_SPECIAL_ACID2): exit;
381 LongWord(TEXTURE_NONE): exit;
382 end;
383 end;
384 if (tx + tw < lightX - radius) then exit;
385 if (ty + th < lightY - radius) then exit;
386 if (tx > lightX + radius) then exit;
387 if (ty > lightY + radius) then exit;
388 //e_DrawFill(TextureIDs[FCurTexture].Tex, X, Y, tw div TextureWidth, th div TextureHeight, Alpha, True, Blending);
389 glBegin(GL_QUADS);
390 drawLine(tx, ty, tx + tw, ty); // top
391 drawLine(tx + tw, ty, tx + tw, ty + th); // right
392 drawLine(tx + tw, ty + th, tx, ty + th); // bottom
393 drawLine(tx, ty + th, tx, ty); // left
394 glEnd;
395 end
396 end;
398 procedure r_Map_Update;
399 begin
400 FlagAnim.Update
401 end;
403 end.