Implemented Warning, if LoadImage fails

Phil7

Active Member
Joined
Jun 26, 2017
Likes
61
#1
I would like to see the LoadImage() function giving an error message, if the loading fails.

Changes in graphics.cxs
Code:
Function LoadImage:Image( path$,frameCount=1,flags=Image.DefaultFlags )
    Local surf:=device.LoadSurface( FixDataPath(path) )
    If surf
        Return (New Image).Init(surf, frameCount, flags)
    Else
        DebugLog "Error - Unable to load image: " + path   
    EndIf
       
End
Some beginners including myself stumbled upon this because of a typo and now I use this modification while setting up a new folder structure for my assets.
 

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
Likes
85
#2
Thats something I have too; LoadImageSafe:Image(file:string)
except mine draws it off screen too, so that its loaded into Graphics memory ready for the game

Even though I have a function to test if a file is there, I'm not sure I would want an error in the Loadimage command. Without an error I could test for a NULL by design and then create an image. By adding an error check it removes functionality

In my latest project I load sounds in via a loop. If a sound file is not there I dont care, coz when I play the sounds I check for a NULL first.
 
Last edited:

MikeHart

Administrator
Staff member
Joined
Jun 19, 2017
Likes
427
Location
Germany
#3
Damn. Both make sense. But then Phil7's solution only throws an error in debug mode. I think that woukd be ok. What do you think @Rich ?
 

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
Likes
85
#6
can we have it so it only tests in debug too please? something like....
Code:
Local surf:=device.LoadSurface( FixDataPath(path) )
#if CONFIG="debug"
If surf
    Return (New Image).Init(surf, frameCount, flags)
Else
    DebugLog "Error - Unable to load image: " + path 
EndIf
#else
    Return (New Image).Init(surf, frameCount, flags)
#end
 

Phil7

Active Member
Joined
Jun 26, 2017
Likes
61
#7
May I ask for what reason?
The if surf test was already there in the original version.
I think it breaks in Init() method if surf is Null because there is no Null testing in that method.
 

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
Likes
85
#8
IMO..
In release mode, there should be no difference to the how the command works. Including doing a test. (I see this as overhead thats not required if no Log is output)
In debug, you are wanting to test and also accept an extra overhead and log if required.
 

Rich

Active Member
3rd Party Module Dev
Joined
Sep 9, 2017
Likes
85
#9
just re-ead your post...

i see, i didnt realise the original had an "if surf" already there. I thought all error checking was added by yourself
Code:
Function LoadImage:Image( path$,frameCount=1,flags=Image.DefaultFlags )
    Local surf:=device.LoadSurface( FixDataPath(path) )
    If surf Return (New Image).Init( surf,frameCount,flags )
End
Ignore me.
 
Top Bottom