Permalink
Browse files

implemented SDLx::Surface::draw_polygone, linking SDL_gfx to SDLx::Su…

…rface if available
  • Loading branch information...
Tobias Leich
Tobias Leich committed Mar 6, 2011
1 parent 8f36d4f commit e26d051c0af84044f1bede0f28cd98934026aec9
Showing with 98 additions and 44 deletions.
  1. +1 −1 Build.PL
  2. +29 −8 lib/SDLx/Surface.pm
  3. +1 −35 src/GFX/Primitives.xs
  4. +38 −0 src/SDLx/Surface.xs
  5. +29 −0 src/helper.h
View
@@ -165,7 +165,7 @@ my %subsystems = (
from => 'src/SDLx/Surface.xs',
to => 'lib/SDLx/Surface.xs',
},
- libraries => [qw( SDL )],
+ libraries => [qw( SDL SDL_gfx_primitives )],
},
TimerX => {
file => {
View
@@ -260,6 +260,35 @@ sub draw_line {
return $self;
}
+sub draw_poly {
+ my ( $self, $x_coords, $y_coords, $color, $antialias ) = @_;
+
+ Carp::confess "Error x_coords needs an array ref [x1,x2,x3,...]"
+ unless ref($x_coords) eq 'ARRAY';
+ Carp::confess "Error y_coords needs an array ref [y1,y2,y3,...]"
+ unless ref($y_coords) eq 'ARRAY';
+ Carp::confess "Error x_coords and y_coords should have the same size"
+ unless scalar @$x_coords == scalar @$y_coords;
+
+ unless ( SDL::Config->has('SDL_gfx_primitives') ) {
+ Carp::cluck("SDL_gfx_primitives support has not been compiled");
+ return;
+ }
+
+ $color = SDLx::Validate::num_rgba($color);
+
+ my $result;
+ if ($antialias) {
+ $result = SDL::GFX::Primitives::aapolygon_color( $self, $x_coords, $y_coords, scalar @$x_coords, $color );
+ } else {
+ $result = SDL::GFX::Primitives::polygon_color( $self, $x_coords, $y_coords, scalar @$x_coords, $color );
+ }
+
+ Carp::confess "Error drawing line: " . SDL::get_error() if ( $result == -1 );
+
+ return $self;
+}
+
sub draw_circle {
my ( $self, $center, $radius, $color, $antialias ) = @_;
@@ -317,19 +346,11 @@ sub draw_ellipse {
return $self;
}
-sub draw_polygon {
- my ( $self, $vector, $color ) = @_;
-
- return $self;
-
-}
-
sub draw_bezier {
my ( $self, $vector, $smooth, $color ) = @_;
}
-
sub draw_gfx_text {
my ( $self, $vector, $color, $text, $font ) = @_;
View
@@ -2,6 +2,7 @@
#include "perl.h"
#include "XSUB.h"
#include "ppport.h"
+#include "helper.h"
#ifndef aTHX_
#define aTHX_
@@ -34,41 +35,6 @@
#endif
-void _svinta_free(Sint16* av, int len_from_av_len)
-{
- if( av == NULL)
- return;
- safefree( av ); /* we only need to free the malloc'd array. It is one block. */
- av = NULL;
-
-}
-
-Sint16* av_to_sint16 (AV* av)
-{
- int len = av_len(av);
- if( len != -1)
- {
- int i;
- Sint16* table = (Sint16 *)safemalloc(sizeof(Sint16)*(len+1));
- for ( i = 0; i < len+1 ; i++ )
- {
- SV ** temp = av_fetch(av,i,0);
- if( temp != NULL )
- {
-
- table[i] = (Sint16) SvIV ( *temp );
- }
- else
- {
- table[i] = 0;
- }
- }
- return table;
-
- }
- return NULL;
-}
-
MODULE = SDL::GFX::Primitives PACKAGE = SDL::GFX::Primitives PREFIX = gfx_prim_
=for documentation
View
@@ -12,6 +12,10 @@
#include <SDL.h>
#include "SDLx/Validate.h"
+#ifdef HAVE_SDL_GFX_PRIMITIVES
+#include <SDL_gfxPrimitives.h>
+#endif
+
SV * get_pixel32 (SDL_Surface *surface, int x, int y)
{
@@ -196,6 +200,40 @@ surfacex_draw_rect ( surface, rt, color )
SDL_FillRect(surface, &r_rect, m_color);
}
+#ifdef HAVE_SDL_GFX_PRIMITIVES
+
+int
+surfacex_draw_polygon(surface, vectors, color, antialias)
+ SDL_Surface * surface
+ AV* vectors
+ Uint32 color
+ SV *antialias
+ CODE:
+ AV* vx = newAV();
+ AV* vy = newAV();
+ int n;
+ for(n = 0; n <= av_len(vectors); n++)
+ {
+ if(n & 1)
+ av_store(vy, (int)((n-1)/2), *av_fetch(vectors, n, 0));
+ else
+ av_store(vx, (int)(n/2), *av_fetch(vectors, n, 0));
+ }
+
+ n = av_len(vx) + 1;
+
+ Sint16 * _vx = av_to_sint16(vx);
+ Sint16 * _vy = av_to_sint16(vy);
+ RETVAL = SvOK(antialias)
+ ? aapolygonColor(surface, _vx, _vy, n, color)
+ : polygonColor(surface, _vx, _vy, n, color);
+ _svinta_free( _vx, av_len(vx) );
+ _svinta_free( _vy, av_len(vy) );
+ OUTPUT:
+ RETVAL
+
+#endif
+
void
surfacex_blit( src, dest, ... )
SV *src
View
@@ -86,4 +86,33 @@ SV *_sv_ref( void *object, int p_size, int s_size, char *package )
return newSVsv(sv_setref_pv(ref, package, (void *)pointers));
}
+void _svinta_free(Sint16* av, int len_from_av_len)
+{
+ if( av == NULL)
+ return;
+ safefree( av ); /* we only need to free the malloc'd array. It is one block. */
+ av = NULL;
+}
+
+Sint16* av_to_sint16 (AV* av)
+{
+ int len = av_len(av);
+ if( len != -1)
+ {
+ int i;
+ Sint16* table = (Sint16 *)safemalloc(sizeof(Sint16)*(len+1));
+ for ( i = 0; i < len+1 ; i++ )
+ {
+ SV ** temp = av_fetch(av,i,0);
+ if( temp != NULL )
+ table[i] = (Sint16) SvIV ( *temp );
+ else
+ table[i] = 0;
+ }
+ return table;
+
+ }
+ return NULL;
+}
+
#endif

0 comments on commit e26d051

Please sign in to comment.