DEADSOFTWARE

gl: fix player model unload
[d2df-sdl.git] / src / game / renders / opengl / r_map.pas
index 5a0c7afed6f09af475718918d26e5bee9494ad14..2ca5f3ef193854bff9789eea2e4d9e37bc5528aa 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;
 
@@ -35,6 +42,7 @@ interface
 implementation
 
   uses
+    Math,
     {$IFDEF USE_GLES1}
       GLES11,
     {$ELSE}
@@ -42,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}
@@ -82,6 +96,55 @@ implementation
     VILEFIRE_DX = 32;
     VILEFIRE_DY = 128;
 
+    ItemAnim: array [0..ITEM_LAST] of record
+      name: AnsiString;
+      w, h: Integer;
+      d: Integer; // delay
+      n: Integer; // count
+      b: Boolean; // backanim
+    end = (
+      (name: '';             w: 0;  h: 0;  d: 0;  n: 0; b: False),
+      (name: 'MED1';         w: 16; h: 16; d: 0;  n: 1; b: False),
+      (name: 'MED2';         w: 32; h: 32; d: 0;  n: 1; b: False),
+      (name: 'BMED';         w: 32; h: 32; d: 0;  n: 1; b: False),
+      (name: 'ARMORGREEN';   w: 32; h: 16; d: 20; n: 3; b: True),
+      (name: 'ARMORBLUE';    w: 32; h: 16; d: 20; n: 3; b: True),
+      (name: 'SBLUE';        w: 32; h: 32; d: 15; n: 4; b: True),
+      (name: 'SWHITE';       w: 32; h: 32; d: 20; n: 4; b: True),
+      (name: 'SUIT';         w: 32; h: 64; d: 0;  n: 1; b: False),
+      (name: 'OXYGEN';       w: 16; h: 32; d: 0;  n: 1; b: False),
+      (name: 'INVUL';        w: 32; h: 32; d: 20; n: 4; b: True),
+      (name: 'SAW';          w: 64; h: 32; d: 0;  n: 1; b: False),
+      (name: 'SHOTGUN1';     w: 64; h: 16; d: 0;  n: 1; b: False),
+      (name: 'SHOTGUN2';     w: 64; h: 16; d: 0;  n: 1; b: False),
+      (name: 'MGUN';         w: 64; h: 16; d: 0;  n: 1; b: False),
+      (name: 'RLAUNCHER';    w: 64; h: 16; d: 0;  n: 1; b: False),
+      (name: 'PGUN';         w: 64; h: 16; d: 0;  n: 1; b: False),
+      (name: 'BFG';          w: 64; h: 64; d: 0;  n: 1; b: False),
+      (name: 'SPULEMET';     w: 64; h: 16; d: 0;  n: 1; b: False),
+      (name: 'CLIP';         w: 16; h: 16; d: 0;  n: 1; b: False),
+      (name: 'AMMO';         w: 32; h: 16; d: 0;  n: 1; b: False),
+      (name: 'SHELL1';       w: 16; h: 8;  d: 0;  n: 1; b: False),
+      (name: 'SHELL2';       w: 32; h: 16; d: 0;  n: 1; b: False),
+      (name: 'ROCKET';       w: 16; h: 32; d: 0;  n: 1; b: False),
+      (name: 'ROCKETS';      w: 64; h: 32; d: 0;  n: 1; b: False),
+      (name: 'CELL';         w: 16; h: 16; d: 0;  n: 1; b: False),
+      (name: 'CELL2';        w: 32; h: 32; d: 0;  n: 1; b: False),
+      (name: 'BPACK';        w: 32; h: 32; d: 0;  n: 1; b: False),
+      (name: 'KEYR';         w: 16; h: 16; d: 0;  n: 1; b: False),
+      (name: 'KEYG';         w: 16; h: 16; d: 0;  n: 1; b: False),
+      (name: 'KEYB';         w: 16; h: 16; d: 0;  n: 1; b: False),
+      (name: 'KASTET';       w: 64; h: 32; d: 0;  n: 1; b: False),
+      (name: 'PISTOL';       w: 64; h: 16; d: 0;  n: 1; b: False),
+      (name: 'BOTTLE';       w: 16; h: 32; d: 20; n: 4; b: True),
+      (name: 'HELMET';       w: 16; h: 16; d: 20; n: 4; b: True),
+      (name: 'JETPACK';      w: 32; h: 32; d: 15; n: 3; b: True),
+      (name: 'INVIS';        w: 32; h: 32; d: 20; n: 4; b: True),
+      (name: 'FLAMETHROWER'; w: 64; h: 32; d: 0;  n: 1; b: False),
+      (name: 'FUELCAN';      w: 16; h: 32; d: 0;  n: 1; b: False)
+    );
+
+{$IFDEF ENABLE_GFX}
     GFXAnim: array [0..R_GFX_LAST] of record
       name: AnsiString;
       w, h: Integer;
@@ -110,6 +173,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;
@@ -126,7 +190,7 @@ implementation
       (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: 11), // 10 FLAMETHROWER
+      (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
@@ -145,6 +209,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
@@ -161,30 +236,33 @@ implementation
       spec: LongInt;
       tex: TGLMultiTexture;
     end;
-    Items: array [0..ITEM_MAX] of record
+    Items: array [0..ITEM_LAST] of record
       tex: TGLMultiTexture;
       anim: TAnimState;
     end;
     MonTextures: array [0..MONSTER_MAN] of TMonsterAnims;
     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;
     VileFire: TGLMultiTexture;
     Models: array of record
       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
@@ -208,6 +286,7 @@ implementation
   procedure r_Map_Initialize;
   begin
     StubShotAnim := TAnimState.Create(true, 1, 1);
+    FlagAnim := TAnimState.Create(true, 8, 5);
     plist := TBinHeapPanelDraw.Create();
   end;
 
@@ -217,9 +296,40 @@ implementation
     StubShotAnim.Invalidate;
   end;
 
+  procedure r_Map_FreeModel (i: Integer);
+    var a: Integer; d: TDirection;
+  begin
+    for d := TDirection.D_LEFT to TDirection.D_RIGHT do
+    begin
+      for a := A_STAND to A_LAST do
+      begin
+        if Models[i].anim[d, a].base <> nil then
+          Models[i].anim[d, a].base.Free;
+        if Models[i].anim[d, a].mask <> nil then
+          Models[i].anim[d, a].mask.Free;
+        Models[i].anim[d, a].base := nil;
+        Models[i].anim[d, a].mask := nil;
+      end;
+    end;
+    {$IFDEF ENABLE_GIBS}
+      if Models[i].gibs.base <> nil then
+        for a := 0 to High(Models[i].gibs.base) do
+          Models[i].gibs.base[a].Free;
+      if Models[i].gibs.mask <> nil then
+        for a := 0 to High(Models[i].gibs.mask) do
+          Models[i].gibs.mask[a].Free;
+      Models[i].gibs.base := nil;
+      Models[i].gibs.mask := nil;
+      Models[i].gibs.rect := nil;
+    {$ENDIF}
+  end;
+
   procedure r_Map_LoadModel (i: Integer);
     var prefix: AnsiString; a: Integer; d: TDirection; m: ^TPlayerModelInfo;
   begin
+    ASSERT(i < Length(Models));
+    ASSERT(i < Length(PlayerModelsArray));
+    r_Map_FreeModel(i);
     m := @PlayerModelsArray[i];
     prefix := m.FileName + ':TEXTURES/';
     for d := TDirection.D_LEFT to TDirection.D_RIGHT do
@@ -234,17 +344,57 @@ 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_LoadMonsterAnim (m, a: Integer; d: TDirection);
+    const dir: array [TDirection] of AnsiString = ('_L', '');
+    var w, h, count: Integer;
+  begin
+    count := MONSTER_ANIMTABLE[m].AnimCount[a];
+    if count > 0 then
+    begin
+      w := MTABLE[m].w;
+      h := MTABLE[m].h;
+      if (m = MONSTER_SOUL) and (a = ANIM_DIE) then
+      begin
+        // special case
+        w := 128;
+        h := 128;
+      end;
+      MonTextures[m, a, d] := r_Textures_LoadMultiFromFileAndInfo(
+        GameWAD + ':MTEXTURES/' + MONSTERTABLE[m].name + '_' + ANIMTABLE[a].name + dir[d],
+        w,
+        h,
+        count,
+        False,
+        False
+      );
+    end
+    else
+      MonTextures[m, a, d] := nil;
+  end;
 
   procedure r_Map_Load;
     const
@@ -253,97 +403,29 @@ implementation
       WeapAct: array [0..W_ACT_LAST] of AnsiString = ('', '_FIRE');
     var
       i, j, k: Integer; d: TDirection;
-
-    procedure LoadItem (i: Integer; const name: AnsiString; w, h, delay, count: Integer; backanim: Boolean);
-    begin
-      ASSERT(i >= 0);
-      ASSERT(i <= ITEM_MAX);
-      Items[i].tex := r_Textures_LoadMultiFromFileAndInfo(GameWAD + ':TEXTURES/' + name, w, h, count, backanim, false);
-      if backanim then count := count * 2 - 2;
-      Items[i].anim := TAnimState.Create(True, delay, count);
-      ASSERT(Items[i].tex <> NIL);
-    end;
-
-    procedure LoadMonster (m, a: Integer; d: TDirection);
-      const
-        dir: array [TDirection] of AnsiString = ('_L', '');
-      var
-        w, h, count: Integer;
+  begin
+    // --------- items --------- //
+    for i := 0 to ITEM_LAST do
     begin
-      count := MONSTER_ANIMTABLE[m].AnimCount[a];
-      if count > 0 then
+      if ItemAnim[i].n > 0 then
       begin
-        w := MTABLE[m].w;
-        h := MTABLE[m].h;
-        if (m = MONSTER_SOUL) and (a = ANIM_DIE) then
-        begin
-          // special case
-          w := 128;
-          h := 128;
-        end;
-        MonTextures[m, a, d] := r_Textures_LoadMultiFromFileAndInfo(
-          GameWAD + ':MTEXTURES/' + MONSTERTABLE[m].name + '_' + ANIMTABLE[a].name + dir[d],
-          w,
-          h,
-          count,
-          False,
-          False
-        )
-      end
-      else
-        MonTextures[m, a, d] := nil
+        Items[i].tex := r_Textures_LoadMultiFromFileAndInfo(
+          GameWAD + ':TEXTURES/' + ItemAnim[i].name,
+          ItemAnim[i].w,
+          ItemAnim[i].h,
+          ItemAnim[i].n,
+          ItemAnim[i].b,
+          false
+        );
+        k := IfThen(ItemAnim[i].b, ItemAnim[i].n * 2 - 2, ItemAnim[i].n);
+        Items[i].anim := TAnimState.Create(True, ItemAnim[i].d, k);
+      end;
     end;
-
-  begin
-    // --------- items --------- //
-    //       i                            name           w    h    d  n  backanim
-    LoadItem(ITEM_NONE,                  'NOTEXTURE',    16,  16,  0, 1, False);
-    LoadItem(ITEM_MEDKIT_SMALL,          'MED1',         16,  16,  0, 1, False);
-    LoadItem(ITEM_MEDKIT_LARGE,          'MED2',         32,  32,  0, 1, False);
-    LoadItem(ITEM_MEDKIT_BLACK,          'BMED',         32,  32,  0, 1, False);
-    LoadItem(ITEM_ARMOR_GREEN,           'ARMORGREEN',   32,  16, 20, 3, True);
-    LoadItem(ITEM_ARMOR_BLUE,            'ARMORBLUE',    32,  16, 20, 3, True);
-    LoadItem(ITEM_SPHERE_BLUE,           'SBLUE',        32,  32, 15, 4, True);
-    LoadItem(ITEM_SPHERE_WHITE,          'SWHITE',       32,  32, 20, 4, True);
-    LoadItem(ITEM_SUIT,                  'SUIT',         32,  64,  0, 1, False);
-    LoadItem(ITEM_OXYGEN,                'OXYGEN',       16,  32,  0, 1, False);
-    LoadItem(ITEM_INVUL,                 'INVUL',        32,  32, 20, 4, True);
-    LoadItem(ITEM_WEAPON_SAW,            'SAW',          64,  32,  0, 1, False);
-    LoadItem(ITEM_WEAPON_SHOTGUN1,       'SHOTGUN1',     64,  16,  0, 1, False);
-    LoadItem(ITEM_WEAPON_SHOTGUN2,       'SHOTGUN2',     64,  16,  0, 1, False);
-    LoadItem(ITEM_WEAPON_CHAINGUN,       'MGUN',         64,  16,  0, 1, False);
-    LoadItem(ITEM_WEAPON_ROCKETLAUNCHER, 'RLAUNCHER',    64,  16,  0, 1, False);
-    LoadItem(ITEM_WEAPON_PLASMA,         'PGUN',         64,  16,  0, 1, False);
-    LoadItem(ITEM_WEAPON_BFG,            'BFG',          64,  64,  0, 1, False);
-    LoadItem(ITEM_WEAPON_SUPERPULEMET,   'SPULEMET',     64,  16,  0, 1, False);
-    LoadItem(ITEM_AMMO_BULLETS,          'CLIP',         16,  16,  0, 1, False);
-    LoadItem(ITEM_AMMO_BULLETS_BOX,      'AMMO',         32,  16,  0, 1, False);
-    LoadItem(ITEM_AMMO_SHELLS,           'SHELL1',       16,   8,  0, 1, False);
-    LoadItem(ITEM_AMMO_SHELLS_BOX,       'SHELL2',       32,  16,  0, 1, False);
-    LoadItem(ITEM_AMMO_ROCKET,           'ROCKET',       16,  32,  0, 1, False);
-    LoadItem(ITEM_AMMO_ROCKET_BOX,       'ROCKETS',      64,  32,  0, 1, False);
-    LoadItem(ITEM_AMMO_CELL,             'CELL',         16,  16,  0, 1, False);
-    LoadItem(ITEM_AMMO_CELL_BIG,         'CELL2',        32,  32,  0, 1, False);
-    LoadItem(ITEM_AMMO_BACKPACK,         'BPACK',        32,  32,  0, 1, False);
-    LoadItem(ITEM_KEY_RED,               'KEYR',         16,  16,  0, 1, False);
-    LoadItem(ITEM_KEY_GREEN,             'KEYG',         16,  16,  0, 1, False);
-    LoadItem(ITEM_KEY_BLUE,              'KEYB',         16,  16,  0, 1, False);
-    LoadItem(ITEM_WEAPON_KASTET,         'KASTET',       64,  32,  0, 1, False);
-    LoadItem(ITEM_WEAPON_PISTOL,         'PISTOL',       64,  16,  0, 1, False);
-    LoadItem(ITEM_BOTTLE,                'BOTTLE',       16,  32, 20, 4, True);
-    LoadItem(ITEM_HELMET,                'HELMET',       16,  16, 20, 4, True);
-    LoadItem(ITEM_JETPACK,               'JETPACK',      32,  32, 15, 3, True);
-    LoadItem(ITEM_INVIS,                 'INVIS',        32,  32, 20, 4, True);
-    LoadItem(ITEM_WEAPON_FLAMETHROWER,   'FLAMETHROWER', 64,  32,  0, 1, False);
-    LoadItem(ITEM_AMMO_FUELCAN,          'FUELCAN',      16,  32,  0, 1, False);
-    // fill with NOTEXURE forgotten item
-    for i := ITEM_AMMO_FUELCAN + 1 to ITEM_MAX do
-      LoadItem(i,'NOTEXTURE', 16, 16, 0, 1, False);
     // --------- monsters --------- //
     for i := MONSTER_DEMON to MONSTER_MAN do
       for j := 0 to ANIM_LAST do
         for d := TDirection.D_LEFT to TDirection.D_RIGHT do
-          LoadMonster(i, j, d);
+          r_Map_LoadMonsterAnim(i, j, d);
     VileFire := r_Textures_LoadMultiFromFileAndInfo(GameWAD + ':TEXTURES/FIRE', 64, 128, 8, False, False);
     // --------- player models --------- //
     if PlayerModelsArray <> nil then
@@ -367,11 +449,35 @@ implementation
     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);
+    // --------- flags --------- //
+    FlagTextures[FLAG_NONE] := nil;
+    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
+        FlagTextures[i].Free;
+      FlagTextures[i] := nil;
+    end;
     for i := 0 to WEAPON_LAST do
     begin
       if ShotTextures[i] <> nil then
@@ -399,18 +505,9 @@ implementation
         end;
       end;
     end;
-    for d := TDirection.D_LEFT to TDirection.D_RIGHT do
-    begin
-      for a := A_STAND to A_LAST do
-      begin
-        if Models[i].anim[d, a].base <> nil then
-          Models[i].anim[d, a].base.Free;
-        if Models[i].anim[d, a].mask <> nil then
-          Models[i].anim[d, a].mask.Free;
-        Models[i].anim[d, a].base := nil;
-        Models[i].anim[d, a].mask := nil;
-      end;
-    end;
+    if Models <> nil then
+      for i := 0 to High(Models) do
+        r_Map_FreeModel(i);
     for i := MONSTER_DEMON to MONSTER_MAN do
     begin
       for j := 0 to ANIM_LAST do
@@ -423,7 +520,7 @@ implementation
         end;
       end;
     end;
-    for i := 0 to ITEM_MAX do
+    for i := 0 to ITEM_LAST do
     begin
       if Items[i].tex <> nil then
       begin
@@ -486,13 +583,12 @@ implementation
 
       if (RenTextures[Texture].spec = 0) or (t <> nil) then
       begin
-        // TODO set alpha and blending type
         if t = nil then
-          r_Draw_TextureRepeat(nil, p.x, p.y, p.width, p.height, false)
+          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)
+          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)
         else
-          r_Draw_TextureRepeat(t.GetTexture(0), p.x, p.y, p.width, p.height, false)
+          r_Draw_TextureRepeat(t.GetTexture(0), p.x, p.y, p.width, p.height, false, 255, 255, 255, 255 - p.alpha, p.blending)
       end;
 
       if t = nil then
@@ -530,13 +626,13 @@ implementation
       for i := 0 to High(ggItems) do
       begin
         it := @ggItems[i];
-        if it.used and it.alive and (it.dropped = drop) and (it.ItemType <> ITEM_NONE) then
+        if it.used and it.alive and (it.dropped = drop) and (it.ItemType > ITEM_NONE) and (it.ItemType <= ITEM_LAST) then
         begin
           t := Items[it.ItemType].tex;
           if g_Collide(it.obj.x, it.obj.y, t.width, t.height, x, y, w, h) then
           begin
             it.obj.Lerp(gLerpFactor, fX, fY);
-            r_Draw_MultiTextureRepeat(t, Items[it.ItemType].anim, fX, fY, t.width, t.height, false);
+            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;
@@ -583,7 +679,7 @@ implementation
     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);
+          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);
   end;
 
   procedure r_Map_DrawMonster (constref mon: TMonster);
@@ -596,7 +692,7 @@ implementation
     mon.obj.Lerp(gLerpFactor, fX, fY);
 
     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);
+      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
@@ -636,11 +732,35 @@ implementation
   end;
 
   procedure r_Map_DrawPlayerModel (pm: TPlayerModel; x, y: Integer);
-    var a, pos, act, xx, yy: Integer; d: TDirection; flip: Boolean; t: TGLMultiTexture; tex: TGLTexture;
+    var a, pos, act, xx, yy, angle: Integer; d: TDirection; flip: Boolean; t: TGLMultiTexture; tex: TGLTexture; c: TRGB;
   begin
     a := pm.CurrentAnimation;
     d := pm.Direction;
-    // TODO draw flag
+
+    (* draw flag*)
+    t := FlagTextures[pm.Flag];
+    if (t <> nil) and not (a in [A_DIE1, A_DIE2])  then
+    begin
+      flip := d = TDirection.D_RIGHT;
+      angle := PlayerModelsArray[pm.id].FlagAngle;
+      xx := PlayerModelsArray[pm.id].FlagPoint.X;
+      yy := PlayerModelsArray[pm.id].FlagPoint.Y;
+      r_Draw_MultiTextureRepeatRotate(
+        t,
+        FlagAnim,
+        x + IfThen(flip, 2 * FLAG_BASEPOINT.X - xx + 1, xx - 1) - FLAG_BASEPOINT.X,
+        y + yy - FLAG_BASEPOINT.Y + 1,
+        t.width,
+        t.height,
+        flip,
+        255, 255, 255, 255, false,
+        IfThen(flip, 64 - FLAG_BASEPOINT.X, FLAG_BASEPOINT.X),
+        FLAG_BASEPOINT.Y,
+        IfThen(flip, angle, -angle)
+      );
+    end;
+
+    (* draw weapon *)
     if PlayerModelsArray[pm.id].HaveWeapon and not (a in [A_DIE1, A_DIE2, A_PAIN]) then
     begin
       case a of
@@ -663,18 +783,23 @@ implementation
           y + yy,
           tex.width,
           tex.height,
-          d = TDirection.D_LEFT
+          d = TDirection.D_LEFT,
+          255, 255, 255, 255, false
         );
       end;
     end;
+
+    (* draw body *)
     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);
-      // TODO colorize mask
+      r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip, 255, 255, 255, 255, false);
       t := Models[pm.id].anim[d, a].mask;
       if t <> nil then
-        r_Draw_MultiTextureRepeat(t, pm.AnimState, x, y, t.width, t.height, flip);
+      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);
+      end;
     end;
   end;
 
@@ -707,6 +832,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;
@@ -842,8 +1007,7 @@ implementation
           begin
             fx := nlerp(gfxlist[i].oldX, gfxlist[i].x, gLerpFactor);
             fy := nlerp(gfxlist[i].oldY, gfxlist[i].y, gLerpFactor);
-            // TODO set GFXAnim[typ].alpha
-            r_Draw_MultiTextureRepeat(tex, gfxlist[i].anim, fx, fy, tex.width, tex.height, false);
+            r_Draw_MultiTextureRepeat(tex, gfxlist[i].anim, fx, fy, tex.width, tex.height, false, 255, 255, 255, 255 - GFXAnim[typ].alpha, false);
           end;
         end;
       end;
@@ -861,11 +1025,9 @@ implementation
         typ := Shots[i].ShotType;
         if typ <> 0 then
         begin
-          e_logwritefln('draw shot %s typ %s', [i, typ]);
           tex := ShotTextures[typ];
           if tex <> nil then
           begin
-            e_logwritefln('draw shot %s typ %s <> nil', [i, typ]);
             a := 0;
             case typ of
               WEAPON_ROCKETLAUNCHER, WEAPON_BARON_FIRE, WEAPON_MANCUB_FIRE, WEAPON_SKEL_FIRE:
@@ -874,16 +1036,65 @@ 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
+            // TODO fix this hack
             if Shots[i].Animation.IsValid() then anim := @Shots[i].Animation else anim := @StubShotAnim;
-            // TODO: change angle and base point
-            r_Draw_MultiTextureRepeat(tex, anim^, fX, fY, tex.width, tex.height, false);
+            r_Draw_MultiTextureRepeatRotate(tex, anim^, fX, fY, tex.width, tex.height, false, 255, 255, 255, 255, false, pX, pY, a);
           end;
         end;
       end;
     end;
   end;
 
+  procedure r_Map_DrawFlags (x, y, w, h: Integer);
+    var i, dx, fx, fy: Integer; flip: Boolean; tex: TGLMultiTexture;
+  begin
+    if gGameSettings.GameMode = GM_CTF then
+    begin
+      for i := FLAG_RED to FLAG_BLUE do
+      begin
+        if not (gFlags[i].state in [FLAG_STATE_NONE, FLAG_STATE_CAPTURED]) then
+        begin
+          gFlags[i].Obj.Lerp(gLerpFactor, fx, fy);
+          flip := gFlags[i].Direction = TDirection.D_LEFT;
+          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;
+  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
@@ -895,7 +1106,7 @@ implementation
     hh := h;
 
     if SkyTexture <> nil then
-      r_Draw_Texture(SkyTexture, x, y, w, h, false);
+      r_Draw_Texture(SkyTexture, x, y, w, h, false, 255, 255, 255, 255, false);
 
     plist.Clear;
     iter := mapGrid.ForEachInAABB(xx, yy, ww, hh, GridDrawableMask);
@@ -910,9 +1121,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}
@@ -924,7 +1139,7 @@ implementation
       r_Map_DrawParticles(xx, yy, ww, hh);
       r_Map_DrawGFX(xx, yy, ww, hh);
     {$ENDIF}
-    // TODO draw flags
+    r_Map_DrawFlags(xx, yy, ww, hh);
     r_Map_DrawPanelType(PANEL_ACID1);
     r_Map_DrawPanelType(PANEL_ACID2);
     r_Map_DrawPanelType(PANEL_WATER);
@@ -935,9 +1150,10 @@ implementation
   procedure r_Map_Update;
     var i: Integer;
   begin
-    for i := 0 to ITEM_MAX do
+    for i := 0 to ITEM_LAST do
       Items[i].anim.Update;
     r_Map_UpdateGFX;
+    FlagAnim.Update;
   end;
 
 end.