# 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();
return events
end
- private fun poll_event: nullable IE is extern import SDLKeyEvent, SDLMouseButtonEvent, SDLMouseMotionEvent, SDLQuitEvent, NativeString::to_s, SDLMouseButtonEvent as (nullable IE), SDLMouseMotionEvent as (nullable IE), SDLKeyEvent as (nullable IE), SDLQuitEvent as (nullable IE) `{
+ private fun poll_event: nullable IE is extern import SDLKeyEvent, SDLMouseButtonEvent, SDLMouseMotionEvent, SDLQuitEvent, NativeString.to_s, SDLMouseButtonEvent.as(nullable IE), SDLMouseMotionEvent.as(nullable IE), SDLKeyEvent.as(nullable IE), SDLQuitEvent.as(nullable IE) `{
SDL_Event event;
SDL_PumpEvents();
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
+# Basic Drawing figures
extern SDLDrawable in "C" `{SDL_Surface*`}
super Drawable
end
end
+# A drawable Image
extern SDLImage in "C" `{SDL_Surface*`} # TODO remove
super DrawableImage
super SDLDrawable
- new from_file( path: String ) is extern import String::to_cstring `{
+ # Import image from a file
+ new from_file( path: String ) is extern import String.to_cstring `{
SDL_Surface *image = IMG_Load( String_to_cstring( path ) );
return image;
`}
+
new partial( original: Image, clip: SDLRectangle ) is extern `{
return NULL;
`}
+ # 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 );
return new_image;
`}
+ # 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
redef fun destroy is extern `{ SDL_FreeSurface( recv ); `}
redef fun width: Int is extern `{ return recv->w; `}
fun is_ok: Bool do return true # TODO
end
+# A simple rectangle
extern SDLRectangle in "C" `{SDL_Rect*`}
+ # 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;
super InputEvent
end
+# MouseEvent class containing the cursor position
class SDLMouseEvent
super PointerEvent
super SDLInputEvent
end
end
+# MouseButtonEvent used to get information when a button is pressed/depressed
class SDLMouseButtonEvent
super SDLMouseEvent
end
end
+# MouseMotionEvent to get the cursor position when the mouse is moved
class SDLMouseMotionEvent
super SDLMouseEvent
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}"
end
+# SDLKeyEvent for when a key is pressed
class SDLKeyEvent
super KeyEvent
super SDLInputEvent
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
redef fun is_down do return down
+ # 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 true if the key is the down arrow
redef fun is_arrow_down do return key_name == "down"
+ # Return true if the key is the right arrow
redef fun is_arrow_right do return key_name == "right"
end
fun delay is extern `{ SDL_Delay( recv ); `}
end
+# Class to load and use TTF_Font
extern SDLFont in "C" `{TTF_Font *`}
- new ( name: String, points: Int ) is extern import String::to_cstring `{
+ # Load a font with a specified name and size
+ new ( name: String, points: Int ) is extern import String.to_cstring `{
char * cname = String_to_cstring( name );
TTF_Font *font = TTF_OpenFont( cname, (int)points);
fun destroy is extern `{ TTF_CloseFont( recv ); `}
- fun render( text: String, r, g, b: Int ): SDLImage is extern import String::to_cstring `{
+ # Create a String with the specified color, return an SDLImage
+ fun render( text: String, r, g, b: Int ): SDLImage is extern import String.to_cstring `{
SDL_Color color;
SDL_Surface *text_surface;
char *ctext;
return TTF_FontDescent( recv );
`}
- # Get the recommended pixel height of a rendered line of text of the loaded font. This is usually larger than the Font::height.
+ # Get the recommended pixel height of a rendered line of text of the loaded font. This is usually larger than the Font.height.
fun line_skip: Int is extern `{
return TTF_FontLineSkip( recv );
`}
+ # Return true is the font used fixed width for each char
fun is_fixed_width: Bool is extern `{
return TTF_FontFaceIsFixedWidth( recv );
`}
- fun family_name: nullable String is extern import String::to_cstring, String as nullable `{
+
+ # Return the family name of the font
+ fun family_name: nullable String is extern import String.to_cstring, String as nullable `{
char *fn = TTF_FontFaceFamilyName( recv );
if ( fn == NULL )
else
return String_as_nullable( NativeString_to_s( fn ) );
`}
- fun style_name: nullable String is extern import String::to_cstring, String as nullable `{
+
+ # Return the style name of the font
+ fun style_name: nullable String is extern import String.to_cstring, String as nullable `{
char *sn = TTF_FontFaceStyleName( recv );
if ( sn == NULL )
return String_as_nullable( NativeString_to_s( sn ) );
`}
- fun width_of( text: String ): Int is extern import NativeString::to_s `{
+ # Return the estimated width of a String if used with the current font
+ fun width_of( text: String ): Int is extern import NativeString.to_s `{
char *ctext = String_to_cstring( text );
int w;
if ( TTF_SizeText( recv, ctext, &w, NULL ) )
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