Permalink
Browse files

Made windo an object

  • Loading branch information...
1 parent 077fafd commit 4e20cdacf08f2cc8f0607e018a6aeec228e1c67f @kthakore kthakore committed Mar 1, 2013
Showing with 215 additions and 30 deletions.
  1. +8 −0 Build.PL
  2. +8 −0 lib/SDL2.pm
  3. +22 −0 lib/SDL2/Window.pm
  4. +1 −1 lib/SDL2pp.pm
  5. +45 −0 src/Core/objects/Window.xs
  6. +0 −24 src/SDL2pp.xs
  7. +125 −0 src/helper.h
  8. +6 −5 t/001_load.t
View
@@ -19,6 +19,14 @@ my %subsystems = (
to => 'lib/SDL2pp.xs',
},
libraries => [qw( SDL2 )],
+ },
+ Window => {
+ file => {
+ from => 'src/Core/objects/Window.xs',
+ to => 'lib/SDL2/Window.xs',
+ },
+ libraries => [qw( SDL2 )],
+
}
);
View
@@ -2,6 +2,14 @@ package SDL2;
use strict;
use warnings;
use SDL2pp;
+use vars qw($VERSION $XS_VERSION @ISA @EXPORT @EXPORT_OK);
+require Exporter;
+require DynaLoader;
+our @ISA = qw(Exporter DynaLoader);
+
+our $VERSION = '0.01';
+our $XS_VERSION = $VERSION;
+$VERSION = eval $VERSION;
sub init {
return SDL2pp::init(@_);
View
@@ -0,0 +1,22 @@
+package SDL2::Window;
+use strict;
+use warnings;
+use vars qw($VERSION $XS_VERSION @ISA @EXPORT @EXPORT_OK);
+require Exporter;
+require DynaLoader;
+our @ISA = qw(Exporter DynaLoader);
+
+our $VERSION = '0.01';
+our $XS_VERSION = $VERSION;
+$VERSION = eval $VERSION;
+
+
+
+use SDL2::Internal::Loader;
+internal_load_dlls(__PACKAGE__);
+
+bootstrap SDL2::Window;
+
+use base 'Exporter';
+
+1;
View
@@ -7,7 +7,7 @@ require DynaLoader;
our @ISA = qw(Exporter DynaLoader);
our $VERSION = '0.01';
-our $XS_VERSION = 0;
+our $XS_VERSION = $VERSION;
$VERSION = eval $VERSION;
use SDL2::Internal::Loader;
View
@@ -0,0 +1,45 @@
+#include "EXTERN.h"
+#include "perl.h"
+#include "XSUB.h"
+#include "ppport.h"
+#include "helper.h"
+#include <stdio.h>
+
+#ifndef aTHX_
+#define aTHX_
+#endif
+
+#include <SDL2/SDL.h>
+
+MODULE = SDL2::Window PACKAGE = SDL2::Window PREFIX = window_
+
+=for documentation
+
+SDL_Window -- Defines an SDL window
+=cut
+
+SDL_Window *
+window_new (CLASS, title, x, y, w, h, flags)
+ char* CLASS
+ char* title
+ int x
+ int y
+ int w
+ int h
+ Uint32 flags
+ CODE:
+ SDL_Window* window = SDL_CreateWindow(title, x, y, w, h, flags);
+ warn( "Made window %p", window);
+ RETVAL = window;
+ OUTPUT:
+ RETVAL
+
+void
+window_DESTROY(bag)
+ SV *bag
+ CODE:
+ void* obj = bag2obj( bag );
+ SDL_Window* window = (SDL_Window*)obj;
+ warn( "Destroying bag: %p obj: %p window %p", bag, obj, window );
+ SDL_DestroyWindow( window );
+ warn( "Destroyed");
View
@@ -1,4 +1,3 @@
-
#include "EXTERN.h"
#include "perl.h"
#include "XSUB.h"
@@ -40,29 +39,6 @@ delay ( time )
SDL_Delay( time );
-SDL_Window *
-create_window (title, x, y, w, h, flags)
- char* title
- int x
- int y
- int w
- int h
- Uint32 flags
- PREINIT:
- char* CLASS = "SDL::Window";
- CODE:
- RETVAL = SDL_CreateWindow(title, x, y, w, h, flags);
- OUTPUT:
- RETVAL
-
-void
-destroy_window (window)
- SDL_Window * window
- CODE:
- SDL_DestroyWindow(window);
-
-
-
void
quit_sub_system ( flags )
Uint32 flags
View
@@ -0,0 +1,125 @@
+
+#ifndef SDL_PERL_HELPER_H
+#define SDL_PERL_HELPER_H
+
+#include <SDL.h>
+#include "SDL_thread.h"
+
+PerlInterpreter * perl = NULL;
+
+void *bag2obj( SV *bag )
+{
+ void *obj = NULL;
+
+ if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) )
+ {
+ void **pointers = (void **)INT2PTR(void *, SvIV((SV *)SvRV( bag )));
+ obj = (void *)(pointers[0]);
+ }
+
+ return obj;
+}
+
+SV *obj2bag( int size_ptr, void *obj, char *CLASS )
+{
+ SV * objref = newSV( size_ptr );
+ void** pointers = safemalloc(3 * sizeof(void*));
+ pointers[0] = (void*)obj;
+ pointers[1] = (void*)PERL_GET_CONTEXT;
+ Uint32 *threadid = (Uint32 *)safemalloc(sizeof(Uint32));
+ *threadid = SDL_ThreadID();
+ pointers[2] = (void*)threadid;
+ sv_setref_pv( objref, CLASS, (void *)pointers);
+ return objref;
+}
+
+SV *cpy2bag( void *object, int p_size, int s_size, char *package )
+{
+ SV *ref = newSV( p_size );
+ void *copy = safemalloc( s_size );
+ memcpy( copy, object, s_size );
+
+ void** pointers = safemalloc(3 * sizeof(void*));
+ pointers[0] = (void*)copy;
+ pointers[1] = (void*)PERL_GET_CONTEXT;
+ Uint32 *threadid = (Uint32 *)safemalloc(sizeof(Uint32));
+ *threadid = SDL_ThreadID();
+ pointers[2] = (void*)threadid;
+ SV* a = sv_setref_pv(ref, package, (void *)pointers);
+ return a;
+}
+
+void objDESTROY(SV *bag, void (* callback)(void *object))
+{
+ if( sv_isobject(bag) && (SvTYPE(SvRV(bag)) == SVt_PVMG) )
+ {
+ void** pointers = (void**)INT2PTR(void *, SvIV((SV *)SvRV( bag )));
+ void* object = pointers[0];
+ Uint32 *threadid = (Uint32*)(pointers[2]);
+
+ if(PERL_GET_CONTEXT == pointers[1]
+ && *threadid == SDL_ThreadID())
+ {
+ pointers[0] = NULL;
+ if(object)
+ callback(object);
+ safefree(threadid);
+ safefree(pointers);
+ }
+ }
+}
+
+SV *_sv_ref( void *object, int p_size, int s_size, char *package )
+{
+ SV *ref = newSV( p_size );
+ void *copy = safemalloc( s_size );
+ memcpy( copy, object, s_size );
+
+ void** pointers = safemalloc(3 * sizeof(void*));
+ pointers[0] = (void*)copy;
+ pointers[1] = (void*)perl;
+ Uint32 *threadid = (Uint32 *)safemalloc(sizeof(Uint32));
+ *threadid = SDL_ThreadID();
+ pointers[2] = (void*)threadid;
+
+ return 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;
+}
+
+void _int_range( int *val, int min, int max )
+{
+ if( *val < min )
+ *val = min;
+ else if ( *val > max )
+ *val = max;
+}
+
+#endif
View
@@ -2,9 +2,12 @@
# t/001_load.t - check module loading and create testing directory
-use Test::More tests => 2;
+use Test::More tests => 3;
-BEGIN { use_ok( 'SDL2pp' ); }
+BEGIN {
+ use_ok( 'SDL2pp' );
+ use_ok( 'SDL2::Window' );
+ }
SDL2pp::init(0);
@@ -23,12 +26,10 @@ my $window_flags => { SDL_WINDOW_FULLSCREEN => 0x00000001,
SDL_WINDOW_FOREIGN => 0x00000800
};
-my $win = SDL2pp::create_window("FIRST WINDOW", 50, 50, 200, 200, $window_flags->{SDL_WINDOW_SHOWN} | $window_flags->{SDL_WINDOW_OPENGL});
+my $win = SDL2::Window->new("FIRST WINDOW", 50, 50, 200, 200, $window_flags->{SDL_WINDOW_SHOWN} | $window_flags->{SDL_WINDOW_OPENGL});
SDL2pp::delay(3000);
-SDL2pp::destroy_window($win);
-
SDL2pp::quit();
pass();

0 comments on commit 4e20cda

Please sign in to comment.