DEADSOFTWARE

cd146a7b9fbeadd62a28e8058a1dc792beaf20ca
[d2df-sdl.git] / src / game / renders / 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 procedure r_Map_Initialize;
21 procedure r_Map_Finalize;
23 procedure r_Map_Load;
24 procedure r_Map_Free;
26 procedure r_Map_LoadTextures;
27 procedure r_Map_FreeTextures;
29 procedure r_Map_NewGFX (typ, x, y: Integer);
31 procedure r_Map_Update;
33 procedure r_Map_Draw (x, y, w, h, camx, camy: Integer);
35 implementation
37 uses
38 {$IFDEF USE_GLES1}
39 GLES11,
40 {$ELSE}
41 GL, GLEXT,
42 {$ENDIF}
43 e_log,
44 binheap, MAPDEF, utils,
45 g_options, g_textures, g_basic, g_base, g_phys,
46 g_game, g_map, g_panel, g_items, g_monsters, g_playermodel, g_player, g_weapons,
47 {$IFDEF ENABLE_CORPSES}
48 g_corpses,
49 {$ENDIF}
50 {$IFDEF ENABLE_GFX}
51 g_gfx,
52 {$ENDIF}
53 r_textures, r_draw
54 ;
56 const
57 MTABLE: array [0..MONSTER_MAN] of record
58 w, h: Integer;
59 end = (
60 (w: 64; h: 64), // NONE
61 (w: 64; h: 64), // DEMON
62 (w: 64; h: 64), // IMP
63 (w: 64; h: 64), // ZOMBY
64 (w: 64; h: 64), // SERG
65 (w: 128; h: 128), // CYBER
66 (w: 64; h: 64), // CGUN
67 (w: 128; h: 128), // BARON
68 (w: 128; h: 128), // KNIGHT
69 (w: 128; h: 128), // CACO
70 (w: 64; h: 64), // SOUL (DIE is 128x128, see load code)
71 (w: 128; h: 128), // PAIN
72 (w: 256; h: 128), // SPIDER
73 (w: 128; h: 64), // BSP
74 (w: 128; h: 128), // MANCUB
75 (w: 128; h: 128), // SKEL
76 (w: 128; h: 128), // VILE
77 (w: 32; h: 32), // FISH
78 (w: 64; h: 64), // BARREL
79 (w: 128; h: 128), // ROBO
80 (w: 64; h: 64) // MAN
81 );
82 VILEFIRE_DX = 32;
83 VILEFIRE_DY = 128;
85 GFXAnim: array [0..R_GFX_LAST] of record
86 name: AnsiString;
87 w, h: Integer;
88 count: Integer;
89 back: Boolean;
90 speed: Integer;
91 rspeed: Integer;
92 alpha: Integer;
93 end = (
94 (name: ''; w: 0; h: 0; count: 0; back: false; speed: 0; rspeed: 0; alpha: 0),
95 (name: 'TELEPORT'; w: 64; h: 64; count: 10; back: false; speed: 6; rspeed: 0; alpha: 0),
96 (name: 'FLAME'; w: 32; h: 32; count: 11; back: false; speed: 3; rspeed: 0; alpha: 0),
97 (name: 'EROCKET'; w: 128; h: 128; count: 6; back: false; speed: 6; rspeed: 0; alpha: 0),
98 (name: 'EBFG'; w: 128; h: 128; count: 6; back: false; speed: 6; rspeed: 0; alpha: 0),
99 (name: 'BFGHIT'; w: 64; h: 64; count: 4; back: false; speed: 4; rspeed: 0; alpha: 0),
100 (name: 'FIRE'; w: 64; h: 128; count: 8; back: false; speed: 4; rspeed: 2; alpha: 0),
101 (name: 'ITEMRESPAWN'; w: 32; h: 32; count: 5; back: true; speed: 4; rspeed: 0; alpha: 0),
102 (name: 'SMOKE'; w: 32; h: 32; count: 10; back: false; speed: 3; rspeed: 0; alpha: 0),
103 (name: 'ESKELFIRE'; w: 64; h: 64; count: 3; back: false; speed: 8; rspeed: 0; alpha: 0),
104 (name: 'EPLASMA'; w: 32; h: 32; count: 4; back: true; speed: 3; rspeed: 0; alpha: 0),
105 (name: 'EBSPFIRE'; w: 32; h: 32; count: 5; back: false; speed: 3; rspeed: 0; alpha: 0),
106 (name: 'EIMPFIRE'; w: 64; h: 64; count: 3; back: false; speed: 6; rspeed: 0; alpha: 0),
107 (name: 'ECACOFIRE'; w: 64; h: 64; count: 3; back: false; speed: 6; rspeed: 0; alpha: 0),
108 (name: 'EBARONFIRE'; w: 64; h: 64; count: 3; back: false; speed: 6; rspeed: 0; alpha: 0),
109 (name: 'TELEPORT'; w: 64; h: 64; count: 10; back: false; speed: 3; rspeed: 0; alpha: 0), // fast
110 (name: 'SMOKE'; w: 32; h: 32; count: 10; back: false; speed: 3; rspeed: 0; alpha: 150), // transparent
111 (name: 'FLAME'; w: 32; h: 32; count: 11; back: false; speed: 3; rspeed: 2; alpha: 0) // random
112 );
114 type
115 TBinHeapPanelDrawCmp = class
116 public
117 class function less (const a, b: TPanel): Boolean; inline;
118 end;
120 TBinHeapPanelDraw = specialize TBinaryHeapBase<TPanel, TBinHeapPanelDrawCmp>;
122 TMonsterAnims = array [0..ANIM_LAST, TDirection] of TGLMultiTexture;
124 var
125 SkyTexture: TGLTexture;
126 RenTextures: array of record
127 spec: LongInt;
128 tex: TGLMultiTexture;
129 end;
130 Items: array [0..ITEM_MAX] of record
131 tex: TGLMultiTexture;
132 anim: TAnimState;
133 end;
134 MonTextures: array [0..MONSTER_MAN] of TMonsterAnims;
135 WeapTextures: array [0..WP_LAST, 0..W_POS_LAST, 0..W_ACT_LAST] of TGLTexture;
136 VileFire: TGLMultiTexture;
137 Models: array of record
138 anim: array [TDirection, 0..A_LAST] of record
139 base, mask: TGLMultiTexture;
140 end;
141 (*
142 {$IFDEF ENABLE_GIBS}
143 gibs: array of record
144 base, mask: TGLTexture;
145 rect: TRectWH;
146 end;
147 {$ENDIF}
148 *)
149 end;
151 {$IFDEF ENABLE_GFX}
152 GFXTextures: array [0..R_GFX_LAST] of TGLMultiTexture;
153 gfxlist: array of record
154 typ: Byte;
155 alpha: Byte;
156 x, y: Integer;
157 oldX, oldY: Integer;
158 anim: TAnimState;
159 end = nil;
160 {$ENDIF}
162 plist: TBinHeapPanelDraw = nil;
164 class function TBinHeapPanelDrawCmp.less (const a, b: TPanel): Boolean; inline;
165 begin
166 if a.tag < b.tag then begin result := true; exit; end;
167 if a.tag > b.tag then begin result := false; exit; end;
168 result := a.arrIdx < b.arrIdx;
169 end;
171 procedure r_Map_Initialize;
172 begin
173 plist := TBinHeapPanelDraw.Create();
174 end;
176 procedure r_Map_Finalize;
177 begin
178 plist.Free
179 end;
181 procedure r_Map_LoadModel (i: Integer);
182 var prefix: AnsiString; a: Integer; d: TDirection; m: ^TPlayerModelInfo;
183 begin
184 m := @PlayerModelsArray[i];
185 prefix := m.FileName + ':TEXTURES/';
186 for d := TDirection.D_LEFT to TDirection.D_RIGHT do
187 begin
188 for a := A_STAND to A_LAST do
189 begin
190 Models[i].anim[d, a].base := nil;
191 Models[i].anim[d, a].mask := nil;
192 if m.anim[d, a].resource <> '' then
193 Models[i].anim[d, a].base := r_Textures_LoadMultiFromFileAndInfo(prefix + m.anim[d, a].resource, 64, 64, m.anim[d, a].frames, m.anim[d, a].back, true);
194 if m.anim[d, a].mask <> '' then
195 Models[i].anim[d, a].mask := r_Textures_LoadMultiFromFileAndInfo(prefix + m.anim[d, a].mask, 64, 64, m.anim[d, a].frames, m.anim[d, a].back, true);
196 end
197 end;
198 (*
199 {$IFDEF ENABLE_GIBS}
200 Models[i].gibs := nil;
201 if m.GibsCount > 0 then
202 begin
203 SetLength(Models[i].gibs, m.GibsCount);
204 end;
205 {$ENDIF}
206 *)
207 end;
210 procedure r_Map_Load;
211 const
212 WeapName: array [0..WP_LAST] of AnsiString = ('', 'CSAW', 'HGUN', 'SG', 'SSG', 'MGUN', 'RKT', 'PLZ', 'BFG', 'SPL', 'FLM');
213 WeapPos: array [0..W_POS_LAST] of AnsiString = ('', '_UP', '_DN');
214 WeapAct: array [0..W_ACT_LAST] of AnsiString = ('', '_FIRE');
215 var
216 i, j, k: Integer; d: TDirection;
218 procedure LoadItem (i: Integer; const name: AnsiString; w, h, delay, count: Integer; backanim: Boolean);
219 begin
220 ASSERT(i >= 0);
221 ASSERT(i <= ITEM_MAX);
222 Items[i].tex := r_Textures_LoadMultiFromFileAndInfo(GameWAD + ':TEXTURES/' + name, w, h, count, backanim, false);
223 if backanim then count := count * 2 - 2;
224 Items[i].anim := TAnimState.Create(True, delay, count);
225 ASSERT(Items[i].tex <> NIL);
226 end;
228 procedure LoadMonster (m, a: Integer; d: TDirection);
229 const
230 dir: array [TDirection] of AnsiString = ('_L', '');
231 var
232 w, h, count: Integer;
233 begin
234 count := MONSTER_ANIMTABLE[m].AnimCount[a];
235 if count > 0 then
236 begin
237 w := MTABLE[m].w;
238 h := MTABLE[m].h;
239 if (m = MONSTER_SOUL) and (a = ANIM_DIE) then
240 begin
241 // special case
242 w := 128;
243 h := 128;
244 end;
245 MonTextures[m, a, d] := r_Textures_LoadMultiFromFileAndInfo(
246 GameWAD + ':MTEXTURES/' + MONSTERTABLE[m].name + '_' + ANIMTABLE[a].name + dir[d],
247 w,
248 h,
249 count,
250 False,
251 False
253 end
254 else
255 MonTextures[m, a, d] := nil
256 end;
258 begin
259 // --------- items --------- //
260 // i name w h d n backanim
261 LoadItem(ITEM_NONE, 'NOTEXTURE', 16, 16, 0, 1, False);
262 LoadItem(ITEM_MEDKIT_SMALL, 'MED1', 16, 16, 0, 1, False);
263 LoadItem(ITEM_MEDKIT_LARGE, 'MED2', 32, 32, 0, 1, False);
264 LoadItem(ITEM_MEDKIT_BLACK, 'BMED', 32, 32, 0, 1, False);
265 LoadItem(ITEM_ARMOR_GREEN, 'ARMORGREEN', 32, 16, 20, 3, True);
266 LoadItem(ITEM_ARMOR_BLUE, 'ARMORBLUE', 32, 16, 20, 3, True);
267 LoadItem(ITEM_SPHERE_BLUE, 'SBLUE', 32, 32, 15, 4, True);
268 LoadItem(ITEM_SPHERE_WHITE, 'SWHITE', 32, 32, 20, 4, True);
269 LoadItem(ITEM_SUIT, 'SUIT', 32, 64, 0, 1, False);
270 LoadItem(ITEM_OXYGEN, 'OXYGEN', 16, 32, 0, 1, False);
271 LoadItem(ITEM_INVUL, 'INVUL', 32, 32, 20, 4, True);
272 LoadItem(ITEM_WEAPON_SAW, 'SAW', 64, 32, 0, 1, False);
273 LoadItem(ITEM_WEAPON_SHOTGUN1, 'SHOTGUN1', 64, 16, 0, 1, False);
274 LoadItem(ITEM_WEAPON_SHOTGUN2, 'SHOTGUN2', 64, 16, 0, 1, False);
275 LoadItem(ITEM_WEAPON_CHAINGUN, 'MGUN', 64, 16, 0, 1, False);
276 LoadItem(ITEM_WEAPON_ROCKETLAUNCHER, 'RLAUNCHER', 64, 16, 0, 1, False);
277 LoadItem(ITEM_WEAPON_PLASMA, 'PGUN', 64, 16, 0, 1, False);
278 LoadItem(ITEM_WEAPON_BFG, 'BFG', 64, 64, 0, 1, False);
279 LoadItem(ITEM_WEAPON_SUPERPULEMET, 'SPULEMET', 64, 16, 0, 1, False);
280 LoadItem(ITEM_AMMO_BULLETS, 'CLIP', 16, 16, 0, 1, False);
281 LoadItem(ITEM_AMMO_BULLETS_BOX, 'AMMO', 32, 16, 0, 1, False);
282 LoadItem(ITEM_AMMO_SHELLS, 'SHELL1', 16, 8, 0, 1, False);
283 LoadItem(ITEM_AMMO_SHELLS_BOX, 'SHELL2', 32, 16, 0, 1, False);
284 LoadItem(ITEM_AMMO_ROCKET, 'ROCKET', 16, 32, 0, 1, False);
285 LoadItem(ITEM_AMMO_ROCKET_BOX, 'ROCKETS', 64, 32, 0, 1, False);
286 LoadItem(ITEM_AMMO_CELL, 'CELL', 16, 16, 0, 1, False);
287 LoadItem(ITEM_AMMO_CELL_BIG, 'CELL2', 32, 32, 0, 1, False);
288 LoadItem(ITEM_AMMO_BACKPACK, 'BPACK', 32, 32, 0, 1, False);
289 LoadItem(ITEM_KEY_RED, 'KEYR', 16, 16, 0, 1, False);
290 LoadItem(ITEM_KEY_GREEN, 'KEYG', 16, 16, 0, 1, False);
291 LoadItem(ITEM_KEY_BLUE, 'KEYB', 16, 16, 0, 1, False);
292 LoadItem(ITEM_WEAPON_KASTET, 'KASTET', 64, 32, 0, 1, False);
293 LoadItem(ITEM_WEAPON_PISTOL, 'PISTOL', 64, 16, 0, 1, False);
294 LoadItem(ITEM_BOTTLE, 'BOTTLE', 16, 32, 20, 4, True);
295 LoadItem(ITEM_HELMET, 'HELMET', 16, 16, 20, 4, True);
296 LoadItem(ITEM_JETPACK, 'JETPACK', 32, 32, 15, 3, True);
297 LoadItem(ITEM_INVIS, 'INVIS', 32, 32, 20, 4, True);
298 LoadItem(ITEM_WEAPON_FLAMETHROWER, 'FLAMETHROWER', 64, 32, 0, 1, False);
299 LoadItem(ITEM_AMMO_FUELCAN, 'FUELCAN', 16, 32, 0, 1, False);
300 // fill with NOTEXURE forgotten item
301 for i := ITEM_AMMO_FUELCAN + 1 to ITEM_MAX do
302 LoadItem(i,'NOTEXTURE', 16, 16, 0, 1, False);
303 // --------- monsters --------- //
304 for i := MONSTER_DEMON to MONSTER_MAN do
305 for j := 0 to ANIM_LAST do
306 for d := TDirection.D_LEFT to TDirection.D_RIGHT do
307 LoadMonster(i, j, d);
308 VileFire := r_Textures_LoadMultiFromFileAndInfo(GameWAD + ':TEXTURES/FIRE', 64, 128, 8, False, False);
309 // --------- player models --------- //
310 if PlayerModelsArray <> nil then
311 begin
312 SetLength(Models, Length(PlayerModelsArray));
313 for i := 0 to High(PlayerModelsArray) do
314 r_Map_LoadModel(i);
315 end;
316 // --------- player weapons --------- //
317 for i := 1 to WP_LAST do
318 for j := 0 to W_POS_LAST do
319 for k := 0 to W_ACT_LAST do
320 WeapTextures[i, j, k] := r_Textures_LoadFromFile(GameWAD + ':WEAPONS\' + WeapName[i] + WeapPos[j] + WeapAct[k]);
321 // --------- gfx animations --------- //
322 {$IFDEF ENABLE_GFX}
323 for i := 1 to R_GFX_LAST do
324 GFXTextures[i] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/' + GFXAnim[i].name, GFXAnim[i].w, GFXAnim[i].h, GFXAnim[i].count, GFXAnim[i].back);
325 {$ENDIF}
326 end;
328 procedure r_Map_Free;
329 var i, j, k, a: Integer; d: TDirection;
330 begin
331 {$IFDEF ENABLE_GFX}
332 gfxlist := nil;
333 for i := 0 to R_GFX_LAST do
334 begin
335 if GFXTextures[i] <> nil then
336 GFXTextures[i].Free;
337 GFXTextures[i] := nil;
338 end;
339 {$ENDIF}
340 for i := 1 to WP_LAST do
341 begin
342 for j := 0 to W_POS_LAST do
343 begin
344 for k := 0 to W_ACT_LAST do
345 begin
346 if WeapTextures[i, j, k] <> nil then
347 WeapTextures[i, j, k].Free;
348 WeapTextures[i, j, k] := nil;
349 end;
350 end;
351 end;
352 for d := TDirection.D_LEFT to TDirection.D_RIGHT do
353 begin
354 for a := A_STAND to A_LAST do
355 begin
356 if Models[i].anim[d, a].base <> nil then
357 Models[i].anim[d, a].base.Free;
358 if Models[i].anim[d, a].mask <> nil then
359 Models[i].anim[d, a].mask.Free;
360 Models[i].anim[d, a].base := nil;
361 Models[i].anim[d, a].mask := nil;
362 end;
363 end;
364 for i := MONSTER_DEMON to MONSTER_MAN do
365 begin
366 for j := 0 to ANIM_LAST do
367 begin
368 for d := TDirection.D_LEFT to TDirection.D_RIGHT do
369 begin
370 if MonTextures[i, j, d] <> nil then
371 MonTextures[i, j, d].Free;
372 MonTextures[i, j, d] := nil;
373 end;
374 end;
375 end;
376 for i := 0 to ITEM_MAX do
377 begin
378 if Items[i].tex <> nil then
379 begin
380 Items[i].tex.Free;
381 Items[i].tex := nil;
382 end;
383 Items[i].anim.Invalidate;
384 end;
385 end;
387 procedure r_Map_LoadTextures;
388 var i, n: Integer;
389 begin
390 if Textures <> nil then
391 begin
392 n := Length(Textures);
393 SetLength(RenTextures, n);
394 for i := 0 to n - 1 do
395 begin
396 RenTextures[i].tex := nil;
397 case Textures[i].TextureName of
398 TEXTURE_NAME_WATER: RenTextures[i].spec := TEXTURE_SPECIAL_WATER;
399 TEXTURE_NAME_ACID1: RenTextures[i].spec := TEXTURE_SPECIAL_ACID1;
400 TEXTURE_NAME_ACID2: RenTextures[i].spec := TEXTURE_SPECIAL_ACID2;
401 else
402 RenTextures[i].spec := 0;
403 RenTextures[i].tex := r_Textures_LoadMultiFromFile(Textures[i].FullName);
404 if RenTextures[i].tex = nil then
405 e_LogWritefln('r_Map_LoadTextures: failed to load texture: %s', [Textures[i].FullName]);
406 end;
407 end;
408 end;
409 if gMapInfo.SkyFullName <> '' then
410 SkyTexture := r_Textures_LoadFromFile(gMapInfo.SkyFullName);
411 plist.Clear;
412 end;
414 procedure r_Map_FreeTextures;
415 var i: Integer;
416 begin
417 plist.Clear;
418 if SkyTexture <> nil then
419 SkyTexture.Free;
420 SkyTexture := nil;
421 if RenTextures <> nil then
422 for i := 0 to High(RenTextures) do
423 if RenTextures[i].tex <> nil then
424 RenTextures[i].tex.Free;
425 RenTextures := nil;
426 end;
428 procedure r_Map_DrawPanel (p: TPanel);
429 var Texture: Integer; t: TGLMultiTexture;
430 begin
431 ASSERT(p <> nil);
432 if p.FCurTexture >= 0 then
433 begin
434 Texture := p.TextureIDs[p.FCurTexture].Texture;
435 t := RenTextures[Texture].tex;
437 if (RenTextures[Texture].spec = 0) or (t <> nil) then
438 begin
439 // TODO set alpha and blending type
440 if t = nil then
441 r_Draw_TextureRepeat(nil, p.x, p.y, p.width, p.height, false)
442 else if p.TextureIDs[p.FCurTexture].AnTex.IsValid() then
443 r_Draw_MultiTextureRepeat(t, p.TextureIDs[p.FCurTexture].AnTex, p.x, p.y, p.width, p.height, false)
444 else
445 r_Draw_TextureRepeat(t.GetTexture(0), p.x, p.y, p.width, p.height, false)
446 end;
448 if t = nil then
449 begin
450 case RenTextures[Texture].spec of
451 TEXTURE_SPECIAL_WATER: r_Draw_Filter(p.x, p.y, p.x + p.width, p.y + p.height, 0, 0, 255, 255);
452 TEXTURE_SPECIAL_ACID1: r_Draw_Filter(p.x, p.y, p.x + p.width, p.y + p.height, 0, 230, 0, 255);
453 TEXTURE_SPECIAL_ACID2: r_Draw_Filter(p.x, p.y, p.x + p.width, p.y + p.height, 230, 0, 0, 255);
454 end
455 end
456 end
457 end;
459 procedure r_Map_DrawPanelType (panelTyp: DWORD);
460 var tagMask, i: Integer; p: TPanel;
461 begin
462 i := 0;
463 tagMask := PanelTypeToTag(panelTyp);
464 while plist.count > 0 do
465 begin
466 p := TPanel(plist.Front());
467 if (p.tag and tagMask) = 0 then
468 break;
469 r_Map_DrawPanel(p);
470 Inc(i);
471 plist.PopFront
472 end;
473 end;
475 procedure r_Map_DrawItems (x, y, w, h: Integer; drop: Boolean);
476 var i, fX, fY: Integer; it: PItem; t: TGLMultiTexture;
477 begin
478 if ggItems <> nil then
479 begin
480 for i := 0 to High(ggItems) do
481 begin
482 it := @ggItems[i];
483 if it.used and it.alive and (it.dropped = drop) and (it.ItemType <> ITEM_NONE) then
484 begin
485 t := Items[it.ItemType].tex;
486 if g_Collide(it.obj.x, it.obj.y, t.width, t.height, x, y, w, h) then
487 begin
488 it.obj.Lerp(gLerpFactor, fX, fY);
489 r_Draw_MultiTextureRepeat(t, Items[it.ItemType].anim, fX, fY, t.width, t.height, false);
490 // if g_debug_frames then // TODO draw collision frame
491 end;
492 end;
493 end;
494 end;
495 end;
497 function r_Map_GetMonsterTexture (m, a: Integer; d: TDirection; out t: TGLMultiTexture; out dx, dy: Integer; out flip: Boolean): Boolean;
498 // var c: Integer;
499 begin
500 t := nil; dx := 0; dy := 0; flip := false;
501 result := MonTextures[m, a, d] <> nil;
502 if result = false then
503 begin
504 flip := true;
505 if d = TDirection.D_RIGHT then d := TDirection.D_LEFT else d := TDirection.D_RIGHT;
506 result := MonTextures[m, a, d] <> nil;
507 end;
508 if result = true then
509 begin
510 t := MonTextures[m, a, d];
511 if d = TDirection.D_LEFT then
512 begin
513 dx := MONSTER_ANIMTABLE[m].AnimDeltaLeft[a].X;
514 dy := MONSTER_ANIMTABLE[m].AnimDeltaLeft[a].Y;
515 end
516 else
517 begin
518 dx := MONSTER_ANIMTABLE[m].AnimDeltaRight[a].X;
519 dy := MONSTER_ANIMTABLE[m].AnimDeltaRight[a].Y;
520 end;
521 if flip then
522 begin
523 // c := (MONSTERTABLE[MonsterType].Rect.X - dx) + MONSTERTABLE[MonsterType].Rect.Width;
524 // dx := MTABLE[m].width - c - MONSTERTABLE[MonsterType].Rect.X;
525 dx := -dx;
526 end;
527 end;
528 end;
530 procedure r_Map_DrawMonsterAttack (constref mon: TMonster);
531 var o: TObj;
532 begin
533 if VileFire <> nil then
534 if (mon.MonsterType = MONSTER_VILE) and (mon.MonsterState = MONSTATE_SHOOT) then
535 if mon.VileFireAnim.IsValid() and GetPos(mon.MonsterTargetUID, @o) then
536 r_Draw_MultiTextureRepeat(VileFire, mon.VileFireAnim, o.x + o.rect.x + (o.rect.width div 2) - VILEFIRE_DX, o.y + o.rect.y + o.rect.height - VILEFIRE_DY, VileFire.width, VileFire.height, False);
537 end;
539 procedure r_Map_DrawMonster (constref mon: TMonster);
540 var m, a, fX, fY, dx, dy: Integer; d: TDirection; flip: Boolean; t: TGLMultiTexture;
541 begin
542 m := mon.MonsterType;
543 a := mon.MonsterAnim;
544 d := mon.GameDirection;
546 mon.obj.Lerp(gLerpFactor, fX, fY);
548 if r_Map_GetMonsterTexture(m, a, d, t, dx, dy, flip) then
549 r_Draw_MultiTextureRepeat(t, mon.DirAnim[a, d], fX + dx, fY + dy, t.width, t.height, flip);
552 if g_debug_frames
553 // TODO draw frame
555 end;
557 procedure r_Map_DrawMonsters (x, y, w, h: Integer);
558 var i: Integer; m: TMonster;
559 begin
560 if gMonsters <> nil then
561 begin
562 for i := 0 to High(gMonsters) do
563 begin
564 m := gMonsters[i];
565 if m <> nil then
566 begin
567 r_Map_DrawMonsterAttack(m);
568 // TODO select from grid
569 if g_Collide(m.obj.x + m.obj.rect.x, m.obj.y + m.obj.rect.y, m.obj.rect.width, m.obj.rect.height, x, y, w, h) then
570 r_Map_DrawMonster(m);
571 end;
572 end;
573 end;
574 end;
576 function r_Map_GetPlayerModelTex (i: Integer; var a: Integer; var d: TDirection; out flip: Boolean): Boolean;
577 begin
578 flip := false;
579 result := Models[i].anim[d, a].base <> nil;
580 if result = false then
581 begin
582 flip := true;
583 if d = TDirection.D_LEFT then d := TDirection.D_RIGHT else d := TDirection.D_LEFT;
584 result := Models[i].anim[d, a].base <> nil;
585 end;
586 end;
588 procedure r_Map_DrawPlayerModel (pm: TPlayerModel; x, y: Integer);
589 var a, pos, act, xx, yy: Integer; d: TDirection; flip: Boolean; t: TGLMultiTexture; tex: TGLTexture;
590 begin
591 a := pm.CurrentAnimation;
592 d := pm.Direction;
593 // TODO draw flag
594 if PlayerModelsArray[pm.id].HaveWeapon and not (a in [A_DIE1, A_DIE2, A_PAIN]) then
595 begin
596 case a of
597 A_SEEUP, A_ATTACKUP: pos := W_POS_UP;
598 A_SEEDOWN, A_ATTACKDOWN: pos := W_POS_DOWN;
599 else pos := W_POS_NORMAL;
600 end;
601 if (a in [A_ATTACK, A_ATTACKUP, A_ATTACKDOWN]) or pm.GetFire() then
602 act := W_ACT_FIRE
603 else
604 act := W_ACT_NORMAL;
605 tex := WeapTextures[pm.CurrentWeapon, pos, act];
606 if tex <> nil then
607 begin
608 xx := PlayerModelsArray[pm.id].WeaponPoints[pm.CurrentWeapon, a, d, pm.AnimState.CurrentFrame].X;
609 yy := PlayerModelsArray[pm.id].WeaponPoints[pm.CurrentWeapon, a, d, pm.AnimState.CurrentFrame].Y;
610 r_Draw_Texture(
611 tex,
612 x + xx,
613 y + yy,
614 tex.width,
615 tex.height,
616 d = TDirection.D_LEFT
617 );
618 end;
619 end;
620 if r_Map_GetPlayerModelTex(pm.id, a, d, flip) then
621 begin
622 t := Models[pm.id].anim[d, a].base;
623 r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip);
624 // TODO colorize mask
625 t := Models[pm.id].anim[d, a].mask;
626 if t <> nil then
627 r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip);
628 end;
629 end;
631 procedure r_Map_DrawPlayer (p: TPlayer);
632 var fX, fY, fSlope: Integer;
633 begin
634 if p.alive then
635 begin
636 fX := p.obj.x; fY := p.obj.y;
637 // TODO fix lerp
638 //p.obj.Lerp(gLerpFactor, fX, fY);
639 fSlope := nlerp(p.SlopeOld, p.obj.slopeUpLeft, gLerpFactor);
640 // TODO draw punch
641 // TODO invul pentagram
642 // TODO draw it with transparency
643 r_Map_DrawPlayerModel(p.Model, fX, fY + fSlope);
644 end;
645 // TODO draw g_debug_frames
646 // TODO draw chat bubble
647 // TODO draw aim
648 end;
650 procedure r_Map_DrawPlayers (x, y, w, h: Integer);
651 var i: Integer;
652 begin
653 // TODO draw only visible
654 if gPlayers <> nil then
655 for i := 0 to High(gPlayers) do
656 if gPlayers[i] <> nil then
657 r_Map_DrawPlayer(gPlayers[i]);
658 end;
660 {$IFDEF ENABLE_CORPSES}
661 procedure r_Map_DrawCorpses (x, y, w, h: Integer);
662 var i, fX, fY: Integer; p: TCorpse;
663 begin
664 if gCorpses <> nil then
665 begin
666 for i := 0 to High(gCorpses) do
667 begin
668 p := gCorpses[i];
669 if (p <> nil) and (p.state <> CORPSE_STATE_REMOVEME) and (p.model <> nil) then
670 begin
671 p.obj.Lerp(gLerpFactor, fX, fY);
672 r_Map_DrawPlayerModel(p.model, fX, fY);
673 end;
674 end;
675 end;
676 end;
677 {$ENDIF}
679 {$IFDEF ENABLE_GFX}
680 function r_Map_GetGFXID (): Integer;
681 var i: Integer;
682 begin
683 i := 0;
684 if gfxlist <> nil then
685 begin
686 while (i < Length(gfxlist)) and gfxlist[i].anim.IsValid() do
687 Inc(i);
688 if i >= Length(gfxlist) then
689 SetLength(gfxlist, Length(gfxlist) + 1)
690 end
691 else
692 SetLength(gfxlist, 1);
693 gfxlist[i].typ := R_GFX_NONE;
694 gfxlist[i].anim.Invalidate;
695 result := i
696 end;
698 procedure r_Map_NewGFX (typ, x, y: Integer);
699 var i: Integer;
700 begin
701 if gpart_dbg_enabled and (typ > 0) and (typ <= R_GFX_LAST) then
702 begin
703 i := r_Map_GetGFXID();
704 if i >= 0 then
705 begin
706 gfxlist[i].typ := typ;
707 gfxlist[i].x := x;
708 gfxlist[i].y := y;
709 gfxlist[i].oldX := x;
710 gfxlist[i].oldY := y;
711 gfxlist[i].anim := TAnimState.Create(false, GFXAnim[typ].speed + Random(GFXAnim[typ].rspeed), GFXAnim[typ].count);
712 gfxlist[i].anim.Reset();
713 gfxlist[i].anim.Enable();
714 end;
715 end;
716 end;
718 procedure r_Map_UpdateGFX;
719 var i: Integer;
720 begin
721 if gfxlist <> nil then
722 begin
723 for i := 0 to High(gfxlist) do
724 begin
725 if gfxlist[i].anim.IsValid() then
726 begin
727 gfxlist[i].oldX := gfxlist[i].x;
728 gfxlist[i].oldY := gfxlist[i].y;
729 case gfxlist[i].typ of
730 R_GFX_FLAME, R_GFX_SMOKE:
731 begin
732 if Random(3) = 0 then
733 gfxlist[i].x := gfxlist[i].x - 1 + Random(3);
734 if Random(2) = 0 then
735 gfxlist[i].y := gfxlist[i].y - Random(2);
736 end;
737 end;
738 if gfxlist[i].anim.played then
739 gfxlist[i].anim.Invalidate
740 else
741 gfxlist[i].anim.Update
742 end;
743 end;
744 end;
745 end;
747 procedure r_Map_DrawParticles (x, y, w, h: Integer);
748 var i, fx, fy: Integer;
749 begin
750 if gpart_dbg_enabled and (Particles <> nil) then
751 begin
752 glDisable(GL_TEXTURE_2D);
753 if (g_dbg_scale < 0.6) then
754 glPointSize(1)
755 else if (g_dbg_scale > 1.3) then
756 glPointSize(g_dbg_scale + 1)
757 else
758 glPointSize(2);
759 glDisable(GL_POINT_SMOOTH);
760 glEnable(GL_BLEND);
761 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
763 glBegin(GL_POINTS);
764 for i := 0 to High(Particles) do
765 begin
766 if Particles[i].alive then
767 begin
768 fx := nlerp(Particles[i].oldX, Particles[i].x, gLerpFactor);
769 fy := nlerp(Particles[i].oldY, Particles[i].y, gLerpFactor);
770 glColor4ub(Particles[i].red, Particles[i].green, Particles[i].blue, Particles[i].alpha);
771 glVertex2f(fx, fy);
772 end;
773 end;
774 glEnd;
776 glDisable(GL_BLEND);
777 end;
778 end;
780 procedure r_Map_DrawGFX (x, y, w, h: Integer);
781 var i, fx, fy, typ: Integer; tex: TGLMultiTexture;
782 begin
783 if gfxlist <> nil then
784 begin
785 for i := 0 to High(gfxlist) do
786 begin
787 if gfxlist[i].anim.IsValid() then
788 begin
789 typ := gfxlist[i].typ;
790 tex := GFXTextures[typ];
791 if tex <> nil then
792 begin
793 fx := nlerp(gfxlist[i].oldX, gfxlist[i].x, gLerpFactor);
794 fy := nlerp(gfxlist[i].oldY, gfxlist[i].y, gLerpFactor);
795 // TODO set GFXAnim[typ].alpha
796 r_Draw_MultiTextureRepeat(tex, gfxlist[i].anim, fx, fy, tex.width, tex.height, false);
797 end;
798 end;
799 end;
800 end;
801 end;
802 {$ENDIF}
804 procedure r_Map_Draw (x, y, w, h, camx, camy: Integer);
805 var iter: TPanelGrid.Iter; p: PPanel; cx, cy, xx, yy, ww, hh: Integer;
806 begin
807 cx := camx - w div 2;
808 cy := camy - h div 2;
809 xx := x + cx;
810 yy := y + cy;
811 ww := w;
812 hh := h;
814 if SkyTexture <> nil then
815 r_Draw_Texture(SkyTexture, x, y, w, h, false);
817 plist.Clear;
818 iter := mapGrid.ForEachInAABB(xx, yy, ww, hh, GridDrawableMask);
819 for p in iter do
820 if ((p^.tag and GridTagDoor) <> 0) = p^.door then
821 plist.Insert(p^);
822 iter.Release;
824 glPushMatrix;
825 glTranslatef(-cx, -cy, 0);
826 r_Map_DrawPanelType(PANEL_BACK);
827 r_Map_DrawPanelType(PANEL_STEP);
828 r_Map_DrawItems(xx, yy, ww, hh, false);
829 // TODO draw weapons
830 // TODO draw shells
831 r_Map_DrawPlayers(xx, yy, ww, hh);
832 // TODO draw gibs
833 {$IFDEF ENABLE_CORPSES}
834 r_Map_DrawCorpses(xx, yy, ww, hh);
835 {$ENDIF}
836 r_Map_DrawPanelType(PANEL_WALL);
837 r_Map_DrawMonsters(xx, yy, ww, hh);
838 r_Map_DrawItems(xx, yy, ww, hh, true);
839 r_Map_DrawPanelType(PANEL_CLOSEDOOR);
840 {$IFDEF ENABLE_GFX}
841 r_Map_DrawParticles(xx, yy, ww, hh);
842 r_Map_DrawGFX(xx, yy, ww, hh);
843 {$ENDIF}
844 // TODO draw flags
845 r_Map_DrawPanelType(PANEL_ACID1);
846 r_Map_DrawPanelType(PANEL_ACID2);
847 r_Map_DrawPanelType(PANEL_WATER);
848 r_Map_DrawPanelType(PANEL_FORE);
849 glPopMatrix;
850 end;
852 procedure r_Map_Update;
853 var i: Integer;
854 begin
855 for i := 0 to ITEM_MAX do
856 Items[i].anim.Update;
857 r_Map_UpdateGFX;
858 end;
860 end.