screenshots: display a msg on failure and log causative error
authorsheepluva
Thu, 15 Sep 2011 10:11:38 +0200
changeset 5912 d31eba29e706
parent 5911 4a28d0ae9624
child 5913 1791f776b726
screenshots: display a msg on failure and log causative error
hedgewars/hwengine.pas
hedgewars/uMisc.pas
--- a/hedgewars/hwengine.pas	Thu Sep 15 09:19:56 2011 +0200
+++ b/hedgewars/hwengine.pas	Thu Sep 15 10:11:38 2011 +0200
@@ -111,10 +111,18 @@
     if flagMakeCapture then
     begin
         flagMakeCapture:= false;
+        {$IFNDEF IPHONEOS}
         s:= 'hw_' + FormatDateTime('YYYY-MM-DD_HH-mm-ss', Now()) + inttostr(GameTicks);
-        WriteLnToConsole('Saving ' + s + '...');
+
         playSound(sndShutter);
-        {$IFNDEF IPHONEOS}MakeScreenshot(s);{$ENDIF}
+        if not MakeScreenshot(s) then
+        begin
+            WriteLnToConsole('Screenshot failed.');
+            AddChatString(#5 + 'screen capture failed (lack of memory or write permissions)');
+        end
+        else
+            WriteLnToConsole('Screenshot saved: ' + s);
+        {$ENDIF}
     end;
 end;
 
--- a/hedgewars/uMisc.pas	Thu Sep 15 09:19:56 2011 +0200
+++ b/hedgewars/uMisc.pas	Thu Sep 15 10:11:38 2011 +0200
@@ -25,7 +25,7 @@
 
 procedure movecursor(dx, dy: LongInt);
 function  doSurfaceConversion(tmpsurf: PSDL_Surface): PSDL_Surface;
-procedure MakeScreenshot(filename: shortstring);
+function  MakeScreenshot(filename: shortstring): boolean;
 function  GetTeamStatString(p: PTeam): shortstring;
 
 procedure initModule;
@@ -45,9 +45,10 @@
 SDL_WarpMouse(x, y);
 end;
 
-
-procedure MakeScreenshot(filename: shortstring);
-var p: Pointer;
+// captures and saves the screen. returns true on success.
+function MakeScreenshot(filename: shortstring): Boolean;
+var success: boolean;
+    p: Pointer;
     size: QWord;
     f: file;
     // Windows Bitmap Header
@@ -79,7 +80,10 @@
 
 // memory could not be allocated
 if p = nil then
-    exit;
+begin
+    AddFileLog('Error: Could not allocate memory for screenshot.');
+    exit(false);
+end;
 
 // update header information and file name
 
@@ -114,10 +118,18 @@
     BlockWrite(f, head, sizeof(head));
     BlockWrite(f, p^, size);
     Close(f);
+    success:= true;
+    end
+else
+    begin
+    AddFileLog('Error: Could not write to ' + filename);
+    success:= false;
     end;
 {$IOCHECKS ON}
 
-FreeMem(p, size)
+FreeMem(p, size);
+
+MakeScreenshot:= success;
 end;
 
 // http://www.idevgames.com/forums/thread-5602-post-21860.html#pid21860