DEADSOFTWARE

gl: implement screenshots
[d2df-sdl.git] / src / game / renders / opengl / r_render.pas
index e6aa258de895144c6148f43f61a4c59c3b44e06a..44ee5b61da1bdf57a5eb97d9c57113eb57f733f0 100644 (file)
@@ -39,13 +39,14 @@ interface
   procedure r_Render_LoadTextures;
   procedure r_Render_FreeTextures;
 
+  procedure r_Render_Reset;
   procedure r_Render_Update;
   procedure r_Render_Draw;
 
   procedure r_Render_Resize (w, h: Integer);
   procedure r_Render_Apply;
 
-  function r_Render_WriteScreenShot (filename: String): Boolean;
+  procedure r_Render_RequestScreenShot;
 
   {$IFDEF ENABLE_GIBS}
     function r_Render_GetGibRect (m, id: Integer): TRectWH;
@@ -82,11 +83,8 @@ interface
 implementation
 
   uses
-    {$IFDEF USE_GLES1}
-      GLES11,
-    {$ELSE}
-      GL, GLEXT,
-    {$ENDIF}
+    {$I ../../../nogl/noGLuses.inc}
+    Imaging, ImagingTypes, ImagingUtility, (* for screenshots *)
     {$IFDEF ENABLE_MENU}
       r_gui,
     {$ENDIF}
@@ -101,7 +99,7 @@ implementation
     {$ENDIF}
     SysUtils, Classes, Math,
     g_basic,
-    e_log, utils, wadreader, mapdef,
+    e_log, e_res, utils, wadreader, mapdef,
     g_game, g_map, g_panel, g_options, g_console, g_player, g_weapons, g_language, g_triggers, g_monsters,
     g_net, g_netmaster,
     r_draw, r_textures, r_fonts, r_common, r_console, r_map, r_loadscreen
@@ -119,6 +117,7 @@ implementation
     hudbflag, hudbflags, hudbflagd: TGLTexture;
 
     FPS, FPSCounter, FPSTime: LongWord;
+    TakeScreenShot: Boolean;
 
   procedure r_Render_LoadTextures;
   begin
@@ -205,11 +204,24 @@ implementation
     info.maximized := gRC_Maximized;
     info.major := 1;
     info.minor := 1;
-    info.profile := TGLProfile.Compat;
+    {$IFDEF USE_GLES1}
+      info.profile := TGLProfile.Common;
+    {$ELSE}
+      info.profile := TGLProfile.Compat;
+    {$ENDIF}
     result := info;
   end;
 {$ENDIF}
 
+  procedure r_Render_LogGLInfo;
+  begin
+    e_LogWritefln('GL Vendor: %s', [glGetString(GL_VENDOR)]);
+    e_LogWritefln('GL Renderer: %s', [glGetString(GL_RENDERER)]);
+    e_LogWritefln('GL Version: %s', [glGetString(GL_VERSION)]);
+    e_LogWritefln('GL Shaders: %s', [glGetString(GL_SHADING_LANGUAGE_VERSION)]);
+    e_LogWritefln('GL Extensions: %s', [glGetString(GL_EXTENSIONS)]);
+  end;
+
   procedure r_Render_Initialize;
   begin
     {$IFDEF ENABLE_SYSTEM}
@@ -217,6 +229,10 @@ implementation
         raise Exception.Create('Failed to set videomode on startup.');
       sys_EnableVSync(gVSync);
     {$ENDIF}
+    {$IFDEF NOGL_INIT}
+      nogl_Init;
+    {$ENDIF}
+    r_Render_LogGLInfo;
     r_LoadScreen_Initialize;
     r_Textures_Initialize;
     r_Console_Initialize;
@@ -229,6 +245,14 @@ implementation
     r_Console_Finalize;
     r_Textures_Finalize;
     r_LoadScreen_Finalize;
+    {$IFDEF NOGL_INIT}
+      nogl_Quit;
+    {$ENDIF}
+  end;
+
+  procedure r_Render_Reset;
+  begin
+    r_Map_Reset;
   end;
 
   procedure r_Render_Update;
@@ -457,33 +481,33 @@ implementation
     r_Draw_GetTextSize('W', stdfont, cw, ch);
     motdh := gScreenHeight - 49 - ch * b_Text_LineCount(slMOTD);
 
-    r_Draw_FillRect(16, 64, gScreenWidth - 16, motdh, 64, 64, 64, 145);
-    r_Draw_Rect(16, 64, gScreenWidth - 16, motdh, 255, 127, 0, 255);
+    (* window background *)
+    r_Draw_Rect(16, 64, gScreenWidth - 16, motdh + 1, 255, 127, 0, 255);
+    r_Draw_FillRect(16 + 1, 64 + 1, gScreenWidth - 16 - 1, motdh, 64, 64, 64, 145);
 
     r_Common_DrawText(_lc[I_NET_SLIST_HELP], gScreenWidth div 2, gScreenHeight - 8, 255, 255, 255, 255, stdfont, TBasePoint.BP_DOWN);
 
     if slMOTD <> '' then
     begin
-      r_Draw_FillRect(16, motdh, gScreenWidth - 16, gScreenHeight - 44, 64, 64, 64, 110);
       r_Draw_Rect(16, motdh, gScreenWidth - 16, gScreenHeight - 44, 255, 127, 0, 255);
+      r_Draw_FillRect(16 + 1, motdh + 1, gScreenWidth - 16 - 1, gScreenHeight - 44 - 1, 64, 64, 64, 145);
       r_Common_DrawFormatText(slMOTD, 20, motdh + 3, 255, stdfont, TBasePoint.BP_LEFTUP);
     end;
 
     if not slReadUrgent and (slUrgent <> '') then
     begin
-      r_Draw_FillRect(17, 65, gScreenWidth - 17, motdh - 1, 64, 64, 64, 127);
-      r_Draw_FillRect(scrx - 256, scry - 60, scrx + 256, scry + 60, 64, 64, 64, 127);
-      r_Draw_Rect(scrx - 256, scry - 60, scrx + 256, scry + 60, 255, 127, 0, 255);
-      r_Draw_FillRect(scrx - 256, scry - 40, scrx + 256, scry - 40, 255, 127, 0, 255);
+      r_Draw_Rect(scrx - 256, scry - 60, scrx + 256, scry + 60 + 1, 255, 127, 0, 255);
+      r_Draw_FillRect(scrx - 256 + 1, scry - 60 + 1, scrx + 256 - 1, scry + 60, 64, 64, 64, 127);
+      r_Draw_FillRect(scrx - 256, scry - 40, scrx + 256, scry - 40 + 1, 255, 127, 0, 255);
+      r_Draw_FillRect(scrx - 256, scry + 40, scrx + 256, scry + 40 + 1, 255, 127, 0, 255);
       r_Common_DrawText(_lc[I_NET_SLIST_URGENT], scrx, scry - 58, 255, 255, 255, 255, stdfont, TBasePoint.BP_UP);
-      r_Common_DrawFormatText(slUrgent, scrx - 253, scry - 38, 255, stdfont, TBasePoint.BP_LEFTUP);
       r_Common_DrawText(_lc[I_NET_SLIST_URGENT_CONT], scrx, scry + 41, 255, 255, 255, 255, stdfont, TBasePoint.BP_UP);
-      r_Draw_FillRect(scrx - 256, scry + 40, scrx + 256, scry + 40, 255, 127, 0, 255);
+      r_Common_DrawFormatText(slUrgent, scrx - 253, scry - 38, 255, stdfont, TBasePoint.BP_LEFTUP);
     end
     else if SL = nil then
     begin
-      r_Draw_FillRect(17, 65, gScreenWidth - 17, motdh - 1, 64, 64, 64, 127);
-      r_Draw_Rect(scrx - 192, scry - 10, scrx + 192, scry + 11, 255, 127, 0, 255);
+      r_Draw_Rect(scrx - 192, scry - 10, scrx + 192, scry + 10, 255, 127, 0, 255);
+      r_Draw_FillRect(scrx - 192 + 1, scry - 10 + 1, scrx + 192 - 1, scry + 10 - 1, 64, 64, 64, 145);
       r_Common_DrawText(slWaitStr, scrx, scry, 255, 255, 255, 255, stdfont, TBasePoint.BP_CENTER);
     end
     else
@@ -501,17 +525,20 @@ implementation
       mw := gScreenWidth - 188;
       mx := 16 + mw;
 
-      r_Draw_FillRect(16 + 1, sy, gScreenWidth - 16 - 1, sy + 40, 64, 64, 64, 255);
-      r_Draw_FillRect(16 + 1, sy, gScreenWidth - 16 - 1, sy, 205, 205, 205, 255);
-      r_Draw_FillRect(16 + 1, sy + 41, gScreenWidth - 16 - 1, sy + 41, 255, 255, 255, 255);
+      (* current selection *)
+      r_Draw_FillRect(16 + 1, sy + 1,      gScreenWidth - 16 - 1, sy + 1 + 40,     64, 64, 64, 255);
+      r_Draw_FillRect(16 + 1, sy,          gScreenWidth - 16 - 1, sy + 1,          255, 255, 255, 255);
+      r_Draw_FillRect(16 + 1, sy + 1 + 40, gScreenWidth - 16 - 1, sy + 1 + 40 + 1, 255, 255, 255, 255);
 
-      r_Draw_FillRect(16, 85, gScreenWidth - 16, 85, 255, 127, 0, 255);
-      r_Draw_FillRect(16, motdh - 20, gScreenWidth - 16, motdh - 20, 255, 127, 0, 255);
+      (* line separators for name/ping/mode.. & address/pasword *)
+      r_Draw_FillRect(16, 85, gScreenWidth - 16, 85 + 1, 255, 127, 0, 255);
+      r_Draw_FillRect(16, motdh - 20, gScreenWidth - 16, motdh - 20 + 1, 255, 127, 0, 255);
 
-      r_Draw_FillRect(mx - 70, 64, mx - 70, motdh, 255, 127, 0, 255);
-      r_Draw_FillRect(mx, 64, mx, motdh - 20, 255, 127, 0, 255);
-      r_Draw_FillRect(mx + 52, 64, mx + 52, motdh - 20, 255, 127, 0, 255);
-      r_Draw_FillRect(mx + 104, 64, mx + 104, motdh - 20, 255, 127, 0, 255);
+      (* column separators for name/ping/mode/players/version *)
+      r_Draw_FillRect(mx - 70,  64 + 1, mx - 70 + 1,  motdh, 255, 127, 0, 255);
+      r_Draw_FillRect(mx,       64 + 1, mx + 1,       motdh - 20, 255, 127, 0, 255);
+      r_Draw_FillRect(mx + 52,  64 + 1, mx + 52 + 1,  motdh - 20, 255, 127, 0, 255);
+      r_Draw_FillRect(mx + 104, 64 + 1, mx + 104 + 1, motdh - 20, 255, 127, 0, 255);
 
       r_Common_DrawText('NAME/MAP', 18, 68, 255, 127, 0, 255, stdfont, TBasePoint.BP_LEFTUP);
       r_Common_DrawText('PING', mx - 68, 68, 255, 127, 0, 255, stdfont, TBasePoint.BP_LEFTUP);
@@ -585,7 +612,7 @@ implementation
          INC(yy, ch);
 
          INC(yy, ch div 4);
-         r_Draw_FillRect(x, yy, x + w - 1, yy, r, g, b, 255);
+         r_Draw_FillRect(x, yy, x + w, yy + 1, r, g, b, 255);
          INC(yy, ch div 4);
 
          for i := 0 to players - 1 do
@@ -639,7 +666,7 @@ implementation
         end;
 
         // Player color
-        r_Draw_Rect(x, yy, x + 16 - 1, yy + 16 - 1, 192, 192, 192, 255);
+        r_Draw_Rect(x, yy, x + 16, yy + 16, 192, 192, 192, 255);
         r_Draw_FillRect(x + 1, yy + 1, x + 16 - 1, yy + 16 - 1, cs.PlayerStat[i].Color.R, cs.PlayerStat[i].Color.G, cs.PlayerStat[i].Color.B, 255);
         // Player name
         if gShowPIDs then s := Format('[%5d] %s', [cs.PlayerStat[i].UID, cs.PlayerStat[i].Name]) else s := cs.PlayerStat[i].Name;
@@ -668,8 +695,8 @@ implementation
   begin
     xoff := 0; yoff := 8;
     r_Draw_GetTextSize('W', stdfont, cw, ch);
-    r_Draw_FillRect(x, y, x + w - 1, y + h - 1, 64, 64, 64, 224);
-    r_Draw_Rect(x, y, x + w - 1, y + h - 1, 255, 127, 0, 255);
+    r_Draw_Rect(x, y, x + w, y + h, 255, 127, 0, 255);
+    r_Draw_FillRect(x + 1, y + 1, x + w - 1, y + h - 1, 64, 64, 64, 224);
 
     (* LINE 1 *)
 
@@ -968,8 +995,8 @@ implementation
     begin
       x0 := x + xx div scale;
       y0 := y + yy div scale;
-      x1 := x + (xx + ww - 1) div scale;
-      y1 := y + (yy + hh - 1) div scale;
+      x1 := x + (xx + ww) div scale;
+      y1 := y + (yy + hh) div scale;
       r_Draw_FillRect(x0, y0, x1, y1, r, g, b, alpha);
     end;
 
@@ -1036,7 +1063,7 @@ implementation
     end;
 
   begin
-    r_Draw_FillRect(x, y, (x + gMapInfo.Width - 1) div scale, (y + gMapInfo.Height - 1) div scale, 0, 0, 0, alpha);
+    r_Draw_FillRect(x, y, (x + gMapInfo.Width) div scale, (y + gMapInfo.Height) div scale, 0, 0, 0, alpha);
     DrawPanels(gSteps);
     DrawPanels(gLifts);
     DrawPanels(gWater);
@@ -1047,12 +1074,78 @@ implementation
     DrawPlayers;
   end;
 
+  function GetScreenShotName (AsStats: Boolean): AnsiString;
+    var dir, date: AnsiString;
+  begin
+    result := '';
+    dir := e_GetWriteableDir(ScreenshotDirs);
+    if dir <> '' then
+    begin
+      if AsStats then
+      begin
+        dir := e_CatPath(dir, 'stats'); (* TODO: use e_GetWriteableDir *)
+        result := e_CatPath(dir, StatFilename + '.png');
+      end
+      else
+      begin
+        DateTimeToString(date, 'yyyy-mm-dd-hh-nn-ss', Now());
+        result := e_CatPath(dir, 'screenshot-' + date + '.png');
+      end;
+    end;
+  end;
+
+  procedure SaveScreenShot (AsStats: Boolean);
+    var img: TImageData; typ: GLenum; ok: Boolean; fname: AnsiString;
+  begin
+    ok := false;
+    fname := GetScreenShotName(AsStats);
+    if fname <> '' then
+    begin
+      if (gWinSizeX > 0) and (gWinSizeY > 0) then
+      begin
+        Imaging.SetOption(ImagingPNGPreFilter, 5);
+        Imaging.SetOption(ImagingPNGCompressLevel, 5);
+        InitImage(img);
+        if NewImage(gWinSizeX, gWinSizeY, TImageFormat.ifA8R8G8B8, img) then
+        begin
+          {$IFDEF ENDIAN_LITTLE}
+            typ := GL_UNSIGNED_INT_8_8_8_8_REV;
+          {$ELSE}
+            typ := GL_UNSIGNED_INT_8_8_8_8;
+          {$ENDIF}
+          glReadPixels(0, 0, gWinSizeX, gWinSizeY, GL_BGRA, typ, img.bits);
+          if glGetError() = GL_NO_ERROR then
+          begin
+            if FlipImage(img) then
+            begin
+              ok := SaveImageToFile(fname, img);
+            end;
+          end;
+        end;
+        FreeImage(img);
+      end;
+    end;
+    if ok then
+      g_Console_Add(Format(_lc[I_CONSOLE_SCREENSHOT], [fname]))
+    else
+      g_Console_Add(Format(_lc[I_CONSOLE_ERROR_WRITE], [fname]));
+  end;
+
   procedure r_Render_Draw;
-    var p1, p2: TPlayer; time: LongWord;
+    var p1, p2: TPlayer; time: LongWord; pw, ph: Integer;
   begin
     if gExit = EXIT_QUIT then
       exit;
 
+    {$IFDEF ENABLE_SYSTEM}
+      (* hack: if r_pixel_scale changed, reset menu and other things *)
+      pw := Round(gWinSizeX / r_pixel_scale);
+      ph := Round(gWinSizeY / r_pixel_scale);
+      if (pw <> gScreenWidth) or (ph <> gScreenHeight) then
+        if assigned(sys_ScreenResize) then
+          sys_ScreenResize(gWinSizeX, gWinSizeY);
+    {$ENDIF}
+
     INC(FPSCounter);
     time := GetTickCount64();
     if time - FPSTime >= 1000 then
@@ -1065,7 +1158,7 @@ implementation
     r_Draw_Setup(gWinSizeX, gWinSizeY, gScreenWidth, gScreenHeight);
 
     glClearColor(0.0, 0.0, 0.0, 0.0);
-    glClear(GL_COLOR_BUFFER_BIT or GL_DEPTH_BUFFER_BIT);
+    glClear(GL_COLOR_BUFFER_BIT);
 
     p1 := nil;
     p2 := nil;
@@ -1145,7 +1238,7 @@ implementation
 
     if gPauseMain and gGameOn {$IFDEF ENABLE_MENU}and (g_ActiveWindow = nil){$ENDIF} then
     begin
-      r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, 105);
+      r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, 105);
       r_Common_DrawText(_lc[I_MENU_PAUSE], gScreenWidth div 2, gScreenHeight div 2, 255, 255, 255, 255, menufont, TBasePoint.BP_CENTER);
     end;
 
@@ -1154,11 +1247,18 @@ implementation
       // TODO F key handle
       case gState of
         STATE_NONE: (* do nothing *) ;
-        STATE_MENU: r_Common_DrawBackground(GameWad + ':TEXTURES/TITLE');
+        STATE_MENU:
+        begin
+          r_Common_DrawBackground(GameWad + ':TEXTURES/TITLE');
+          {$IFDEF ENABLE_MENU}
+            if g_ActiveWindow <> nil then
+              r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, 105);
+          {$ENDIF}
+        end;
         STATE_FOLD:
         begin
           if EndingGameCounter > 0 then
-            r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, MIN(MAX(255 - EndingGameCounter, 0), 255));
+            r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, MIN(MAX(255 - EndingGameCounter, 0), 255));
         end;
         STATE_INTERCUSTOM:
         begin
@@ -1174,14 +1274,14 @@ implementation
 
           {$IFDEF ENABLE_MENU}
             if g_ActiveWindow <> nil then
-              r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, 105);
+              r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, 105);
           {$ENDIF}
         end;
         STATE_INTERSINGLE, STATE_INTERTEXT, STATE_INTERPIC:
         begin
           if EndingGameCounter > 0 then
           begin
-            r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, MIN(MAX(255 - EndingGameCounter, 0), 255));
+            r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, MIN(MAX(255 - EndingGameCounter, 0), 255));
           end
           else
           begin
@@ -1189,7 +1289,7 @@ implementation
             r_Render_DrawSingleStats;
             {$IFDEF ENABLE_MENU}
               if g_ActiveWindow <> nil then
-                r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, 105);
+                r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, 105);
             {$ENDIF}
           end;
         end;
@@ -1201,13 +1301,13 @@ implementation
             r_Common_DrawBackground(GameWad + ':TEXTURES/' + _lc[I_TEXTURE_ENDPIC]);
           {$IFDEF ENABLE_MENU}
             if g_ActiveWindow <> nil then
-              r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, 105);
+              r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, 105);
           {$ENDIF}
         end;
         STATE_SLIST:
         begin
           r_Common_DrawBackground(GameWad + ':TEXTURES/TITLE');
-          r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, 105);
+          r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, 105);
           r_Render_DrawServerList(slCurrent, slTable);
         end;
       end;
@@ -1217,7 +1317,7 @@ implementation
       if g_ActiveWindow <> nil then
       begin
         if gGameOn then
-          r_Draw_FillRect(0, 0, gScreenWidth - 1, gScreenHeight - 1, 0, 0, 0, 105);
+          r_Draw_FillRect(0, 0, gScreenWidth, gScreenHeight, 0, 0, 0, 105);
         r_GUI_Draw_Window(g_ActiveWindow);
       end;
     {$ENDIF}
@@ -1244,9 +1344,23 @@ implementation
     {$ENDIF}
 
     {$IFDEF ENABLE_TOUCH}
+      glScalef(1 / r_pixel_scale, 1 / r_pixel_scale, 0);
       r_Touch_Draw;
     {$ENDIF}
 
+    if TakeScreenShot then
+    begin
+      SaveScreenShot(false);
+      TakeScreenShot := false;
+    end;
+
+    (* take stats screenshot immediately after the first frame of the stats showing *)
+    if gScreenshotStats and (StatShotDone = false) and (Length(CustomStat.PlayerStat) > 1) then
+    begin
+      SaveScreenShot(true);
+      StatShotDone := true;
+    end;
+
     sys_Repaint;
   end;
 
@@ -1267,14 +1381,16 @@ implementation
         e_LogWriteln('resolution changed')
       else
         e_LogWriteln('resolution not changed');
-      sys_EnableVSync(gVSync)
+      sys_EnableVSync(gVSync);
+    {$ENDIF}
+    {$IFDEF NOGL_INIT}
+      nogl_Init;
     {$ENDIF}
   end;
 
-  function r_Render_WriteScreenShot (filename: String): Boolean;
+  procedure r_Render_RequestScreenShot;
   begin
-    // TODO write screenshot file
-    Result := False;
+    TakeScreenShot := true;
   end;
 
 {$IFDEF ENABLE_GIBS}