839c2e12a98a14a19f85929dab09ed358ea62d52
1 # This file is part of NIT ( http://www.nitlanguage.org ).
3 # Copyright 2011-2013 Alexis Laferrière <alexis.laf@xymus.net>
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
9 # http://www.apache.org/licenses/LICENSE-2.0
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.
17 # SDL display support (used in Linux for windows and inputes only)
19 c_compiler_option
(exec
("sdl-config", "--cflags"))
20 c_linker_option
(exec
("sdl-config", "--libs"), "-lSDL_ttf")
30 #include <SDL/SDL_syswm.h>
31 #include <SDL/SDL_image.h>
32 #include <SDL/SDL_ttf.h>
35 # Represent a screen surface
36 extern SDLDisplay in "C" `{SDL_Surface *`}
39 redef type I: SDLImage
41 # Initialize a surface with width and height
42 new ( w, h: Int) is extern `{
43 SDL_Init(SDL_INIT_VIDEO);
46 printf
("TTF_Init: %s\n", TTF_GetError());
50 /* ignores mousemotion
for performance reasons
*/
51 SDL_EventState( SDL_MOUSEMOTION, SDL_IGNORE );
53 return SDL_SetVideoMode( w
, h
, 24, SDL_HWSURFACE );
57 fun destroy is extern `{
58 if ( SDL_WasInit( SDL_INIT_VIDEO ) )
65 redef fun finish is extern `{ SDL_Flip( recv ); `}
67 # Clear the entire window with given RGB color (integer values)
68 fun clear_int
( r
, g
, b
: Int ) is extern `{
69 SDL_FillRect( recv, NULL, SDL_MapRGB(recv->format,r,g,b) );
72 redef fun width
: Int is extern `{ return recv->w; `}
73 redef fun height: Int is extern `{ return recv->h; `}
75 # Fill a rectangle with given color
76 fun fill_rect
( rect
: SDLRectangle, r
, g
, b
: Int ) is extern `{
77 SDL_FillRect( recv, rect, SDL_MapRGB(recv->format,r,g,b) );
80 redef fun clear
( r
, g
, b
: Float ) is extern `{
85 SDL_FillRect( recv, NULL, SDL_MapRGB(recv->format,ri,gi,bi) );
88 fun events
: Sequence[ IE ]
90 var new_event
: nullable Object = null
91 var events
= new List[ IE ]
93 new_event
= poll_event
94 if new_event
!= null then # new_event isa Event then #
95 events
.add
( new_event
)
103 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) `{
108 if ( SDL_PollEvent(&event) )
110 switch (event.type ) {
114 printf("The \"%s\" key was pressed!\n",
115 SDL_GetKeyName(event.key.keysym.sym));
118 return SDLKeyEvent_as_nullable_InputEvent(
119 new_SDLKeyEvent( NativeString_to_s(
120 SDL_GetKeyName(event.key.keysym.sym) ),
121 event.type==SDL_KEYDOWN ) );
123 case SDL_MOUSEMOTION:
125 printf("Mouse moved by %d,%d to (%d,%d)\n",
126 event.motion.xrel, event.motion.yrel,
127 event.motion.x, event.motion.y);
130 return SDLMouseMotionEvent_as_nullable_InputEvent(
131 new_SDLMouseMotionEvent( event.motion.x, event.motion.y,
132 event.motion.xrel, event.motion.yrel ) );
134 case SDL_MOUSEBUTTONDOWN:
135 case SDL_MOUSEBUTTONUP:
137 printf("Mouse button \"%d\" pressed at (%d,%d)\n",
138 event.button.button, event.button.x, event.button.y);
140 return SDLMouseButtonEvent_as_nullable_InputEvent(
141 new_SDLMouseButtonEvent( event.button.x, event.button.y,
142 event.button.button, event.type == SDL_MOUSEBUTTONDOWN ) );
146 printf("Quit event\n");
148 return SDLQuitEvent_as_nullable_InputEvent( new_SDLQuitEvent() );
152 return null_InputEvent();
155 # Set the position of the cursor to x,y
156 fun warp_mouse
( x
,y
: Int ) `{ SDL_WarpMouse( x, y ); `}
158 # Show or hide the cursor
159 fun show_cursor( show: Bool ) `{ SDL_ShowCursor( show ); `}
162 # Basic Drawing figures
163 extern SDLDrawable in "C" `{SDL_Surface*`}
166 redef type I: SDLImage
168 redef fun blit( img, x, y ) is extern `{
175 SDL_BlitSurface( img
, NULL, recv
, &dst
);
178 redef fun blit_centered( img, x, y )
180 x = x - img.width / 2
181 y = y - img.height / 2
187 extern SDLImage in "C" `{SDL_Surface*`} # TODO remove
191 # Import image from a file
192 new from_file
( path
: String ) is extern import String.to_cstring
`{
193 SDL_Surface *image = IMG_Load( String_to_cstring( path ) );
198 new partial
( original
: Image, clip
: SDLRectangle ) is extern `{
202 # Copy of an existing SDLImage
203 new copy_of
( image
: SDLImage ) is extern `{
204 SDL_Surface *new_image = SDL_CreateRGBSurface( image->flags, image->w, image->h, 24,
212 SDL_BlitSurface( image, NULL, new_image, &dst );
217 # Save the image into the specified file
218 fun save_to_file
( path
: String ) is extern import String::to_cstring
`{ `}
220 # Destroy the image and free the memory
221 redef fun destroy is extern `{ SDL_FreeSurface( recv ); `}
223 redef fun width
: Int is extern `{ return recv->w; `}
224 redef fun height: Int is extern `{ return recv->h; `}
226 fun is_ok
: Bool do return true # TODO
230 extern SDLRectangle in "C" `{SDL_Rect*`}
231 # Constructor with x,y positions width and height of the rectangle
232 new ( x: Int, y: Int, w: Int, h: Int ) is extern `{
233 SDL_Rect *rect
= malloc
( sizeof
( SDL_Rect ) );
241 fun x=( v: Int ) is extern `{ recv->x = (Sint16)v; `}
242 fun x
: Int is extern `{ return recv->x; `}
244 fun y=( v: Int ) is extern `{ recv->y = (Sint16)v; `}
245 fun y
: Int is extern `{ return recv->y; `}
247 fun w=( v: Int ) is extern `{ recv->w = (Uint16)v; `}
248 fun w
: Int is extern `{ return recv->w; `}
250 fun h=( v: Int ) is extern `{ recv->h = (Uint16)v; `}
251 fun h
: Int is extern `{ return recv->h; `}
253 fun destroy is extern `{ `}
256 interface SDLInputEvent
260 # MouseEvent class containing the cursor position
268 private init ( x
, y
: Float )
275 # MouseButtonEvent used to get information when a button is pressed/depressed
276 class SDLMouseButtonEvent
281 redef var pressed
: Bool
282 redef fun depressed
: Bool do return not pressed
284 init ( x
, y
: Float, button
: Int, pressed
: Bool )
289 self.pressed
= pressed
295 return "MouseButtonEvent button {button} down at {x}, {y}"
297 return "MouseButtonEvent button {button} up at {x}, {y}"
302 # MouseMotionEvent to get the cursor position when the mouse is moved
303 class SDLMouseMotionEvent
309 init ( x
, y
, rel_x
, rel_y
: Float )
317 redef fun to_s
do return "MouseMotionEvent at {x}, {y}"
320 # SDLKeyEvent for when a key is pressed
328 init ( key_name
: String, down
: Bool )
330 self.key_name
= key_name
334 redef fun to_c
: nullable Char
336 if key_name
.length
== 1 then return key_name
.chars
.first
343 return "KeyboardEvent key {key_name} down"
345 return "KeyboardEvent key {key_name} up"
349 # Return true if the key is down, false otherwise
350 redef fun is_down
do return down
352 # Return true if the key is the up arrow
353 redef fun is_arrow_up
do return key_name
== "up"
354 # Return true if the key is the left arrow
355 redef fun is_arrow_left
do return key_name
== "left"
356 # Return true if the key is the down arrow
357 redef fun is_arrow_down
do return key_name
== "down"
358 # Return true if the key is the right arrow
359 redef fun is_arrow_right
do return key_name
== "right"
368 fun delay
is extern `{ SDL_Delay( recv ); `}
371 # Class to load and use TTF_Font
372 extern SDLFont in "C" `{TTF_Font *`}
373 # Load a font with a specified name and size
374 new ( name
: String, points
: Int ) is extern import String.to_cstring
`{
375 char * cname = String_to_cstring( name );
377 TTF_Font *font = TTF_OpenFont( cname, (int)points);
379 printf("TTF_OpenFont: %s\n", TTF_GetError());
386 fun destroy
is extern `{ TTF_CloseFont( recv ); `}
388 # Create a String with the specified color, return an SDLImage
389 fun render( text: String, r, g, b: Int ): SDLImage is extern import String.to_cstring `{
391 SDL_Surface *text_surface
;
398 ctext
= String_to_cstring( text
);
399 if( !(text_surface
=TTF_RenderText_Blended( recv
, ctext
, color
)) )
401 fprintf
( stderr
, "SDL TFF error: %s\n", TTF_GetError() );
408 # TODO reactivate fun below when updating libsdl_ttf to 2.0.10 or above
409 #fun outline: Int is extern # TODO check to make inline/nitside only
410 #fun outline=( v: Int ) is extern
412 #fun kerning: Bool is extern
413 #fun kerning=( v: Bool ) is extern
415 # Maximum pixel height of all glyphs of this font.
416 fun height: Int is extern `{
417 return TTF_FontHeight( recv
);
420 fun ascent: Int is extern `{
421 return TTF_FontAscent( recv
);
424 fun descent: Int is extern `{
425 return TTF_FontDescent( recv
);
428 # Get the recommended pixel height of a rendered line of text of the loaded font. This is usually larger than the Font.height.
429 fun line_skip: Int is extern `{
430 return TTF_FontLineSkip( recv
);
433 # Return true is the font used fixed width for each char
434 fun is_fixed_width: Bool is extern `{
435 return TTF_FontFaceIsFixedWidth( recv
);
438 # Return the family name of the font
439 fun family_name: nullable String is extern import String.to_cstring, String as nullable `{
440 char
*fn
= TTF_FontFaceFamilyName( recv
);
443 return null_String
();
445 return String_as_nullable( NativeString_to_s( fn
) );
448 # Return the style name of the font
449 fun style_name: nullable String is extern import String.to_cstring, String as nullable `{
450 char
*sn
= TTF_FontFaceStyleName( recv
);
453 return null_String
();
455 return String_as_nullable( NativeString_to_s( sn
) );
458 # Return the estimated width of a String if used with the current font
459 fun width_of( text: String ): Int is extern import NativeString.to_s `{
460 char
*ctext
= String_to_cstring( text
);
462 if ( TTF_SizeText( recv
, ctext
, &w
, NULL ) )
464 fprintf
( stderr
, "SDL TFF error: %s\n", TTF_GetError() );
472 # Information on the SDL window
473 # Used in other modules to get window handle
474 extern class SDLSystemWindowManagerInfo `{SDL_SysWMinfo *`}
477 SDL_SysWMinfo *val = malloc(sizeof(SDL_SysWMinfo));
479 SDL_VERSION(&val->version);
481 if(SDL_GetWMInfo(val) <= 0) {
482 printf("Unable to get window handle");
489 # Returns the handle of this window on a X11 window system
490 fun x11_window_handle
: Pointer `{
491 return (void*)recv->info.x11.window;