-# This file is free software, which comes along with NIT. This software is
-# distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
-# without even the implied warranty of MERCHANTABILITY or FITNESS FOR A
-# PARTICULAR PURPOSE. You can modify it is you want, provided this header
-# is kept unaltered, and a notification of the changes is added.
-# You are allowed to redistribute it and sell it, alone or is a part of
-# another product.
-
-# Binding to the SDL multomedia library
-package sdl
-
-universal SDL_Surface
-special Pointer
- meth width: Int is extern "sdl_surface_width"
- meth height: Int is extern "sdl_surface_height"
-
- meth lock_surface: Int is extern "SDL_LockSurface"
- meth unlock_surface: Int is extern "SDL_LockSurface"
-
- meth blit_on(dest: SDL_Surface) is extern "sdl_blit_surface"
- meth blit_on_xy(dest: SDL_Surface, dest_x: Int, dest_y: Int) is extern "sdl_blit_surface_xy"
-
- meth update_rect(x: Int, y: Int, w: Int, h: Int) is extern "SDL_UpdateRect"
- meth update_all
+# 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.
+
+# SDL display support (used in Linux for windows and inputes only)
+module sdl is
+ c_compiler_option(exec("sdl-config", "--cflags"))
+ c_linker_option(exec("sdl-config", "--libs"), "-lSDL_image -lSDL_ttf")
+end
+
+import mnit_display
+
+in "C header" `{
+ #include <unistd.h>
+ #include <SDL/SDL.h>
+ #include <SDL/SDL_syswm.h>
+ #include <SDL/SDL_image.h>
+ #include <SDL/SDL_ttf.h>
+`}
+
+# Represent a screen surface
+extern class SDLDisplay `{SDL_Surface *`}
+ super Display
+
+ redef type I: SDLImage
+
+ # Initialize a surface with width and height
+ new (w, h: Int) import enable_mouse_motion_events `{
+ SDL_Init(SDL_INIT_VIDEO);
+
+ if(TTF_Init()==-1) {
+ printf("TTF_Init: %s\n", TTF_GetError());
+ exit(2);
+ }
+
+ SDL_Surface *self = SDL_SetVideoMode(w, h, 24, SDL_HWSURFACE);
+
+ if (!SDLDisplay_enable_mouse_motion_events(self)) {
+ /* ignores mousemotion for performance reasons */
+ SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
+ }
+
+ return self;
+ `}
+
+ # Indicates wether we want the SDL mouse motion event (or only clicks).
+ # Disabled by defaut for performance reason. To activate, redef this method
+ # andd return true
+ fun enable_mouse_motion_events: Bool do return false
+
+ # Destroy the surface
+ fun destroy `{
+ if (SDL_WasInit(SDL_INIT_VIDEO))
+ SDL_Quit();
+
+ if (TTF_WasInit())
+ TTF_Quit();
+ `}
+
+ redef fun finish `{ SDL_Flip(recv); `}
+
+ # Clear the entire window with given RGB color (integer values)
+ fun clear_int(r, g, b: Int) `{
+ SDL_FillRect(recv, NULL, SDL_MapRGB(recv->format,r,g,b));
+ `}
+
+ redef fun width: Int `{ return recv->w; `}
+ redef fun height: Int `{ return recv->h; `}
+
+ # Fill a rectangle with given color
+ fun fill_rect(rect: SDLRectangle, r, g, b: Int) `{
+ SDL_FillRect(recv, rect, SDL_MapRGB(recv->format,r,g,b));
+ `}
+
+ redef fun clear(r, g, b: Float) `{
+ Uint8 ri, gi, bi;
+ ri = (Uint8)r*255;
+ gi = (Uint8)g*255;
+ bi = (Uint8)b*255;
+ SDL_FillRect(recv, NULL, SDL_MapRGB(recv->format,ri,gi,bi));
+ `}
+
+ # SDL events since the last call to this method
+ fun events: Sequence[SDLInputEvent]