/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

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