DEADSOFTWARE

gl: draw screen flashes
[d2df-sdl.git] / src / game / renders / opengl / r_map.pas
index 2ca5f3ef193854bff9789eea2e4d9e37bc5528aa..c974664acf4e043c7f1f96edfb988b8878b8b556 100644 (file)
@@ -17,7 +17,7 @@ unit r_map;
 
 interface
 
-  uses g_base; // TRectWH
+  uses g_base, g_player; // TRectWH, TPlayer
 
   procedure r_Map_Initialize;
   procedure r_Map_Finalize;
@@ -37,7 +37,7 @@ interface
 
   procedure r_Map_Update;
 
-  procedure r_Map_Draw (x, y, w, h, camx, camy: Integer);
+  procedure r_Map_Draw (x, y, w, h, camx, camy: Integer; player: TPlayer);
 
 implementation
 
@@ -51,7 +51,7 @@ implementation
     e_log,
     binheap, MAPDEF, utils,
     g_options, g_textures, g_basic, g_phys,
-    g_game, g_map, g_panel, g_items, g_monsters, g_playermodel, g_player, g_weapons,
+    g_game, g_map, g_panel, g_items, g_monsters, g_playermodel, g_weapons,
     {$IFDEF ENABLE_CORPSES}
       g_corpses,
     {$ENDIF}
@@ -244,7 +244,9 @@ implementation
     WeapTextures: array [0..WP_LAST, 0..W_POS_LAST, 0..W_ACT_LAST] of TGLTexture;
     ShotTextures: array [0..WEAPON_LAST] of TGLMultiTexture;
     FlagTextures: array [0..FLAG_LAST] of TGLMultiTexture;
+    PunchTextures: array [Boolean, 0..2] of TGLMultiTexture; // [berserk, center/up/down]
     VileFire: TGLMultiTexture;
+    InvulPenta: TGLTexture;
     Models: array of record
       anim: array [TDirection, 0..A_LAST] of record
         base, mask: TGLMultiTexture;
@@ -257,7 +259,7 @@ implementation
 {$ENDIF}
     end;
 
-    StubShotAnim: TAnimState;
+    StubShotAnim: TAnimState; // TODO remove this hack
     FlagAnim: TAnimState;
 
 {$IFDEF ENABLE_SHELLS}
@@ -362,8 +364,6 @@ implementation
             // ok
           end;
         end;
-        for a := 0 to m.GibsCount - 1 do
-          e_logwritefln('model %s gib %s: %sx%s:%sx%s', [i, a, Models[i].gibs.rect[a].x, Models[i].gibs.rect[a].y, Models[i].gibs.rect[a].width, Models[i].gibs.rect[a].height]);
       end;
     {$ENDIF}
   end;
@@ -401,8 +401,9 @@ implementation
       WeapName: array [0..WP_LAST] of AnsiString = ('', 'CSAW', 'HGUN', 'SG', 'SSG', 'MGUN', 'RKT', 'PLZ', 'BFG', 'SPL', 'FLM');
       WeapPos: array [0..W_POS_LAST] of AnsiString = ('', '_UP', '_DN');
       WeapAct: array [0..W_ACT_LAST] of AnsiString = ('', '_FIRE');
+      PunchName: array [Boolean] of AnsiString = ('PUNCH', 'PUNCHB');
     var
-      i, j, k: Integer; d: TDirection;
+      i, j, k: Integer; d: TDirection; b: Boolean;
   begin
     // --------- items --------- //
     for i := 0 to ITEM_LAST do
@@ -459,11 +460,31 @@ implementation
       for i := 0 to SHELL_LAST do
         ShellTextures[i] := r_Textures_LoadFromFile(GameWad + ':TEXTURES/' + ShellAnim[i].name);
     {$ENDIF}
+    // --------- punch --------- //
+    for b := false to true do
+    begin
+      for i := 0 to 2 do
+        PunchTextures[b, i] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':WEAPONS/' + PunchName[b] + WeapPos[i], 64, 64, 4, false);
+    end;
+    // --------- other --------- //
+    InvulPenta := r_Textures_LoadFromFile(GameWad + ':TEXTURES/PENTA');
   end;
 
   procedure r_Map_Free;
-    var i, j, k, a: Integer; d: TDirection;
+    var i, j, k: Integer; d: TDirection; b: Boolean;
   begin
+    if InvulPenta <> nil then
+      InvulPenta.Free;
+    InvulPenta := nil;
+    for b := false to true do
+    begin
+      for i := 0 to 2 do
+      begin
+        if PunchTextures[b, i] <> nil then
+          PunchTextures[b, i].Free;
+        PunchTextures[b, i] := nil;
+      end;
+    end;
     {$IFDEF ENABLE_SHELLS}
       for i := 0 to SHELL_LAST do
       begin
@@ -633,11 +654,11 @@ implementation
           begin
             it.obj.Lerp(gLerpFactor, fX, fY);
             r_Draw_MultiTextureRepeat(t, Items[it.ItemType].anim, fX, fY, t.width, t.height, false, 255, 255, 255, 255, false);
-            // if g_debug_frames then // TODO draw collision frame
           end;
         end;
       end;
     end;
+    // TODO draw g_debug_frames
   end;
 
   function r_Map_GetMonsterTexture (m, a: Integer; d: TDirection; out t: TGLMultiTexture; out dx, dy: Integer; out flip: Boolean): Boolean;
@@ -694,10 +715,7 @@ implementation
     if r_Map_GetMonsterTexture(m, a, d, t, dx, dy, flip) then
       r_Draw_MultiTextureRepeat(t, mon.DirAnim[a, d], fX + dx, fY + dy, t.width, t.height, flip, 255, 255, 255, 255, false);
 
-{
-    if g_debug_frames
-      // TODO draw frame
-}
+    // TODO draw g_debug_frames
   end;
 
   procedure r_Map_DrawMonsters (x, y, w, h: Integer);
@@ -731,7 +749,7 @@ implementation
     end;
   end;
 
-  procedure r_Map_DrawPlayerModel (pm: TPlayerModel; x, y: Integer);
+  procedure r_Map_DrawPlayerModel (pm: TPlayerModel; x, y: Integer; alpha: Byte);
     var a, pos, act, xx, yy, angle: Integer; d: TDirection; flip: Boolean; t: TGLMultiTexture; tex: TGLTexture; c: TRGB;
   begin
     a := pm.CurrentAnimation;
@@ -784,7 +802,7 @@ implementation
           tex.width,
           tex.height,
           d = TDirection.D_LEFT,
-          255, 255, 255, 255, false
+          255, 255, 255, alpha, false
         );
       end;
     end;
@@ -793,18 +811,18 @@ implementation
     if r_Map_GetPlayerModelTex(pm.id, a, d, flip) then
     begin
       t := Models[pm.id].anim[d, a].base;
-      r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip, 255, 255, 255, 255, false);
+      r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip, 255, 255, 255, alpha, false);
       t := Models[pm.id].anim[d, a].mask;
       if t <> nil then
       begin
         c := pm.Color;
-        r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip, c.r, c.g, c.b, 255, false);
+        r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip, c.r, c.g, c.b, alpha, false);
       end;
     end;
   end;
 
-  procedure r_Map_DrawPlayer (p: TPlayer);
-    var fX, fY, fSlope: Integer;
+  procedure r_Map_DrawPlayer (p, drawed: TPlayer);
+    var fX, fY, fSlope, ax, ay, w, h: Integer; b, flip: Boolean; t: TGLMultiTexture; alpha: Byte;
   begin
     if p.alive then
     begin
@@ -812,24 +830,63 @@ implementation
       // TODO fix lerp
       //p.obj.Lerp(gLerpFactor, fX, fY);
       fSlope := nlerp(p.SlopeOld, p.obj.slopeUpLeft, gLerpFactor);
-      // TODO draw punch
-      // TODO invul pentagram
-      // TODO draw it with transparency
-      r_Map_DrawPlayerModel(p.Model, fX, fY + fSlope);
+
+      (* punch effect *)
+      if p.PunchAnim.IsValid() and p.PunchAnim.enabled then
+      begin
+        b := R_BERSERK in p.FRulez;
+        if p.FKeys[KEY_DOWN].pressed then
+          t := PunchTextures[b, 2]
+        else if p.FKeys[KEY_UP].pressed then
+          t := PunchTextures[b, 1]
+        else
+          t := PunchTextures[b, 0];
+        if t <> nil then
+        begin
+          flip := p.Direction = TDirection.D_LEFT;
+          ax := IfThen(flip, 15 - p.Obj.Rect.X, p.Obj.Rect.X - 15); // ???
+          ay := p.Obj.Rect.Y - 11;
+          r_Draw_MultiTextureRepeat(t, p.PunchAnim, fx + ax, fy + fSlope + ay, t.width, t.height, flip, 255, 255, 255, 255, false)
+        end;
+      end;
+
+      (* invulnerability effect *)
+      if (InvulPenta <> nil) and (p.FMegaRulez[MR_INVUL] > gTime) and ((p <> drawed) or (p.SpawnInvul >= gTime)) then
+      begin
+        w := InvulPenta.width;
+        h := InvulPenta.height;
+        ax := p.Obj.Rect.X + (p.Obj.Rect.Width div 2) - (w div 2); // + IfThen(flip, +4, -2) // ???
+        ay := p.Obj.Rect.Y + (p.Obj.Rect.Height div 2) - (h div 2) - 7; // ???
+        r_Draw_Texture(InvulPenta, fx + ax, fy + ay + fSlope, w, h, false, 255, 255, 255, 255, false);
+      end;
+
+      (* invisibility effect *)
+      alpha := 255;
+      if p.FMegaRulez[MR_INVIS] > gTime then
+      begin
+        if (drawed <> nil) and ((p = drawed) or ((p.Team = drawed.Team) and (gGameSettings.GameMode <> GM_DM))) then
+        begin
+          if (p.FMegaRulez[MR_INVIS] - gTime > 2100) or not ODD((p.FMegaRulez[MR_INVIS] - gTime) div 300) then
+            alpha := 55;
+        end
+        else
+          alpha := 1; // ???
+      end;
+
+      r_Map_DrawPlayerModel(p.Model, fX, fY + fSlope, alpha);
     end;
     // TODO draw g_debug_frames
     // TODO draw chat bubble
     // TODO draw aim
   end;
 
-  procedure r_Map_DrawPlayers (x, y, w, h: Integer);
+  procedure r_Map_DrawPlayers (x, y, w, h: Integer; player: TPlayer);
     var i: Integer;
   begin
-    // TODO draw only visible
     if gPlayers <> nil then
       for i := 0 to High(gPlayers) do
         if gPlayers[i] <> nil then
-          r_Map_DrawPlayer(gPlayers[i]);
+          r_Map_DrawPlayer(gPlayers[i], player);
   end;
 
 {$IFDEF ENABLE_GIBS}
@@ -869,6 +926,7 @@ implementation
         end;
       end;
     end;
+    // TODO draw g_debug_frames
   end;
 {$ENDIF}
 
@@ -884,10 +942,11 @@ implementation
         if (p <> nil) and (p.state <> CORPSE_STATE_REMOVEME) and (p.model <> nil) then
         begin
           p.obj.Lerp(gLerpFactor, fX, fY);
-          r_Map_DrawPlayerModel(p.model, fX, fY);
+          r_Map_DrawPlayerModel(p.model, fX, fY, 255);
         end;
       end;
     end;
+    // TODO draw g_debug_frames
   end;
 {$ENDIF}
 
@@ -1043,6 +1102,7 @@ implementation
         end;
       end;
     end;
+    // TODO draw g_debug_frames
   end;
 
   procedure r_Map_DrawFlags (x, y, w, h: Integer);
@@ -1059,10 +1119,10 @@ implementation
           if flip then dx := -1 else dx := +1;
           tex := FlagTextures[i];
           r_Draw_MultiTextureRepeat(tex, FlagAnim, fx + dx, fy + 1, tex.width, tex.height, flip, 255, 255, 255, 255, false)
-          // TODO g_debug_frames
         end;
       end;
     end;
+    // TODO g_debug_frames
   end;
 
 {$IFDEF ENABLE_SHELLS}
@@ -1095,8 +1155,97 @@ implementation
   end;
 {$ENDIF}
 
-  procedure r_Map_Draw (x, y, w, h, camx, camy: Integer);
-    var iter: TPanelGrid.Iter; p: PPanel; cx, cy, xx, yy, ww, hh: Integer;
+  procedure r_Map_CalcAspect (ow, oh, nw, nh: LongInt; horizontal: Boolean; out ww, hh: LongInt);
+  begin
+    if horizontal then
+    begin
+      ww := nw;
+      hh := nw * oh div ow;
+    end
+    else
+    begin
+      ww := nh * ow div oh;
+      hh := nh;
+    end;
+  end;
+
+  procedure r_Map_CalcSkyParallax (cx, cy, vw, vh, sw, sh, mw, mh: LongInt; out x, y, w, h: LongInt);
+    const
+      factor = 120; (* size ratio between view and sky (120%) *)
+      limit = 100;  (* max speed for parallax *)
+    var
+      msw, msh, mvw, mvh, svw, svh: LongInt;
+  begin
+    msw := vw * factor div 100;
+    msh := vh * factor div 100;
+    r_Map_CalcAspect(sw, sh, msw, msh, (sw / sh) <= (msw / msh), w, h);
+
+    (* calc x parallax or sky center on speed limit *)
+    mvw := MAX(1, mw - vw);
+    svw := w - vw;
+    if 100 * svw div mvw <= limit then
+      x := -cx * svw div mvw
+    else
+      x := -svw div 2;
+
+    (* calc y parallax or sky center on speed limit *)
+    mvh := MAX(1, mh - vh);
+    svh := h - vh;
+    if 100 * svh div mvh <= limit then
+      y := -cy * svh div mvh
+    else
+      y := -svh div 2;
+
+    (* handle out of map bounds *)
+    if x > 0 then x := 0;
+    if y > 0 then y := 0;
+    if x < -svw then x := -svw;
+    if y < -svh then y := -svh;
+  end;
+
+  procedure r_Map_DrawScreenEffect (x, y, w, h, level: Integer; r, g, b: Byte);
+    var i: Integer;
+  begin
+    if level > 0 then
+    begin
+      case level of
+        0..14: i := 0;
+        15..34: i := 1;
+        35..54: i := 2;
+        55..74: i := 3;
+        75..94: i := 4;
+      else i := 5
+      end;
+      r_Draw_FillRect(x, y, x + w, y + h, r, g, b, i * 50)
+    end;
+  end;
+
+  procedure r_Map_DrawScreenEffects (x, y, w, h: Integer; p: TPlayer);
+    var i: Integer;
+  begin
+    if p <> nil then
+    begin
+      r_Map_DrawScreenEffect(x, y, w, h, p.pain, 255, 0, 0);
+      r_Map_DrawScreenEffect(x, y, w, h, p.pickup, 150, 200, 150);
+      if (p.FMegaRulez[MR_INVUL] >= gTime) and (p.SpawnInvul < gTime) then
+      begin
+        if ((p.FMegaRulez[MR_INVUL] - gTime) > 2100) or not ODD((p.FMegaRulez[MR_INVUL] - gTime) div 300) then
+          r_Draw_InvertRect(x, y, x + w, y + h, 191, 191, 191, 255);
+      end;
+      if p.FMegaRulez[MR_SUIT] >= gTime then
+      begin
+        if ((p.FMegaRulez[MR_SUIT] - gTime) > 2100) or not ODD((p.FMegaRulez[MR_SUIT] - gTime) div 300) then
+          r_Draw_FillRect(x, y, x + w, y + h, 0, 96, 0, 55);
+      end;
+      if (p.Berserk >= 0) and (p.Berserk >= gTime) and (gFlash = 2) then
+      begin
+        r_Draw_FillRect(x, y, x + w, y + h, 255, 0, 0, 55);
+      end;
+    end;
+  end;
+
+  procedure r_Map_Draw (x, y, w, h, camx, camy: Integer; player: TPlayer);
+    var iter: TPanelGrid.Iter; p: PPanel; cx, cy, xx, yy, ww, hh: Integer; sx, sy, sw, sh: LongInt;
   begin
     cx := camx - w div 2;
     cy := camy - h div 2;
@@ -1105,8 +1254,25 @@ implementation
     ww := w;
     hh := h;
 
+    if g_dbg_ignore_bounds = false then
+    begin
+      if xx + ww > gMapInfo.Width then
+        xx := gMapInfo.Width - ww;
+      if yy + hh > gMapInfo.Height then
+        yy := gMapInfo.Height - hh;
+      if xx < 0 then
+        xx := 0;
+      if yy < 0 then
+        yy := 0;
+      cx := xx - x;
+      cy := yy - y;
+    end;
+
     if SkyTexture <> nil then
-      r_Draw_Texture(SkyTexture, x, y, w, h, false, 255, 255, 255, 255, false);
+    begin
+      r_Map_CalcSkyParallax(cx, cy, ww, hh, SkyTexture.width, SkyTexture.height, gMapInfo.Width, gMapInfo.Height, sx, sy, sw, sh);
+      r_Draw_Texture(SkyTexture, x + sx, y + sy, sw, sh, false, 255, 255, 255, 255, false);
+    end;
 
     plist.Clear;
     iter := mapGrid.ForEachInAABB(xx, yy, ww, hh, GridDrawableMask);
@@ -1124,7 +1290,7 @@ implementation
     {$IFDEF ENABLE_SHELLS}
       r_Map_DrawShells(xx, yy, ww, hh);
     {$ENDIF}
-    r_Map_DrawPlayers(xx, yy, ww, hh);
+    r_Map_DrawPlayers(xx, yy, ww, hh, player);
     {$IFDEF ENABLE_GIBS}
       r_Map_DrawGibs(xx, yy, ww, hh);
     {$ENDIF}
@@ -1144,7 +1310,15 @@ implementation
     r_Map_DrawPanelType(PANEL_ACID2);
     r_Map_DrawPanelType(PANEL_WATER);
     r_Map_DrawPanelType(PANEL_FORE);
+    // TODO draw monsters health bar
+    // TODO draw players health bar
+    // TODO draw players indicators
     glPopMatrix;
+
+    r_Map_DrawScreenEffects(x, y, w, h, player);
+
+    // TODO draw minimap (gShowMap)
+    // TODO draw g_debug_player
   end;
 
   procedure r_Map_Update;