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

http://github.com/PerlGameDev/SDL · Perl · 194 lines · 129 code · 48 blank · 17 comment · 17 complexity · be5ae830c68d7060c518f6614bd5a767 MD5 · raw file

  1. package SDL::Tutorial::MoP::Model::Map;
  2. use strict;
  3. use warnings;
  4. use base 'SDL::Tutorial::MoP::Base';
  5. use File::ShareDir qw(module_file);
  6. use Carp;
  7. use SDL;
  8. use SDL::Video;
  9. use SDL::Surface;
  10. use SDL::Tutorial::MoP::Models;
  11. #BEGIN {
  12. # use Exporter ();
  13. # use vars qw($VERSION @ISA @EXPORT @EXPORT_OK %EXPORT_TAGS);
  14. # $VERSION = '0.01';
  15. # @ISA = qw(Exporter);
  16. # #Give a hoot don't pollute, do not export more than needed by default
  17. # @EXPORT = qw();
  18. # @EXPORT_OK = qw(draw_map);
  19. # %EXPORT_TAGS = ();
  20. #}
  21. my $tile_size = 10;
  22. my $path = module_file( 'SDL::Tutorial::MoP', 'data/tiles.bmp' );
  23. my $tiles = SDL::Video::load_BMP($path);
  24. Carp::confess 'Error: ' . SDL::get_error() if ( !$tiles );
  25. my $map_surface; # the image(s) of the current map are here
  26. my $is_up_to_date = 0;
  27. my ( $x, $y, $w, $h ) = ( 0, 0, 0, 0 );
  28. sub new {
  29. my ( $class, %params ) = (@_);
  30. my $self = $class->SUPER::new(%params);
  31. $self->evt_manager->reg_listener($self);
  32. $self->init(%params);
  33. return $self;
  34. }
  35. sub init {
  36. my ( $self, %params ) = @_;
  37. $self->load_map() || Carp::cluck("load_map() failed");
  38. $self->x(0);
  39. $self->y(0);
  40. $self->w( $self->surface()->w() );
  41. $self->h( $self->surface()->h() );
  42. $self->{map} ||= [];
  43. }
  44. sub notify {
  45. my ( $self, $event ) = (@_);
  46. print Carp::cluck( sprintf( "Notify '%s'in Map", $event->{name} ) )
  47. if $self->{EDEBUG};
  48. my %event_action = (
  49. # 'MapMoveRequest' => sub {
  50. # $self->move_map($event->{direction}) if $map_rect;
  51. # $self->evt_manager->post({ name => 'MapMove' });
  52. # },
  53. );
  54. my $action = $event_action{ $event->{name} };
  55. if ( defined $action ) {
  56. print "Event $event->{name}\n" if $self->{GDEBUG};
  57. $action->();
  58. }
  59. }
  60. # loads the bitmap file into $self->surface and also the tile-definitions into @map
  61. sub load_map {
  62. my $self = shift;
  63. my $_path = module_file( 'SDL::Tutorial::MoP', 'data/main.bmp' );
  64. my $_surface = SDL::Video::load_BMP($_path);
  65. #my $_surface = SDL::IMG_Load($_path);
  66. if ($_surface) {
  67. $self->surface($_surface);
  68. $is_up_to_date = 0;
  69. $_surface = undef;
  70. return 1;
  71. } else {
  72. Carp::cluck("Could not load bitmap $_path.");
  73. return -1;
  74. }
  75. }
  76. sub get_tile {
  77. my $self = shift;
  78. my $x = shift;
  79. my $y = shift;
  80. # return $self->get_tile_by_index(${$map[$y + $map_center[1]]}[$x + $map_center[0]] ? 5 : 6);
  81. }
  82. sub get_tile_by_index {
  83. my $self = shift;
  84. my $index = shift || 0;
  85. Carp::cluck 'Unable to load tiles ' . SDL::get_error() if ( !$tiles );
  86. my $x = ( $index * $tile_size ) % $tiles->w;
  87. my $y = int( ( $index * $tile_size ) / $tiles->w ) * $tile_size;
  88. return SDL::Rect->new( $x, $y, $tile_size, $tile_size );
  89. }
  90. sub tile_size {
  91. my $self = shift;
  92. $tile_size = shift || return $tile_size;
  93. }
  94. sub tiles {
  95. my $self = shift;
  96. $tiles = shift || return $tiles;
  97. }
  98. sub surface {
  99. my $self = shift;
  100. $map_surface = shift || return $map_surface;
  101. }
  102. sub x {
  103. my $self = shift;
  104. my $_x = shift;
  105. if ( defined $_x ) {
  106. $x = $_x;
  107. $self->is_up_to_date(0);
  108. return $self;
  109. }
  110. return $x;
  111. }
  112. sub y {
  113. my $self = shift;
  114. my $_y = shift;
  115. if ( defined $_y ) {
  116. $y = $_y;
  117. $self->is_up_to_date(0);
  118. return $self;
  119. }
  120. return $y;
  121. }
  122. sub w {
  123. my $self = shift;
  124. my $_w = shift;
  125. if ( defined $_w ) {
  126. $w = $_w;
  127. $self->is_up_to_date(0);
  128. return $self;
  129. }
  130. return $w;
  131. }
  132. sub h {
  133. my $self = shift;
  134. my $_h = shift;
  135. if ( defined $_h ) {
  136. $h = $_h;
  137. $self->is_up_to_date(0);
  138. return $self;
  139. }
  140. return $h;
  141. }
  142. sub is_up_to_date {
  143. my $self = shift;
  144. $is_up_to_date = shift || return $is_up_to_date;
  145. }
  146. 1;