ZIO_Win32.pm untested

filter: Statementstmt 0.0% 0 / 161 Branchbran 0.0% 0 / 58 Conditioncond 0.0% 0 / 34 MC/DCmcdc 0.0% 0 / 19 Subroutinesub 0.0% 0 / 27 Podpod 0.0% 0 / 19 totaltotal 0.0% 0 / 318 CC 29 SCAR 38.2
CC
29
Cov
0%
get_input
132.0
new
90.0
get_attr
30.0
CRAP 45.8
1 package Games::Rezrov::ZIO_Win32;
2 # z-machine i/o for perls with Win32::Console
3 # TO DO:
4 # - handle scrollbars/buffering properly...it would be nice
5 # if old game text could be seen via an existing scrollbar.
6 # But this requires untangling screen Size() mess.
7 # - can we set hourglass when busy?
8  
9 use strict;
10 use Win32::Console;
11  
12 use Games::Rezrov::ZIO_Generic;
13 use Games::Rezrov::ZIO_Color;
14 use Games::Rezrov::ZConst;
15  
16 use Carp qw(cluck);
17  
18 use constant DEBUG => 0;
19  
20 @Games::Rezrov::ZIO_Win32::ISA = qw(Games::Rezrov::ZIO_Generic
21 Games::Rezrov::ZIO_Color
22 );
23  
24 my ($upper_lines, $rows, $columns, $in_status);
25 # number of lines in upper window, geometry
26  
27 my ($IN, $OUT);
28 # Win32::Console instances
29  
30 my @ORIG_SIZE;
31  
32 if (DEBUG) {
33 # debugging; tough to redirect STDERR under win32 :(
34 open(LOG, ">zio.log") || die;
35 select(LOG);
36 $|=1;
37 select(STDOUT);
38 }
39  
40 my %KEYCODES = (
41 38 => Games::Rezrov::ZConst::Z_UP,
42 40 => Games::Rezrov::ZConst::Z_DOWN,
43 37 => Games::Rezrov::ZConst::Z_LEFT,
44 39 => Games::Rezrov::ZConst::Z_RIGHT,
45 );
46  
47 my (%FOREGROUND, %BACKGROUND);
48 foreach (qw(black
49 blue
50 lightblue
51 red
52 lightred
53 green
54 lightgreen
55 magenta
56 lightmagenta
57 cyan
58 lightcyan
59 brown
60 yellow
61 gray
62 white)) {
63 # make hash translating names to color codes exported by Win32::Console
64 no strict "refs";
65 $FOREGROUND{$_} = ${"main::FG_" . uc($_)};
66 $BACKGROUND{$_} = ${"main::BG_" . uc($_)};
67 }
68  
69 sub new {
70 my ($type, %options) = @_;
71 my $self = new Games::Rezrov::ZIO_Generic(%options);
72 bless $self, $type;
73  
74 if ($options{fg}) {
75 $options{fg} = "gray" if $options{fg} eq "white";
76 # since INTENSITY mode has no effect "white",
77 # use gray instead. Feh.
78 # How to get *true* bold here???
79 } else {
80 $options{fg} = "gray" unless $options{fg};
81 $options{bg} = "blue" unless $options{bg};
82 $options{sfg} = "black" unless $options{sfg};
83 $options{sbg} = "cyan" unless $options{sbg};
84 }
85  
86 $self->parse_color_options(\%options);
87  
88 foreach ("bg", "fg", "sfg", "sbg") {
89 next unless exists $options{$_};
90 my $c = $self->$_() || next;
91 die sprintf "Unknown color \"%s\"; available colors: %s\n", $c, join(", ", sort keys %FOREGROUND)
92 unless exists $FOREGROUND{$c};
93 }
94
95 # set up i/o
96 $IN = new Win32::Console(STD_INPUT_HANDLE);
97 $OUT = new Win32::Console(STD_OUTPUT_HANDLE);
98  
99 @ORIG_SIZE = $OUT->Size();
100 # save original buffer geometry to restore on exit
101  
102 my ($top_row, $bottom_row, $left_col, $right_col);
103 ($columns, $left_col, $top_row, $right_col, $bottom_row) = ($OUT->Info())[0,5,6,7,8];
104 # die $bottom_row;
105 my $visible_rows = ($bottom_row - $top_row) + 1;
106 my $visible_columns = ($right_col - $left_col) + 1;
107  
108 $OUT->Size($visible_columns, $visible_rows);
109 # 11/2004: set the screen buffer to be the same size as the visible area
110 # of the window. Has the effect of removing any scrollbars, etc.
111 #
112 # Under XP the default number of rows in the buffer is generally
113 # larger than the number of visible rows (under Windows 98
114 # the two were generally the same). So without this change we'd be
115 # drawing the status window and/or upper window lines somewhere
116 # way up in the buffer, which won't be visible...this is because
117 # the Cursor() method moves within the *buffer* rather than the
118 # visible area.
119 #
120 # It's probably possible to finesse this module to only draw to
121 # the visible area while leaving the buffers and scrollbars
122 # alone, but for now I Don't Care.
123
124 my @size = $OUT->Size();
125 $columns = $options{"columns"} || $size[0] || die "need columns!";
126 $rows = $options{"rows"} || $size[1] || die "need rows!";
127  
128 $OUT->Size($columns, $rows);
129 # resize again (possible user override limiting rows/cols)
130  
131 $upper_lines = 0;
132 return $self;
133 }
134  
135 sub update {
136 $OUT->Flush();
137 }
138  
139 sub set_version {
140 # called by the game
141 my ($self, $status_needed, $callback) = @_;
142 Games::Rezrov::StoryFile::rows($rows);
143 Games::Rezrov::StoryFile::columns($columns);
144 return 0;
145 }
146  
147 sub absolute_move {
148 # move to X, Y
149 $OUT->Cursor($_[1], $_[2]);
150 }
151  
152 sub write_string {
153 my ($self, $string, $x, $y) = @_;
154 $self->absolute_move($x, $y) if defined($x) and defined($y);
155 # $OUT->Attr($current_attr);
156 $OUT->Attr($self->get_attr());
157 $OUT->Write($string);
158 }
159  
160 sub newline {
161 # newline/scroll
162 my ($x, $y) = $OUT->Cursor();
163 if (++$y >= $rows) {
164 # scroll needed
165 my $last_line = $rows - 1;
166 $y = $last_line;
167 my $top = $upper_lines;
168 # $OUT->Write(sprintf "before: at %d,%d, top=%d last=%d\n", $x, $y, $top, $last_line);
169 # log_it(sprintf "before: at %d,%d, top=%d last=%d\n", $x, $y, $top, $last_line);
170 # sleep(1);
171 $OUT->Scroll(0, $top + 1, $columns - 1, $last_line,
172 0, $top, Games::Rezrov::ZConst::ASCII_SPACE, $_[0]->get_attr(0),
173 0, $top, $columns - 1, $last_line);
174 # ugh: we have to specify the clipping region, or else
175 # Win32::Console barfs about uninitialized variables (with -w)
176 }
177 Games::Rezrov::StoryFile::register_newline();
178 $_[0]->absolute_move(0, $y);
179 }
180  
181 sub write_zchar {
182 # log_it("wzchar: " . chr($_[1]));
183 $OUT->Attr($_[0]->get_attr());
184 $OUT->Write(chr($_[1]));
185 }
186  
187 sub status_hook {
188 my ($self, $type) = @_;
189 # 0 = before
190 # 1 = after
191 if ($type == 0) {
192 # before printing status line
193 $OUT->Cursor(0,0);
194 $in_status = 1;
195 $OUT->FillAttr($self->get_attr(), $columns, 0, 0);
196 } else {
197 # after printing status line
198 $in_status = 0;
199 }
200 }
201  
202 sub get_input {
203 my ($self, $max, $single_char, %options) = @_;
204 # $IN->Flush();
205 # don't flush input (allow buffered keystrokes)
206
207 my ($start_x, $y) = $OUT->Cursor();
208 my $buf = $options{"-preloaded"} || "";
209 # preloaded text in the buffer, but already displayed by the game; ugh.
210 my @event;
211 my ($code, $char);
212  
213 if ($self->listening) {
214 $buf = $self->recognize_line();
215 $OUT->Write($buf);
216 } else {
217 while (1) {
218 @event = $IN->Input();
219 next unless defined $event[0];
220 my $known;
221 if ($event[0] == 1 and $event[1]) {
222 # a key pressed
223 $code = $event[5];
224 if ($code == 0) {
225 # non-character key pressed
226 if ($KEYCODES{$event[3]}) {
227 $code = $KEYCODES{$event[3]};
228 $known = 1;
229 } else {
230 log_it(sprintf "got unknown non-char: %s", join ",", @event);
231 }
232 }
233  
234 if ($single_char and ($known or ($code >= 1 and $code <= 127))) {
235 return chr($code);
236 } elsif ($code == Games::Rezrov::ZConst::ASCII_BS) {
237 if (length($buf) > 0) {
238 # log_it("backsp " . length($buf) . " " . $buf);
239 my ($x, $y) = $OUT->Cursor();
240 $OUT->Cursor($x - 1, $y);
241 $OUT->Write(" ");
242 $OUT->Cursor($x - 1, $y);
243 $buf = substr($buf, 0, length($buf) - 1);
244 }
245 } elsif ($code == Games::Rezrov::ZConst::ASCII_CR) {
246 last;
247 } else {
248 if ($code >= 32 and $code <= 127) {
249 $char = chr($code);
250 $buf .= $char;
251 $OUT->Attr($self->get_attr(0));
252 $OUT->Write($char);
253 }
254 }
255 }
256 }
257 }
258 $self->newline();
259 return $buf;
260 }
261  
262 sub clear_screen {
263 $OUT->Cls($_[0]->get_attr(0));
264 # log_it("cls");
265 }
266  
267 sub clear_to_eol {
268 $OUT->Attr($_[0]->get_attr(0));
269 $OUT->Write(' ' x ($columns - ($OUT->Cursor())[1]));
270 }
271  
272 sub split_window {
273 # split upper window to specified number of lines
274 my ($self, $lines) = @_;
275 # $w_main->setscrreg($lines, $rows - 1);
276 $upper_lines = $lines;
277 # print STDERR "split_window to $lines\n";
278 }
279  
280 sub can_change_title {
281 return 1;
282 }
283  
284 sub can_use_color {
285 return 1;
286 }
287  
288 sub set_game_title {
289 $OUT->Title($_[1]);
290 }
291  
292 sub log_it {
293 if (DEBUG) {
294 print LOG $_[0] . "\n";
295 }
296 }
297  
298 sub get_attr {
299 # return attribute code for color/style currently in effect.
300 my ($self, $mask) = @_;
301
302 $mask = Games::Rezrov::StoryFile::font_mask() unless defined($mask);
303 # might be called with an override
304 my ($fg, $bg);
305 if ($in_status) {
306 $fg = $self->sfg();
307 $bg = $self->sbg();
308 } else {
309 if ($mask & Games::Rezrov::ZConst::STYLE_REVERSE) {
310 $fg = $self->bg();
311 $bg = $self->fg();
312 } else {
313 $fg = $self->fg();
314 $bg = $self->bg();
315 }
316 }
317  
318 my $code = $BACKGROUND{$bg} | $FOREGROUND{$fg};
319 $code |= main::FOREGROUND_INTENSITY if
320 ($mask & (Games::Rezrov::ZConst::STYLE_BOLD|Games::Rezrov::ZConst::STYLE_ITALIC));
321 return $code;
322 }
323  
324 sub get_position {
325 # with no arguments, return absolute X and Y coordinates.
326 # With an argument, return a sub that will restore the current cursor
327 # position.
328 my ($self, $sub) = @_;
329 my ($x, $y) = $OUT->Cursor();
330 if ($sub) {
331 return sub { $OUT->Cursor($x, $y); };
332 } else {
333 return ($x, $y);
334 }
335 }
336  
337 sub cleanup {
338 $OUT->Size(@ORIG_SIZE) if $OUT;
339 # restore original window buffer sizes
340 }
341  
342 1;