DEADSOFTWARE

profiler
[d2df-sdl.git] / src / game / g_game.pas
index 8aa98c68cef328bdf3623d39f78f0863865aca12..f45ac3d7e55ae8284caabcf6987912b6bdb3c905 100644 (file)
  * You should have received a copy of the GNU General Public License
  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
  *)
-{$MODE DELPHI}
+{$INCLUDE ../shared/a_modes.inc}
 unit g_game;
 
 interface
 
 uses
   g_basic, g_player, e_graphics, Classes, g_res_downloader,
-  SysUtils, g_sound, g_gui, MAPSTRUCT, wadreader, md5;
+  SysUtils, g_sound, g_gui, MAPSTRUCT, wadreader, md5, xprofiler;
 
 type
   TGameSettings = record
@@ -124,6 +124,7 @@ procedure GameCVars(P: SArray);
 procedure GameCommands(P: SArray);
 procedure GameCheats(P: SArray);
 procedure DebugCommands(P: SArray);
+procedure ProfilerCommands(P: SArray);
 procedure g_Game_Process_Params;
 procedure g_Game_SetLoadingText(Text: String; Max: Integer; reWrite: Boolean);
 procedure g_Game_StepLoading();
@@ -309,6 +310,9 @@ var
   P1MoveButton: Byte = 0;
   P2MoveButton: Byte = 0;
 
+  g_profile_frame_update: Boolean = false;
+  g_profile_frame_draw: Boolean = true;
+
 procedure g_ResetDynlights ();
 procedure g_AddDynLight (x, y, radius: Integer; r, g, b, a: Single);
 procedure g_DynLightExplosion (x, y, radius: Integer; r, g, b: Single);
@@ -324,6 +328,8 @@ uses
   ENet, e_fixedbuffer, g_netmsg, g_netmaster, GL, GLExt,
   utils, sfs;
 
+
+// ////////////////////////////////////////////////////////////////////////// //
 type
   TDynLight = record
     x, y, radius: Integer;
@@ -341,6 +347,7 @@ procedure g_ResetDynlights ();
 var
   lnum, idx: Integer;
 begin
+  if not gwin_has_stencil then begin g_dynLightCount := 0; exit; end;
   lnum := 0;
   for idx := 0 to g_dynLightCount-1 do
   begin
@@ -367,6 +374,7 @@ end;
 
 procedure g_AddDynLight (x, y, radius: Integer; r, g, b, a: Single);
 begin
+  if not gwin_has_stencil then exit;
   if g_dynLightCount = length(g_dynLights) then SetLength(g_dynLights, g_dynLightCount+1024);
   g_dynLights[g_dynLightCount].x := x;
   g_dynLights[g_dynLightCount].y := y;
@@ -381,6 +389,7 @@ end;
 
 procedure g_DynLightExplosion (x, y, radius: Integer; r, g, b: Single);
 begin
+  if not gwin_has_stencil then exit;
   if g_dynLightCount = length(g_dynLights) then SetLength(g_dynLights, g_dynLightCount+1024);
   g_dynLights[g_dynLightCount].x := x;
   g_dynLights[g_dynLightCount].y := y;
@@ -394,6 +403,42 @@ begin
   Inc(g_dynLightCount);
 end;
 
+
+// ////////////////////////////////////////////////////////////////////////// //
+procedure drawProfiles (x, y: Integer; title: AnsiString);
+var
+  wdt, hgt: Integer;
+  yy: Integer;
+
+  procedure drawItems ();
+  begin
+    repeat
+      e_TextureFontPrintEx(x+2+4*xprofItDepth, yy, Format('%s: %d', [xprofItName, xprofItMicro]), gStdFont, 255, 255, 0, 1, false);
+      Inc(yy, 16+2);
+      if xprofItHasChildren then
+      begin
+        xprofItDive();
+        drawItems();
+        xprofItPop();
+      end;
+    until xprofItNext();
+  end;
+
+begin
+  // gScreenWidth
+  if not xprofItReset() then exit;
+  wdt := 256;
+  hgt := 16+2+xprofTotalCount*(16+2); // title, items
+  // background
+  e_DrawFillQuad(x, y, x+wdt-1, y+hgt-1, 255, 255, 255, 200, B_BLEND);
+  // title
+  e_TextureFontPrintEx(x+2, y+2, Format('%s: %d', [title, Integer(xprofTotalMicro)]), gStdFont, 255, 255, 0, 1, false);
+  yy := y+16+2;
+  //drawItems();
+end;
+
+
+// ////////////////////////////////////////////////////////////////////////// //
 type
   TEndCustomGameStat = record
     PlayerStat: TPlayerStatArray;
@@ -1361,16 +1406,19 @@ begin
   end;
 
   // HACK: add dynlight here
-  if e_KeyPressed(IK_F8) and gGameOn and (not gConsoleShow) and (g_ActiveWindow = nil) then
-  begin
-    g_playerLight := true;
-  end;
-  if e_KeyPressed(IK_F9) and gGameOn and (not gConsoleShow) and (g_ActiveWindow = nil) then
+  if gwin_k8_enable_light_experiments then
   begin
-    g_playerLight := false;
+    if e_KeyPressed(IK_F8) and gGameOn and (not gConsoleShow) and (g_ActiveWindow = nil) then
+    begin
+      g_playerLight := true;
+    end;
+    if e_KeyPressed(IK_F9) and gGameOn and (not gConsoleShow) and (g_ActiveWindow = nil) then
+    begin
+      g_playerLight := false;
+    end;
   end;
 
-  if (g_playerLight) then g_AddDynLight(plr.GameX+32, plr.GameY+40, 128, 1, 1, 0, 0.6);
+  if gwin_has_stencil and g_playerLight then g_AddDynLight(plr.GameX+32, plr.GameY+40, 128, 1, 1, 0, 0.6);
 end;
 
 procedure g_Game_Update();
@@ -2601,29 +2649,27 @@ begin
 
   glTranslatef(-x, -y, 0);
 
-  g_Map_BuildPVP(sX, sY, sX+sWidth-1, sY+sHeight-1);
-  g_Map_DrawPanels(PANEL_BACK);
-  g_Map_DrawPanels(PANEL_STEP);
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_BACK);
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_STEP);
   g_Items_Draw();
   g_Weapon_Draw();
   g_Player_DrawShells();
   g_Player_DrawAll();
   g_Player_DrawCorpses();
-  g_Map_DrawPanels(PANEL_WALL);
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_WALL);
   g_Monsters_Draw();
-  g_Map_DrawPanels(PANEL_CLOSEDOOR);
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_CLOSEDOOR);
   g_GFX_Draw();
   g_Map_DrawFlags();
-  g_Map_DrawPanels(PANEL_ACID1);
-  g_Map_DrawPanels(PANEL_ACID2);
-  g_Map_DrawPanels(PANEL_WATER);
-  g_Map_DrawPanels(PANEL_FORE);
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_ACID1);
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_ACID2);
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_WATER);
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_FORE);
   if g_debug_HealthBar then
   begin
     g_Monsters_DrawHealth();
     g_Player_DrawHealth();
   end;
-  g_Map_ResetPVP();
 
   glPopMatrix();
 end;
@@ -2634,7 +2680,6 @@ var
   //R: TRect;
   lln: Integer;
   lx, ly, lrad: Integer;
-  ltminx, ltminy, ltmaxx, ltmaxy: Integer;
 begin
   if (p = nil) or (p.FDummy) then
   begin
@@ -2711,7 +2756,9 @@ begin
     else
       d := Round((py-p.IncCam-(gPlayerScreenSize.Y div 2))/(gMapInfo.Height-gPlayerScreenSize.Y)*(gBackSize.Y-gPlayerScreenSize.Y));
 
+  xprofBeginSection('map background');
   g_Map_DrawBack(-c, -d);
+  xprofEndSection();
 
   sX := -a;
   sY := -(b+p.IncCam);
@@ -2720,109 +2767,148 @@ begin
 
   glTranslatef(a, b+p.IncCam, 0);
 
-  g_Map_BuildPVP(sX, sY, sX+sWidth-1, sY+sHeight-1);
+  xprofBeginSection('map rendering');
 
-  g_Map_DrawPanels(PANEL_BACK);
-  g_Map_DrawPanels(PANEL_STEP);
+  xprofBeginSection('panel_back');
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_BACK);
+  xprofEndSection();
+
+  xprofBeginSection('panel_step');
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_STEP);
+  xprofEndSection();
+
+  xprofBeginSection('items');
   g_Items_Draw();
+  xprofEndSection();
+
+  xprofBeginSection('weapons');
   g_Weapon_Draw();
+  xprofEndSection();
+
+  xprofBeginSection('shells');
   g_Player_DrawShells();
+  xprofEndSection();
+
+  xprofBeginSection('drawall');
   g_Player_DrawAll();
+  xprofEndSection();
+
+  xprofBeginSection('corpses');
   g_Player_DrawCorpses();
-  g_Map_DrawPanels(PANEL_WALL);
+  xprofEndSection();
+
+  xprofBeginSection('panel_wall');
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_WALL);
+  xprofEndSection();
+
+  xprofBeginSection('monsters');
   g_Monsters_Draw();
-  g_Map_DrawPanels(PANEL_CLOSEDOOR);
+  xprofEndSection();
+
+  xprofBeginSection('panel_closedoor');
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_CLOSEDOOR);
+  xprofEndSection();
+
+  xprofBeginSection('gfx');
   g_GFX_Draw();
+  xprofEndSection();
+
+  xprofBeginSection('flags');
   g_Map_DrawFlags();
-  g_Map_DrawPanels(PANEL_ACID1);
-  g_Map_DrawPanels(PANEL_ACID2);
-  g_Map_DrawPanels(PANEL_WATER);
+  xprofEndSection();
+
+  xprofBeginSection('panel_acid1');
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_ACID1);
+  xprofEndSection();
 
+  xprofBeginSection('panel_acid2');
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_ACID2);
+  xprofEndSection();
+
+  xprofBeginSection('panel_water');
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_WATER);
+  xprofEndSection();
+
+  //TODO: lights should be in separate grid, i think
+  //      but on the other side: grid may be slower for dynlights, as their lifetime is short
   if gwin_has_stencil and (g_dynLightCount > 0) then
   begin
-    // get light bounds
-    ltminx := $3fffffff;
-    ltminy := $3fffffff;
-    ltmaxx := -$3fffffff;
-    ltmaxy := -$3fffffff;
+    xprofBeginSection('dynlights');
+
+    // setup OpenGL parameters
+    glStencilMask($FFFFFFFF);
+    glStencilFunc(GL_ALWAYS, 0, $FFFFFFFF);
+    glEnable(GL_STENCIL_TEST);
+    glEnable(GL_SCISSOR_TEST);
+    glClear(GL_STENCIL_BUFFER_BIT);
+    glStencilFunc(GL_EQUAL, 0, $ff);
 
     for lln := 0 to g_dynLightCount-1 do
     begin
-      lx := g_dynLights[lln].x-sX;
-      ly := g_dynLights[lln].y-sY;
+      lx := g_dynLights[lln].x;
+      ly := g_dynLights[lln].y;
       lrad := g_dynLights[lln].radius;
       if lrad < 3 then continue;
 
-      if lx+lrad < 0 then continue;
-      if ly+lrad < 0 then continue;
-      if lx-lrad >= gPlayerScreenSize.X then continue;
-      if ly-lrad >= gPlayerScreenSize.Y then continue;
-
-      lx := lx+sX;
-      ly := ly+sY;
-
-      if ltminx > lx-lrad then ltminx := lx-lrad;
-      if ltminy > ly-lrad then ltminy := ly-lrad;
-      if ltmaxx < lx+lrad then ltmaxx := lx+lrad;
-      if ltmaxy < ly+lrad then ltmaxy := ly+lrad;
+      if lx-sX+lrad < 0 then continue;
+      if ly-sY+lrad < 0 then continue;
+      if lx-sX-lrad >= gPlayerScreenSize.X then continue;
+      if ly-sY-lrad >= gPlayerScreenSize.Y then continue;
+
+      // set scissor to optimize drawing
+      glScissor((lx-sX)-lrad+2, gPlayerScreenSize.Y-(ly-sY)-lrad-1+2, lrad*2-4, lrad*2-4);
+      // no need to clear stencil buffer, light blitting will do it for us
+      glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
+      // draw extruded panels
+      glDisable(GL_TEXTURE_2D);
+      glDisable(GL_BLEND);
+      glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // no need to modify color buffer
+      if (lrad > 4) then g_Map_DrawPanelShadowVolumes(lx, ly, lrad);
+      // render light texture
+      glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
+      glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); // draw light, and clear stencil buffer
+      // blend it
+      glEnable(GL_BLEND);
+      glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
+      glEnable(GL_TEXTURE_2D);
+      // color and opacity
+      glColor4f(g_dynLights[lln].r, g_dynLights[lln].g, g_dynLights[lln].b, g_dynLights[lln].a);
+      glBindTexture(GL_TEXTURE_2D, g_Texture_Light());
+      glBegin(GL_QUADS);
+        glTexCoord2f(0.0, 0.0); glVertex2i(lx-lrad, ly-lrad); // top-left
+        glTexCoord2f(1.0, 0.0); glVertex2i(lx+lrad, ly-lrad); // top-right
+        glTexCoord2f(1.0, 1.0); glVertex2i(lx+lrad, ly+lrad); // bottom-right
+        glTexCoord2f(0.0, 1.0); glVertex2i(lx-lrad, ly+lrad); // bottom-left
+      glEnd();
     end;
 
-    if g_Map_BuildPLP(ltminx, ltminy, ltmaxx, ltmaxy) then
-    begin
-      // setup OpenGL parameters
-      glStencilMask($FFFFFFFF);
-      glStencilFunc(GL_ALWAYS, 0, $FFFFFFFF);
-      glEnable(GL_STENCIL_TEST);
-      glEnable(GL_SCISSOR_TEST);
-      glClear(GL_STENCIL_BUFFER_BIT);
-      glStencilFunc(GL_EQUAL, 0, $ff);
-
-      for lln := 0 to g_dynLightCount-1 do
-      begin
-        lx := g_dynLights[lln].x;
-        ly := g_dynLights[lln].y;
-        lrad := g_dynLights[lln].radius;
-        if lrad < 3 then continue;
-        // set scissor to optimize drawing
-        glScissor((lx-sX)-lrad+2, gPlayerScreenSize.Y-(ly-sY)-lrad-1+2, lrad*2-4, lrad*2-4);
-        // no need to clear stencil buffer, light blitting will do it for us
-        glStencilOp(GL_KEEP, GL_KEEP, GL_INCR);
-        // draw extruded panels
-        glDisable(GL_TEXTURE_2D);
-        glDisable(GL_BLEND);
-        glColorMask(GL_FALSE, GL_FALSE, GL_FALSE, GL_FALSE); // no need to modify color buffer
-        if (lrad > 4) then g_Map_DrawPanelShadowVolumes(lx, ly, lrad);
-        // render light texture
-        glColorMask(GL_TRUE, GL_TRUE, GL_TRUE, GL_TRUE); // modify color buffer
-        glStencilOp(GL_ZERO, GL_ZERO, GL_ZERO); // draw light, and clear stencil buffer
-        // blend it
-        glEnable(GL_BLEND);
-        glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
-        glEnable(GL_TEXTURE_2D);
-        // color and opacity
-        glColor4f(g_dynLights[lln].r, g_dynLights[lln].g, g_dynLights[lln].b, g_dynLights[lln].a);
-        glBindTexture(GL_TEXTURE_2D, g_Texture_Light());
-        glBegin(GL_QUADS);
-          glTexCoord2f(0.0, 0.0); glVertex2i(lx-lrad, ly-lrad); // top-left
-          glTexCoord2f(1.0, 0.0); glVertex2i(lx+lrad, ly-lrad); // top-right
-          glTexCoord2f(1.0, 1.0); glVertex2i(lx+lrad, ly+lrad); // bottom-right
-          glTexCoord2f(0.0, 1.0); glVertex2i(lx-lrad, ly+lrad); // bottom-left
-        glEnd();
-      end;
+    // done
+    glDisable(GL_STENCIL_TEST);
+    glDisable(GL_BLEND);
+    glDisable(GL_SCISSOR_TEST);
+    glScissor(0, 0, sWidth, sHeight);
 
-      // done
-      glDisable(GL_STENCIL_TEST);
-      glDisable(GL_BLEND);
-      glDisable(GL_SCISSOR_TEST);
-      glScissor(0, 0, sWidth, sHeight);
-    end;
+    xprofEndSection();
+  end
+  else
+  begin
+    xprofBeginSection('dynlights');
+    xprofEndSection();
   end;
 
-  g_Map_DrawPanels(PANEL_FORE);
+  xprofBeginSection('panel_fore');
+  g_Map_DrawPanels(sX, sY, sWidth, sHeight, PANEL_FORE);
+  xprofEndSection();
+
   if g_debug_HealthBar then
   begin
+    xprofBeginSection('monster health');
     g_Monsters_DrawHealth();
+    xprofEndSection();
+
+    xprofBeginSection('player health');
     g_Player_DrawHealth();
+    xprofEndSection();
   end;
 
   if p.FSpectator then
@@ -2848,7 +2934,8 @@ begin
   }
 
   glPopMatrix();
-  g_Map_ResetPVP();
+
+  xprofEndSection(); // map rendering
 
   p.DrawPain();
   p.DrawPickup();
@@ -2880,6 +2967,8 @@ begin
     FPSTime := Time;
   end;
 
+  xprofBegin(g_profile_frame_draw);
+
   if gGameOn or (gState = STATE_FOLD) then
   begin
     if (gPlayer1 <> nil) and (gPlayer2 <> nil) then
@@ -3180,6 +3269,9 @@ begin
   e_TextureFontPrint(gScreenWidth-72, 0,
                      Format('%d:%.2d:%.2d', [gTime div 1000 div 3600, (gTime div 1000 div 60) mod 60, gTime div 1000 mod 60]),
                      gStdFont);
+
+  xprofEnd();
+  if g_profile_frame_draw then drawProfiles(0, 0, 'MAP RENDER');
 end;
 
 procedure g_Game_Quit();
@@ -4841,6 +4933,25 @@ begin
   end;
 end;
 
+// profiler console commands
+procedure ProfilerCommands (P: SArray);
+var
+  cmd: string;
+begin
+  //if not gDebugMode then exit;
+  cmd := LowerCase(P[0]);
+  if cmd = 'dpp' then
+  begin
+    g_profile_frame_draw := not g_profile_frame_draw;
+    exit;
+  end;
+  if cmd = 'dpu' then
+  begin
+    g_profile_frame_update := not g_profile_frame_update;
+    exit;
+  end;
+end;
+
 procedure DebugCommands(P: SArray);
 var
   a, b: Integer;