Skip to content
Browse files

Attempting to draw stuff on the window through the renderer class. Ad…

…ded SDL2::Rect too
  • Loading branch information...
1 parent 97204a5 commit 8bbe668a1ca1085f97e803fbd29532ccf76d184a @kthakore kthakore committed Mar 1, 2013
Showing with 174 additions and 4 deletions.
  1. +8 −0 Build.PL
  2. +16 −0 lib/SDL2/Rect.pm
  3. +83 −0 src/Core/objects/Rect.xs
  4. +49 −1 src/Core/objects/Renderer.xs
  5. +18 −3 t/001_load.t
View
8 Build.PL
@@ -33,8 +33,16 @@ my %subsystems = (
to => 'lib/SDL2/Renderer.xs',
},
libraries => [qw( SDL2 )],
+ },
+ Rect => {
+ file => {
+ from => 'src/Core/objects/Rect.xs',
+ to => 'lib/SDL2/Rect.xs',
+ },
+ libraries => [qw( SDL2 )],
}
+
);
View
16 lib/SDL2/Rect.pm
@@ -0,0 +1,16 @@
+package SDL2::Rect;
+use strict;
+use warnings;
+use vars qw(@ISA @EXPORT @EXPORT_OK);
+require Exporter;
+require DynaLoader;
+our @ISA = qw(Exporter DynaLoader);
+
+use SDL2::Internal::Loader;
+internal_load_dlls(__PACKAGE__);
+
+bootstrap SDL2::Rect;
+
+use base 'Exporter';
+
+1;
View
83 src/Core/objects/Rect.xs
@@ -0,0 +1,83 @@
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+#include "ppport.h"
+#include "helper.h"
+
+#ifndef aTHX_
+#define aTHX_
+#endif
+
+#include <SDL2/SDL.h>
+
+MODULE = SDL2::Rect PACKAGE = SDL2::Rect PREFIX = rect_
+
+=for documentation
+
+SDL_Rect -- Defines a rectangular area
+
+ typedef struct{
+ Sint16 x, y;
+ Uint16 w, h;
+ } SDL_Rect;
+
+=cut
+
+SDL_Rect *
+rect_new (CLASS, x, y, w, h)
+ char* CLASS
+ Sint16 x
+ Sint16 y
+ Uint16 w
+ Uint16 h
+ CODE:
+ RETVAL = (SDL_Rect *) safemalloc (sizeof(SDL_Rect));
+ RETVAL->x = x;
+ RETVAL->y = y;
+ RETVAL->w = w;
+ RETVAL->h = h;
+ OUTPUT:
+ RETVAL
+
+Sint16
+rect_x ( rect, ... )
+ SDL_Rect *rect
+ CODE:
+ if (items > 1 ) rect->x = SvIV(ST(1));
+ RETVAL = rect->x;
+ OUTPUT:
+ RETVAL
+
+Sint16
+rect_y ( rect, ... )
+ SDL_Rect *rect
+ CODE:
+ if (items > 1 ) rect->y = SvIV(ST(1));
+ RETVAL = rect->y;
+ OUTPUT:
+ RETVAL
+
+Uint16
+rect_w ( rect, ... )
+ SDL_Rect *rect
+ CODE:
+ if (items > 1 ) rect->w = SvIV(ST(1));
+ RETVAL = rect->w;
+ OUTPUT:
+ RETVAL
+
+Uint16
+rect_h ( rect, ... )
+ SDL_Rect *rect
+ CODE:
+ if (items > 1 ) rect->h = SvIV(ST(1));
+ RETVAL = rect->h;
+ OUTPUT:
+ RETVAL
+
+
+void
+rect_DESTROY(bag)
+ SV *bag
+ CODE:
+ objDESTROY(bag, safefree);
View
50 src/Core/objects/Renderer.xs
@@ -26,11 +26,59 @@ renderer_new (CLASS, window, index, flags)
Uint32 flags
CODE:
SDL_Renderer* renderer = SDL_CreateRenderer(window, index, flags);
- //warn( "Made renderer %p", renderer);
+ if( renderer == NULL )
+ warn( "ERROR making renderer: %s", SDL_GetError() );
RETVAL = renderer;
OUTPUT:
RETVAL
+int
+renderer_clear ( renderer )
+ SDL_Renderer* renderer
+ CODE:
+ RETVAL = SDL_RenderClear( renderer );
+ OUTPUT:
+ RETVAL
+
+int
+renderer_draw_rect (renderer, rect)
+ SDL_Renderer* renderer
+ SDL_Rect* rect
+ CODE:
+ RETVAL = SDL_RenderDrawRect(renderer, rect);
+ OUTPUT:
+ RETVAL
+
+
+int
+renderer_fill_rect (renderer, rect)
+ SDL_Renderer* renderer
+ SDL_Rect* rect
+ CODE:
+ RETVAL = SDL_RenderFillRect(renderer, rect);
+ OUTPUT:
+ RETVAL
+
+
+void
+renderer_present ( renderer )
+ SDL_Renderer* renderer
+ CODE:
+ SDL_RenderPresent( renderer );
+
+int
+renderer_set_draw_color (renderer, r, g, b, a)
+ SDL_Renderer* renderer
+ Uint8 r
+ Uint8 g
+ Uint8 b
+ Uint8 a
+ CODE:
+ RETVAL = SDL_SetRenderDrawColor(renderer, r, g, b, a);
+ OUTPUT:
+ RETVAL
+
+
void
renderer_DESTROY(bag)
SV *bag
View
21 t/001_load.t
@@ -2,15 +2,16 @@
# t/001_load.t - check module loading and create testing directory
-use Test::More tests => 4;
+use Test::More tests => 5;
BEGIN {
use_ok( 'SDL2pp' );
use_ok( 'SDL2::Window' );
use_ok( 'SDL2::Renderer' );
+ use_ok( 'SDL2::Rect' );
}
-SDL2pp::init(0);
+exit 0 if SDL2pp::init(0x00000020) < 0 ; #SDL_INIT_VIDEO
my $window_flags => { SDL_WINDOW_FULLSCREEN => 0x00000001,
SDL_WINDOW_OPENGL => 0x00000002,
@@ -29,7 +30,21 @@ my $window_flags => { SDL_WINDOW_FULLSCREEN => 0x00000001,
my $win = SDL2::Window->new("FIRST WINDOW", 50, 50, 200, 200, $window_flags->{SDL_WINDOW_SHOWN} | $window_flags->{SDL_WINDOW_OPENGL});
-my $renderer = SDL2::Renderer->new($win, -1, 0);
+my $renderer = SDL2::Renderer->new($win, -1, 0); #Hardware accelerated with software fallback
+
+my $rect = SDL2::Rect->new(0,0,4,4);
+
+warn $renderer->set_draw_color(255,0,0,255);
+
+warn $renderer->clear();
+
+warn $renderer->set_draw_color(0, 255,0,255);
+
+warn $renderer->fill_rect($rect);
+
+my $rect2 = SDL2::Rect->new(4,4,10,10);
+
+warn $renderer->draw_rect($rect);
SDL2pp::delay(3000);

0 comments on commit 8bbe668

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