lib/sdl2: wrap the SDL 2 image library
authorAlexis Laferrière <alexis.laf@xymus.net>
Sat, 29 Nov 2014 16:57:14 +0000 (11:57 -0500)
committerAlexis Laferrière <alexis.laf@xymus.net>
Tue, 2 Dec 2014 15:33:03 +0000 (10:33 -0500)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/sdl2/image.nit [new file with mode: 0644]

diff --git a/lib/sdl2/image.nit b/lib/sdl2/image.nit
new file mode 100644 (file)
index 0000000..6dc53b3
--- /dev/null
@@ -0,0 +1,78 @@
+# This file is part of NIT ( http://www.nitlanguage.org ).
+#
+# Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
+#
+# Licensed under the Apache License, Version 2.0 (the "License");
+# you may not use this file except in compliance with the License.
+# You may obtain a copy of the License at
+#
+#     http://www.apache.org/licenses/LICENSE-2.0
+#
+# Unless required by applicable law or agreed to in writing, software
+# distributed under the License is distributed on an "AS IS" BASIS,
+# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+# See the License for the specific language governing permissions and
+# limitations under the License.
+
+# Services of the SDL_image 2.0 library
+#
+# Offers `SDLSurface::load` which supports more image formats than `sdl2::sdl2`
+# alone: JPG, PNG, TIF, GIT, ICO and much more.
+module image is
+       pkgconfig "sdl2"
+       c_linker_option "-lSDL2_image"
+end
+
+import sdl2
+
+in "C" `{
+       #include <SDL2/SDL_image.h>
+`}
+
+redef class SDL
+       # Access to the global methods of `sdl2::image`
+       var img = new IMG is lazy
+end
+
+# Holds the global methods of `sdl2::image`
+class IMG
+       # Get the `IMG` singleton
+       new do return once new IMG.internal
+
+       # TODO make this private and only called through `sys.sdl.img`
+       init internal do end
+
+       # Initialize the image library
+       fun initialize(flags: SDLImgInitFlags): SDLImgInitFlags `{
+               return IMG_Init(flags);
+       `}
+
+       # Finalize and clean up the image library
+       fun quit `{ IMG_Quit(); `}
+
+       # Get the latest image library error
+       fun error: NativeString `{ return (char*)IMG_GetError(); `}
+end
+
+redef extern class SDLSurface
+       # Load the image at `path` inferring its type from the file extension
+       new load(path: NativeString) `{ return IMG_Load(path); `}
+end
+
+# Flags from `sys.sdl.img.initialize`
+extern class SDLImgInitFlags `{ int `}
+       # Get the default empty flag set
+       new `{ return 0; `}
+
+       # Add the JPG support to this flag set
+       fun jpg: SDLImgInitFlags `{ return recv | IMG_INIT_JPG; `}
+
+       # Add the PNG support to this flag set
+       fun png: SDLImgInitFlags `{ return recv | IMG_INIT_PNG; `}
+
+       # Add the TIF support to this flag set
+       fun tif: SDLImgInitFlags `{ return recv | IMG_INIT_TIF; `}
+
+       # Add the WEBP support to this flag set
+       fun webp: SDLImgInitFlags `{ return recv | IMG_INIT_WEBP; `}
+end