DEADSOFTWARE

panels: handle panel animation in render (bump protocol)
[d2df-sdl.git] / src / game / renders / opengl / r_map.pas
index 4ffd6e18345e203525615484b677c7390e090f50..786bd5409728417dc9f342797999f857a85f94b7 100644 (file)
@@ -45,7 +45,7 @@ interface
 implementation
 
   uses
-    Math,
+    Math, SysUtils,
     {$IFDEF USE_GLES1}
       GLES11,
     {$ELSE}
@@ -67,7 +67,7 @@ implementation
     {$IFDEF ENABLE_GFX}
       g_gfx,
     {$ENDIF}
-    r_textures, r_draw
+    r_textures, r_draw, r_common
   ;
 
   const
@@ -177,35 +177,35 @@ implementation
     ShotAnim: array [0..WEAPON_LAST] of record
       name: AnsiString;
       w, h: Integer;
-      count: Integer;
+      anim: TAnimInfo;
     end = (
-      (name: '';            w: 0;  h: 0;  count: 0),  // 0  KASTET
-      (name: '';            w: 0;  h: 0;  count: 0),  // 1  SAW
-      (name: '';            w: 0;  h: 0;  count: 0),  // 2  PISTOL
-      (name: '';            w: 0;  h: 0;  count: 0),  // 3  SHOTGUN1
-      (name: '';            w: 0;  h: 0;  count: 0),  // 4  SHOTGUN2
-      (name: '';            w: 0;  h: 0;  count: 0),  // 5  CHAINGUN
-      (name: 'BROCKET';     w: 64; h: 32; count: 1),  // 6  ROCKETLAUNCHER
-      (name: 'BPLASMA';     w: 16; h: 16; count: 2),  // 7  PLASMA
-      (name: 'BBFG';        w: 64; h: 64; count: 2),  // 8  BFG
-      (name: '';            w: 0;  h: 0;  count: 0),  // 9  SUPERPULEMET
-      (name: 'FLAME';       w: 32; h: 32; count: 0{11}), // 10 FLAMETHROWER
-      (name: '';            w: 0;  h: 0;  count: 0),  // 11
-      (name: '';            w: 0;  h: 0;  count: 0),  // 12
-      (name: '';            w: 0;  h: 0;  count: 0),  // 13
-      (name: '';            w: 0;  h: 0;  count: 0),  // 14
-      (name: '';            w: 0;  h: 0;  count: 0),  // 15
-      (name: '';            w: 0;  h: 0;  count: 0),  // 16
-      (name: '';            w: 0;  h: 0;  count: 0),  // 17
-      (name: '';            w: 0;  h: 0;  count: 0),  // 18
-      (name: '';            w: 0;  h: 0;  count: 0),  // 19
-      (name: '';            w: 0;  h: 0;  count: 0),  // 20 ZOMPY_PISTOL
-      (name: 'BIMPFIRE';    w: 16; h: 16; count: 2),  // 21 IMP_FIRE
-      (name: 'BBSPFIRE';    w: 16; h: 16; count: 2),  // 22 BSP_FIRE
-      (name: 'BCACOFIRE';   w: 16; h: 16; count: 2),  // 23 CACO_FIRE
-      (name: 'BBARONFIRE';  w: 64; h: 16; count: 2),  // 24 BARON_FIRE
-      (name: 'BMANCUBFIRE'; w: 64; h: 32; count: 2),  // 25 MANCUB_FIRE
-      (name: 'BSKELFIRE';   w: 64; h: 64; count: 2)   // 26 SKEL_FIRE
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 0  KASTET
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 1  SAW
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 2  PISTOL
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 3  SHOTGUN1
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 4  SHOTGUN2
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 5  CHAINGUN
+      (name: 'BROCKET';     w: 64; h: 32; anim: (loop: true; delay: 1; frames: 1; back: false)),  // 6  ROCKETLAUNCHER
+      (name: 'BPLASMA';     w: 16; h: 16; anim: (loop: true; delay: 5; frames: 2; back: false)),  // 7  PLASMA
+      (name: 'BBFG';        w: 64; h: 64; anim: (loop: true; delay: 6; frames: 2; back: false)),  // 8  BFG
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 9  SUPERPULEMET
+      (name: 'FLAME';       w: 32; h: 32; anim: (loop: true; delay: 6; frames: 0{11}; back: false)), // 10 FLAMETHROWER
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 11
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 12
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 13
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 14
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 15
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 16
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 17
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 18
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 19
+      (name: '';            w: 0;  h: 0;  anim: (loop: true; delay: 0; frames: 0; back: false)),  // 20 ZOMPY_PISTOL
+      (name: 'BIMPFIRE';    w: 16; h: 16; anim: (loop: true; delay: 4; frames: 2; back: false)),  // 21 IMP_FIRE
+      (name: 'BBSPFIRE';    w: 16; h: 16; anim: (loop: true; delay: 4; frames: 2; back: false)),  // 22 BSP_FIRE
+      (name: 'BCACOFIRE';   w: 16; h: 16; anim: (loop: true; delay: 4; frames: 2; back: false)),  // 23 CACO_FIRE
+      (name: 'BBARONFIRE';  w: 64; h: 16; anim: (loop: true; delay: 4; frames: 2; back: false)),  // 24 BARON_FIRE
+      (name: 'BMANCUBFIRE'; w: 64; h: 32; anim: (loop: true; delay: 4; frames: 2; back: false)),  // 25 MANCUB_FIRE
+      (name: 'BSKELFIRE';   w: 64; h: 64; anim: (loop: true; delay: 5; frames: 2; back: false))   // 26 SKEL_FIRE
     );
 
 {$IFDEF ENABLE_SHELLS}
@@ -219,7 +219,9 @@ implementation
     );
 {$ENDIF}
 
+    PunchAnim: TAnimInfo = (loop: false; delay: 1; frames: 4; back: false);
     FlagAnim: TAnimInfo = (loop: true; delay: 8; frames: 5; back: false);
+    VileFireAnim: TAnimInfo = (loop: true; delay:  2; frames: 8; back: false);
 
   type
     TBinHeapPanelDrawCmp = class
@@ -236,6 +238,7 @@ implementation
     RenTextures: array of record
       spec: LongInt;
       tex: TGLMultiTexture;
+      anim: TAnimInfo;
     end;
     Items: array [0..ITEM_LAST] of record
       tex: TGLMultiTexture;
@@ -260,9 +263,6 @@ implementation
 {$ENDIF}
     end;
 
-    StubShotAnim: TAnimState; // TODO remove this hack
-    FlagFrame: LongInt;
-
 {$IFDEF ENABLE_SHELLS}
     ShellTextures: array [0..SHELL_LAST] of TGLTexture;
 {$ENDIF}
@@ -278,6 +278,7 @@ implementation
     end = nil;
 {$ENDIF}
 
+    FlagFrame: LongInt;
     plist: TBinHeapPanelDraw = nil;
 
   class function TBinHeapPanelDrawCmp.less (const a, b: TPanel): Boolean; inline;
@@ -289,7 +290,6 @@ implementation
 
   procedure r_Map_Initialize;
   begin
-    StubShotAnim := TAnimState.Create(true, 1, 1);
     FlagFrame := 0;
     plist := TBinHeapPanelDraw.Create();
   end;
@@ -298,7 +298,6 @@ implementation
   begin
     plist.Free;
     FlagFrame := 0;
-    StubShotAnim.Invalidate;
   end;
 
   procedure r_Map_FreeModel (i: Integer);
@@ -426,7 +425,7 @@ implementation
       for j := 0 to ANIM_LAST do
         for d := TDirection.D_LEFT to TDirection.D_RIGHT do
           r_Map_LoadMonsterAnim(i, j, d);
-    VileFire := r_Textures_LoadMultiFromFileAndInfo(GameWAD + ':TEXTURES/FIRE', 64, 128, 8, False, False);
+    VileFire := r_Textures_LoadMultiFromFileAndInfo(GameWAD + ':TEXTURES/FIRE', 64, 128, VileFireAnim.frames, VileFireAnim.back);
     // --------- player models --------- //
     if PlayerModelsArray <> nil then
     begin
@@ -447,8 +446,8 @@ implementation
     {$ENDIF}
     // --------- shots --------- //
     for i := 0 to WEAPON_LAST do
-      if ShotAnim[i].count > 0 then
-        ShotTextures[i] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/' + ShotAnim[i].name, ShotAnim[i].w, ShotAnim[i].h, ShotAnim[i].count, false);
+      if ShotAnim[i].anim.frames > 0 then
+        ShotTextures[i] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/' + ShotAnim[i].name, ShotAnim[i].w, ShotAnim[i].h, ShotAnim[i].anim.frames, ShotAnim[i].anim.back);
     // --------- flags --------- //
     FlagTextures[FLAG_NONE] := nil;
     FlagTextures[FLAG_RED]  := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/FLAGRED',  64, 64, 5, false);
@@ -463,7 +462,7 @@ implementation
     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);
+        PunchTextures[b, i] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':WEAPONS/' + PunchName[b] + WeapPos[i], 64, 64, PunchAnim.frames, PunchAnim.back);
     end;
     // --------- other --------- //
     InvulPenta := r_Textures_LoadFromFile(GameWad + ':TEXTURES/PENTA');
@@ -549,7 +548,8 @@ implementation
   end;
 
   procedure r_Map_LoadTextures;
-    var i, n: Integer;
+    const DefaultAnimInfo: TAnimInfo = (loop: true; delay: 1; frames: 1; back: false);
+    var i, n: Integer; txt: TAnimTextInfo;
   begin
     if Textures <> nil then
     begin
@@ -557,6 +557,7 @@ implementation
       SetLength(RenTextures, n);
       for i := 0 to n - 1 do
       begin
+        txt.anim := DefaultAnimInfo;
         RenTextures[i].tex := nil;
         case Textures[i].TextureName of
           TEXTURE_NAME_WATER: RenTextures[i].spec := TEXTURE_SPECIAL_WATER;
@@ -564,10 +565,13 @@ implementation
           TEXTURE_NAME_ACID2: RenTextures[i].spec := TEXTURE_SPECIAL_ACID2;
           else
             RenTextures[i].spec := 0;
-            RenTextures[i].tex := r_Textures_LoadMultiFromFile(Textures[i].FullName);
+            e_LogWritefln('r_Map_LoadTextures: begin load texture: %s', [Textures[i].FullName]);
+            RenTextures[i].tex := r_Textures_LoadMultiTextFromFile(Textures[i].FullName, txt);
+            e_LogWritefln('r_Map_LoadTextures: end load texture: %s', [Textures[i].FullName]);
             if RenTextures[i].tex = nil then
               e_LogWritefln('r_Map_LoadTextures: failed to load texture: %s', [Textures[i].FullName]);
         end;
+        RenTextures[i].anim := txt.anim;
       end;
     end;
     if gMapInfo.SkyFullName <> '' then
@@ -590,22 +594,29 @@ implementation
   end;
 
   procedure r_Map_DrawPanel (p: TPanel);
-    var Texture: Integer; t: TGLMultiTexture;
+    var Texture: Integer; t: TGLMultiTexture; tex: TGLTexture; count, frame: LongInt; a: TAnimInfo;
   begin
     ASSERT(p <> nil);
     if p.FCurTexture >= 0 then
     begin
       Texture := p.TextureIDs[p.FCurTexture].Texture;
       t := RenTextures[Texture].tex;
-
       if (RenTextures[Texture].spec = 0) or (t <> nil) then
       begin
-        if t = nil then
-          r_Draw_TextureRepeat(nil, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending)
-        else if p.TextureIDs[p.FCurTexture].AnTex.IsValid() then
-          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)
+        count := 0; frame := 0;
+        if p.AnimTime <= gTime then
+        begin
+          a := RenTextures[Texture].anim;
+          a.loop := p.AnimLoop;
+          g_Anim_GetFrameByTime(a, (gTime - p.AnimTime) DIV GAME_TICK, count, frame);
+        end;
+        if t <> nil then
+        begin
+          tex := t.GetTexture(frame);
+          r_Draw_TextureRepeat(tex, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending);
+        end
         else
-          r_Draw_TextureRepeat(t.GetTexture(0), p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending)
+          r_Draw_TextureRepeat(nil, p.x, p.y, p.width, p.height, false, 255, 255, 255, 255, false);
       end;
 
       if t = nil then
@@ -692,12 +703,16 @@ implementation
   end;
 
   procedure r_Map_DrawMonsterAttack (constref mon: TMonster);
-    var o: TObj;
+    var o: TObj; count, frame: LongInt; tex: TGLTexture;
   begin
     if VileFire <> nil then
       if (mon.MonsterType = MONSTER_VILE) and (mon.MonsterState = MONSTATE_SHOOT) then
-        if mon.VileFireAnim.IsValid() and GetPos(mon.MonsterTargetUID, @o) then
-          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);
+        if (mon.VileFireTime <= gTime) and GetPos(mon.MonsterTargetUID, @o) then
+        begin
+          g_Anim_GetFrameByTime(VileFireAnim, (gTime - mon.VileFireTime) DIV GAME_TICK, count, frame);
+          tex := VileFire.GetTexture(frame);
+          r_Draw_TextureRepeat(tex, o.x + o.rect.x + (o.rect.width div 2) - VILEFIRE_DX, o.y + o.rect.y + o.rect.height - VILEFIRE_DY, tex.width, tex.height, False, 255, 255, 255, 255, false);
+        end;
   end;
 
   procedure r_Map_DrawMonster (constref mon: TMonster);
@@ -819,7 +834,7 @@ implementation
   end;
 
   procedure r_Map_DrawPlayer (p, drawed: TPlayer);
-    var fX, fY, fSlope, ax, ay, w, h: Integer; b, flip: Boolean; t: TGLMultiTexture; alpha: Byte;
+    var fX, fY, fSlope, ax, ay, w, h: Integer; b, flip: Boolean; t: TGLMultiTexture; tex: TGLTexture; alpha: Byte; count, frame: LongInt;
   begin
     if p.alive then
     begin
@@ -829,21 +844,26 @@ implementation
       fSlope := nlerp(p.SlopeOld, p.obj.slopeUpLeft, gLerpFactor);
 
       (* punch effect *)
-      if p.PunchAnim.IsValid() and p.PunchAnim.enabled then
+      if p.PunchTime <= gTime 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
+        g_Anim_GetFrameByTime(PunchAnim, (gTime - p.PunchTime) DIV GAME_TICK, count, frame);
+        if count < 1 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)
+          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;
+            tex := t.GetTexture(frame);
+            r_Draw_TextureRepeat(tex, fx + ax, fy + fSlope + ay, tex.width, tex.height, flip, 255, 255, 255, 255, false)
+          end;
         end;
       end;
 
@@ -1075,7 +1095,7 @@ implementation
 {$ENDIF}
 
   procedure r_Map_DrawShots (x, y, w, h: Integer);
-    var i, a, fX, fY, pX, pY, typ: Integer; tex: TGLMultiTexture; anim: ^TAnimState;
+    var i, a, fX, fY, pX, pY, typ: Integer; count, frame: LongInt; t: TGLMultiTexture; tex: TGLTexture;
   begin
     if Shots <> nil then
     begin
@@ -1084,8 +1104,8 @@ implementation
         typ := Shots[i].ShotType;
         if typ <> 0 then
         begin
-          tex := ShotTextures[typ];
-          if tex <> nil then
+          t := ShotTextures[typ];
+          if t <> nil then
           begin
             a := 0;
             case typ of
@@ -1095,9 +1115,9 @@ implementation
             Shots[i].Obj.Lerp(gLerpFactor, fX, fY);
             pX := Shots[i].Obj.Rect.Width div 2;
             pY := Shots[i].Obj.Rect.Height div 2;
-            // TODO fix this hack
-            if Shots[i].Animation.IsValid() then anim := @Shots[i].Animation else anim := @StubShotAnim;
-            r_Draw_MultiTextureRepeatRotate(tex, anim^, fX, fY, tex.width, tex.height, false, 255, 255, 255, 255, false, pX, pY, a);
+            g_Anim_GetFrameByTime(ShotAnim[typ].anim, (gTime - Shots[i].time) DIV GAME_TICK, count, frame);
+            tex := t.GetTexture(frame);
+            r_Draw_TextureRepeatRotate(tex, fX, fY, tex.width, tex.height, false, 255, 255, 255, 255, false, pX, pY, a);
           end;
         end;
       end;