The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
Is IMG_Init() necessary?
dandago


Joined: 30 Aug 2013
Posts: 34
Is IMG_Init() actually necessary? I noticed that IMG_Load() works even if I don't init.

The documentation link in the SDL_image 2.0 website points to the 1.2.8 docs, so that doesn't help.
Is IMG_Init() necessary?
Bob Rubbens
Guest

If you look at the source you'll see that a lot of stuff happens in the Init() call, as well as the separate init calls for various file formats. If I were you I'd just call it to avoid subtle bugs. Is there a reason to avoid calling it? Op 17 jan. 2016 6:09 p.m. schreef "dandago":
Quote:
Is IMG_Init() actually necessary? I noticed that IMG_Load() works even if I don't init.

The documentation link in the SDL_image 2.0 website points to the 1.2.8 docs, so that doesn't help.



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Is IMG_Init() necessary?
Jonny D


Joined: 12 Sep 2009
Posts: 932
It's not strictly necessary.  You can ignore it if you don't think you need it.  Things get initialized if they are used, which is already one good reason to have control over when that happens.

You can also use IMG_Init() to enforce image format support and treat failure as an error.

Jonny D





On Sun, Jan 17, 2016 at 1:01 PM, Bob Rubbens wrote:
Quote:

If you look at the source you'll see that a lot of stuff happens in the Init() call, as well as the separate init calls for various file formats. If I were you I'd just call it to avoid subtle bugs. Is there a reason to avoid calling it? Op 17 jan. 2016 6:09 p.m. schreef "dandago":
Quote:
Is IMG_Init() actually necessary? I noticed that IMG_Load() works even if I don't init.

The documentation link in the SDL_image 2.0 website points to the 1.2.8 docs, so that doesn't help.



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/




_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org



_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

dandago


Joined: 30 Aug 2013
Posts: 34
Let me give you some scenarios so that you can better understand why I'm asking.

Scenario 1: I know I need to load a PNG.

No problem. Just use IMG_Init(IMG_INIT_PNG) and be on your way.

Scenario 2: I know I need to load a GIF.

IMG_Init(IMG_INIT_... what? There's no init flag for this type (and for many others). So how should I initialize SDL_image exactly?

Scenario 3: I don't know what type I need to load. A filename is provided by the user so loading happens dynamically at runtime.

I think there's no point in initializing every known type if it "things get initialized if they are used", i.e. lazy initialization. I'd just call IMG_Load() without calling IMG_Init(), and then clean up with IMG_Quit().
Is IMG_Init() necessary?
MrOzBarry


Joined: 26 Jun 2010
Posts: 620
Are you after an animated GIF? That might be out of scope of SDL_image.

Otherwise, you're going to want to look at the mime type of the file, which usually ends up being a guessing game based on the file extension.  I'd recommend saying you only support PNG, TIF, and JPEG, and do some guess work when examining the file, something like (pseudo):


if (filename.extension in ['jpg', 'jpeg']) then IMG_Init(IMG_INIT_JPG);
if (filename.extension in ['png']) then IMG_Init(IMG_INIT_PNG);
// etc.


If you really need a lot of image types to be supported, there might be some other image libraries that do some of this guess work for you, and provide raw buffer access so you can import it into an SDL_Surface pointer.


On Mon, Jan 18, 2016 at 3:40 PM, dandago wrote:
Quote:
Let me give you some scenarios so that you can better understand why I'm asking.

Scenario 1: I know I need to load a PNG.

No problem. Just use IMG_Init(IMG_INIT_PNG) and be on your way.

Scenario 2: I know I need to load a GIF.

IMG_Init(IMG_INIT_... what? There's no init flag for this type (and for many others). So how should I initialize SDL_image exactly?

Scenario 3: I don't know what type I need to load. A filename is provided by the user so loading happens dynamically at runtime.

I think there's no point in initializing every known type if it "things get initialized if they are used", i.e. lazy initialization. I'd just call IMG_Load() without calling IMG_Init(), and then clean up with IMG_Quit().



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Is IMG_Init() necessary?
Sam Lantinga


Joined: 10 Sep 2009
Posts: 1765
It's totally safe to load your images without calling IMG_Init() and then just call IMG_Quit() when you're done. Each loader will automatically initialize itself if necessary at image load time.

On Sun, Jan 17, 2016 at 9:08 AM, dandago wrote:
Quote:
Is IMG_Init() actually necessary? I noticed that IMG_Load() works even if I don't init.

The documentation link in the SDL_image 2.0 website points to the 1.2.8 docs, so that doesn't help.



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/


_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org

Is IMG_Init() necessary?
Bob Rubbens
Guest

On 19 January 2016 at 06:08, Sam Lantinga wrote:
Quote:
It's totally safe to load your images without calling IMG_Init() and then just call IMG_Quit() when you're done. Each loader will automatically initialize itself if necessary at image load time.

On Sun, Jan 17, 2016 at 9:08 AM, dandago wrote:


Quote:
Is IMG_Init() actually necessary? I noticed that IMG_Load() works even if I don't init.

The documentation link in the SDL_image 2.0 website points to the 1.2.8 docs, so that doesn't help.



Daniel D'Agostino
http://gigi.nullneuron.net/gigilabs/




_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org





_______________________________________________
SDL mailing list

http://lists.libsdl.org/listinfo.cgi/sdl-libsdl.org




It's even in the docs:

"Note: If the image format loader requires initialization, it will attempt to do that the first time it is needed if you have not already called IMG_Init to load support for your image format."


Didn't know that, pretty neat.