PageRenderTime 27ms CodeModel.GetById 9ms app.highlight 14ms RepoModel.GetById 1ms app.codeStats 0ms

/t_backcompat/MoP/lib/SDL/Tutorial/MoP/View/Map.pm

http://github.com/PerlGameDev/SDL
Perl | 103 lines | 79 code | 23 blank | 1 comment | 18 complexity | 14382f11291b98acab9e3e03726431d1 MD5 | raw file
  1package SDL::Tutorial::MoP::View::Map;
  2use strict;
  3use SDL;
  4use SDLx::App;
  5use SDL::Event;
  6use SDL::Video;
  7use File::Spec::Functions qw(rel2abs splitpath catpath catfile);
  8use Carp;
  9use SDL::Tutorial::MoP::Models;
 10
 11BEGIN {
 12	use Exporter ();
 13	use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
 14	$VERSION = '0.01';
 15	@ISA     = qw(Exporter);
 16
 17	#Give a hoot don't pollute, do not export more than needed by default
 18	@EXPORT      = qw();
 19	@EXPORT_OK   = qw(draw_map);
 20	%EXPORT_TAGS = ();
 21}
 22
 23use constant {
 24	MOP_TOP    => 0,
 25	MOP_BOTTOM => 1,
 26	MOP_RIGHT  => 2,
 27	MOP_LEFT   => 3
 28};
 29
 30my $screen;
 31my $screen_width  = 640;
 32my $screen_height = 480;
 33my $tile_size     = 10;
 34my $model         = SDL::Tutorial::MoP::Models->new;
 35my @map           = $model->map();
 36
 37my @map_center = ( 32, 24 ); # x, y
 38
 39my ( $volume, $dirs ) = splitpath( rel2abs(__FILE__) );
 40my $diff = 'MoP/../../';
 41$diff = '../' if ( $^O =~ /linux/ );
 42my $path = catpath( $volume, catfile( $dirs, $diff . 'tiles.bmp' ) );
 43my $tiles = SDL::Video::load_BMP($path);
 44Carp::confess 'Error: ' . SDL::get_error() if ( !$tiles );
 45
 46sub draw_map {
 47	Carp::cluck 'Unable to init SDL: ' . SDL::get_error()
 48		if ( SDL::init(SDL_INIT_VIDEO) < 0 );
 49
 50	$screen = SDL::Video::set_video_mode( 640, 480, 32, SDL_SWSURFACE );
 51
 52	Carp::cluck 'Unable to set 640x480x32 video ' . SDL::get_error() if ( !$screen );
 53
 54	move_map(MOP_LEFT);
 55
 56	my $x_offset = $map_center[0] - ( $screen_width / $tile_size / 2 );
 57	my $y_offset = $map_center[1] - ( $screen_height / $tile_size / 2 );
 58
 59	$y_offset = 0 if ( $y_offset < 0 );
 60	$x_offset = 0 if ( $x_offset < 0 );
 61
 62	for ( my $y = 0; $y < $screen_height / $tile_size; $y++ ) {
 63		for ( my $x = 0; $x < $screen_width / $tile_size; $x++ ) {
 64			my $tiles_rect = get_tile( ${ $map[ $y + $y_offset ] }[ $x + $x_offset ] ? 5 : 6 );
 65			my $screen_rect = SDL::Rect->new(
 66				$x * $tile_size, $y * $tile_size,
 67				$screen_width,   $screen_height
 68			);
 69
 70			SDL::Video::blit_surface(
 71				$tiles, $tiles_rect, $screen,
 72				$screen_rect
 73			);
 74		}
 75	}
 76	SDL::Video::update_rect( $screen, 0, 0, $screen_width, $screen_height );
 77
 78	sleep(5);
 79
 80	return 1;
 81}
 82
 83sub move_map {
 84	my $direction = shift;
 85
 86	$map_center[0]++ if $direction == MOP_LEFT;
 87	$map_center[0]-- if $direction == MOP_RIGHT;
 88	$map_center[1]++ if $direction == MOP_TOP;
 89	$map_center[1]-- if $direction == MOP_BOTTOM;
 90}
 91
 92sub get_tile {
 93	my $index = shift || 0;
 94
 95	Carp::cluck 'Unable to load tiles ' . SDL::get_error() if ( !$tiles );
 96
 97	my $x = ( $index * $tile_size ) % $tiles->w;
 98	my $y = int( ( $index * $tile_size ) / $tiles->w ) * $tile_size;
 99
100	return SDL::Rect->new( $x, $y, $tile_size, $tile_size );
101}
102
1031;