File: testconsole.pl

package info (click to toggle)
sdlperl 1.20.3-1
  • links: PTS
  • area: main
  • in suites: sarge
  • size: 1,804 kB
  • ctags: 2,169
  • sloc: perl: 7,397; ansic: 232; makefile: 72; sh: 1
file content (301 lines) | stat: -rwxr-xr-x 5,523 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
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
#!/usr/bin/env perl
#
# testsdlconsole.pl
#
# *** WARNING ***
#
# This is a first draft of SDL::Console, the interface may change.
#
# Usage: sdlconsole.pl [-bpp N] [-hw] [-flip] [-fast] [-fullscreen]

package main;

#use strict;
use Getopt::Long;
use Data::Dumper;

use SDL;
use SDL::App;
use SDL::Event;
use SDL::Surface;
use SDL::Color;
use SDL::Rect;
use SDL::Console;

use vars qw/ $done $app $app_rect $background $event $sprite $sprite_rect $videoflags /;

## User tweakable settings (via cmd-line)
my %settings = (
	'numsprites'    => 10,
	'screen_width'  => 640,
	'screen_height' => 480,
	'video_bpp'     => 8,
	'fast'          => 0,
	'hw'            => 0,
	'flip'          => 1,
	'fullscreen'    => 0,
	'bpp'           => undef,
);

## Process commandline arguments

sub get_cmd_args
{
  GetOptions("width:i"  => \$settings{screen_width},
	     "height:i" => \$settings{screen_height},
	     "bpp:i"    => \$settings{bpp},
	     "fast!"   => \$settings{fast},
	     "hw!"     => \$settings{hw},
	     "flip!"    => \$settings{flip},
	     "fullscreen!" => \$settings{fullscreen},
	    );
}

## Initialize application options

sub set_app_args
{
  $settings{bpp} ||= 8;  	# default to 8 bits per pix

  $videoflags |= SDL_HWACCEL     if $settings{hw};  
  $videoflags |= SDL_DOUBLEBUF   if $settings{flip};  
  $videoflags |= SDL_FULLSCREEN  if $settings{fullscreen}; 
}

## Setup 

sub  init_game_context
{
  $done = 0;
  $app = new SDL::App (
		       -width => $settings{screen_width}, 
		       -height=> $settings{screen_height}, 
		       -title => "SDL_console test",
		       -icon  => "icon.bmp",
		       -flags => $videoflags,
			);

  $app_rect= new SDL::Rect(
			   -height => $settings{screen_height}, 
			   -width  => $settings{screen_width},
			  );

  $background = $SDL::Color::blue;

  $sprite = new SDL::Surface(-name =>"data/logo.png"); 

  # Set transparent pixel as the pixel at (0,0) 

  $sprite->set_color_key(SDL_SRCCOLORKEY,0,0);	# sets the transparent color to that at (0,0)

  $sprite->display_format();

  $sprite_rect = new SDL::Rect(-x     => 0, 
			       -y     => 0,
			       -width => $sprite->width,
			       -height=> $sprite->height,
			      );
  
  $event = new SDL::Event();
}

## Prints diagnostics

sub instruments
{
  if ( ($app->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
    printf("Screen is in video memory\n");
  } else {
    printf("Screen is in system memory\n");
  }

  if ( ($app->flags & SDL_DOUBLEBUF) == SDL_DOUBLEBUF ) {
    printf("Screen has double-buffering enabled\n");
  }

  if ( ($sprite->flags & SDL_HWSURFACE) == SDL_HWSURFACE ) {
    printf("Sprite is in video memory\n");
  } else {
    printf("Sprite is in system memory\n");
  }
  
  # Run a sample blit to trigger blit (if posssible)
  # acceleration before the check just after 
  $sprite->blit(0, $app, 0);  
  
  if ( ($sprite->flags & SDL_HWACCEL) == SDL_HWACCEL ) {
    printf("Sprite blit uses hardware acceleration\n");
  }
  if ( ($sprite->flags & SDL_RLEACCEL) == SDL_RLEACCEL ) {
    printf("Sprite blit uses RLE acceleration\n");
  }
  
}



# a hash containing our command functionality

my %commands = (

"!" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
  my $output = `$rawparams`;
  $console->print($output);
},


"!!" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
  eval $rawparams;
  
  my $output = $@ || "OK";
  $console->print($output);
},

"help" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
  $console->list_commands();
},

"alpha" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
  my @params = @_;
  $console->alpha(@_);
},

"echo" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
  $console->print($rawparams);
},


"background" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
  $console->background(@_);
},

"move" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
  $console->position(@_);
},

"resize" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
  my ($x, $y , $w, $h) = @_;
  my $rect = new SDL::Rect (-x => $x,
			    -y => $y,
			    -width => $w,
			    -height => $h
			   );
  $console->resize($rect);
},

"demo" => sub 
{
  my $console = shift;  
  my $rawparams= shift;
},

"quit" => sub 
{
  $done=1;
},


);





sub init_console
{
  my $console_rect= new SDL::Rect(
				  -width =>$app->width, 
				  -height=>128
				 );
  
  my $console = new SDL::Console(
				 -surface => $app,
				 -rect  => $console_rect,
				 -font  => "data/LargeFont.bmp",
				 -lines => 100,
				);

  # add commands, they are available to all consoles.
  while ( my ($command, $func) = each %commands)
  {
    SDL::Console::AddCommand($command, $func);
  }

  $console->topmost();
  return $console;
}
  


sub game_loop
{
  my $console = init_console();

 FRAME:
  while (not $done) 
  {
    # process event queue
    #$event->pump;
    if ($event->poll)
    {
      my $etype=$event->type;      
      SDL::Console::Event($event);

      # handle quit events
      last FRAME if ($etype eq SDL_QUIT );
      last FRAME if (SDL::GetKeyState(SDLK_ESCAPE));
    }

    $app->flip if $settings{flip};

    # needed for HW surface locking
    #$app->lock() if $app->lockp();  
    # draw..
    #$app->unlock();

    ################################################
    # do some drawing 

    $app->fill($app_rect, $background);

    $console->draw;
    
  }

}



## Main program loop

get_cmd_args();
set_app_args();
init_game_context();
instruments();
game_loop();
exit(0);