File: Console.pm

package info (click to toggle)
sdlperl 1.20.3dfsg-1
  • links: PTS
  • area: main
  • in suites: etch, etch-m68k
  • size: 1,796 kB
  • ctags: 2,171
  • sloc: perl: 7,394; ansic: 232; makefile: 75; sh: 1
file content (257 lines) | stat: -rw-r--r-- 4,782 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
#	Console.pm
#
#	A package for SDL_Console
#
#	Copyright (C) 2002 Wayne Keenan

package SDL::Console;
use strict;
use SDL;

# class instance hash used to map
# the console command to the perl sub
my %cmdCallbacks =();

# class instance hash used to
# map the C SDL_console object
# to the perl object which created it.
# this allows us to pass the perl
# object to the perl callback, so
# it can easily, say, print to 'this/self'
# console.
my %cmdObjects =();


sub new {
	my $proto = shift;	
	my $class = ref($proto) || $proto;
	my $self = {};
	my %options = @_;

	verify (%options, qw/ -surface -font -lines  -rect  /) if $SDL::DEBUG;
	
	# possibility $options{-surface} || SDL::GetVideoSurface();

	my $surface = $options{-surface} || die "No surface given";
	my $font    = $options{-font}  || "ConsoleFont.bmp";
	my $lines   = $options{-lines} || 32;
	my $rect    = $options{-rect}  || new SDL::Rect(-width  =>$surface->width,
							-height =>$surface->height/4
						       );
	
	my $console = SDL::ConsoleInit($font, 
				    $surface->{-surface}, 
				    $lines,
				    $rect->{-rect}
				   ) ;
	
	$$self{-console} = $console 
	  or die "failed to create console";

	$cmdObjects{$console}=$self; # use for callback context
	SDL::EnableUnicode(1);
	SDL::ConsoleSendFullCommand(1); # we need to see the command for context
	
	bless $self,$class;
	return $self;
}

sub DESTROY {		
	my $self = shift;		
	my $con = $$self{-console};

	delete $cmdObjects{$con};
	SDL::ConsoleDestroy($con);	
}

sub draw 
{		
  my $self = shift;	
  SDL::ConsoleDrawConsole($$self{-console});	
}


# set the focus
sub topmost
{		
  my $self = shift;	
  SDL::ConsoleTopmost($$self{-console});	
}


# class static to add new callbacks
sub AddCommand
{
  my ($cmd, $cb) = @_;

  die "not a subroutine ref (actully given '".ref($cb)."' for '$cmd')"  
    unless (ref($cb) eq "CODE");

  SDL::ConsoleAddCommand($cmd);
  $cmdCallbacks{$cmd}= $cb;
}

# class static to allow SDL_Console to handle it's events
sub Event
{
  my $event = shift;
  SDL::ConsoleEvents($event->{-event});
}


sub print
{
  my $self = shift;
  # join all the @_ args togeter, then split on newlines.
  # there is a max line width in SDL_Console;
  map {SDL::ConsoleOut($$self{-console}, $_)} split "\n", join ' ', @_;
}

sub alpha
{
  my ($self, $alpha) = @_;  
  SDL::ConsoleAlpha($$self{-console}, $alpha);
}

sub background
{
  my ($self, $file, $x, $y) = @_;  
  SDL::ConsoleBackground($$self{-console}, $file, $x,$y);
}

sub list_commands
{
  my $self = shift;  
  return SDL::ConsoleListCommands($$self{-console});
}

sub position
{
  my ($self, $x, $y) = @_;  
  SDL::ConsolePosition($$self{-console}, $x,$y);
}

sub resize
{
  my ($self, $rect) = @_;  
  return SDL::ConsoleResize($$self{-console}, $rect->{-rect});
}


# class static method called by
# the (single) XS SDL_Console callback
sub CommandDispatch
{
  my ($console, $line) = @_;

  if ($line =~ /^\s*([^\s]+)\s*(.*)$/)
  {
    my $cmd =$1;
    my $params= $2;
    my $func = $cmdCallbacks{$cmd};    
    &{$func}($cmdObjects{$console}, $params, split ' ', $params);
  }
  else
  {
    warn "Failed to parse command from : '$line'\n";
  }
}

1;

__END__;

=pod

=head1 NAME

SDL::Console - a SDL perl extension

=head1 SYNOPSIS

  use SDL::Console;
  
  my $console = new SDL::Console(
				 -surface => $app,
				 -rect  => $console_rect, 
				 -font  => "ConsoleFont.bmp",
				 -lines => 100,
				);

=head1 DESCRIPTION

The C<SDL::Console> module encapsulates the SDL_Console library, and
many of its ancillatory functions.    

=head1 METHODS

=head2 new (-surface => $app, ... )

Specifies the surface on which the console will be blitted.  
This method takes the following additional parameters:

=over 4

=item *

-font		the font file to use, default: "ConsoleFont.bmp"

=item *

-lines		number of lines in the console, default: 32

=item *

-rect		a SDL::Rect which specifies the console bounding box, 

=back

=head2 topmost       ()
give this console the keyboard focus.

=head2 draw          ()
blit this console to the associated surface

=head2 print         ( @strings )
write a string(s) the console

=head2 alpha         ( $alpha )

set the console alpha component (0-255)

=head2 background    ( $file, $x,$y)

load a background image

=head2 position      ( $x, $y)

set the console origin

=head2 resize        ( SDL::Rect )

resize the console

=head2 Event         ( SDL::Event )

class method to be called when events are processed.

=head2 AddCommand    ( $command, sub {} )

class method to add a command to the Consoles repository

=head2 list_commands ()

display the registered commands to the console & stdout



=head1 AUTHOR

Wayne Keenan

=head1 SEE ALSO

L<perl> L<SDL::Surface> L<SDL::Rect> L<SDL::App> L<SDL::Event>

=cut