--- a/hedgewars/uMisc.pas Fri Sep 16 17:36:05 2011 +0200
+++ b/hedgewars/uMisc.pas Fri Sep 16 18:17:16 2011 +0200
@@ -25,14 +25,14 @@
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;
procedure freeModule;
implementation
-uses typinfo, sysutils, uVariables;
+uses typinfo, sysutils, uVariables, uUtils;
procedure movecursor(dx, dy: LongInt);
var x, y: LongInt;
@@ -45,10 +45,11 @@
SDL_WarpMouse(x, y);
end;
-
-procedure MakeScreenshot(filename: shortstring);
-var p: Pointer;
- size: Longword;
+// 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
head: array[0..53] of Byte = (
@@ -74,9 +75,16 @@
ScreenFadeValue:= sfMax;
ScreenFadeSpeed:= 5;
-size:= cScreenWidth * cScreenHeight * 3;
+size:= toPowerOf2(cScreenWidth) * toPowerOf2(cScreenHeight) * 3;
p:= GetMem(size);
+// memory could not be allocated
+if p = nil then
+begin
+ AddFileLog('Error: Could not allocate memory for screenshot.');
+ exit(false);
+end;
+
// update header information and file name
filename:= UserPathPrefix + '/Screenshots/' + filename + '.bmp';
@@ -102,7 +110,7 @@
//glReadBuffer(GL_FRONT);
glReadPixels(0, 0, cScreenWidth, cScreenHeight, GL_BGR, GL_UNSIGNED_BYTE, p);
-{$I-}
+{$IOCHECKS OFF}
Assign(f, filename);
Rewrite(f, 1);
if IOResult = 0 then
@@ -110,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;
-{$I+}
+{$IOCHECKS ON}
-FreeMem(p)
+FreeMem(p, size);
+
+MakeScreenshot:= success;
end;
// http://www.idevgames.com/forums/thread-5602-post-21860.html#pid21860