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

http://github.com/PerlGameDev/SDL · Perl · 183 lines · 133 code · 42 blank · 8 comment · 16 complexity · 852290fb9b02d0dc11b2335dfda53c0d MD5 · raw file

  1. package SDL::Tutorial::MoP::View::Game;
  2. use strict;
  3. use warnings;
  4. use Data::Dumper;
  5. use Carp;
  6. use Time::HiRes qw(time usleep);
  7. use base 'SDL::Tutorial::MoP::Base';
  8. use SDL;
  9. use SDL::Video;
  10. use SDL::Rect;
  11. my $screen_width = 800;
  12. my $screen_height = 600;
  13. my $map;
  14. my $view_rect = SDL::Rect->new( 0, 0, $screen_width, $screen_height );
  15. sub init {
  16. my $self = shift;
  17. SDL::init(SDL_INIT_VIDEO);
  18. $self->{app} = SDL::Video::set_video_mode(
  19. $screen_width, $screen_height, 32,
  20. SDL_SWSURFACE | SDL_DOUBLEBUF | SDL_FULLSCREEN
  21. );
  22. $map = SDL::Tutorial::MoP::Model::Map->new();
  23. $self->{count} = 0;
  24. $self->{iTime} = time;
  25. $self->clear();
  26. }
  27. sub notify {
  28. my ( $self, $event ) = (@_);
  29. print "Notify in View Game \n" if $self->{EDEBUG};
  30. #if we did not have a tick event then some other controller needs to do
  31. #something so game state is still beign process we cannot have new input
  32. #now
  33. return if !defined $event;
  34. my %event_action = (
  35. 'Tick' => sub {
  36. frame_rate(1) if $self->{FPS};
  37. },
  38. 'MapBuilt' => sub {
  39. $self->{'map'} = $event->{map};
  40. },
  41. 'GameStart' => sub {
  42. $self->{game} = $event->{game};
  43. $self->draw_scene();
  44. },
  45. 'MapMove' => sub {
  46. $self->clear();
  47. $self->draw_scene();
  48. },
  49. 'MapMoveRequest' => sub {
  50. $self->clear();
  51. $self->map_move( $event->{direction} );
  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. sub clear {
  61. my $self = shift;
  62. my $mapped_color = SDL::Video::map_RGB( $self->{app}->format(), 0, 0, 0 );
  63. SDL::Video::fill_rect(
  64. $self->{app},
  65. SDL::Rect->new( 0, 0, $screen_width, $screen_height ),
  66. $mapped_color
  67. );
  68. }
  69. sub map_move_rel {
  70. my $self = shift;
  71. my $_x = shift;
  72. my $_y = shift;
  73. $self->clear();
  74. my $step = $_x > 0 ? 2 : -2;
  75. for ( my $x = 0; $x != $_x; $x += $step ) {
  76. $map->x( $map->x() + $step );
  77. $self->draw_scene();
  78. #usleep(1000);
  79. }
  80. $step = $_y > 0 ? 2 : -2;
  81. for ( my $y = 0; $y != $_y; $y += $step ) {
  82. $map->y( $map->y() + $step );
  83. $self->draw_scene();
  84. #usleep(1000);
  85. }
  86. }
  87. sub map_move {
  88. my $self = shift;
  89. my $direction = shift;
  90. $self->map_move_rel( +16, 0 ) if $direction eq 'RIGHT';
  91. $self->map_move_rel( -16, 0 ) if $direction eq 'LEFT';
  92. $self->map_move_rel( 0, -16 ) if $direction eq 'UP';
  93. $self->map_move_rel( 0, +16 ) if $direction eq 'DOWN';
  94. }
  95. sub draw_map {
  96. my $self = shift;
  97. Carp::cluck('There is no surface to draw to') unless $self->{app};
  98. Carp::cluck('There are no tiles to draw') unless $map->tiles;
  99. # blitting the whole map-surface to app-surface
  100. my $srect = SDL::Rect->new( 0, 0, $map->w(), $map->h() ); #we want all of map;
  101. my $drect = SDL::Rect->new( $map->x(), $map->y(), $screen_width, $screen_height );
  102. unless ( $map->is_up_to_date ) {
  103. SDL::Video::blit_surface( $map->surface, $srect, $self->{app}, $drect );
  104. $map->is_up_to_date(0);
  105. }
  106. return 1;
  107. }
  108. sub draw_scene {
  109. my $self = shift;
  110. my $game = $self->{game};
  111. $self->draw_map();
  112. Carp::cluck('There is no surface to draw to') unless $self->{app};
  113. $self->{count} = $self->{count} + 1;
  114. if ( $self->{count} > 10 ) {
  115. #reset fps
  116. $self->{count} = 0;
  117. $self->{iTime} = time;
  118. }
  119. SDL::Video::update_rect(
  120. $self->{app}, 0, 0, $screen_width,
  121. $screen_height
  122. );
  123. SDL::Video::flip( $self->{app} );
  124. }
  125. # Should be in Game::Utility
  126. my $frame_rate = 0;
  127. my $time = time;
  128. sub frame_rate {
  129. my $secs = shift || 2;
  130. my $fps = 0;
  131. $frame_rate++;
  132. my $elapsed_time = time - $time;
  133. if ( $elapsed_time > $secs ) {
  134. $fps = ( $frame_rate / $secs );
  135. print "Frames per second: $frame_rate\n";
  136. $frame_rate = 0;
  137. $time = time;
  138. }
  139. return $fps;
  140. }
  141. 1;