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

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