f09af5859033018d2da87d77199d2ddafbbd2486
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)
27 #include <SDL/SDL_syswm.h>
28 #include <SDL/SDL_image.h>
29 #include <SDL/SDL_ttf.h>
32 # Represent a screen surface
33 extern SDLDisplay in "C" `{SDL_Surface *`}
36 redef type I: SDLImage
38 # Initialize a surface with width and height
39 new ( w, h: Int) is extern `{
40 SDL_Init(SDL_INIT_VIDEO);
43 printf
("TTF_Init: %s\n", TTF_GetError());
47 /* ignores mousemotion
for performance reasons
*/
48 SDL_EventState( SDL_MOUSEMOTION, SDL_IGNORE );
50 return SDL_SetVideoMode( w
, h
, 24, SDL_HWSURFACE );
54 fun destroy is extern `{
55 if ( SDL_WasInit( SDL_INIT_VIDEO ) )
62 redef fun finish is extern `{ SDL_Flip( recv ); `}
64 # Clear the entire window with given RGB color (integer values)
65 fun clear_int
( r
, g
, b
: Int ) is extern `{
66 SDL_FillRect( recv, NULL, SDL_MapRGB(recv->format,r,g,b) );
69 redef fun width
: Int is extern `{ return recv->w; `}
70 redef fun height: Int is extern `{ return recv->h; `}
72 # Fill a rectangle with given color
73 fun fill_rect
( rect
: SDLRectangle, r
, g
, b
: Int ) is extern `{
74 SDL_FillRect( recv, rect, SDL_MapRGB(recv->format,r,g,b) );
77 redef fun clear
( r
, g
, b
: Float ) is extern `{
82 SDL_FillRect( recv, NULL, SDL_MapRGB(recv->format,ri,gi,bi) );
85 fun events
: Sequence[ IE ]
87 var new_event
: nullable Object = null
88 var events
= new List[ IE ]
90 new_event
= poll_event
91 if new_event
!= null then # new_event isa Event then #
92 events
.add
( new_event
)
100 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) `{
105 if ( SDL_PollEvent(&event) )
107 switch (event.type ) {
111 printf("The \"%s\" key was pressed!\n",
112 SDL_GetKeyName(event.key.keysym.sym));
115 return SDLKeyEvent_as_nullable_InputEvent(
116 new_SDLKeyEvent( NativeString_to_s(
117 SDL_GetKeyName(event.key.keysym.sym) ),
118 event.type==SDL_KEYDOWN ) );
120 case SDL_MOUSEMOTION:
122 printf("Mouse moved by %d,%d to (%d,%d)\n",
123 event.motion.xrel, event.motion.yrel,
124 event.motion.x, event.motion.y);
127 return SDLMouseMotionEvent_as_nullable_InputEvent(
128 new_SDLMouseMotionEvent( event.motion.x, event.motion.y,
129 event.motion.xrel, event.motion.yrel ) );
131 case SDL_MOUSEBUTTONDOWN:
132 case SDL_MOUSEBUTTONUP:
134 printf("Mouse button \"%d\" pressed at (%d,%d)\n",
135 event.button.button, event.button.x, event.button.y);
137 return SDLMouseButtonEvent_as_nullable_InputEvent(
138 new_SDLMouseButtonEvent( event.button.x, event.button.y,
139 event.button.button, event.type == SDL_MOUSEBUTTONDOWN ) );
143 printf("Quit event\n");
145 return SDLQuitEvent_as_nullable_InputEvent( new_SDLQuitEvent() );
149 return null_InputEvent();
152 # Set the position of the cursor to x,y
153 fun warp_mouse
( x
,y
: Int ) `{ SDL_WarpMouse( x, y ); `}
155 # Show or hide the cursor
156 fun show_cursor( show: Bool ) `{ SDL_ShowCursor( show ); `}
159 # Basic Drawing figures
160 extern SDLDrawable in "C" `{SDL_Surface*`}
163 redef type I: SDLImage
165 redef fun blit( img, x, y ) is extern `{
172 SDL_BlitSurface( img
, NULL, recv
, &dst
);
175 redef fun blit_centered( img, x, y )
177 x = x - img.width / 2
178 y = y - img.height / 2
183 extern SDLImage in "C" `{SDL_Surface*`} # TODO remove
187 new from_file
( path
: String ) is extern import String::to_cstring
`{
188 SDL_Surface *image = IMG_Load( String_to_cstring( path ) );
192 new partial
( original
: Image, clip
: SDLRectangle ) is extern `{
196 new copy_of
( image
: SDLImage ) is extern `{
197 SDL_Surface *new_image = SDL_CreateRGBSurface( image->flags, image->w, image->h, 24,
205 SDL_BlitSurface( image, NULL, new_image, &dst );
210 fun save_to_file
( path
: String ) is extern import String::to_cstring
`{ `}
212 redef fun destroy is extern `{ SDL_FreeSurface( recv ); `}
214 redef fun width
: Int is extern `{ return recv->w; `}
215 redef fun height: Int is extern `{ return recv->h; `}
217 fun is_ok
: Bool do return true # TODO
220 extern SDLRectangle in "C" `{SDL_Rect*`}
221 new ( x: Int, y: Int, w: Int, h: Int ) is extern `{
222 SDL_Rect *rect
= malloc
( sizeof
( SDL_Rect ) );
230 fun x=( v: Int ) is extern `{ recv->x = (Sint16)v; `}
231 fun x
: Int is extern `{ return recv->x; `}
233 fun y=( v: Int ) is extern `{ recv->y = (Sint16)v; `}
234 fun y
: Int is extern `{ return recv->y; `}
236 fun w=( v: Int ) is extern `{ recv->w = (Uint16)v; `}
237 fun w
: Int is extern `{ return recv->w; `}
239 fun h=( v: Int ) is extern `{ recv->h = (Uint16)v; `}
240 fun h
: Int is extern `{ return recv->h; `}
242 fun destroy is extern `{ `}
245 interface SDLInputEvent
256 private init ( x
, y
: Float )
263 class SDLMouseButtonEvent
268 redef var pressed
: Bool
269 redef fun depressed
: Bool do return not pressed
271 init ( x
, y
: Float, button
: Int, pressed
: Bool )
276 self.pressed
= pressed
282 return "MouseButtonEvent button {button} down at {x}, {y}"
284 return "MouseButtonEvent button {button} up at {x}, {y}"
289 class SDLMouseMotionEvent
295 init ( x
, y
, rel_x
, rel_y
: Float )
303 redef fun to_s
do return "MouseMotionEvent at {x}, {y}"
313 init ( key_name
: String, down
: Bool )
315 self.key_name
= key_name
319 redef fun to_c
: nullable Char
321 if key_name
.length
== 1 then return key_name
.first
328 return "KeyboardEvent key {key_name} down"
330 return "KeyboardEvent key {key_name} up"
334 redef fun is_down
do return down
336 redef fun is_arrow_up
do return key_name
== "up"
337 redef fun is_arrow_left
do return key_name
== "left"
338 redef fun is_arrow_down
do return key_name
== "down"
339 redef fun is_arrow_right
do return key_name
== "right"
348 fun delay
is extern `{ SDL_Delay( recv ); `}
351 extern SDLFont in "C" `{TTF_Font *`}
352 new ( name
: String, points
: Int ) is extern import String::to_cstring
`{
353 char * cname = String_to_cstring( name );
355 TTF_Font *font = TTF_OpenFont( cname, (int)points);
357 printf("TTF_OpenFont: %s\n", TTF_GetError());
364 fun destroy
is extern `{ TTF_CloseFont( recv ); `}
366 fun render( text: String, r, g, b: Int ): SDLImage is extern import String::to_cstring `{
368 SDL_Surface *text_surface
;
375 ctext
= String_to_cstring( text
);
376 if( !(text_surface
=TTF_RenderText_Blended( recv
, ctext
, color
)) )
378 fprintf
( stderr
, "SDL TFF error: %s\n", TTF_GetError() );
385 # TODO reactivate fun below when updating libsdl_ttf to 2.0.10 or above
386 #fun outline: Int is extern # TODO check to make inline/nitside only
387 #fun outline=( v: Int ) is extern
389 #fun kerning: Bool is extern
390 #fun kerning=( v: Bool ) is extern
392 # Maximum pixel height of all glyphs of this font.
393 fun height: Int is extern `{
394 return TTF_FontHeight( recv
);
397 fun ascent: Int is extern `{
398 return TTF_FontAscent( recv
);
401 fun descent: Int is extern `{
402 return TTF_FontDescent( recv
);
405 # Get the recommended pixel height of a rendered line of text of the loaded font. This is usually larger than the Font::height.
406 fun line_skip: Int is extern `{
407 return TTF_FontLineSkip( recv
);
410 fun is_fixed_width: Bool is extern `{
411 return TTF_FontFaceIsFixedWidth( recv
);
413 fun family_name: nullable String is extern import String::to_cstring, String as nullable `{
414 char
*fn
= TTF_FontFaceFamilyName( recv
);
417 return null_String
();
419 return String_as_nullable( NativeString_to_s( fn
) );
421 fun style_name: nullable String is extern import String::to_cstring, String as nullable `{
422 char
*sn
= TTF_FontFaceStyleName( recv
);
425 return null_String
();
427 return String_as_nullable( NativeString_to_s( sn
) );
430 fun width_of( text: String ): Int is extern import NativeString::to_s `{
431 char
*ctext
= String_to_cstring( text
);
433 if ( TTF_SizeText( recv
, ctext
, &w
, NULL ) )
435 fprintf
( stderr
, "SDL TFF error: %s\n", TTF_GetError() );