Skip to content

Commit

Permalink
Scrolling sucks for now but will work on it later. Have to do some pa…
Browse files Browse the repository at this point in the history
…rallax and smooth scrolling
  • Loading branch information
kthakore committed Aug 7, 2011
1 parent 59f7247 commit 43bc14b
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 16 deletions.
17 changes: 7 additions & 10 deletions bin/zombie-demo.pl
Original file line number Diff line number Diff line change
@@ -1,24 +1,21 @@
#! perl
package main;
use Modern::Perl;
use SDL;
use SDL::Rect;
use SDL::Video;
use SDLx::App;
use SDL::Event;
use SDL::Events;
use FindBin;
use YAML::Tiny;

use SDLx::Surface;
use Box2D;
use lib 'lib';

use ZT::Object::Wall;
use ZT::Actor::Zombie;
use ZT::Util;
use ZT::Camera;
use ZT::Object::Wall;
use ZT::Actor::Zombie;
use BoxSDL::Controller;
use Data::Dumper;

use SDL::Event;
use SDL::Events;
use SDLx::Surface;

my $config = YAML::Tiny->read("$FindBin::Bin/../data/level1.yaml")->[0];

Expand Down
28 changes: 22 additions & 6 deletions lib/ZT/Camera.pm
Original file line number Diff line number Diff line change
Expand Up @@ -5,17 +5,32 @@ use SDLx::App;

sub new {

my $self = bless { x=> 0, y => 0, w=>400, h=> 300, c_x => 200, c_y => 150 }, $_[0];
my $self = bless { x=> 0, y => 0, w=>400, h=> 300, c_x => 200, c_y => 150 }, $_[0];

$self->{app} = SDLx::App->new(
width => $self->{w},
height => $self->{h},
flags => SDL_DOUBLEBUF | SDL_HWSURFACE,
);
width => $self->{w},
height => $self->{h},
flags => SDL_DOUBLEBUF | SDL_HWSURFACE,
);


return $self;
}

sub scroll {
my ($self, $m_x, $m_y ) = @_;

# Get the direction and just keep moving that way
my( $x, $y ) = (0, 0);

if( $m_x > ($self->{w}/2) ) { $x = 1 } else{ $x = -1 }
if( $m_y > ($self->{h}/2) ) { $y = 1 } else{ $y = -1 }

my $mag = 1;
return $self->move_rel( $x * $mag, $y * $mag);

}

sub move_rel {
my ($self, $m_x, $m_y ) = @_;

Expand All @@ -37,9 +52,10 @@ sub move_to {
sub update_view {
my ($self, $map_surface) = @_;

# The camera determines offset of the surface to show on here
# The camera determines offset of the surface to show on here
my $src_rect = [$self->{x}, $self->{y}, $self->{w}, $self->{h}];

$self->{app}->draw_rect([0,0,$self->{w}, $self->{h}], 0x000000FF);
$self->{app}->blit_by( $map_surface, $src_rect, [0,0, $self->{w}, $self->{h}] );

$self->{app}->update();
Expand Down

0 comments on commit 43bc14b

Please sign in to comment.