DEADSOFTWARE

gl: draw flags
[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 Math,
39 {$IFDEF USE_GLES1}
40 GLES11,
41 {$ELSE}
42 GL, GLEXT,
43 {$ENDIF}
44 e_log,
45 binheap, MAPDEF, utils,
46 g_options, g_textures, g_basic, g_base, g_phys,
47 g_game, g_map, g_panel, g_items, g_monsters, g_playermodel, g_player, g_weapons,
48 {$IFDEF ENABLE_CORPSES}
49 g_corpses,
50 {$ENDIF}
51 {$IFDEF ENABLE_GFX}
52 g_gfx,
53 {$ENDIF}
54 r_textures, r_draw
55 ;
57 const
58 MTABLE: array [0..MONSTER_MAN] of record
59 w, h: Integer;
60 end = (
61 (w: 64; h: 64), // NONE
62 (w: 64; h: 64), // DEMON
63 (w: 64; h: 64), // IMP
64 (w: 64; h: 64), // ZOMBY
65 (w: 64; h: 64), // SERG
66 (w: 128; h: 128), // CYBER
67 (w: 64; h: 64), // CGUN
68 (w: 128; h: 128), // BARON
69 (w: 128; h: 128), // KNIGHT
70 (w: 128; h: 128), // CACO
71 (w: 64; h: 64), // SOUL (DIE is 128x128, see load code)
72 (w: 128; h: 128), // PAIN
73 (w: 256; h: 128), // SPIDER
74 (w: 128; h: 64), // BSP
75 (w: 128; h: 128), // MANCUB
76 (w: 128; h: 128), // SKEL
77 (w: 128; h: 128), // VILE
78 (w: 32; h: 32), // FISH
79 (w: 64; h: 64), // BARREL
80 (w: 128; h: 128), // ROBO
81 (w: 64; h: 64) // MAN
82 );
83 VILEFIRE_DX = 32;
84 VILEFIRE_DY = 128;
86 GFXAnim: array [0..R_GFX_LAST] of record
87 name: AnsiString;
88 w, h: Integer;
89 count: Integer;
90 back: Boolean;
91 speed: Integer;
92 rspeed: Integer;
93 alpha: Integer;
94 end = (
95 (name: ''; w: 0; h: 0; count: 0; back: false; speed: 0; rspeed: 0; alpha: 0),
96 (name: 'TELEPORT'; w: 64; h: 64; count: 10; back: false; speed: 6; rspeed: 0; alpha: 0),
97 (name: 'FLAME'; w: 32; h: 32; count: 11; back: false; speed: 3; rspeed: 0; alpha: 0),
98 (name: 'EROCKET'; w: 128; h: 128; count: 6; back: false; speed: 6; rspeed: 0; alpha: 0),
99 (name: 'EBFG'; w: 128; h: 128; count: 6; back: false; speed: 6; rspeed: 0; alpha: 0),
100 (name: 'BFGHIT'; w: 64; h: 64; count: 4; back: false; speed: 4; rspeed: 0; alpha: 0),
101 (name: 'FIRE'; w: 64; h: 128; count: 8; back: false; speed: 4; rspeed: 2; alpha: 0),
102 (name: 'ITEMRESPAWN'; w: 32; h: 32; count: 5; back: true; speed: 4; rspeed: 0; alpha: 0),
103 (name: 'SMOKE'; w: 32; h: 32; count: 10; back: false; speed: 3; rspeed: 0; alpha: 0),
104 (name: 'ESKELFIRE'; w: 64; h: 64; count: 3; back: false; speed: 8; rspeed: 0; alpha: 0),
105 (name: 'EPLASMA'; w: 32; h: 32; count: 4; back: true; speed: 3; rspeed: 0; alpha: 0),
106 (name: 'EBSPFIRE'; w: 32; h: 32; count: 5; back: false; speed: 3; rspeed: 0; alpha: 0),
107 (name: 'EIMPFIRE'; w: 64; h: 64; count: 3; back: false; speed: 6; rspeed: 0; alpha: 0),
108 (name: 'ECACOFIRE'; w: 64; h: 64; count: 3; back: false; speed: 6; rspeed: 0; alpha: 0),
109 (name: 'EBARONFIRE'; w: 64; h: 64; count: 3; back: false; speed: 6; rspeed: 0; alpha: 0),
110 (name: 'TELEPORT'; w: 64; h: 64; count: 10; back: false; speed: 3; rspeed: 0; alpha: 0), // fast
111 (name: 'SMOKE'; w: 32; h: 32; count: 10; back: false; speed: 3; rspeed: 0; alpha: 150), // transparent
112 (name: 'FLAME'; w: 32; h: 32; count: 11; back: false; speed: 3; rspeed: 2; alpha: 0) // random
113 );
115 ShotAnim: array [0..WEAPON_LAST] of record
116 name: AnsiString;
117 w, h: Integer;
118 count: Integer;
119 end = (
120 (name: ''; w: 0; h: 0; count: 0), // 0 KASTET
121 (name: ''; w: 0; h: 0; count: 0), // 1 SAW
122 (name: ''; w: 0; h: 0; count: 0), // 2 PISTOL
123 (name: ''; w: 0; h: 0; count: 0), // 3 SHOTGUN1
124 (name: ''; w: 0; h: 0; count: 0), // 4 SHOTGUN2
125 (name: ''; w: 0; h: 0; count: 0), // 5 CHAINGUN
126 (name: 'BROCKET'; w: 64; h: 32; count: 1), // 6 ROCKETLAUNCHER
127 (name: 'BPLASMA'; w: 16; h: 16; count: 2), // 7 PLASMA
128 (name: 'BBFG'; w: 64; h: 64; count: 2), // 8 BFG
129 (name: ''; w: 0; h: 0; count: 0), // 9 SUPERPULEMET
130 (name: 'FLAME'; w: 32; h: 32; count: 0{11}), // 10 FLAMETHROWER
131 (name: ''; w: 0; h: 0; count: 0), // 11
132 (name: ''; w: 0; h: 0; count: 0), // 12
133 (name: ''; w: 0; h: 0; count: 0), // 13
134 (name: ''; w: 0; h: 0; count: 0), // 14
135 (name: ''; w: 0; h: 0; count: 0), // 15
136 (name: ''; w: 0; h: 0; count: 0), // 16
137 (name: ''; w: 0; h: 0; count: 0), // 17
138 (name: ''; w: 0; h: 0; count: 0), // 18
139 (name: ''; w: 0; h: 0; count: 0), // 19
140 (name: ''; w: 0; h: 0; count: 0), // 20 ZOMPY_PISTOL
141 (name: 'BIMPFIRE'; w: 16; h: 16; count: 2), // 21 IMP_FIRE
142 (name: 'BBSPFIRE'; w: 16; h: 16; count: 2), // 22 BSP_FIRE
143 (name: 'BCACOFIRE'; w: 16; h: 16; count: 2), // 23 CACO_FIRE
144 (name: 'BBARONFIRE'; w: 64; h: 16; count: 2), // 24 BARON_FIRE
145 (name: 'BMANCUBFIRE'; w: 64; h: 32; count: 2), // 25 MANCUB_FIRE
146 (name: 'BSKELFIRE'; w: 64; h: 64; count: 2) // 26 SKEL_FIRE
147 );
149 type
150 TBinHeapPanelDrawCmp = class
151 public
152 class function less (const a, b: TPanel): Boolean; inline;
153 end;
155 TBinHeapPanelDraw = specialize TBinaryHeapBase<TPanel, TBinHeapPanelDrawCmp>;
157 TMonsterAnims = array [0..ANIM_LAST, TDirection] of TGLMultiTexture;
159 var
160 SkyTexture: TGLTexture;
161 RenTextures: array of record
162 spec: LongInt;
163 tex: TGLMultiTexture;
164 end;
165 Items: array [0..ITEM_MAX] of record
166 tex: TGLMultiTexture;
167 anim: TAnimState;
168 end;
169 MonTextures: array [0..MONSTER_MAN] of TMonsterAnims;
170 WeapTextures: array [0..WP_LAST, 0..W_POS_LAST, 0..W_ACT_LAST] of TGLTexture;
171 ShotTextures: array [0..WEAPON_LAST] of TGLMultiTexture;
172 FlagTextures: array [0..FLAG_LAST] of TGLMultiTexture;
173 VileFire: TGLMultiTexture;
174 Models: array of record
175 anim: array [TDirection, 0..A_LAST] of record
176 base, mask: TGLMultiTexture;
177 end;
178 (*
179 {$IFDEF ENABLE_GIBS}
180 gibs: array of record
181 base, mask: TGLTexture;
182 rect: TRectWH;
183 end;
184 {$ENDIF}
185 *)
186 end;
188 StubShotAnim: TAnimState;
189 FlagAnim: TAnimState;
191 {$IFDEF ENABLE_GFX}
192 GFXTextures: array [0..R_GFX_LAST] of TGLMultiTexture;
193 gfxlist: array of record
194 typ: Byte;
195 alpha: Byte;
196 x, y: Integer;
197 oldX, oldY: Integer;
198 anim: TAnimState;
199 end = nil;
200 {$ENDIF}
202 plist: TBinHeapPanelDraw = nil;
204 class function TBinHeapPanelDrawCmp.less (const a, b: TPanel): Boolean; inline;
205 begin
206 if a.tag < b.tag then begin result := true; exit; end;
207 if a.tag > b.tag then begin result := false; exit; end;
208 result := a.arrIdx < b.arrIdx;
209 end;
211 procedure r_Map_Initialize;
212 begin
213 StubShotAnim := TAnimState.Create(true, 1, 1);
214 FlagAnim := TAnimState.Create(true, 8, 5);
215 plist := TBinHeapPanelDraw.Create();
216 end;
218 procedure r_Map_Finalize;
219 begin
220 plist.Free;
221 StubShotAnim.Invalidate;
222 end;
224 procedure r_Map_LoadModel (i: Integer);
225 var prefix: AnsiString; a: Integer; d: TDirection; m: ^TPlayerModelInfo;
226 begin
227 m := @PlayerModelsArray[i];
228 prefix := m.FileName + ':TEXTURES/';
229 for d := TDirection.D_LEFT to TDirection.D_RIGHT do
230 begin
231 for a := A_STAND to A_LAST do
232 begin
233 Models[i].anim[d, a].base := nil;
234 Models[i].anim[d, a].mask := nil;
235 if m.anim[d, a].resource <> '' then
236 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);
237 if m.anim[d, a].mask <> '' then
238 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);
239 end
240 end;
241 (*
242 {$IFDEF ENABLE_GIBS}
243 Models[i].gibs := nil;
244 if m.GibsCount > 0 then
245 begin
246 SetLength(Models[i].gibs, m.GibsCount);
247 end;
248 {$ENDIF}
249 *)
250 end;
253 procedure r_Map_Load;
254 const
255 WeapName: array [0..WP_LAST] of AnsiString = ('', 'CSAW', 'HGUN', 'SG', 'SSG', 'MGUN', 'RKT', 'PLZ', 'BFG', 'SPL', 'FLM');
256 WeapPos: array [0..W_POS_LAST] of AnsiString = ('', '_UP', '_DN');
257 WeapAct: array [0..W_ACT_LAST] of AnsiString = ('', '_FIRE');
258 var
259 i, j, k: Integer; d: TDirection;
261 procedure LoadItem (i: Integer; const name: AnsiString; w, h, delay, count: Integer; backanim: Boolean);
262 begin
263 ASSERT(i >= 0);
264 ASSERT(i <= ITEM_MAX);
265 Items[i].tex := r_Textures_LoadMultiFromFileAndInfo(GameWAD + ':TEXTURES/' + name, w, h, count, backanim, false);
266 if backanim then count := count * 2 - 2;
267 Items[i].anim := TAnimState.Create(True, delay, count);
268 ASSERT(Items[i].tex <> NIL);
269 end;
271 procedure LoadMonster (m, a: Integer; d: TDirection);
272 const
273 dir: array [TDirection] of AnsiString = ('_L', '');
274 var
275 w, h, count: Integer;
276 begin
277 count := MONSTER_ANIMTABLE[m].AnimCount[a];
278 if count > 0 then
279 begin
280 w := MTABLE[m].w;
281 h := MTABLE[m].h;
282 if (m = MONSTER_SOUL) and (a = ANIM_DIE) then
283 begin
284 // special case
285 w := 128;
286 h := 128;
287 end;
288 MonTextures[m, a, d] := r_Textures_LoadMultiFromFileAndInfo(
289 GameWAD + ':MTEXTURES/' + MONSTERTABLE[m].name + '_' + ANIMTABLE[a].name + dir[d],
290 w,
291 h,
292 count,
293 False,
294 False
296 end
297 else
298 MonTextures[m, a, d] := nil
299 end;
301 begin
302 // --------- items --------- //
303 // i name w h d n backanim
304 LoadItem(ITEM_NONE, 'NOTEXTURE', 16, 16, 0, 1, False);
305 LoadItem(ITEM_MEDKIT_SMALL, 'MED1', 16, 16, 0, 1, False);
306 LoadItem(ITEM_MEDKIT_LARGE, 'MED2', 32, 32, 0, 1, False);
307 LoadItem(ITEM_MEDKIT_BLACK, 'BMED', 32, 32, 0, 1, False);
308 LoadItem(ITEM_ARMOR_GREEN, 'ARMORGREEN', 32, 16, 20, 3, True);
309 LoadItem(ITEM_ARMOR_BLUE, 'ARMORBLUE', 32, 16, 20, 3, True);
310 LoadItem(ITEM_SPHERE_BLUE, 'SBLUE', 32, 32, 15, 4, True);
311 LoadItem(ITEM_SPHERE_WHITE, 'SWHITE', 32, 32, 20, 4, True);
312 LoadItem(ITEM_SUIT, 'SUIT', 32, 64, 0, 1, False);
313 LoadItem(ITEM_OXYGEN, 'OXYGEN', 16, 32, 0, 1, False);
314 LoadItem(ITEM_INVUL, 'INVUL', 32, 32, 20, 4, True);
315 LoadItem(ITEM_WEAPON_SAW, 'SAW', 64, 32, 0, 1, False);
316 LoadItem(ITEM_WEAPON_SHOTGUN1, 'SHOTGUN1', 64, 16, 0, 1, False);
317 LoadItem(ITEM_WEAPON_SHOTGUN2, 'SHOTGUN2', 64, 16, 0, 1, False);
318 LoadItem(ITEM_WEAPON_CHAINGUN, 'MGUN', 64, 16, 0, 1, False);
319 LoadItem(ITEM_WEAPON_ROCKETLAUNCHER, 'RLAUNCHER', 64, 16, 0, 1, False);
320 LoadItem(ITEM_WEAPON_PLASMA, 'PGUN', 64, 16, 0, 1, False);
321 LoadItem(ITEM_WEAPON_BFG, 'BFG', 64, 64, 0, 1, False);
322 LoadItem(ITEM_WEAPON_SUPERPULEMET, 'SPULEMET', 64, 16, 0, 1, False);
323 LoadItem(ITEM_AMMO_BULLETS, 'CLIP', 16, 16, 0, 1, False);
324 LoadItem(ITEM_AMMO_BULLETS_BOX, 'AMMO', 32, 16, 0, 1, False);
325 LoadItem(ITEM_AMMO_SHELLS, 'SHELL1', 16, 8, 0, 1, False);
326 LoadItem(ITEM_AMMO_SHELLS_BOX, 'SHELL2', 32, 16, 0, 1, False);
327 LoadItem(ITEM_AMMO_ROCKET, 'ROCKET', 16, 32, 0, 1, False);
328 LoadItem(ITEM_AMMO_ROCKET_BOX, 'ROCKETS', 64, 32, 0, 1, False);
329 LoadItem(ITEM_AMMO_CELL, 'CELL', 16, 16, 0, 1, False);
330 LoadItem(ITEM_AMMO_CELL_BIG, 'CELL2', 32, 32, 0, 1, False);
331 LoadItem(ITEM_AMMO_BACKPACK, 'BPACK', 32, 32, 0, 1, False);
332 LoadItem(ITEM_KEY_RED, 'KEYR', 16, 16, 0, 1, False);
333 LoadItem(ITEM_KEY_GREEN, 'KEYG', 16, 16, 0, 1, False);
334 LoadItem(ITEM_KEY_BLUE, 'KEYB', 16, 16, 0, 1, False);
335 LoadItem(ITEM_WEAPON_KASTET, 'KASTET', 64, 32, 0, 1, False);
336 LoadItem(ITEM_WEAPON_PISTOL, 'PISTOL', 64, 16, 0, 1, False);
337 LoadItem(ITEM_BOTTLE, 'BOTTLE', 16, 32, 20, 4, True);
338 LoadItem(ITEM_HELMET, 'HELMET', 16, 16, 20, 4, True);
339 LoadItem(ITEM_JETPACK, 'JETPACK', 32, 32, 15, 3, True);
340 LoadItem(ITEM_INVIS, 'INVIS', 32, 32, 20, 4, True);
341 LoadItem(ITEM_WEAPON_FLAMETHROWER, 'FLAMETHROWER', 64, 32, 0, 1, False);
342 LoadItem(ITEM_AMMO_FUELCAN, 'FUELCAN', 16, 32, 0, 1, False);
343 // fill with NOTEXURE forgotten item
344 for i := ITEM_AMMO_FUELCAN + 1 to ITEM_MAX do
345 LoadItem(i,'NOTEXTURE', 16, 16, 0, 1, False);
346 // --------- monsters --------- //
347 for i := MONSTER_DEMON to MONSTER_MAN do
348 for j := 0 to ANIM_LAST do
349 for d := TDirection.D_LEFT to TDirection.D_RIGHT do
350 LoadMonster(i, j, d);
351 VileFire := r_Textures_LoadMultiFromFileAndInfo(GameWAD + ':TEXTURES/FIRE', 64, 128, 8, False, False);
352 // --------- player models --------- //
353 if PlayerModelsArray <> nil then
354 begin
355 SetLength(Models, Length(PlayerModelsArray));
356 for i := 0 to High(PlayerModelsArray) do
357 r_Map_LoadModel(i);
358 end;
359 // --------- player weapons --------- //
360 for i := 1 to WP_LAST do
361 for j := 0 to W_POS_LAST do
362 for k := 0 to W_ACT_LAST do
363 WeapTextures[i, j, k] := r_Textures_LoadFromFile(GameWAD + ':WEAPONS\' + WeapName[i] + WeapPos[j] + WeapAct[k]);
364 // --------- gfx animations --------- //
365 {$IFDEF ENABLE_GFX}
366 for i := 1 to R_GFX_LAST do
367 if GFXAnim[i].count > 0 then
368 GFXTextures[i] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/' + GFXAnim[i].name, GFXAnim[i].w, GFXAnim[i].h, GFXAnim[i].count, GFXAnim[i].back);
369 {$ENDIF}
370 // --------- shots --------- //
371 for i := 0 to WEAPON_LAST do
372 if ShotAnim[i].count > 0 then
373 ShotTextures[i] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/' + ShotAnim[i].name, ShotAnim[i].w, ShotAnim[i].h, ShotAnim[i].count, false);
374 // --------- flags --------- //
375 FlagTextures[FLAG_NONE] := nil;
376 FlagTextures[FLAG_RED] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/FLAGRED', 64, 64, 5, false);
377 FlagTextures[FLAG_BLUE] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/FLAGBLUE', 64, 64, 5, false);
378 // FlagTextures[FLAG_DOM] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/FLAGDOM', 64, 64, 8, false);
379 end;
381 procedure r_Map_Free;
382 var i, j, k, a: Integer; d: TDirection;
383 begin
384 for i := 0 to FLAG_LAST do
385 begin
386 if FlagTextures[i] <> nil then
387 FlagTextures[i].Free;
388 FlagTextures[i] := nil;
389 end;
390 for i := 0 to WEAPON_LAST do
391 begin
392 if ShotTextures[i] <> nil then
393 ShotTextures[i].Free;
394 ShotTextures[i] := nil;
395 end;
396 {$IFDEF ENABLE_GFX}
397 gfxlist := nil;
398 for i := 0 to R_GFX_LAST do
399 begin
400 if GFXTextures[i] <> nil then
401 GFXTextures[i].Free;
402 GFXTextures[i] := nil;
403 end;
404 {$ENDIF}
405 for i := 1 to WP_LAST do
406 begin
407 for j := 0 to W_POS_LAST do
408 begin
409 for k := 0 to W_ACT_LAST do
410 begin
411 if WeapTextures[i, j, k] <> nil then
412 WeapTextures[i, j, k].Free;
413 WeapTextures[i, j, k] := nil;
414 end;
415 end;
416 end;
417 for d := TDirection.D_LEFT to TDirection.D_RIGHT do
418 begin
419 for a := A_STAND to A_LAST do
420 begin
421 if Models[i].anim[d, a].base <> nil then
422 Models[i].anim[d, a].base.Free;
423 if Models[i].anim[d, a].mask <> nil then
424 Models[i].anim[d, a].mask.Free;
425 Models[i].anim[d, a].base := nil;
426 Models[i].anim[d, a].mask := nil;
427 end;
428 end;
429 for i := MONSTER_DEMON to MONSTER_MAN do
430 begin
431 for j := 0 to ANIM_LAST do
432 begin
433 for d := TDirection.D_LEFT to TDirection.D_RIGHT do
434 begin
435 if MonTextures[i, j, d] <> nil then
436 MonTextures[i, j, d].Free;
437 MonTextures[i, j, d] := nil;
438 end;
439 end;
440 end;
441 for i := 0 to ITEM_MAX do
442 begin
443 if Items[i].tex <> nil then
444 begin
445 Items[i].tex.Free;
446 Items[i].tex := nil;
447 end;
448 Items[i].anim.Invalidate;
449 end;
450 end;
452 procedure r_Map_LoadTextures;
453 var i, n: Integer;
454 begin
455 if Textures <> nil then
456 begin
457 n := Length(Textures);
458 SetLength(RenTextures, n);
459 for i := 0 to n - 1 do
460 begin
461 RenTextures[i].tex := nil;
462 case Textures[i].TextureName of
463 TEXTURE_NAME_WATER: RenTextures[i].spec := TEXTURE_SPECIAL_WATER;
464 TEXTURE_NAME_ACID1: RenTextures[i].spec := TEXTURE_SPECIAL_ACID1;
465 TEXTURE_NAME_ACID2: RenTextures[i].spec := TEXTURE_SPECIAL_ACID2;
466 else
467 RenTextures[i].spec := 0;
468 RenTextures[i].tex := r_Textures_LoadMultiFromFile(Textures[i].FullName);
469 if RenTextures[i].tex = nil then
470 e_LogWritefln('r_Map_LoadTextures: failed to load texture: %s', [Textures[i].FullName]);
471 end;
472 end;
473 end;
474 if gMapInfo.SkyFullName <> '' then
475 SkyTexture := r_Textures_LoadFromFile(gMapInfo.SkyFullName);
476 plist.Clear;
477 end;
479 procedure r_Map_FreeTextures;
480 var i: Integer;
481 begin
482 plist.Clear;
483 if SkyTexture <> nil then
484 SkyTexture.Free;
485 SkyTexture := nil;
486 if RenTextures <> nil then
487 for i := 0 to High(RenTextures) do
488 if RenTextures[i].tex <> nil then
489 RenTextures[i].tex.Free;
490 RenTextures := nil;
491 end;
493 procedure r_Map_DrawPanel (p: TPanel);
494 var Texture: Integer; t: TGLMultiTexture;
495 begin
496 ASSERT(p <> nil);
497 if p.FCurTexture >= 0 then
498 begin
499 Texture := p.TextureIDs[p.FCurTexture].Texture;
500 t := RenTextures[Texture].tex;
502 if (RenTextures[Texture].spec = 0) or (t <> nil) then
503 begin
504 if t = nil then
505 r_Draw_TextureRepeat(nil, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending)
506 else if p.TextureIDs[p.FCurTexture].AnTex.IsValid() then
507 r_Draw_MultiTextureRepeat(t, p.TextureIDs[p.FCurTexture].AnTex, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending)
508 else
509 r_Draw_TextureRepeat(t.GetTexture(0), p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending)
510 end;
512 if t = nil then
513 begin
514 case RenTextures[Texture].spec of
515 TEXTURE_SPECIAL_WATER: r_Draw_Filter(p.x, p.y, p.x + p.width, p.y + p.height, 0, 0, 255, 255);
516 TEXTURE_SPECIAL_ACID1: r_Draw_Filter(p.x, p.y, p.x + p.width, p.y + p.height, 0, 230, 0, 255);
517 TEXTURE_SPECIAL_ACID2: r_Draw_Filter(p.x, p.y, p.x + p.width, p.y + p.height, 230, 0, 0, 255);
518 end
519 end
520 end
521 end;
523 procedure r_Map_DrawPanelType (panelTyp: DWORD);
524 var tagMask, i: Integer; p: TPanel;
525 begin
526 i := 0;
527 tagMask := PanelTypeToTag(panelTyp);
528 while plist.count > 0 do
529 begin
530 p := TPanel(plist.Front());
531 if (p.tag and tagMask) = 0 then
532 break;
533 r_Map_DrawPanel(p);
534 Inc(i);
535 plist.PopFront
536 end;
537 end;
539 procedure r_Map_DrawItems (x, y, w, h: Integer; drop: Boolean);
540 var i, fX, fY: Integer; it: PItem; t: TGLMultiTexture;
541 begin
542 if ggItems <> nil then
543 begin
544 for i := 0 to High(ggItems) do
545 begin
546 it := @ggItems[i];
547 if it.used and it.alive and (it.dropped = drop) and (it.ItemType <> ITEM_NONE) then
548 begin
549 t := Items[it.ItemType].tex;
550 if g_Collide(it.obj.x, it.obj.y, t.width, t.height, x, y, w, h) then
551 begin
552 it.obj.Lerp(gLerpFactor, fX, fY);
553 r_Draw_MultiTextureRepeat(t, Items[it.ItemType].anim, fX, fY, t.width, t.height, false, 255, 255, 255, 255, false);
554 // if g_debug_frames then // TODO draw collision frame
555 end;
556 end;
557 end;
558 end;
559 end;
561 function r_Map_GetMonsterTexture (m, a: Integer; d: TDirection; out t: TGLMultiTexture; out dx, dy: Integer; out flip: Boolean): Boolean;
562 // var c: Integer;
563 begin
564 t := nil; dx := 0; dy := 0; flip := false;
565 result := MonTextures[m, a, d] <> nil;
566 if result = false then
567 begin
568 flip := true;
569 if d = TDirection.D_RIGHT then d := TDirection.D_LEFT else d := TDirection.D_RIGHT;
570 result := MonTextures[m, a, d] <> nil;
571 end;
572 if result = true then
573 begin
574 t := MonTextures[m, a, d];
575 if d = TDirection.D_LEFT then
576 begin
577 dx := MONSTER_ANIMTABLE[m].AnimDeltaLeft[a].X;
578 dy := MONSTER_ANIMTABLE[m].AnimDeltaLeft[a].Y;
579 end
580 else
581 begin
582 dx := MONSTER_ANIMTABLE[m].AnimDeltaRight[a].X;
583 dy := MONSTER_ANIMTABLE[m].AnimDeltaRight[a].Y;
584 end;
585 if flip then
586 begin
587 // c := (MONSTERTABLE[MonsterType].Rect.X - dx) + MONSTERTABLE[MonsterType].Rect.Width;
588 // dx := MTABLE[m].width - c - MONSTERTABLE[MonsterType].Rect.X;
589 dx := -dx;
590 end;
591 end;
592 end;
594 procedure r_Map_DrawMonsterAttack (constref mon: TMonster);
595 var o: TObj;
596 begin
597 if VileFire <> nil then
598 if (mon.MonsterType = MONSTER_VILE) and (mon.MonsterState = MONSTATE_SHOOT) then
599 if mon.VileFireAnim.IsValid() and GetPos(mon.MonsterTargetUID, @o) then
600 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, 255, 255, 255, 255, false);
601 end;
603 procedure r_Map_DrawMonster (constref mon: TMonster);
604 var m, a, fX, fY, dx, dy: Integer; d: TDirection; flip: Boolean; t: TGLMultiTexture;
605 begin
606 m := mon.MonsterType;
607 a := mon.MonsterAnim;
608 d := mon.GameDirection;
610 mon.obj.Lerp(gLerpFactor, fX, fY);
612 if r_Map_GetMonsterTexture(m, a, d, t, dx, dy, flip) then
613 r_Draw_MultiTextureRepeat(t, mon.DirAnim[a, d], fX + dx, fY + dy, t.width, t.height, flip, 255, 255, 255, 255, false);
616 if g_debug_frames
617 // TODO draw frame
619 end;
621 procedure r_Map_DrawMonsters (x, y, w, h: Integer);
622 var i: Integer; m: TMonster;
623 begin
624 if gMonsters <> nil then
625 begin
626 for i := 0 to High(gMonsters) do
627 begin
628 m := gMonsters[i];
629 if m <> nil then
630 begin
631 r_Map_DrawMonsterAttack(m);
632 // TODO select from grid
633 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
634 r_Map_DrawMonster(m);
635 end;
636 end;
637 end;
638 end;
640 function r_Map_GetPlayerModelTex (i: Integer; var a: Integer; var d: TDirection; out flip: Boolean): Boolean;
641 begin
642 flip := false;
643 result := Models[i].anim[d, a].base <> nil;
644 if result = false then
645 begin
646 flip := true;
647 if d = TDirection.D_LEFT then d := TDirection.D_RIGHT else d := TDirection.D_LEFT;
648 result := Models[i].anim[d, a].base <> nil;
649 end;
650 end;
652 procedure r_Map_DrawPlayerModel (pm: TPlayerModel; x, y: Integer);
653 var a, pos, act, xx, yy, angle: Integer; d: TDirection; flip: Boolean; t: TGLMultiTexture; tex: TGLTexture; c: TRGB;
654 begin
655 a := pm.CurrentAnimation;
656 d := pm.Direction;
658 (* draw flag*)
659 t := FlagTextures[pm.Flag];
660 if (t <> nil) and not (a in [A_DIE1, A_DIE2]) then
661 begin
662 flip := d = TDirection.D_RIGHT;
663 angle := PlayerModelsArray[pm.id].FlagAngle;
664 xx := PlayerModelsArray[pm.id].FlagPoint.X;
665 yy := PlayerModelsArray[pm.id].FlagPoint.Y;
666 r_Draw_MultiTextureRepeatRotate(
667 t,
668 FlagAnim,
669 x + IfThen(flip, 2 * FLAG_BASEPOINT.X - xx + 1, xx - 1) - FLAG_BASEPOINT.X,
670 y + yy - FLAG_BASEPOINT.Y + 1,
671 t.width,
672 t.height,
673 flip,
674 255, 255, 255, 255, false,
675 IfThen(flip, 64 - FLAG_BASEPOINT.X, FLAG_BASEPOINT.X),
676 FLAG_BASEPOINT.Y,
677 IfThen(flip, angle, -angle)
678 );
679 end;
681 (* draw weapon *)
682 if PlayerModelsArray[pm.id].HaveWeapon and not (a in [A_DIE1, A_DIE2, A_PAIN]) then
683 begin
684 case a of
685 A_SEEUP, A_ATTACKUP: pos := W_POS_UP;
686 A_SEEDOWN, A_ATTACKDOWN: pos := W_POS_DOWN;
687 else pos := W_POS_NORMAL;
688 end;
689 if (a in [A_ATTACK, A_ATTACKUP, A_ATTACKDOWN]) or pm.GetFire() then
690 act := W_ACT_FIRE
691 else
692 act := W_ACT_NORMAL;
693 tex := WeapTextures[pm.CurrentWeapon, pos, act];
694 if tex <> nil then
695 begin
696 xx := PlayerModelsArray[pm.id].WeaponPoints[pm.CurrentWeapon, a, d, pm.AnimState.CurrentFrame].X;
697 yy := PlayerModelsArray[pm.id].WeaponPoints[pm.CurrentWeapon, a, d, pm.AnimState.CurrentFrame].Y;
698 r_Draw_Texture(
699 tex,
700 x + xx,
701 y + yy,
702 tex.width,
703 tex.height,
704 d = TDirection.D_LEFT,
705 255, 255, 255, 255, false
706 );
707 end;
708 end;
710 (* draw body *)
711 if r_Map_GetPlayerModelTex(pm.id, a, d, flip) then
712 begin
713 t := Models[pm.id].anim[d, a].base;
714 r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip, 255, 255, 255, 255, false);
715 t := Models[pm.id].anim[d, a].mask;
716 if t <> nil then
717 begin
718 c := pm.Color;
719 r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip, c.r, c.g, c.b, 255, false);
720 end;
721 end;
722 end;
724 procedure r_Map_DrawPlayer (p: TPlayer);
725 var fX, fY, fSlope: Integer;
726 begin
727 if p.alive then
728 begin
729 fX := p.obj.x; fY := p.obj.y;
730 // TODO fix lerp
731 //p.obj.Lerp(gLerpFactor, fX, fY);
732 fSlope := nlerp(p.SlopeOld, p.obj.slopeUpLeft, gLerpFactor);
733 // TODO draw punch
734 // TODO invul pentagram
735 // TODO draw it with transparency
736 r_Map_DrawPlayerModel(p.Model, fX, fY + fSlope);
737 end;
738 // TODO draw g_debug_frames
739 // TODO draw chat bubble
740 // TODO draw aim
741 end;
743 procedure r_Map_DrawPlayers (x, y, w, h: Integer);
744 var i: Integer;
745 begin
746 // TODO draw only visible
747 if gPlayers <> nil then
748 for i := 0 to High(gPlayers) do
749 if gPlayers[i] <> nil then
750 r_Map_DrawPlayer(gPlayers[i]);
751 end;
753 {$IFDEF ENABLE_CORPSES}
754 procedure r_Map_DrawCorpses (x, y, w, h: Integer);
755 var i, fX, fY: Integer; p: TCorpse;
756 begin
757 if gCorpses <> nil then
758 begin
759 for i := 0 to High(gCorpses) do
760 begin
761 p := gCorpses[i];
762 if (p <> nil) and (p.state <> CORPSE_STATE_REMOVEME) and (p.model <> nil) then
763 begin
764 p.obj.Lerp(gLerpFactor, fX, fY);
765 r_Map_DrawPlayerModel(p.model, fX, fY);
766 end;
767 end;
768 end;
769 end;
770 {$ENDIF}
772 {$IFDEF ENABLE_GFX}
773 function r_Map_GetGFXID (): Integer;
774 var i: Integer;
775 begin
776 i := 0;
777 if gfxlist <> nil then
778 begin
779 while (i < Length(gfxlist)) and gfxlist[i].anim.IsValid() do
780 Inc(i);
781 if i >= Length(gfxlist) then
782 SetLength(gfxlist, Length(gfxlist) + 1)
783 end
784 else
785 SetLength(gfxlist, 1);
786 gfxlist[i].typ := R_GFX_NONE;
787 gfxlist[i].anim.Invalidate;
788 result := i
789 end;
791 procedure r_Map_NewGFX (typ, x, y: Integer);
792 var i: Integer;
793 begin
794 if gpart_dbg_enabled and (typ > 0) and (typ <= R_GFX_LAST) then
795 begin
796 i := r_Map_GetGFXID();
797 if i >= 0 then
798 begin
799 gfxlist[i].typ := typ;
800 gfxlist[i].x := x;
801 gfxlist[i].y := y;
802 gfxlist[i].oldX := x;
803 gfxlist[i].oldY := y;
804 gfxlist[i].anim := TAnimState.Create(false, GFXAnim[typ].speed + Random(GFXAnim[typ].rspeed), GFXAnim[typ].count);
805 gfxlist[i].anim.Reset();
806 gfxlist[i].anim.Enable();
807 end;
808 end;
809 end;
811 procedure r_Map_UpdateGFX;
812 var i: Integer;
813 begin
814 if gfxlist <> nil then
815 begin
816 for i := 0 to High(gfxlist) do
817 begin
818 if gfxlist[i].anim.IsValid() then
819 begin
820 gfxlist[i].oldX := gfxlist[i].x;
821 gfxlist[i].oldY := gfxlist[i].y;
822 case gfxlist[i].typ of
823 R_GFX_FLAME, R_GFX_SMOKE:
824 begin
825 if Random(3) = 0 then
826 gfxlist[i].x := gfxlist[i].x - 1 + Random(3);
827 if Random(2) = 0 then
828 gfxlist[i].y := gfxlist[i].y - Random(2);
829 end;
830 end;
831 if gfxlist[i].anim.played then
832 gfxlist[i].anim.Invalidate
833 else
834 gfxlist[i].anim.Update
835 end;
836 end;
837 end;
838 end;
840 procedure r_Map_DrawParticles (x, y, w, h: Integer);
841 var i, fx, fy: Integer;
842 begin
843 if gpart_dbg_enabled and (Particles <> nil) then
844 begin
845 glDisable(GL_TEXTURE_2D);
846 if (g_dbg_scale < 0.6) then
847 glPointSize(1)
848 else if (g_dbg_scale > 1.3) then
849 glPointSize(g_dbg_scale + 1)
850 else
851 glPointSize(2);
852 glDisable(GL_POINT_SMOOTH);
853 glEnable(GL_BLEND);
854 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
856 glBegin(GL_POINTS);
857 for i := 0 to High(Particles) do
858 begin
859 if Particles[i].alive then
860 begin
861 fx := nlerp(Particles[i].oldX, Particles[i].x, gLerpFactor);
862 fy := nlerp(Particles[i].oldY, Particles[i].y, gLerpFactor);
863 glColor4ub(Particles[i].red, Particles[i].green, Particles[i].blue, Particles[i].alpha);
864 glVertex2f(fx, fy);
865 end;
866 end;
867 glEnd;
869 glDisable(GL_BLEND);
870 end;
871 end;
873 procedure r_Map_DrawGFX (x, y, w, h: Integer);
874 var i, fx, fy, typ: Integer; tex: TGLMultiTexture;
875 begin
876 if gfxlist <> nil then
877 begin
878 for i := 0 to High(gfxlist) do
879 begin
880 if gfxlist[i].anim.IsValid() then
881 begin
882 typ := gfxlist[i].typ;
883 tex := GFXTextures[typ];
884 if tex <> nil then
885 begin
886 fx := nlerp(gfxlist[i].oldX, gfxlist[i].x, gLerpFactor);
887 fy := nlerp(gfxlist[i].oldY, gfxlist[i].y, gLerpFactor);
888 r_Draw_MultiTextureRepeat(tex, gfxlist[i].anim, fx, fy, tex.width, tex.height, false, 255, 255, 255, 255 - GFXAnim[typ].alpha, false);
889 end;
890 end;
891 end;
892 end;
893 end;
894 {$ENDIF}
896 procedure r_Map_DrawShots (x, y, w, h: Integer);
897 var i, a, fX, fY, pX, pY, typ: Integer; tex: TGLMultiTexture; anim: ^TAnimState;
898 begin
899 if Shots <> nil then
900 begin
901 for i := 0 to High(Shots) do
902 begin
903 typ := Shots[i].ShotType;
904 if typ <> 0 then
905 begin
906 tex := ShotTextures[typ];
907 if tex <> nil then
908 begin
909 a := 0;
910 case typ of
911 WEAPON_ROCKETLAUNCHER, WEAPON_BARON_FIRE, WEAPON_MANCUB_FIRE, WEAPON_SKEL_FIRE:
912 a := -GetAngle2(Shots[i].Obj.Vel.X, Shots[i].Obj.Vel.Y)
913 end;
914 Shots[i].Obj.Lerp(gLerpFactor, fX, fY);
915 pX := Shots[i].Obj.Rect.Width div 2;
916 pY := Shots[i].Obj.Rect.Height div 2;
917 // TODO fix this hack
918 if Shots[i].Animation.IsValid() then anim := @Shots[i].Animation else anim := @StubShotAnim;
919 r_Draw_MultiTextureRepeatRotate(tex, anim^, fX, fY, tex.width, tex.height, false, 255, 255, 255, 255, false, pX, pY, a);
920 end;
921 end;
922 end;
923 end;
924 end;
926 procedure r_Map_DrawFlags (x, y, w, h: Integer);
927 var i, dx, fx, fy: Integer; flip: Boolean; tex: TGLMultiTexture;
928 begin
929 if gGameSettings.GameMode = GM_CTF then
930 begin
931 for i := FLAG_RED to FLAG_BLUE do
932 begin
933 if not (gFlags[i].state in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
934 begin
935 gFlags[i].Obj.Lerp(gLerpFactor, fx, fy);
936 flip := gFlags[i].Direction = TDirection.D_LEFT;
937 if flip then dx := -1 else dx := +1;
938 tex := FlagTextures[i];
939 r_Draw_MultiTextureRepeat(tex, FlagAnim, fx + dx, fy + 1, tex.width, tex.height, flip, 255, 255, 255, 255, false)
940 // TODO g_debug_frames
941 end;
942 end;
943 end;
944 end;
946 procedure r_Map_Draw (x, y, w, h, camx, camy: Integer);
947 var iter: TPanelGrid.Iter; p: PPanel; cx, cy, xx, yy, ww, hh: Integer;
948 begin
949 cx := camx - w div 2;
950 cy := camy - h div 2;
951 xx := x + cx;
952 yy := y + cy;
953 ww := w;
954 hh := h;
956 if SkyTexture <> nil then
957 r_Draw_Texture(SkyTexture, x, y, w, h, false, 255, 255, 255, 255, false);
959 plist.Clear;
960 iter := mapGrid.ForEachInAABB(xx, yy, ww, hh, GridDrawableMask);
961 for p in iter do
962 if ((p^.tag and GridTagDoor) <> 0) = p^.door then
963 plist.Insert(p^);
964 iter.Release;
966 glPushMatrix;
967 glTranslatef(-cx, -cy, 0);
968 r_Map_DrawPanelType(PANEL_BACK);
969 r_Map_DrawPanelType(PANEL_STEP);
970 r_Map_DrawItems(xx, yy, ww, hh, false);
971 r_Map_DrawShots(xx, yy, ww, hh);
972 // TODO draw shells
973 r_Map_DrawPlayers(xx, yy, ww, hh);
974 // TODO draw gibs
975 {$IFDEF ENABLE_CORPSES}
976 r_Map_DrawCorpses(xx, yy, ww, hh);
977 {$ENDIF}
978 r_Map_DrawPanelType(PANEL_WALL);
979 r_Map_DrawMonsters(xx, yy, ww, hh);
980 r_Map_DrawItems(xx, yy, ww, hh, true);
981 r_Map_DrawPanelType(PANEL_CLOSEDOOR);
982 {$IFDEF ENABLE_GFX}
983 r_Map_DrawParticles(xx, yy, ww, hh);
984 r_Map_DrawGFX(xx, yy, ww, hh);
985 {$ENDIF}
986 r_Map_DrawFlags(xx, yy, ww, hh);
987 r_Map_DrawPanelType(PANEL_ACID1);
988 r_Map_DrawPanelType(PANEL_ACID2);
989 r_Map_DrawPanelType(PANEL_WATER);
990 r_Map_DrawPanelType(PANEL_FORE);
991 glPopMatrix;
992 end;
994 procedure r_Map_Update;
995 var i: Integer;
996 begin
997 for i := 0 to ITEM_MAX do
998 Items[i].anim.Update;
999 r_Map_UpdateGFX;
1000 FlagAnim.Update;
1001 end;
1003 end.