From 5b22cba0f10d08e626cd5fae4430c9930f735e92 Mon Sep 17 00:00:00 2001 From: Sam Lantinga Date: Sat, 26 Sep 2009 09:09:53 +0000 Subject: [PATCH] Mason Wheeler - 2009-06-10 06:29:45 PDT * Added IMG_Init()/IMG_Quit() to prevent constantly loading and unloading DLLs --- CHANGES | 2 ++ IMG.c | 47 +++++++++++++++++++++++++++++++++++++++++++++++ IMG_jpg.c | 18 ++++++++++++++---- IMG_png.c | 18 ++++++++++++++---- IMG_tif.c | 18 ++++++++++++++---- SDL_image.h | 14 ++++++++++++++ 6 files changed, 105 insertions(+), 12 deletions(-) diff --git a/CHANGES b/CHANGES index 5cba5a2d..f11a768f 100644 --- a/CHANGES +++ b/CHANGES @@ -1,4 +1,6 @@ 1.2.8: +Mason Wheeler - 2009-06-10 06:29:45 PDT + * Added IMG_Init()/IMG_Quit() to prevent constantly loading and unloading DLLs Couriersud - Mon, 12 Jan 2009 17:21:13 -0800 * Added support for ICO and CUR image files Eric Wing - Fri, 2 Jan 2009 02:01:16 -0800 diff --git a/IMG.c b/IMG.c index 2cde547a..9ebd7769 100644 --- a/IMG.c +++ b/IMG.c @@ -60,6 +60,53 @@ const SDL_version *IMG_Linked_Version(void) return(&linked_version); } +extern int IMG_InitJPG(); +extern int IMG_QuitJPG(); +extern int IMG_InitPNG(); +extern int IMG_QuitPNG(); +extern int IMG_InitTIF(); +extern int IMG_QuitTIF(); + +static int initialized = 0; + +int IMG_Init(int flags) +{ + int result = 0; + + if ((flags & IMG_INIT_JPG) && !(initialized & IMG_INIT_JPG)) { + if (IMG_InitJPG() == 0) { + result |= IMG_INIT_JPG; + } + } + if ((flags & IMG_INIT_PNG) && !(initialized & IMG_INIT_PNG)) { + if (IMG_InitPNG() == 0) { + result |= IMG_INIT_PNG; + } + } + if ((flags & IMG_INIT_TIF) && !(initialized & IMG_INIT_TIF)) { + if (IMG_InitTIF() == 0) { + result |= IMG_INIT_TIF; + } + } + initialized |= result; + + return (result); +} + +void IMG_Quit() +{ + if (initialized & IMG_INIT_JPG) { + IMG_QuitJPG(); + } + if (initialized & IMG_INIT_PNG) { + IMG_QuitPNG(); + } + if (initialized & IMG_INIT_TIF) { + IMG_QuitTIF(); + } + initialized = 0; +} + #if !defined(__APPLE__) || defined(SDL_IMAGE_USE_COMMON_BACKEND) /* Load an image from a file */ SDL_Surface *IMG_Load(const char *file) diff --git a/IMG_jpg.c b/IMG_jpg.c index 3b6ac672..2aa59235 100644 --- a/IMG_jpg.c +++ b/IMG_jpg.c @@ -53,7 +53,7 @@ static struct { } lib; #ifdef LOAD_JPG_DYNAMIC -static int IMG_InitJPG() +int IMG_InitJPG() { if ( lib.loaded == 0 ) { lib.handle = SDL_LoadObject(LOAD_JPG_DYNAMIC); @@ -128,7 +128,7 @@ static int IMG_InitJPG() return 0; } -static void IMG_QuitJPG() +void IMG_QuitJPG() { if ( lib.loaded == 0 ) { return; @@ -139,7 +139,7 @@ static void IMG_QuitJPG() --lib.loaded; } #else -static int IMG_InitJPG() +int IMG_InitJPG() { if ( lib.loaded == 0 ) { lib.jpeg_calc_output_dimensions = jpeg_calc_output_dimensions; @@ -156,7 +156,7 @@ static int IMG_InitJPG() return 0; } -static void IMG_QuitJPG() +void IMG_QuitJPG() { if ( lib.loaded == 0 ) { return; @@ -465,6 +465,16 @@ SDL_Surface *IMG_LoadJPG_RW(SDL_RWops *src) #else +int IMG_InitJPG() +{ + IMG_SetError("JPEG images are not supported"); + return(-1); +} + +void IMG_QuitJPG() +{ +} + /* See if an image is contained in a data source */ int IMG_isJPG(SDL_RWops *src) { diff --git a/IMG_png.c b/IMG_png.c index 794b75f4..bf0deb9b 100644 --- a/IMG_png.c +++ b/IMG_png.c @@ -92,7 +92,7 @@ static struct { } lib; #ifdef LOAD_PNG_DYNAMIC -static int IMG_InitPNG() +int IMG_InitPNG() { if ( lib.loaded == 0 ) { lib.handle = SDL_LoadObject(LOAD_PNG_DYNAMIC); @@ -216,7 +216,7 @@ static int IMG_InitPNG() return 0; } -static void IMG_QuitPNG() +void IMG_QuitPNG() { if ( lib.loaded == 0 ) { return; @@ -227,7 +227,7 @@ static void IMG_QuitPNG() --lib.loaded; } #else -static int IMG_InitPNG() +int IMG_InitPNG() { if ( lib.loaded == 0 ) { lib.png_create_info_struct = png_create_info_struct; @@ -251,7 +251,7 @@ static int IMG_InitPNG() return 0; } -static void IMG_QuitPNG() +void IMG_QuitPNG() { if ( lib.loaded == 0 ) { return; @@ -507,6 +507,16 @@ SDL_Surface *IMG_LoadPNG_RW(SDL_RWops *src) #else +int IMG_InitPNG() +{ + IMG_SetError("PNG images are not supported"); + return(-1); +} + +void IMG_QuitPNG() +{ +} + /* See if an image is contained in a data source */ int IMG_isPNG(SDL_RWops *src) { diff --git a/IMG_tif.c b/IMG_tif.c index 17dc7719..da0d8078 100644 --- a/IMG_tif.c +++ b/IMG_tif.c @@ -41,7 +41,7 @@ static struct { } lib; #ifdef LOAD_TIF_DYNAMIC -static int IMG_InitTIF() +int IMG_InitTIF() { if ( lib.loaded == 0 ) { lib.handle = SDL_LoadObject(LOAD_TIF_DYNAMIC); @@ -88,7 +88,7 @@ static int IMG_InitTIF() return 0; } -static void IMG_QuitTIF() +void IMG_QuitTIF() { if ( lib.loaded == 0 ) { return; @@ -99,7 +99,7 @@ static void IMG_QuitTIF() --lib.loaded; } #else -static int IMG_InitTIF() +int IMG_InitTIF() { if ( lib.loaded == 0 ) { lib.TIFFClientOpen = TIFFClientOpen; @@ -112,7 +112,7 @@ static int IMG_InitTIF() return 0; } -static void IMG_QuitTIF() +void IMG_QuitTIF() { if ( lib.loaded == 0 ) { return; @@ -272,6 +272,16 @@ SDL_Surface* IMG_LoadTIF_RW(SDL_RWops* src) #else +int IMG_InitTIF() +{ + IMG_SetError("TIFF images are not supported"); + return(-1); +} + +void IMG_QuitTIF() +{ +} + /* See if an image is contained in a data source */ int IMG_isTIF(SDL_RWops *src) { diff --git a/SDL_image.h b/SDL_image.h index 318cdc0b..170095dd 100644 --- a/SDL_image.h +++ b/SDL_image.h @@ -56,6 +56,20 @@ extern "C" { */ extern DECLSPEC const SDL_version * SDLCALL IMG_Linked_Version(void); +typedef enum +{ + IMG_INIT_JPG = 0x00000001, + IMG_INIT_PNG = 0x00000002, + IMG_INIT_TIF = 0x00000004, +} IMG_InitFlags; + +/* Loads dynamic libraries and prepares them for use. Flags should be + one or more flags from IMG_InitFlags OR'd together */ +extern DECLSPEC int SDLCALL IMG_Init(int flags); + +/* Unloads libraries loaded with IMG_Init */ +extern DECLSPEC void SDLCALL IMG_Quit(); + /* Load an image from an SDL data source. The 'type' may be one of: "BMP", "GIF", "PNG", etc.