DEADSOFTWARE

gl: draw shells
[d2df-sdl.git] / src / game / renders / opengl / r_map.pas
index 8438e2d35db92e44cf931e8c34881487d55f10e0..8c555bf57f4eb696b56508e953f65331eac99719 100644 (file)
@@ -17,6 +17,8 @@ unit r_map;
 
 interface
 
+  uses g_base; // TRectWH
+
   procedure r_Map_Initialize;
   procedure r_Map_Finalize;
 
@@ -26,7 +28,12 @@ interface
   procedure r_Map_LoadTextures;
   procedure r_Map_FreeTextures;
 
+{$IFDEF ENABLE_GFX}
   procedure r_Map_NewGFX (typ, x, y: Integer);
+{$ENDIF}
+{$IFDEF ENABLE_GIBS}
+  function r_Map_GetGibSize (m, i: Integer): TRectWH;
+{$ENDIF}
 
   procedure r_Map_Update;
 
@@ -43,11 +50,17 @@ implementation
     {$ENDIF}
     e_log,
     binheap, MAPDEF, utils,
-    g_options, g_textures, g_basic, g_base, g_phys,
+    g_options, g_textures, g_basic, g_phys,
     g_game, g_map, g_panel, g_items, g_monsters, g_playermodel, g_player, g_weapons,
     {$IFDEF ENABLE_CORPSES}
       g_corpses,
     {$ENDIF}
+    {$IFDEF ENABLE_SHELLS}
+      g_shells,
+    {$ENDIF}
+    {$IFDEF ENABLE_GIBS}
+      g_gibs,
+    {$ENDIF}
     {$IFDEF ENABLE_GFX}
       g_gfx,
     {$ENDIF}
@@ -83,6 +96,7 @@ implementation
     VILEFIRE_DX = 32;
     VILEFIRE_DY = 128;
 
+{$IFDEF ENABLE_GFX}
     GFXAnim: array [0..R_GFX_LAST] of record
       name: AnsiString;
       w, h: Integer;
@@ -111,6 +125,7 @@ implementation
       (name: 'SMOKE';       w: 32;  h: 32;  count: 10; back: false; speed: 3; rspeed: 0; alpha: 150), // transparent
       (name: 'FLAME';       w: 32;  h: 32;  count: 11; back: false; speed: 3; rspeed: 2; alpha: 0)    // random
     );
+{$ENDIF}
 
     ShotAnim: array [0..WEAPON_LAST] of record
       name: AnsiString;
@@ -146,6 +161,17 @@ implementation
       (name: 'BSKELFIRE';   w: 64; h: 64; count: 2)   // 26 SKEL_FIRE
     );
 
+{$IFDEF ENABLE_SHELLS}
+    ShellAnim: array [0..SHELL_LAST] of record
+      name: AnsiString;
+      dx, dy: Integer;
+    end = (
+      (name: 'EBULLET'; dx: 2; dy: 1), // 0 SHELL_BULLET
+      (name: 'ESHELL';  dx: 4; dy: 2), // 1 SHELL_SHELL
+      (name: 'ESHELL';  dx: 4; dy: 2)  // 2 SHELL_DBLSHELL
+    );
+{$ENDIF}
+
   type
     TBinHeapPanelDrawCmp = class
       public
@@ -175,19 +201,20 @@ implementation
       anim: array [TDirection, 0..A_LAST] of record
         base, mask: TGLMultiTexture;
       end;
-(*
-      {$IFDEF ENABLE_GIBS}
-        gibs: array of record
-          base, mask: TGLTexture;
-          rect: TRectWH;
-        end;
-      {$ENDIF}
-*)
+{$IFDEF ENABLE_GIBS}
+      gibs: record
+        base, mask: TGLTextureArray;
+        rect: TRectArray;
+      end;
+{$ENDIF}
     end;
 
     StubShotAnim: TAnimState;
     FlagAnim: TAnimState;
 
+{$IFDEF ENABLE_SHELLS}
+    ShellTextures: array [0..SHELL_LAST] of TGLTexture;
+{$ENDIF}
 {$IFDEF ENABLE_GFX}
     GFXTextures: array [0..R_GFX_LAST] of TGLMultiTexture;
     gfxlist: array of record
@@ -224,6 +251,8 @@ implementation
   procedure r_Map_LoadModel (i: Integer);
     var prefix: AnsiString; a: Integer; d: TDirection; m: ^TPlayerModelInfo;
   begin
+    ASSERT(i < Length(Models));
+    ASSERT(i < Length(PlayerModelsArray));
     m := @PlayerModelsArray[i];
     prefix := m.FileName + ':TEXTURES/';
     for d := TDirection.D_LEFT to TDirection.D_RIGHT do
@@ -238,18 +267,30 @@ implementation
           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);
       end
     end;
-(*
     {$IFDEF ENABLE_GIBS}
-      Models[i].gibs := nil;
+      Models[i].gibs.base := nil;
+      Models[i].gibs.mask := nil;
+      Models[i].gibs.rect := nil;
       if m.GibsCount > 0 then
       begin
-        SetLength(Models[i].gibs, m.GibsCount);
+        SetLength(Models[i].gibs.base, m.GibsCount);
+        SetLength(Models[i].gibs.mask, m.GibsCount);
+        SetLength(Models[i].gibs.rect, m.GibsCount);
+        for a := 0 to m.GibsCount - 1 do
+          Models[i].gibs.rect[a] := DefaultGibSize;
+        if r_Textures_LoadStreamFromFile(prefix + m.GibsResource, 32, 32, m.GibsCount, Models[i].gibs.base, Models[i].gibs.rect) then
+        begin
+          if r_Textures_LoadStreamFromFile(prefix + m.GibsMask, 32, 32, m.GibsCount, Models[i].gibs.mask, nil) then
+          begin
+            // 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;
 
-
   procedure r_Map_Load;
     const
       WeapName: array [0..WP_LAST] of AnsiString = ('', 'CSAW', 'HGUN', 'SG', 'SSG', 'MGUN', 'RKT', 'PLZ', 'BFG', 'SPL', 'FLM');
@@ -376,11 +417,24 @@ implementation
     FlagTextures[FLAG_RED]  := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/FLAGRED',  64, 64, 5, false);
     FlagTextures[FLAG_BLUE] := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/FLAGBLUE', 64, 64, 5, false);
     // FlagTextures[FLAG_DOM]  := r_Textures_LoadMultiFromFileAndInfo(GameWad + ':TEXTURES/FLAGDOM',  64, 64, 8, false);
+    // --------- shells --------- //
+    {$IFDEF ENABLE_SHELLS}
+      for i := 0 to SHELL_LAST do
+        ShellTextures[i] := r_Textures_LoadFromFile(GameWad + ':TEXTURES/' + ShellAnim[i].name);
+    {$ENDIF}
   end;
 
   procedure r_Map_Free;
     var i, j, k, a: Integer; d: TDirection;
   begin
+    {$IFDEF ENABLE_SHELLS}
+      for i := 0 to SHELL_LAST do
+      begin
+        if ShellTextures[i] <> nil then
+          ShellTextures[i].Free;
+        ShellTextures[i] := nil;
+      end;
+    {$ENDIF}
     for i := 0 to FLAG_LAST do
     begin
       if FlagTextures[i] <> nil then
@@ -750,6 +804,46 @@ implementation
           r_Map_DrawPlayer(gPlayers[i]);
   end;
 
+{$IFDEF ENABLE_GIBS}
+  function r_Map_GetGibSize (m, i: Integer): TRectWH;
+  begin
+    result := Models[m].gibs.rect[i];
+  end;
+
+  procedure r_Map_DrawGibs (x, y, w, h: Integer);
+    var i, fx, fy, m, id, rx, ry, ra: Integer; p: PObj; t: TGLTexture;
+  begin
+    if gGibs <> nil then
+    begin
+      for i := 0 to High(gGibs) do
+      begin
+        if gGibs[i].alive then
+        begin
+          p := @gGibs[i].Obj;
+          if g_Obj_Collide(x, y, w, h, p) then
+          begin
+            p.Lerp(gLerpFactor, fx, fy);
+            id := gGibs[i].GibID;
+            m := gGibs[i].ModelID;
+            t := Models[m].gibs.base[id];
+            if t <> nil then
+            begin
+              rx := p.Rect.X + p.Rect.Width div 2;
+              ry := p.Rect.Y + p.Rect.Height div 2;
+              ra := gGibs[i].RAngle;
+              r_Draw_TextureRepeatRotate(t, fx, fy, t.width, t.height, false, 255, 255, 255, 255, false, rx, ry, ra);
+              t := Models[m].gibs.mask[id];
+              if t <> nil then
+                r_Draw_TextureRepeatRotate(t, fx, fy, t.width, t.height, false, gGibs[i].Color.R, gGibs[i].Color.G, gGibs[i].Color.B, 255, false, rx, ry, ra);
+              // r_Draw_TextureRepeatRotate(nil, fx + p.Rect.X, fy + p.Rect.Y, p.Rect.Width, p.Rect.Height, false, 255, 255, 255, 255, false, p.Rect.Width div 2, p.Rect.Height div 2, ra);
+            end;
+          end;
+        end;
+      end;
+    end;
+  end;
+{$ENDIF}
+
 {$IFDEF ENABLE_CORPSES}
   procedure r_Map_DrawCorpses (x, y, w, h: Integer);
     var i, fX, fY: Integer; p: TCorpse;
@@ -943,6 +1037,36 @@ implementation
     end;
   end;
 
+{$IFDEF ENABLE_SHELLS}
+  procedure r_Map_DrawShells (x, y, w, h: Integer);
+    var i, fx, fy, typ: Integer; t: TGLTexture; p: PObj;
+  begin
+    if gShells <> nil then
+    begin
+      for i := 0 to High(gShells) do
+      begin
+        if gShells[i].alive then
+        begin
+          typ := gShells[i].SType;
+          if typ <= SHELL_LAST then
+          begin
+            p := @gShells[i].Obj;
+            if g_Obj_Collide(x, y, w, h, p) then
+            begin
+              t := ShellTextures[typ];
+              if t <> nil then
+              begin
+                p.Lerp(gLerpFactor, fx, fy);
+                r_Draw_TextureRepeatRotate(t, fx, fy, t.width, t.height, false, 255, 255, 255, 255, false, ShellAnim[typ].dx, ShellAnim[typ].dy, gShells[i].RAngle);
+              end;
+            end;
+          end;
+        end;
+      end;
+    end;
+  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;
   begin
@@ -969,9 +1093,13 @@ implementation
     r_Map_DrawPanelType(PANEL_STEP);
     r_Map_DrawItems(xx, yy, ww, hh, false);
     r_Map_DrawShots(xx, yy, ww, hh);
-    // TODO draw shells
+    {$IFDEF ENABLE_SHELLS}
+      r_Map_DrawShells(xx, yy, ww, hh);
+    {$ENDIF}
     r_Map_DrawPlayers(xx, yy, ww, hh);
-    // TODO draw gibs
+    {$IFDEF ENABLE_GIBS}
+      r_Map_DrawGibs(xx, yy, ww, hh);
+    {$ENDIF}
     {$IFDEF ENABLE_CORPSES}
       r_Map_DrawCorpses(xx, yy, ww, hh);
     {$ENDIF}