clean up image loading a bit. gets rid of useless "Passed NULL data source" or PhysFS file not found errors
authorsheepluva
Mon, 09 Jun 2014 20:14:11 +0200
changeset 10260 25155aaede77
parent 10259 c85d241d9cc9
child 10261 c81125782cf6
clean up image loading a bit. gets rid of useless "Passed NULL data source" or PhysFS file not found errors
hedgewars/uStore.pas
--- a/hedgewars/uStore.pas	Sat Jun 07 16:25:40 2014 +0200
+++ b/hedgewars/uStore.pas	Mon Jun 09 20:14:11 2014 +0200
@@ -599,17 +599,40 @@
 function LoadImage(const filename: shortstring; imageFlags: LongInt): PSDL_Surface;
 var tmpsurf: PSDL_Surface;
     s: shortstring;
+    rwops: PSDL_RWops;
 begin
     LoadImage:= nil;
     WriteToConsole(msgLoading + filename + '.png [flags: ' + inttostr(imageFlags) + '] ');
 
     s:= filename + '.png';
-    tmpsurf:= IMG_Load_RW(rwopsOpenRead(s), true);
+
+    rwops:= nil;
+    tmpsurf:= nil;
 
+    if pfsExists(s) then
+        begin
+        // get data source
+        rwops:= rwopsOpenRead(s);
+
+        // load image with SDL (with freesrc param set to true)
+        if rwops <> nil then
+            tmpsurf:= IMG_Load_RW(rwops, true);
+        end;
+
+    // loading failed
     if tmpsurf = nil then
         begin
+
+        // anounce that loading failed
         OutError(msgFailed, false);
-        SDLTry(false, (imageFlags and ifCritical) <> 0);
+
+        // output sdl error if loading failed when data source was available
+        if rwops <> nil then
+            begin
+            SDLTry(false, (imageFlags and ifCritical) <> 0);
+            // rwops was already freed by IMG_Load_RW
+            rwops:= nil;
+            end;
         exit;
         end;