lib/sdl: fix coding style
authorAlexis Laferrière <alexis.laf@xymus.net>
Fri, 4 Apr 2014 16:46:33 +0000 (12:46 -0400)
committerAlexis Laferrière <alexis.laf@xymus.net>
Fri, 4 Apr 2014 19:31:34 +0000 (15:31 -0400)
Signed-off-by: Alexis Laferrière <alexis.laf@xymus.net>

lib/mnit_linux/sdl.nit

index 527624a..7e39e21 100644 (file)
@@ -20,26 +20,24 @@ module sdl is
        c_linker_option(exec("sdl-config", "--libs"), "-lSDL_ttf")
 end
 
-import mnit # for
-# import display
-# mport input_events
+import mnit
 
 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>
+       #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 SDLDisplay in "C" `{SDL_Surface *`}
+extern class SDLDisplay `{SDL_Surface *`}
        super Display
 
        redef type I: SDLImage
 
        # Initialize a surface with width and height
-       new ( w, h: Int) is extern import enable_mouse_motion_events `{
+       new (w, h: Int) import enable_mouse_motion_events `{
                SDL_Init(SDL_INIT_VIDEO);
 
                if(TTF_Init()==-1) {
@@ -47,11 +45,11 @@ extern SDLDisplay in "C" `{SDL_Surface *`}
                        exit(2);
                }
 
-               SDL_Surface *self = SDL_SetVideoMode( w, h, 24, SDL_HWSURFACE );
+               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 );
+                       SDL_EventState(SDL_MOUSEMOTION, SDL_IGNORE);
                }
 
                return self;
@@ -63,45 +61,45 @@ extern SDLDisplay in "C" `{SDL_Surface *`}
        fun enable_mouse_motion_events: Bool do return false
 
        # Destroy the surface
-       fun destroy is extern `{
-       if ( SDL_WasInit( SDL_INIT_VIDEO ) )
-               SDL_Quit();
+       fun destroy `{
+               if (SDL_WasInit(SDL_INIT_VIDEO))
+                       SDL_Quit();
 
-       if ( TTF_WasInit() )
-               TTF_Quit();
+               if (TTF_WasInit())
+                       TTF_Quit();
        `}
 
-       redef fun finish is extern `{ SDL_Flip( recv ); `}
+       redef fun finish `{ SDL_Flip(recv); `}
 
        # Clear the entire window with given RGB color (integer values)
-       fun clear_int( r, g, b: Int ) is extern `{
-               SDL_FillRect( recv, NULL, SDL_MapRGB(recv->format,r,g,b) ); 
+       fun clear_int(r, g, b: Int) `{
+               SDL_FillRect(recv, NULL, SDL_MapRGB(recv->format,r,g,b)); 
        `}
 
-       redef fun width: Int is extern `{ return recv->w; `}
-       redef fun height: Int is extern `{ return recv->h; `}
+       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 ) is extern `{
-               SDL_FillRect( recv, rect,  SDL_MapRGB(recv->format,r,g,b) );
+       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 ) is extern `{
+       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_FillRect(recv, NULL, SDL_MapRGB(recv->format,ri,gi,bi));
        `}
 
-       fun events: Sequence[ IE ]
+       fun events: Sequence[IE]
        do
                var new_event: nullable Object = null
-               var events = new List[ IE ]
+               var events = new List[IE]
                loop
                        new_event = poll_event
                        if new_event != null then # new_event isa Event then #
-                               events.add( new_event )
+                               events.add(new_event)
                        else
                                break
                        end
@@ -109,14 +107,14 @@ extern SDLDisplay in "C" `{SDL_Surface *`}
                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 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();
 
-               if ( SDL_PollEvent(&event) )
+               if (SDL_PollEvent(&event))
                {
-                       switch (event.type ) {
+                       switch (event.type) {
                                case SDL_KEYDOWN:
                                case SDL_KEYUP:
        #ifdef DEBUG
@@ -125,9 +123,9 @@ extern SDLDisplay in "C" `{SDL_Surface *`}
        #endif
 
                                        return SDLKeyEvent_as_nullable_IE(
-                                                       new_SDLKeyEvent( NativeString_to_s(
-                                                               SDL_GetKeyName(event.key.keysym.sym) ),
-                                                               event.type==SDL_KEYDOWN ) );
+                                                       new_SDLKeyEvent(NativeString_to_s(
+                                                               SDL_GetKeyName(event.key.keysym.sym)),
+                                                               event.type==SDL_KEYDOWN));
 
                                case SDL_MOUSEMOTION:
        #ifdef DEBUG
@@ -137,8 +135,8 @@ extern SDLDisplay in "C" `{SDL_Surface *`}
        #endif
 
                                        return SDLMouseMotionEvent_as_nullable_IE(
-                                                       new_SDLMouseMotionEvent( event.motion.x, event.motion.y,
-                                                               event.motion.xrel, event.motion.yrel, SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)) );
+                                                       new_SDLMouseMotionEvent(event.motion.x, event.motion.y,
+                                                               event.motion.xrel, event.motion.yrel, SDL_GetMouseState(NULL, NULL)&SDL_BUTTON(1)));
 
                                case SDL_MOUSEBUTTONDOWN:
                                case SDL_MOUSEBUTTONUP:
@@ -147,14 +145,14 @@ extern SDLDisplay in "C" `{SDL_Surface *`}
                                                   event.button.button, event.button.x, event.button.y);
        #endif
                                        return SDLMouseButtonEvent_as_nullable_IE(
-                                                       new_SDLMouseButtonEvent( event.button.x, event.button.y,
-                                                               event.button.button, event.type == SDL_MOUSEBUTTONDOWN ) );
+                                                       new_SDLMouseButtonEvent(event.button.x, event.button.y,
+                                                               event.button.button, event.type == SDL_MOUSEBUTTONDOWN));
 
                                case SDL_QUIT:
        #ifdef DEBUG
                                        printf("Quit event\n");
        #endif
-                                       return SDLQuitEvent_as_nullable_IE( new_SDLQuitEvent() );
+                                       return SDLQuitEvent_as_nullable_IE(new_SDLQuitEvent());
                        }
                }
 
@@ -162,84 +160,84 @@ extern SDLDisplay in "C" `{SDL_Surface *`}
        `}
 
        # Set the position of the cursor to x,y
-       fun warp_mouse( x,y: Int ) `{ SDL_WarpMouse( x, y ); `}
+       fun warp_mouse(x,y: Int) `{ SDL_WarpMouse(x, y); `}
 
        # Show or hide the cursor
-       fun show_cursor( show: Bool ) `{ SDL_ShowCursor( show ); `}
+       fun show_cursor(show: Bool) `{ SDL_ShowCursor(show); `}
 end
 
 # Basic Drawing figures
-extern SDLDrawable in "C" `{SDL_Surface*`}
+extern class SDLDrawable `{SDL_Surface*`}
        super Drawable
 
        redef type I: SDLImage
 
-       redef fun blit( img, x, y ) is extern `{
+       redef fun blit(img, x, y) `{
                SDL_Rect dst;
                dst.x = x;
                dst.y = y;
                dst.w = 0;
                dst.h = 0;
 
-               SDL_BlitSurface( img, NULL, recv, &dst );
+               SDL_BlitSurface(img, NULL, recv, &dst);
        `}
 
-       redef fun blit_centered( img, x, y )
+       redef fun blit_centered(img, x, y)
        do
                x = x - img.width / 2
                y = y - img.height / 2
-               blit( img, x, y )
+               blit(img, x, y)
        end
 end
 
 # A drawable Image
-extern SDLImage in "C" `{SDL_Surface*`} # TODO remove
+extern class SDLImage `{SDL_Surface*`} # TODO remove
        super DrawableImage
        super SDLDrawable
 
        # 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 ) );
+       new from_file(path: String) import String.to_cstring `{
+               SDL_Surface *image = IMG_Load(String_to_cstring(path));
                return image;
        `}
 
-
-       new partial( original: Image, clip: SDLRectangle ) is extern `{
+       new partial(original: Image, clip: SDLRectangle) `{
                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 );
+       new copy_of(image: SDLImage) `{
+               SDL_Surface *new_image = SDL_CreateRGBSurface(
+                       image->flags, image->w, image->h, 24,
+                       0, 0, 0, 0);
 
                SDL_Rect dst;
                dst.x = 0;
                dst.y = 0;
                dst.w = image->w;
                dst.h = image->h;
-               SDL_BlitSurface( image, NULL, new_image, &dst );
+               SDL_BlitSurface(image, NULL, new_image, &dst);
 
                return new_image;
        `}
 
        # Save the image into the specified file
-       fun save_to_file( path: String ) is extern import String::to_cstring `{ `}
+       fun save_to_file(path: String) import String::to_cstring `{ `}
 
        # Destroy the image and free the memory
-       redef fun destroy is extern `{ SDL_FreeSurface( recv ); `}
+       redef fun destroy `{ SDL_FreeSurface(recv); `}
 
-       redef fun width: Int is extern `{ return recv->w; `}
-       redef fun height: Int is extern `{ return recv->h; `}
+       redef fun width: Int `{ return recv->w; `}
+       redef fun height: Int `{ return recv->h; `}
 
        fun is_ok: Bool do return true # TODO
 end
 
 # A simple rectangle
-extern SDLRectangle in "C" `{SDL_Rect*`}
+extern class SDLRectangle `{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 ) );
+       new (x: Int, y: Int, w: Int, h: Int) `{
+               SDL_Rect *rect = malloc(sizeof(SDL_Rect));
                rect->x = (Sint16)x;
                rect->y = (Sint16)y;
                rect->w = (Uint16)w;
@@ -247,19 +245,19 @@ extern SDLRectangle in "C" `{SDL_Rect*`}
                return rect;
        `}
 
-       fun x=( v: Int ) is extern `{ recv->x = (Sint16)v; `}
-       fun x: Int is extern `{ return recv->x; `}
+       fun x=(v: Int) `{ recv->x = (Sint16)v; `}
+       fun x: Int `{ return recv->x; `}
 
-       fun y=( v: Int ) is extern `{ recv->y = (Sint16)v; `}
-       fun y: Int is extern `{ return recv->y; `}
+       fun y=(v: Int) `{ recv->y = (Sint16)v; `}
+       fun y: Int `{ return recv->y; `}
 
-       fun w=( v: Int ) is extern `{ recv->w = (Uint16)v; `}
-       fun w: Int is extern `{ return recv->w; `}
+       fun w=(v: Int) `{ recv->w = (Uint16)v; `}
+       fun w: Int `{ return recv->w; `}
 
-       fun h=( v: Int ) is extern `{ recv->h = (Uint16)v; `}
-       fun h: Int is extern `{ return recv->h; `}
+       fun h=(v: Int) `{ recv->h = (Uint16)v; `}
+       fun h: Int `{ return recv->h; `}
 
-       fun destroy is extern `{ `}
+       fun destroy `{ `}
 end
 
 interface SDLInputEvent
@@ -274,7 +272,7 @@ class SDLMouseEvent
        redef var x: Float
        redef var y: Float
 
-       private init ( x, y: Float )
+       private init (x, y: Float)
        do
                self.x = x
                self.y = y
@@ -290,9 +288,9 @@ class SDLMouseButtonEvent
        redef var pressed: Bool
        redef fun depressed: Bool do return not pressed
 
-       init ( x, y: Float, button: Int, pressed: Bool )
+       init (x, y: Float, button: Int, pressed: Bool)
        do
-               super( x, y )
+               super(x, y)
 
                self.button = button
                self.pressed = pressed
@@ -318,9 +316,9 @@ class SDLMouseMotionEvent
        redef var pressed: Bool
        redef fun depressed: Bool do return not pressed
 
-       init ( x, y, rel_x, rel_y: Float, pressed: Bool )
+       init (x, y, rel_x, rel_y: Float, pressed: Bool)
        do
-               super( x, y )
+               super(x, y)
 
                self.rel_x = rel_x
                self.rel_y = rel_y
@@ -338,7 +336,7 @@ class SDLKeyEvent
        var key_name: String
        var down: Bool
 
-       init ( key_name: String, down: Bool )
+       init (key_name: String, down: Bool)
        do
                self.key_name = key_name
                self.down = down
@@ -378,28 +376,28 @@ class SDLQuitEvent
 end
 
 redef class Int
-       fun delay is extern `{ SDL_Delay( recv ); `}
+       fun delay `{ SDL_Delay(recv); `}
 end
 
 # Class to load and use TTF_Font
-extern SDLFont in "C" `{TTF_Font *`}
+extern class SDLFont `{TTF_Font *`}
        # 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 );
+       new (name: String, points: Int) import String.to_cstring `{
+       char * cname = String_to_cstring(name);
 
-       TTF_Font *font = TTF_OpenFont( cname, (int)points);
+       TTF_Font *font = TTF_OpenFont(cname, (int)points);
        if(!font) {
                printf("TTF_OpenFont: %s\n", TTF_GetError());
-               exit( 1 );
+               exit(1);
        }
 
        return font;
        `}
 
-       fun destroy is extern `{ TTF_CloseFont( recv ); `}
+       fun destroy `{ TTF_CloseFont(recv); `}
 
        # 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 `{
+       fun render(text: String, r, g, b: Int): SDLImage import String.to_cstring `{
                SDL_Color color;
                SDL_Surface *text_surface;
                char *ctext;
@@ -408,74 +406,74 @@ extern SDLFont in "C" `{TTF_Font *`}
                color.g = g;
                color.b = b;
 
-               ctext = String_to_cstring( text );
-               if( !(text_surface=TTF_RenderText_Blended( recv, ctext, color )) )
+               ctext = String_to_cstring(text);
+               if(!(text_surface=TTF_RenderText_Blended(recv, ctext, color)))
                {
-                       fprintf( stderr, "SDL TFF error: %s\n", TTF_GetError() );
-                       exit( 1 );
+                       fprintf(stderr, "SDL TFF error: %s\n", TTF_GetError());
+                       exit(1);
                }
                else
                        return text_surface;
        `}
 
        # TODO reactivate fun below when updating libsdl_ttf to 2.0.10 or above
-       #fun outline: Int is extern # TODO check to make inline/nitside only
-       #fun outline=( v: Int ) is extern
+       #fun outline: Int # TODO check to make inline/nitside only
+       #fun outline=(v: Int) is extern
 
        #fun kerning: Bool is extern
-       #fun kerning=( v: Bool ) is extern
+       #fun kerning=(v: Bool) is extern
 
        # Maximum pixel height of all glyphs of this font.
-       fun height: Int is extern `{
-               return TTF_FontHeight( recv );
+       fun height: Int `{
+               return TTF_FontHeight(recv);
        `}
 
-       fun ascent: Int is extern `{
-               return TTF_FontAscent( recv );
+       fun ascent: Int `{
+               return TTF_FontAscent(recv);
        `}
 
-       fun descent: Int is extern `{
-               return TTF_FontDescent( recv );
+       fun descent: Int `{
+               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.
-       fun line_skip: Int is extern `{
-               return TTF_FontLineSkip( recv );
+       fun line_skip: Int `{
+               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 is_fixed_width: Bool `{
+               return TTF_FontFaceIsFixedWidth(recv);
        `}
 
        # 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 );
+       fun family_name: nullable String import String.to_cstring, String as nullable `{
+               char *fn = TTF_FontFaceFamilyName(recv);
 
-               if ( fn == NULL )
+               if (fn == NULL)
                        return null_String();
                else
-                       return String_as_nullable( NativeString_to_s( fn ) );
+                       return String_as_nullable(NativeString_to_s(fn));
        `}
 
        # 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 );
+       fun style_name: nullable String import String.to_cstring, String as nullable `{
+               char *sn = TTF_FontFaceStyleName(recv);
 
-               if ( sn == NULL )
+               if (sn == NULL)
                        return null_String();
                else
-                       return String_as_nullable( NativeString_to_s( sn ) );
+                       return String_as_nullable(NativeString_to_s(sn));
        `}
 
        # 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 );
+       fun width_of(text: String): Int import NativeString.to_s `{
+               char *ctext = String_to_cstring(text);
                int w;
-               if ( TTF_SizeText( recv, ctext, &w, NULL ) )
+               if (TTF_SizeText(recv, ctext, &w, NULL))
                {
-                       fprintf( stderr, "SDL TFF error: %s\n", TTF_GetError() );
-                       exit( 1 );
+                       fprintf(stderr, "SDL TFF error: %s\n", TTF_GetError());
+                       exit(1);
                }
                else
                        return w;