Permalink
Browse files

First commit. Fleshing out some oop way of doing this. Needs more Work

  • Loading branch information...
0 parents commit 8c7213a4987b84b5b907cbb68198f02bd9189f37 @kthakore committed Aug 12, 2010
Showing with 103 additions and 0 deletions.
  1. +2 −0 .gitignore
  2. +58 −0 lib/Game.pm
  3. +37 −0 lib/Object.pm
  4. +6 −0 scroller.pl
@@ -0,0 +1,2 @@
+*.bak
+ref/*
@@ -0,0 +1,58 @@
+package Game;
+use strict;
+use warnings;
+
+use base 'SDLx::App';
+
+use SDL;
+use SDL::Event;
+use SDL::Events;
+use Scalar::Util 'refaddr';
+use lib 'lib';
+use Object;
+
+my %update;
+my %hero;
+
+sub new {
+ my $class = shift;
+ my $obj =
+ SDLx::App->new( w => 200, h => 200, dt => 10, title => 'Scroller' );
+ my $self = bless $obj, $class;
+ $obj->add_event_handler( sub { $self->_dispatcher( $_[0] ) } );
+ $obj->add_show_handler( sub { $self->_render } );
+ $self->on_init();
+ return $self;
+
+}
+
+sub on_init {
+ $hero{ refaddr $_[0] } = Object->new( width => 10, height => 10 );
+ $hero{ refaddr $_[0] }->x(10);
+ $hero{ refaddr $_[0] }->y( $_[0]->w - 50 );
+
+}
+
+sub _dispatcher {
+ my ( $self, $event ) = @_;
+
+ return if $event->type == SDL_QUIT;
+
+ return $self->hero->event($event);
+
+ return 1;
+}
+
+sub _render {
+ $_[0]->hero->draw_xy( $_[0] );
+ $_[0]->update();
+
+}
+
+sub hero {
+
+ return $hero{ refaddr $_[0] };
+
+}
+
+1;
@@ -0,0 +1,37 @@
+package Object;
+use strict;
+use warnings;
+use SDL::Event;
+use SDL::Events;
+use base 'SDLx::Sprite';
+
+sub new {
+ my $class = shift;
+ my $obj = SDLx::Sprite->new( width => 10, height => 10 );
+ my $self = bless $obj, $class;
+ $self->_render();
+
+ $self->{state} = 'stand';
+ return $self;
+
+}
+
+sub _render {
+ my $self = shift;
+ $self->surface->draw_rect( [ 0, 0, 10, 10 ], 0xFF00FFF );
+ return $self;
+
+}
+
+sub event {
+ my ( $self, $event ) = @_;
+
+ $self->x( $self->x + 2 ) if $event->key_sym == SDLK_LEFT;
+
+ return 1;
+}
+
+sub evaluate { }
+
+sub accelerate { }
+1;
@@ -0,0 +1,6 @@
+use strict;
+use warnings;
+use lib 'lib';
+use Game;
+
+Game->new->run();

0 comments on commit 8c7213a

Please sign in to comment.