# limitations under the License.
# SDL display support (used in Linux for windows and inputes only)
-module sdl
+module sdl is
+ c_compiler_option(exec("sdl-config", "--cflags"))
+ c_linker_option(exec("sdl-config", "--libs"), "-lSDL_ttf")
+end
import mnit # for
# import display
redef type I: SDLImage
- # Initialize a surface with width and height
- new ( w, h: Int) is extern `{
+ # Initialize a surface with width and height
+ new ( w, h: Int) is extern import enable_mouse_motion_events `{
SDL_Init(SDL_INIT_VIDEO);
if(TTF_Init()==-1) {
exit(2);
}
- /* ignores mousemotion for performance reasons */
- SDL_EventState( SDL_MOUSEMOTION, SDL_IGNORE );
+ 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 SDL_SetVideoMode( w, h, 24, SDL_HWSURFACE );
+ return self;
`}
- # Destroy the surface
+ # 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 is extern `{
if ( SDL_WasInit( SDL_INIT_VIDEO ) )
SDL_Quit();
SDL_GetKeyName(event.key.keysym.sym));
#endif
- return SDLKeyEvent_as_nullable_InputEvent(
+ return SDLKeyEvent_as_nullable_IE(
new_SDLKeyEvent( NativeString_to_s(
SDL_GetKeyName(event.key.keysym.sym) ),
event.type==SDL_KEYDOWN ) );
event.motion.x, event.motion.y);
#endif
- return SDLMouseMotionEvent_as_nullable_InputEvent(
+ return SDLMouseMotionEvent_as_nullable_IE(
new_SDLMouseMotionEvent( event.motion.x, event.motion.y,
- event.motion.xrel, event.motion.yrel ) );
+ event.motion.xrel, event.motion.yrel, SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) );
case SDL_MOUSEBUTTONDOWN:
case SDL_MOUSEBUTTONUP:
printf("Mouse button \"%d\" pressed at (%d,%d)\n",
event.button.button, event.button.x, event.button.y);
#endif
- return SDLMouseButtonEvent_as_nullable_InputEvent(
+ return SDLMouseButtonEvent_as_nullable_IE(
new_SDLMouseButtonEvent( event.button.x, event.button.y,
event.button.button, event.type == SDL_MOUSEBUTTONDOWN ) );
#ifdef DEBUG
printf("Quit event\n");
#endif
- return SDLQuitEvent_as_nullable_InputEvent( new_SDLQuitEvent() );
+ return SDLQuitEvent_as_nullable_IE( new_SDLQuitEvent() );
}
}
# Set the position of the cursor to x,y
fun warp_mouse( x,y: Int ) `{ SDL_WarpMouse( x, y ); `}
- # Show or hide the cursor
+ # Show or hide the cursor
fun show_cursor( show: Bool ) `{ SDL_ShowCursor( show ); `}
end
return NULL;
`}
- # Copy of an existing SDLImage
+ # Copy of an existing SDLImage
new copy_of( image: SDLImage ) is extern `{
SDL_Surface *new_image = SDL_CreateRGBSurface( image->flags, image->w, image->h, 24,
0, 0, 0, 0 );
# Save the image into the specified file
fun save_to_file( path: String ) is extern import String::to_cstring `{ `}
- # Destroy the image and free the memory
+ # Destroy the image and free the memory
redef fun destroy is extern `{ SDL_FreeSurface( recv ); `}
redef fun width: Int is extern `{ return recv->w; `}
# A simple rectangle
extern SDLRectangle in "C" `{SDL_Rect*`}
- # Constructor with x,y positions width and height of the rectangle
+ # Constructor with x,y positions width and height of the rectangle
new ( x: Int, y: Int, w: Int, h: Int ) is extern `{
SDL_Rect *rect = malloc( sizeof( SDL_Rect ) );
rect->x = (Sint16)x;
var rel_x: Float
var rel_y: Float
- init ( x, y, rel_x, rel_y: Float )
+ redef var pressed: Bool
+ redef fun depressed: Bool do return not pressed
+
+ init ( x, y, rel_x, rel_y: Float, pressed: Bool )
do
super( x, y )
self.rel_x = rel_x
self.rel_y = rel_y
+ self.pressed = pressed
end
redef fun to_s do return "MouseMotionEvent at {x}, {y}"
redef fun to_c: nullable Char
do
- if key_name.length == 1 then return key_name.first
+ if key_name.length == 1 then return key_name.chars.first
return null
end
end
end
- # Return true if the key is down, false otherwise
+ # Return true if the key is down, false otherwise
redef fun is_down do return down
- # Return true if the key is the up arrow
+ # Return true if the key is the up arrow
redef fun is_arrow_up do return key_name == "up"
# Return true if the key is the left arrow
redef fun is_arrow_left do return key_name == "left"
return TTF_FontLineSkip( recv );
`}
- # Return true is the font used fixed width for each char
+ # Return true is the font used fixed width for each char
fun is_fixed_width: Bool is extern `{
return TTF_FontFaceIsFixedWidth( recv );
`}
return w;
`}
end
+
+# Information on the SDL window
+# Used in other modules to get window handle
+extern class SDLSystemWindowManagerInfo `{SDL_SysWMinfo *`}
+
+ new `{
+ SDL_SysWMinfo *val = malloc(sizeof(SDL_SysWMinfo));
+
+ SDL_VERSION(&val->version);
+
+ if(SDL_GetWMInfo(val) <= 0) {
+ printf("Unable to get window handle");
+ return 0;
+ }
+
+ return val;
+ `}
+
+ # Returns the handle of this window on a X11 window system
+ fun x11_window_handle: Pointer `{
+ return (void*)recv->info.x11.window;
+ `}
+end