X-Git-Url: http://git.shadowcat.co.uk/gitweb/gitweb.cgi?a=blobdiff_plain;f=pages%2Fblog-0018.html-inc;h=0de3c832cedb93b3d65e9876f3ff91874c42d50a;hb=ca0a3441dd72000cbbbb4be484b18d0d305fdb29;hp=f5d4032823b6c03d8dddcead7217e725791e5374;hpb=56d4907c407e8564b4a8a9d09e252573131fad3e;p=sdlgit%2FSDL-Site.git diff --git a/pages/blog-0018.html-inc b/pages/blog-0018.html-inc index f5d4032..0de3c83 100644 --- a/pages/blog-0018.html-inc +++ b/pages/blog-0018.html-inc @@ -1,26 +1,59 @@

-The beginnings of modular design for SDL Perl +Once in a while .... (set_event_filter)

-
“Do or do not... there is no try.”
-
--yoda
+

+Once in a while
+Things just work!
+

+


-

The design before


-The bindings before were all in one huge XS file. This was then exported into the SDL module. This means that the XS file has to handle with macros if any component (e.x SDL_Mixer) is not compiled. Moreover having ever binding in one XS file prevents use to treat C structs as object with only one point of free and malloc. This would be BEGIN and DESTROY in Perl. Also the monolithic design introduces a lot of bugs because we have to use free and malloc all over the place. Lastly SDL monolithic design has the constructor for all structs in both Perl and in XS.
+So I have been hacking for a while on SDL::Events::set_event_filter. The code below is an example usage. The magic behind this is here

-

The design we are aiming for

Simple one XS per Module. This would also simplify the Build code.
-
-

First Step


-We have began with SDL Rect. It is in github master branch now. We are in the progress of making it back compatible. Originally SDL::Rect took named variables as parameters for new(). Now since the constructor is in XS we have only unnamed parameters.
-
-
-

Before


-SDL::Rect->new( -x => 0, -y => 0, -width => 0, -height => 0);
-
-

After


-SDL::Rect->new(0, 0, 0, 0);
-
-Ideally we would like both ways of constructing Rect. -


-

\ No newline at end of file +
 1 #!/usr/bin/perl -w
+ 2 use strict;
+ 3 use warnings;
+ 4 use SDL v2.3; #Require the redesign branch
+ 5 use SDL::Video;
+ 6 use SDL::Event;
+ 7 use SDL::Events;
+ 8 
+ 9 SDL::init(SDL_INIT_VIDEO);
+10 my $display = SDL::Video::set_video_mode(640,480,32, SDL_SWSURFACE );
+11 my  $event = SDL::Event->new();
+12 
+13 #This filters out all ActiveEvents
+14 my $filter = sub { 
+15      my ($e, $type) = ($_[0], $_[0]->type); 
+16      if($type == SDL_ACTIVEEVENT){ return 0 } 
+17      elsif($type == SDL_MOUSEBUTTONDOWN && $e->button_button == 1){ return 0 }
+18      else { return 1; }
+19       };
+20 
+21 SDL::Events::set_event_filter($filter);
+22 
+23 while(1)
+24 {
+25 
+26   SDL::Events::pump_events();
+27   if(SDL::Events::poll_event($event))
+28   {
+29 
+30   if(  $event->type == SDL_ACTIVEEVENT)
+31  {
+32  print "Hello Mouse!!!\n" if ($event->active_gain && ($event->active_state == SDL_APPMOUSEFOCUS) );
+33  print "Bye Mouse!!!\n" if (!$event->active_gain && ($event->active_state == SDL_APPMOUSEFOCUS) );
+34         }
+35   if( $event->type == SDL_MOUSEBUTTONDOWN)
+36    {
+37  my ($x, $y, $but ) = ($event->button_x, $event->button_y, $event->button_button);
+38  warn "$but CLICK!!! at $x and $y \n";
+39  }
+40 
+41       last if($event->type == SDL_QUIT);
+42   }
+43 }
+44 SDL::quit()
 
 
Tinker with $filter and look at perldoc lib/SDL/pods/Event.pod. 
 
Have fun,
--yapgh
+


+

\ No newline at end of file