6dc53b307552084b2802cf55082e0a11a0ec3cc1
[nit.git] / lib / sdl2 / image.nit
1 # This file is part of NIT ( http://www.nitlanguage.org ).
2 #
3 # Copyright 2014 Alexis Laferrière <alexis.laf@xymus.net>
4 #
5 # Licensed under the Apache License, Version 2.0 (the "License");
6 # you may not use this file except in compliance with the License.
7 # You may obtain a copy of the License at
8 #
9 # http://www.apache.org/licenses/LICENSE-2.0
10 #
11 # Unless required by applicable law or agreed to in writing, software
12 # distributed under the License is distributed on an "AS IS" BASIS,
13 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 # See the License for the specific language governing permissions and
15 # limitations under the License.
16
17 # Services of the SDL_image 2.0 library
18 #
19 # Offers `SDLSurface::load` which supports more image formats than `sdl2::sdl2`
20 # alone: JPG, PNG, TIF, GIT, ICO and much more.
21 module image is
22 pkgconfig "sdl2"
23 c_linker_option "-lSDL2_image"
24 end
25
26 import sdl2
27
28 in "C" `{
29 #include <SDL2/SDL_image.h>
30 `}
31
32 redef class SDL
33 # Access to the global methods of `sdl2::image`
34 var img = new IMG is lazy
35 end
36
37 # Holds the global methods of `sdl2::image`
38 class IMG
39 # Get the `IMG` singleton
40 new do return once new IMG.internal
41
42 # TODO make this private and only called through `sys.sdl.img`
43 init internal do end
44
45 # Initialize the image library
46 fun initialize(flags: SDLImgInitFlags): SDLImgInitFlags `{
47 return IMG_Init(flags);
48 `}
49
50 # Finalize and clean up the image library
51 fun quit `{ IMG_Quit(); `}
52
53 # Get the latest image library error
54 fun error: NativeString `{ return (char*)IMG_GetError(); `}
55 end
56
57 redef extern class SDLSurface
58 # Load the image at `path` inferring its type from the file extension
59 new load(path: NativeString) `{ return IMG_Load(path); `}
60 end
61
62 # Flags from `sys.sdl.img.initialize`
63 extern class SDLImgInitFlags `{ int `}
64 # Get the default empty flag set
65 new `{ return 0; `}
66
67 # Add the JPG support to this flag set
68 fun jpg: SDLImgInitFlags `{ return recv | IMG_INIT_JPG; `}
69
70 # Add the PNG support to this flag set
71 fun png: SDLImgInitFlags `{ return recv | IMG_INIT_PNG; `}
72
73 # Add the TIF support to this flag set
74 fun tif: SDLImgInitFlags `{ return recv | IMG_INIT_TIF; `}
75
76 # Add the WEBP support to this flag set
77 fun webp: SDLImgInitFlags `{ return recv | IMG_INIT_WEBP; `}
78 end