Skip to content
Browse files

refactor SDL to unbreak BubbleBreaker

  • Loading branch information...
1 parent 9205cf7 commit e7cbce4be893cbee08ba41252dd2defb1aafd71c @FROGGS FROGGS committed
Showing with 312 additions and 400 deletions.
  1. +1 −0 META.info
  2. +9 −9 lib/SDL/App.pm6
  3. +165 −172 lib/SDL/CStructs.pm6
  4. +1 −1 lib/SDL/Color.pm
  5. +1 −1 lib/SDL/Palette.pm6
  6. +1 −1 lib/SDL/PixelFormat.pm6
  7. +8 −7 lib/SDL/Rect.pm6
  8. +59 −65 lib/SDL/SFont.pm6
  9. +63 −144 lib/SDL/Surface.pm6
  10. +4 −0 lib/SDL/Surfacish.pm6
View
1 META.info
@@ -12,6 +12,7 @@
"SDL::SFont" : "lib/lib/SDL/SFont.pm6",
"SDL::Video" : "lib/lib/SDL/Video.pm6",
"SDL::Surface" : "lib/lib/SDL/Surface.pm6",
+ "SDL::Surfacish" : "lib/lib/SDL/Surfacish.pm6",
"SDL::App" : "lib/lib/SDL/App.pm6",
"SDL::Rect" : "lib/lib/SDL/Rect.pm6",
"SDL::SDL_video" : "lib/lib/SDL/SDL_video.pm",
View
18 lib/SDL/App.pm6
@@ -1,15 +1,15 @@
-unit module SDL::App;
-
use NativeCall;
use SDL::Controller;
use SDL::Surface;
+use SDL::Surfacish;
-class SDL::App is SDL::Surface {
- also is SDL::Controller;
- method new( $w, $h, $bpp, $flags ) {
- self.bless( file => Str, rw => Pointer, pointer => _set_video_mode( $w, $h, $bpp, $flags ) );
- }
-}
+class SDL::App is SDL::Controller does SDL::Surfacish {
+ method new( $w, $h, $bpp, $flags ) {
+ self.bless: surface => SDL::Surface::_set_video_mode( $w, $h, $bpp, $flags )
+ }
-our sub _set_video_mode( int32 $w, int32 $h, int32 $bpp, int32 $flags ) returns SDL::Surface is native('libSDL') is symbol('SDL_SetVideoMode') { * }
+ method FALLBACK($name, |c) {
+ $!surface."$name"(|c)
+ }
+}
View
337 lib/SDL/CStructs.pm6
@@ -1,4 +1,5 @@
+use NativeCall;
# Ideally this file could be autogenerated.
class SDL_version is repr('CStruct') { # typedef struct {
@@ -14,175 +15,167 @@ class SDL_Rect is repr('CStruct') { # typedef struct SDL_Rect {
has uint16 $.w; has uint16 $.h; # Uint16 w, h;
} # } SDL_Rect;
-typedef struct SDL_Color {
- Uint8 r;
- Uint8 g;
- Uint8 b;
- Uint8 unused;
-} SDL_Color;
-#define SDL_Colour SDL_Color
-
-typedef struct SDL_Palette {
- int ncolors;
- SDL_Color *colors;
-} SDL_Palette;
-/*@}*/
-
-/** Everything in the pixel format structure is read-only */
-typedef struct SDL_PixelFormat {
- SDL_Palette *palette;
- Uint8 BitsPerPixel;
- Uint8 BytesPerPixel;
- Uint8 Rloss;
- Uint8 Gloss;
- Uint8 Bloss;
- Uint8 Aloss;
- Uint8 Rshift;
- Uint8 Gshift;
- Uint8 Bshift;
- Uint8 Ashift;
- Uint32 Rmask;
- Uint32 Gmask;
- Uint32 Bmask;
- Uint32 Amask;
-
- /** RGB color key information */
- Uint32 colorkey;
- /** Alpha value information (per-surface alpha) */
- Uint8 alpha;
-} SDL_PixelFormat;
-
-/** This structure should be treated as read-only, except for 'pixels',
- * which, if not NULL, contains the raw pixel data for the surface.
- */
-typedef struct SDL_Surface {
- Uint32 flags; /**< Read-only */
- SDL_PixelFormat *format; /**< Read-only */
- int w, h; /**< Read-only */
- Uint16 pitch; /**< Read-only */
- void *pixels; /**< Read-write */
- int offset; /**< Private */
-
- /** Hardware-specific surface info */
- struct private_hwdata *hwdata;
-
- /** clipping information */
- SDL_Rect clip_rect; /**< Read-only */
- Uint32 unused1; /**< for binary compatibility */
-
- /** Allow recursive locks */
- Uint32 locked; /**< Private */
-
- /** info for fast blit mapping to other surfaces */
- struct SDL_BlitMap *map; /**< Private */
-
- /** format version, bumped at every change to invalidate blit maps */
- unsigned int format_version; /**< Private */
-
- /** Reference count -- used when freeing surface */
- int refcount; /**< Read-mostly */
-} SDL_Surface;
-
-/** @name SDL_Surface Flags
- * These are the currently supported flags for the SDL_surface
- */
-/*@{*/
-
-/** Available for SDL_CreateRGBSurface() or SDL_SetVideoMode() */
-/*@{*/
-#define SDL_SWSURFACE 0x00000000 /**< Surface is in system memory */
-#define SDL_HWSURFACE 0x00000001 /**< Surface is in video memory */
-#define SDL_ASYNCBLIT 0x00000004 /**< Use asynchronous blits if possible */
-/*@}*/
-
-/** Available for SDL_SetVideoMode() */
-/*@{*/
-#define SDL_ANYFORMAT 0x10000000 /**< Allow any video depth/pixel-format */
-#define SDL_HWPALETTE 0x20000000 /**< Surface has exclusive palette */
-#define SDL_DOUBLEBUF 0x40000000 /**< Set up double-buffered video mode */
-#define SDL_FULLSCREEN 0x80000000 /**< Surface is a full screen display */
-#define SDL_OPENGL 0x00000002 /**< Create an OpenGL rendering context */
-#define SDL_OPENGLBLIT 0x0000000A /**< Create an OpenGL rendering context and use it for blitting */
-#define SDL_RESIZABLE 0x00000010 /**< This video mode may be resized */
-#define SDL_NOFRAME 0x00000020 /**< No window caption or edge frame */
-/*@}*/
-
-/** Used internally (read-only) */
-/*@{*/
-#define SDL_HWACCEL 0x00000100 /**< Blit uses hardware acceleration */
-#define SDL_SRCCOLORKEY 0x00001000 /**< Blit uses a source color key */
-#define SDL_RLEACCELOK 0x00002000 /**< Private flag */
-#define SDL_RLEACCEL 0x00004000 /**< Surface is RLE encoded */
-#define SDL_SRCALPHA 0x00010000 /**< Blit uses source alpha blending */
-#define SDL_PREALLOC 0x01000000 /**< Surface uses preallocated memory */
-/*@}*/
-
-/*@}*/
-
-/** Evaluates to true if the surface needs to be locked before access */
-#define SDL_MUSTLOCK(surface) \
- (surface->offset || \
- ((surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_RLEACCEL)) != 0))
-
-/** typedef for private surface blitting functions */
-typedef int (*SDL_blit)(struct SDL_Surface *src, SDL_Rect *srcrect,
- struct SDL_Surface *dst, SDL_Rect *dstrect);
-
-
-/** Useful for determining the video hardware capabilities */
-typedef struct SDL_VideoInfo {
- Uint32 hw_available :1; /**< Flag: Can you create hardware surfaces? */
- Uint32 wm_available :1; /**< Flag: Can you talk to a window manager? */
- Uint32 UnusedBits1 :6;
- Uint32 UnusedBits2 :1;
- Uint32 blit_hw :1; /**< Flag: Accelerated blits HW --> HW */
- Uint32 blit_hw_CC :1; /**< Flag: Accelerated blits with Colorkey */
- Uint32 blit_hw_A :1; /**< Flag: Accelerated blits with Alpha */
- Uint32 blit_sw :1; /**< Flag: Accelerated blits SW --> HW */
- Uint32 blit_sw_CC :1; /**< Flag: Accelerated blits with Colorkey */
- Uint32 blit_sw_A :1; /**< Flag: Accelerated blits with Alpha */
- Uint32 blit_fill :1; /**< Flag: Accelerated color fill */
- Uint32 UnusedBits3 :16;
- Uint32 video_mem; /**< The total amount of video memory (in K) */
- SDL_PixelFormat *vfmt; /**< Value: The format of the video surface */
- int current_w; /**< Value: The current video mode width */
- int current_h; /**< Value: The current video mode height */
-} SDL_VideoInfo;
-
-
-/** @name Overlay Formats
- * The most common video overlay formats.
- * For an explanation of these pixel formats, see:
- * http://www.webartz.com/fourcc/indexyuv.htm
- *
- * For information on the relationship between color spaces, see:
- * http://www.neuro.sfc.keio.ac.jp/~aly/polygon/info/color-space-faq.html
- */
-/*@{*/
-#define SDL_YV12_OVERLAY 0x32315659 /**< Planar mode: Y + V + U (3 planes) */
-#define SDL_IYUV_OVERLAY 0x56555949 /**< Planar mode: Y + U + V (3 planes) */
-#define SDL_YUY2_OVERLAY 0x32595559 /**< Packed mode: Y0+U0+Y1+V0 (1 plane) */
-#define SDL_UYVY_OVERLAY 0x59565955 /**< Packed mode: U0+Y0+V0+Y1 (1 plane) */
-#define SDL_YVYU_OVERLAY 0x55595659 /**< Packed mode: Y0+V0+Y1+U0 (1 plane) */
-/*@}*/
-
-/** The YUV hardware video overlay */
-typedef struct SDL_Overlay {
- Uint32 format; /**< Read-only */
- int w, h; /**< Read-only */
- int planes; /**< Read-only */
- Uint16 *pitches; /**< Read-only */
- Uint8 **pixels; /**< Read-write */
-
- /** @name Hardware-specific surface info */
- /*@{*/
- struct private_yuvhwfuncs *hwfuncs;
- struct private_yuvhwdata *hwdata;
- /*@{*/
-
- /** @name Special flags */
- /*@{*/
- Uint32 hw_overlay :1; /**< Flag: This overlay hardware accelerated? */
- Uint32 UnusedBits :31;
- /*@}*/
-} SDL_Overlay;
+class SDL_Color is repr('CStruct') { # typedef struct SDL_Color {
+ has uint8 $.r; # Uint8 r;
+ has uint8 $.g; # Uint8 g;
+ has uint8 $.b; # Uint8 b;
+ has uint8 $.unused; # Uint8 unused;
+} # } SDL_Color;
+constant SDL_Colour = SDL_Color; #define SDL_Colour SDL_Color
+
+class SDL_Palette is repr('CStruct') { # typedef struct SDL_Palette {
+ has int32 $.ncolors; # int ncolors;
+ has Pointer[SDL_Color] $.colors; # SDL_Color *colors;
+} # } SDL_Palette;
+
+#~ /** Everything in the pixel format structure is read-only */
+class SDL_PixelFormat is repr('CStruct') { # typedef struct SDL_PixelFormat {
+ has SDL_Palette $.palette; # SDL_Palette *palette;
+ has uint8 $.BitsPerPixel; # Uint8 BitsPerPixel;
+ has uint8 $.BytesPerPixel; # Uint8 BytesPerPixel;
+ has uint8 $.Rloss; # Uint8 Rloss;
+ has uint8 $.Gloss; # Uint8 Gloss;
+ has uint8 $.Bloss; # Uint8 Bloss;
+ has uint8 $.Aloss; # Uint8 Aloss;
+ has uint8 $.Rshift; # Uint8 Rshift;
+ has uint8 $.Gshift; # Uint8 Gshift;
+ has uint8 $.Bshift; # Uint8 Bshift;
+ has uint8 $.Ashift; # Uint8 Ashift;
+ has uint32 $.Rmask; # Uint32 Rmask;
+ has uint32 $.Gmask; # Uint32 Gmask;
+ has uint32 $.Bmask; # Uint32 Bmask;
+ has uint32 $.Amask; # Uint32 Amask;
+ # /** RGB color key information */
+ has uint32 $.colorkey; # Uint32 colorkey;
+ # /** Alpha value information (per-surface alpha) */
+ has uint8 $.alpha; # Uint8 alpha;
+} # } SDL_PixelFormat;
+
+#~ /** This structure should be treated as read-only, except for 'pixels',
+ #~ * which, if not NULL, contains the raw pixel data for the surface.
+ #~ */
+class SDL_Surface is repr('CStruct') { # typedef struct SDL_Surface {
+ has uint32 $.flags; # Uint32 flags; /**< Read-only */
+ has SDL_PixelFormat $.format; # SDL_PixelFormat *format; /**< Read-only */
+ has int32 $.w; has int32 $.h; # int w, h; /**< Read-only */
+ has uint16 $.pitch; # Uint16 pitch; /**< Read-only */
+ has Pointer $.pixels; # void *pixels; /**< Read-write */
+ has int32 $.offset; # int offset; /**< Private */
+ # /** Hardware-specific surface info */
+ has Pointer $.hwdata; # struct private_hwdata *hwdata;
+ # /** clipping information */
+ HAS SDL_Rect $.clip_rect; # SDL_Rect clip_rect; /**< Read-only */
+ has uint32 $.unused1; # Uint32 unused1; /**< for binary compatibility */
+ # /** Allow recursive locks */
+ has uint32 $.locked; # Uint32 locked; /**< Private */
+ # /** info for fast blit mapping to other surfaces */
+ has Pointer $.map; # struct SDL_BlitMap *map; /**< Private */
+ # /** format version, bumped at every change to invalidate blit maps */
+ has uint32 $.format_version; # unsigned int format_version; /**< Private */
+ # /** Reference count -- used when freeing surface */
+ has int32 $.refcount; # int refcount; /**< Read-mostly */
+} # } SDL_Surface;
+
+#~ /** @name SDL_Surface Flags
+ #~ * These are the currently supported flags for the SDL_surface
+ #~ */
+#~ /*@{*/
+
+#~ /** Available for SDL_CreateRGBSurface() or SDL_SetVideoMode() */
+#~ /*@{*/
+#~ #define SDL_SWSURFACE 0x00000000 /**< Surface is in system memory */
+#~ #define SDL_HWSURFACE 0x00000001 /**< Surface is in video memory */
+#~ #define SDL_ASYNCBLIT 0x00000004 /**< Use asynchronous blits if possible */
+#~ /*@}*/
+
+#~ /** Available for SDL_SetVideoMode() */
+#~ /*@{*/
+#~ #define SDL_ANYFORMAT 0x10000000 /**< Allow any video depth/pixel-format */
+#~ #define SDL_HWPALETTE 0x20000000 /**< Surface has exclusive palette */
+#~ #define SDL_DOUBLEBUF 0x40000000 /**< Set up double-buffered video mode */
+#~ #define SDL_FULLSCREEN 0x80000000 /**< Surface is a full screen display */
+#~ #define SDL_OPENGL 0x00000002 /**< Create an OpenGL rendering context */
+#~ #define SDL_OPENGLBLIT 0x0000000A /**< Create an OpenGL rendering context and use it for blitting */
+#~ #define SDL_RESIZABLE 0x00000010 /**< This video mode may be resized */
+#~ #define SDL_NOFRAME 0x00000020 /**< No window caption or edge frame */
+#~ /*@}*/
+
+#~ /** Used internally (read-only) */
+#~ /*@{*/
+#~ #define SDL_HWACCEL 0x00000100 /**< Blit uses hardware acceleration */
+#~ #define SDL_SRCCOLORKEY 0x00001000 /**< Blit uses a source color key */
+#~ #define SDL_RLEACCELOK 0x00002000 /**< Private flag */
+#~ #define SDL_RLEACCEL 0x00004000 /**< Surface is RLE encoded */
+#~ #define SDL_SRCALPHA 0x00010000 /**< Blit uses source alpha blending */
+#~ #define SDL_PREALLOC 0x01000000 /**< Surface uses preallocated memory */
+#~ /*@}*/
+
+#~ /*@}*/
+
+#~ /** Evaluates to true if the surface needs to be locked before access */
+#~ #define SDL_MUSTLOCK(surface) \
+ #~ (surface->offset || \
+ #~ ((surface->flags & (SDL_HWSURFACE|SDL_ASYNCBLIT|SDL_RLEACCEL)) != 0))
+
+#~ /** typedef for private surface blitting functions */
+#~ typedef int (*SDL_blit)(struct SDL_Surface *src, SDL_Rect *srcrect,
+ #~ struct SDL_Surface *dst, SDL_Rect *dstrect);
+
+
+#~ /** Useful for determining the video hardware capabilities */
+#~ typedef struct SDL_VideoInfo {
+ #~ Uint32 hw_available :1; /**< Flag: Can you create hardware surfaces? */
+ #~ Uint32 wm_available :1; /**< Flag: Can you talk to a window manager? */
+ #~ Uint32 UnusedBits1 :6;
+ #~ Uint32 UnusedBits2 :1;
+ #~ Uint32 blit_hw :1; /**< Flag: Accelerated blits HW --> HW */
+ #~ Uint32 blit_hw_CC :1; /**< Flag: Accelerated blits with Colorkey */
+ #~ Uint32 blit_hw_A :1; /**< Flag: Accelerated blits with Alpha */
+ #~ Uint32 blit_sw :1; /**< Flag: Accelerated blits SW --> HW */
+ #~ Uint32 blit_sw_CC :1; /**< Flag: Accelerated blits with Colorkey */
+ #~ Uint32 blit_sw_A :1; /**< Flag: Accelerated blits with Alpha */
+ #~ Uint32 blit_fill :1; /**< Flag: Accelerated color fill */
+ #~ Uint32 UnusedBits3 :16;
+ #~ Uint32 video_mem; /**< The total amount of video memory (in K) */
+ #~ SDL_PixelFormat *vfmt; /**< Value: The format of the video surface */
+ #~ int current_w; /**< Value: The current video mode width */
+ #~ int current_h; /**< Value: The current video mode height */
+#~ } SDL_VideoInfo;
+
+
+#~ /** @name Overlay Formats
+ #~ * The most common video overlay formats.
+ #~ * For an explanation of these pixel formats, see:
+ #~ * http://www.webartz.com/fourcc/indexyuv.htm
+ #~ *
+ #~ * For information on the relationship between color spaces, see:
+ #~ * http://www.neuro.sfc.keio.ac.jp/~aly/polygon/info/color-space-faq.html
+ #~ */
+#~ /*@{*/
+#~ #define SDL_YV12_OVERLAY 0x32315659 /**< Planar mode: Y + V + U (3 planes) */
+#~ #define SDL_IYUV_OVERLAY 0x56555949 /**< Planar mode: Y + U + V (3 planes) */
+#~ #define SDL_YUY2_OVERLAY 0x32595559 /**< Packed mode: Y0+U0+Y1+V0 (1 plane) */
+#~ #define SDL_UYVY_OVERLAY 0x59565955 /**< Packed mode: U0+Y0+V0+Y1 (1 plane) */
+#~ #define SDL_YVYU_OVERLAY 0x55595659 /**< Packed mode: Y0+V0+Y1+U0 (1 plane) */
+#~ /*@}*/
+
+#~ /** The YUV hardware video overlay */
+#~ typedef struct SDL_Overlay {
+ #~ Uint32 format; /**< Read-only */
+ #~ int w, h; /**< Read-only */
+ #~ int planes; /**< Read-only */
+ #~ Uint16 *pitches; /**< Read-only */
+ #~ Uint8 **pixels; /**< Read-write */
+
+ #~ /** @name Hardware-specific surface info */
+ #~ /*@{*/
+ #~ struct private_yuvhwfuncs *hwfuncs;
+ #~ struct private_yuvhwdata *hwdata;
+ #~ /*@{*/
+
+ #~ /** @name Special flags */
+ #~ /*@{*/
+ #~ Uint32 hw_overlay :1; /**< Flag: This overlay hardware accelerated? */
+ #~ Uint32 UnusedBits :31;
+ #~ /*@}*/
+#~ } SDL_Overlay;
View
2 lib/SDL/Color.pm
@@ -1,5 +1,5 @@
-class SDL::Color is repr('CStruct');
+unit class SDL::Color is repr('CStruct');
#~ has uint8 $.r;
#~ has uint8 $.g;
View
2 lib/SDL/Palette.pm6
@@ -1,5 +1,5 @@
-class SDL::Palette is repr('CStruct');
+unit class SDL::Palette is repr('CStruct');
use NativeCall;
use SDL::Color;
View
2 lib/SDL/PixelFormat.pm6
@@ -1,5 +1,5 @@
-class SDL::PixelFormat is repr('CStruct');
+unit class SDL::PixelFormat is repr('CStruct');
use SDL::Palette;
View
15 lib/SDL/Rect.pm6
@@ -1,14 +1,15 @@
-unit class SDL::Rect is repr('CStruct');
-has int16 $.x;
-has int16 $.y;
-has uint16 $.w;
-has uint16 $.h;
+use SDL::CStructs;
+unit class SDL::Rect is repr('CStruct') is SDL_Rect;
+#~ has int16 $.x;
+#~ has int16 $.y;
+#~ has uint16 $.w;
+#~ has uint16 $.h;
multi method new( :$x!, :$y = 0, :$w = 0, :$h = 0 ) {
- self.bless( :$x, :$y, :$w, :$h );
+ self.bless( :$x, :$y, :$w, :$h )
}
multi method new( $x = 0, $y = 0, $w = 0, $h = 0 ) {
- self.bless( :$x, :$y, :$w, :$h );
+ self.bless( :$x, :$y, :$w, :$h )
}
View
124 lib/SDL/SFont.pm6
@@ -2,87 +2,81 @@
use NativeCall;
use SDL::Rect;
use SDL::Surface;
+use SDL::Surfacish;
-sub IMG_Load_RW( Pointer, int32 ) returns SDL::Surface is native('libSDL_image') { * }
+unit class SDL::SFont does SDL::Surfacish;
+has $.char_pos = [];
-class SDL::SFont is SDL::Surface {
- has $.char_pos = Array.new;
-
- method new($file) {
- IMG_Load_RW($file)
-
- }
-
- method init {
- my $x = 0;
- my $i = 0;
- my $pointer = SDL::Surface::_get_pointer(self.struct[1]);
- my $color = SDL::Surface::_map_rgb( $pointer, 255, 0, 255 );
+method new($file) {
+ my $self = self.bless: surface => SDL::Surface.new($file);
+ $self.init;
+ $self
+}
- #if (SDL_MUSTLOCK(Font->Surface)) SDL_LockSurface(Font->Surface);
- while ( $x < self.width ) {
- if self.get_pixel( $x ) == $color {
- $!char_pos[$i++] = $x;
- while ($x < self.width - 1) && (self.get_pixel( $x ) == $color) {
- $x++
- }
- $!char_pos[$i++] = $x;
- }
- $x++;
- }
+method FALLBACK($name, |c) {
+ $!surface."$name"(|c)
+}
- #if (SDL_MUSTLOCK(Font->Surface)) SDL_UnlockSurface(Font->Surface);
+method init {
+ my $x = 0;
+ my $i = 0;
+ my $color = SDL::Surface::_map_rgb(self.format, 255, 0, 255);
- #Font->h=Font->Surface->h;
- #SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, SFont_GetPixel(Font->Surface, 0, Font->Surface->h-1));
+ #if (SDL_MUSTLOCK(Font->Surface)) SDL_LockSurface(Font->Surface);
+ while ( $x < self.w ) {
+ if self.get_pixel( $x ) == $color {
+ $!char_pos[$i++] = $x;
+ while ($x < self.surface.w - 1) && (self.surface.get_pixel( $x ) == $color) {
+ $x++
+ }
+ $!char_pos[$i++] = $x;
+ }
+ $x++;
}
- multi method text_width ( Int $text ) {
- self.text_width( $text.Str )
- }
+ #if (SDL_MUSTLOCK(Font->Surface)) SDL_UnlockSurface(Font->Surface);
- multi method text_width ( Str $text ) {
- my Int $o;
- my Int $i = 0;
- my Int $x = 0;
+ #Font->h=Font->Surface->h;
+ #SDL_SetColorKey(Font->Surface, SDL_SRCCOLORKEY, SFont_GetPixel(Font->Surface, 0, Font->Surface->h-1));
+}
- while $i < $text.chars {
- if $text.substr( $i, 1 ) ~~ ' ' {
- $x += $!char_pos[2] - $!char_pos[1];
- }
- else {
- $o = ($text.substr( $i, 1 ).ord - 33) * 2 + 1;
- $x += $!char_pos[$o + 1] - $!char_pos[$o];
- }
- $i++;
- }
+method text_width ( Str() $text ) {
+ my Int $o;
+ my Int $i = 0;
+ my Int $x = 0;
- return $x
+ while $i < $text.chars {
+ if $text.substr( $i, 1 ) ~~ ' ' {
+ $x += $!char_pos[2] - $!char_pos[1];
+ }
+ else {
+ $o = ($text.substr( $i, 1 ).ord - 33) * 2 + 1;
+ $x += $!char_pos[$o + 1] - $!char_pos[$o];
+ }
+ $i++;
}
- multi method blit_text ( SDL::Surface $target, Int $x is copy, Int $y, Int $text ) {
- self.blit_text( $target, $x, $y, $text.Str );
- }
+ return $x
+}
- multi method blit_text ( SDL::Surface $target, Int $x is copy, Int $y, Str $text ) {
- my Int $o;
- my Int $i = 0;
+method blit_text($target, Int $x is copy, Int $y, Str() $text ) {
+ my Int $o;
+ my Int $i = 0;
- while $i < $text.chars {
- if $text.substr( $i, 1 ) ~~ ' ' {
- $x += $!char_pos[2] - $!char_pos[1];
- }
- else {
- $o = ($text.substr( $i, 1 ).ord - 33) * 2 + 1;
- my $srcrect = SDL::Rect.new( (($!char_pos[$o] + $!char_pos[$o - 1]) / 2).Int, 1,
- (($!char_pos[$o + 2] + $!char_pos[$o + 1]) / 2 - ($!char_pos[$o] + $!char_pos[$o - 1]) / 2).Int, self.height - 1 );
- my $dstrect = SDL::Rect.new( ($x - ($!char_pos[$o] - $!char_pos[$o - 1]) / 2).Int, $y, 0, 0 );
+ while $i < $text.chars {
+ if $text.substr( $i, 1 ) ~~ ' ' {
+ $x += $!char_pos[2] - $!char_pos[1];
+ }
+ else {
+ $o = ($text.substr( $i, 1 ).ord - 33) * 2 + 1;
+ my $srcrect = SDL::Rect.new( (($!char_pos[$o] + $!char_pos[$o - 1]) / 2).Int, 1,
+ (($!char_pos[$o + 2] + $!char_pos[$o + 1]) / 2 - ($!char_pos[$o] + $!char_pos[$o - 1]) / 2).Int, self.h - 1 );
+ my $dstrect = SDL::Rect.new( ($x - ($!char_pos[$o] - $!char_pos[$o - 1]) / 2).Int, $y, 0, 0 );
- SDL::Surface::_blit( self.pointer, $srcrect.CArray, $target.pointer, $dstrect.CArray );
+ self.surface.blit($target, $srcrect, $dstrect);
- $x += $!char_pos[$o + 1] - $!char_pos[$o];
- }
- $i++;
+ $x += $!char_pos[$o + 1] - $!char_pos[$o];
}
+ $i++;
}
}
View
207 lib/SDL/Surface.pm6
@@ -3,110 +3,25 @@ use NativeCall;
use SDL::Rect;
use SDL::PixelFormat;
-#~ use soft;
-#~ use Inline;
-
-class SDL::Surface is repr('CStruct') {
- has uint32 $.flags;
- has SDL::PixelFormat $.format;
- has int32 $.w;
- has int32 $.h;
- has uint16 $.pitch;
- #~ has int16 $.pitch;
- has Pointer $.pixels;
- has int32 $.offset;
- has Pointer $.hwdata; # private_hwdata
- has SDL::Rect $.clip_rect;
- #~ has uint32 $.unused1;
- has int32 $.unused1;
- #~ has uint32 $.locked;
- has int32 $.locked;
- has Pointer $.map; # SDL::BlitMap
- #~ has uint32 $.format_version;
- has int32 $.format_version;
- has int32 $.refcount;
-
- #~ has Pointer $.pointer;
- #~ has Pointer $.rw;
- #~ has Str $.file;
- #~ has SDL_Surface $.surface;
- #~ has $.struct;
- #~ has $.format;
- #~ has $.format_bpp;
- #~ has $.format_Bpp;
- #~ has $.format_rloss;
- #~ has $.format_gloss;
- #~ has $.format_bloss;
- #~ has $.format_aloss;
- #~ has $.format_rshift;
- #~ has $.format_gshift;
- #~ has $.format_bshift;
- #~ has $.format_ashift;
- #~ has $.format_rmask;
- #~ has $.format_gmask;
- #~ has $.format_bmask;
- #~ has $.format_amask;
- #~ has $.format_colorkey;
- #~ has $.format_alpha;
- #~ has $.palette;
- #~ has $.pixels;
- #~ has Bool $.is_bmp;
- #~ has Bool $.is_cur;
- #~ has Bool $.is_ico;
- #~ has Bool $.is_gif;
- #~ has Bool $.is_jpg;
- #~ has Bool $.is_lbm;
- #~ has Bool $.is_pcx;
- #~ has Bool $.is_png;
- #~ has Bool $.is_pnm;
- #~ has Bool $.is_tif;
- #~ has Bool $.is_xcf;
- #~ has Bool $.is_xpm;
- #~ has Bool $.is_xv;
-
- #~ method new( Str $file ) {
- #~ self.bless( :$file );
- #~ }
+use SDL::CStructs;
+use SDL::Surfacish;
- #~ submethod BUILD( :$file, :$rw is copy = Pointer, :$pointer = Pointer ) {
- #~ if $file {
- #~ $rw = _rw_from_file( $file, 'rb' );
- #~ }
- #~ elsif $pointer {
- #~ $rw = _rw_from_const_mem( $pointer, 42 ); # this rw doesn't get freed
- #~ }
-
- #~ $!is_bmp = _is_bmp( $!rw ).Bool;
- #~ $!is_cur = _is_cur( $!rw ).Bool;
- #~ $!is_ico = _is_ico( $!rw ).Bool;
- #~ $!is_gif = _is_gif( $!rw ).Bool;
- #~ $!is_jpg = _is_jpg( $!rw ).Bool;
- #~ $!is_lbm = _is_lbm( $!rw ).Bool;
- #~ $!is_pcx = _is_pcx( $!rw ).Bool;
- #~ $!is_png = _is_png( $!rw ).Bool;
- #~ $!is_pnm = _is_pnm( $!rw ).Bool;
- #~ $!is_tif = _is_tif( $!rw ).Bool;
- #~ $!is_xcf = _is_xcf( $!rw ).Bool;
- #~ $!is_xpm = _is_xpm( $!rw ).Bool;
- #~ $!is_xv = _is_xv( $!rw ).Bool;
-
- #~ unless $!pointer {
- #~ $!pointer = _load_rw( $!rw, 1 ); # $rw gets freed after that
- #~ }
-
-
- #~ self.init();
- #~ }
+class SDL::Surface is SDL_Surface is repr('CStruct') {
- #~ method init { }
+ multi method new(Str $file) {
+ _load($file)
+ }
- #~ method blit( SDL::Surface $target, SDL::Rect $clip = SDL::Rect, SDL::Rect $pos = SDL::Rect ) returns Int {
- #~ _blit( $!pointer, $clip ?? $clip.CArray !! CArray[int], $target.pointer, $pos ?? $pos.CArray !! CArray[int] )
- #~ }
+ multi method blit( SDL::Surface $target, SDL::Rect $clip = SDL::Rect, SDL::Rect $pos = SDL::Rect ) returns int32 {
+ _blit( self, $clip, $target, $pos )
+ }
+ multi method blit( SDL::Surfacish $target, SDL::Rect $clip = SDL::Rect, SDL::Rect $pos = SDL::Rect ) returns int32 {
+ _blit( self, $clip, $target.surface, $pos )
+ }
- #~ method update( $x = 0, $y = 0, $w = 0, $h = 0 ) {
- #~ _update( $!pointer, $x, $y, $w, $h )
- #~ }
+ method update( $x = 0, $y = 0, $w = 0, $h = 0 ) {
+ _update( self, $x, $y, $w, $h )
+ }
#~ method get_clip_rect {
#~ my $clip = CArray[int].new();
@@ -122,53 +37,57 @@ class SDL::Surface is repr('CStruct') {
#~ _fill_rect( $!pointer, SDL::Rect.new( $x, $y, 1, 1 ).CArray, $color )
#~ }
- #~ method get_pixel ( Int $x = 0, Int $y = 0 ) returns Int {
- #~ my $offset = ($x + $y * self.width) * $!format_Bpp;
- #~ my $chunk = ($offset / 8).Int; # amd64 only :/
- #~ my $pos = $offset % 8 * 8;
- #~ return $pos ?? $!pixels[ $chunk ] +& 0xFFFFFFFF00000000 +> $pos !! $!pixels[ $chunk ] +& 0xFFFFFFFF; # 1 Bpp => 32bpp currently only
- #~ }
-
- #~ # http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
- #~ method draw_line ( Int $x0 is copy = 0, Int $y0 is copy = 0, Int $x1 is copy = 0, Int $y1 is copy = 0, Int $color = 0xFFFFFFFF ) {
- #~ my Int $dx = ($x1 - $x0).abs;
- #~ my Int $dy = ($y1 - $y0).abs;
- #~ my Int $sx = $x0 < $x1 ?? 1 !! -1;
- #~ my Int $sy = $y0 < $y1 ?? 1 !! -1;
- #~ my Int $err = $dx - $dy;
-
- #~ if $dx == 0 {
- #~ _fill_rect( $!pointer, SDL::Rect.new( $x0, $y0, 1, $dy ).CArray, $color );
- #~ }
- #~ elsif $dy == 0 {
- #~ _fill_rect( $!pointer, SDL::Rect.new( $x0, $y0, $dx, 1 ).CArray, $color );
- #~ }
- #~ else {
- #~ while 1 {
- #~ _fill_rect( $!pointer, SDL::Rect.new( $x0, $y0, 1, 1 ).CArray, $color );
- #~ last if $x0 == $x1 && $y0 == $y1;
-
- #~ my Int $e2 = 2 * $err;
-
- #~ if $e2 > -$dy {
- #~ $err = $err - $dy;
- #~ $x0 = $x0 + $sx;
- #~ }
-
- #~ if $e2 < $dx {
- #~ $err = $err + $dx;
- #~ $y0 = $y0 + $sy;
- #~ }
- #~ }
- #~ }
- #~ }
+ method get_pixel( Int $x = 0, Int $y = 0 ) {
+ my $offset = ($x + $y * self.w) * $.format.BytesPerPixel;
+ do given $.format.BitsPerPixel {
+ when 8 { nativecast(uint8, Pointer.new(+$.pixels + $offset)) }
+ when 16 { nativecast(uint16, Pointer.new(+$.pixels + $offset)) }
+ when 32 { nativecast(uint32, Pointer.new(+$.pixels + $offset)) }
+ }
+ }
+
+ # http://en.wikipedia.org/wiki/Bresenham%27s_line_algorithm
+ method draw_line( Int $x0 is copy = 0, Int $y0 is copy = 0, Int $x1 is copy = 0, Int $y1 is copy = 0, Int $color = 0xFFFFFFFF ) {
+ my Int $dx = ($x1 - $x0).abs;
+ my Int $dy = ($y1 - $y0).abs;
+ my Int $sx = $x0 < $x1 ?? 1 !! -1;
+ my Int $sy = $y0 < $y1 ?? 1 !! -1;
+ my Int $err = $dx - $dy;
+
+ if $dx == 0 {
+ _fill_rect( self, SDL::Rect.new( $x0, $y0, 1, $dy ), $color );
+ }
+ elsif $dy == 0 {
+ _fill_rect( self, SDL::Rect.new( $x0, $y0, $dx, 1 ), $color );
+ }
+ else {
+ while 1 {
+ _fill_rect( self, SDL::Rect.new( $x0, $y0, 1, 1 ), $color );
+ last if $x0 == $x1 && $y0 == $y1;
+
+ my Int $e2 = 2 * $err;
+
+ if $e2 > -$dy {
+ $err = $err - $dy;
+ $x0 = $x0 + $sx;
+ }
+
+ if $e2 < $dx {
+ $err = $err + $dx;
+ $y0 = $y0 + $sy;
+ }
+ }
+ }
+ }
}
+our sub _set_video_mode( int32 $w, int32 $h, int32 $bpp, int32 $flags ) returns SDL::Surface is native('libSDL') is symbol('SDL_SetVideoMode') { * }
our sub _init( int32 ) returns Int is native('libSDL_image') is symbol('IMG_Init') { * }
+our sub _load( Str ) returns SDL::Surface is native('libSDL_image') is symbol('IMG_Load') { * }
our sub _load_rw( Pointer, int32 ) returns SDL::Surface is native('libSDL_image') is symbol('IMG_Load_RW') { * }
-our sub _blit( Pointer, CArray[int32], Pointer, CArray[int32] ) returns Int is native('libSDL') is symbol('SDL_UpperBlit') { * }
+our sub _blit( SDL_Surface, SDL_Rect, SDL_Surface, SDL_Rect ) returns int32 is native('libSDL') is symbol('SDL_UpperBlit') { * }
our sub _flip( Pointer ) returns Int is native('libSDL') is symbol('SDL_Flip') { * }
-our sub _update( Pointer, int32, int32, int32, int32 ) is native('libSDL') is symbol('SDL_UpdateRect') { * }
+our sub _update( SDL_Surface, int32, int32, int32, int32 ) is native('libSDL') is symbol('SDL_UpdateRect') { * }
our sub _is_bmp( Pointer ) returns Int is native('libSDL_image') is symbol('IMG_isBMP') { * }
our sub _is_cur( Pointer ) returns Int is native('libSDL_image') is symbol('IMG_isCUR') { * }
our sub _is_ico( Pointer ) returns Int is native('libSDL_image') is symbol('IMG_isICO') { * }
@@ -184,6 +103,6 @@ our sub _is_xpm( Pointer ) returns Int
our sub _is_xv( Pointer ) returns Int is native('libSDL_image') is symbol('IMG_isXV') { * }
our sub _rw_from_file( Str, Str ) returns Pointer is native('libSDL') is symbol('SDL_RWFromFile') { * }
our sub _rw_from_const_mem( Pointer, int32 ) returns Pointer is native('libSDL') is symbol('SDL_RWFromConstMem') { * }
-our sub _fill_rect( Pointer, CArray[int32], int32 ) returns Int is native('libSDL') is symbol('SDL_FillRect') { * }
+our sub _fill_rect( SDL_Surface, SDL_Rect, int32 ) returns int32 is native('libSDL') is symbol('SDL_FillRect') { * }
our sub _get_clip_rect( Pointer, CArray[int32] ) is native('libSDL') is symbol('SDL_GetClipRect') { * }
-our sub _map_rgb( Pointer, int32, int32, int32 ) returns Int is native('libSDL') is symbol('SDL_MapRGB') { * }
+our sub _map_rgb( SDL_PixelFormat, uint8, uint8, uint8 ) returns uint32 is native('libSDL') is symbol('SDL_MapRGB') { * }
View
4 lib/SDL/Surfacish.pm6
@@ -0,0 +1,4 @@
+
+unit role SDL::Surfacish;
+
+has $.surface;

0 comments on commit e7cbce4

Please sign in to comment.
Something went wrong with that request. Please try again.