File: dizzy

package info (click to toggle)
dizzy 0.1.1-1
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 104 kB
  • ctags: 44
  • sloc: perl: 676; makefile: 4
file content (333 lines) | stat: -rwxr-xr-x 7,695 bytes parent folder | download
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
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
#!/usr/bin/env perl
use strict;
use warnings;
use 5.010;

our $VERSION = '0.1.1';

use Getopt::Long qw(:config no_ignore_case bundling);

sub usage_message {
	print STDERR << "EOH";
usage: dizzy [options]

   Graphics settings:
     -w num           set window width
     -h num           set window height
     -f               run in fullscreen mode
     -r num           set texture resolution (power of two)
     -s num           set texture display scale

   Auto mode:
     -a num           set a new texture every num seconds

   Texture switching options:
     -t switcher      choose the texture switcher
     -T opt=val       pass options to the texture switcher

   Keyboard commands:
     cursor left      select previous texture
     cursor right     select next texture
     escape           exit dizzy
EOH
}

my %options = (
	help                   => sub { usage_message(); exit(0); },

	width                  => 0,
	height                 => 0,
	fullscreen             => 0,
	texture_resolution     => 64,
	texture_scale          => 50,

	automode               => 0,

	texswitcher            => 'Simple',
	texswitcher_options    => {},
);

GetOptions(\%options,
	'help|?',

	'width|w=i',
	'height|h=i',
	'fullscreen|f+',
	'texture_resolution|texture-resolution|r=i',
	'texture_scale|texture-scale|s=f',

	'automode|a=f',

	'texswitcher|t=s',
	'texswitcher_options|texswitcher-options|T=s',

	'debug_show_planes|debug-show-planes+',
	'debug_show_messages|debug-show-messages+',
) or (usage_message(), exit(1));

########
use lib 'lib';
use OpenGL qw(:all);
use Math::Trig;
use Time::HiRes qw(sleep time);
use Dizzy::TextureManager;
use Dizzy::TextureSwitcher;
use Dizzy::Textures;
use Dizzy::Render;
use Dizzy::GLUT;
use Dizzy::Handlers;

Dizzy::GLUT::init(title => "Dizzy", width => $options{width}, height => $options{height}, fullscreen => $options{fullscreen});
Dizzy::Render::init_view(texture_scale => $options{texture_scale});

Dizzy::TextureManager::init(
	texture_resolution => $options{texture_resolution},
);

# read textures
foreach my $texture (Dizzy::Textures::textures()) {
	Dizzy::TextureManager::add(%{$texture});
}

Dizzy::TextureSwitcher::init($options{texswitcher}, %{$options{texswitcher_options}});

# text rendering
my ($text_start, $text_text) = (0, "");
if ($options{debug_show_messages}) {
	Dizzy::Handlers::register_last(
		render => sub {
			if (defined $text_text and time() - $text_start <= 1.5 + length($text_text) / 13) {
				# disable blending and texturing, otherwise the text would look funny.
				glDisable(GL_BLEND);
				glDisable(GL_TEXTURE_2D);

				# draw the text shadow
				glColor3f(0, 0, 0);
				glRasterPos2f(-3.0, 2.2);
				foreach (split(//, $text_text)) {
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ord $_);
				}

				# draw the text itself
				glColor3f(1, 1, 1);
				glRasterPos2f(-3.01, 2.19);
				foreach (split(//, $text_text)) {
					glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, ord $_);
				}

				# reenable texturing and blending
				glEnable(GL_BLEND);
				glEnable(GL_TEXTURE_2D);

				# update display
				glFlush();
			}
			Dizzy::Handlers::GO_ON;
		},
	);
}


Dizzy::Handlers::register(
	keyboard => sub {
		my %args = @_;

		# so we don't get warning spam
		$args{ascii} //= "";
		$args{special} //= -1;

		if ($args{ascii} eq "\e" or $args{ascii} eq "q") { # escape/q
			exit(0);
		} elsif ($args{special} == GLUT_KEY_LEFT or $args{special} == GLUT_KEY_RIGHT) {
			Dizzy::Handlers::invoke("texture_switch",
				direction => (($args{special} == GLUT_KEY_LEFT) ? -1 : +1),
			);
		}

		Dizzy::Handlers::GO_ON;
	},

	# notify user about texture switches
	texture_changed => sub {
		my %args = @_;
		print "*** selected texture \"$args{name}\"\n";

		# draw an informational message.
		$text_start = time;
		$text_text = $args{name};
		Dizzy::Handlers::GO_ON;
	},

	# auto mode. move? or leave it here?
	render => sub {
		state $last_switch = 0;

		if ($options{automode} > 0) {
			if ($last_switch + $options{automode} <= time()) {
				$last_switch = time();
				Dizzy::Handlers::invoke("texture_switch", direction => +1);
			}
		}

		Dizzy::Handlers::GO_ON;
	},

	# to be moved to: somewhere else (or keep it here? it's nice and warm)
	render => sub {
		Dizzy::Render::render_planes(
			tick => time() - $^T,
			rotator_func => sub {
				my ($tick, $plane) = @_;
				if ($options{debug_show_planes}) {
					glScalef(0.2, 0.2, 0.2);
				}
				if ($plane == 1) {
					glRotatef($tick * 5, 0, 0, 1);
					glTranslatef(sin($tick * 0.5), cos($tick * 0.75), 0);
				} elsif ($plane == 2) {
					glRotatef($tick * -2.5, 0, 0, 1);
					glTranslatef(sin($tick * 0.5), cos($tick * 0.75), 0);
				}
			},
		);

		Dizzy::Handlers::GO_ON;
	},
);

Dizzy::TextureManager::set(0);

Dizzy::GLUT::run();

__END__

=head1 NAME

B<dizzy> - a graphics demo that makes you dizzy using rotating textures

=head1 SYNOPSIS

B<dizzy> [B<-f>|B<-w> I<width> B<-h> I<height>] [B<-t> I<switch_module>] [B<-T> I<options>]

=head1 DESCRIPTION

B<dizzy> is a graphics demo that rotates planes of patterns on a colored
background to make you dizzy. Textures can be cross-faded and there is a mode
that automatically changes textures, allowing Dizzy to be run as a screensaver.

=head1 OPTIONS

=over

=item B<-w> I<width>

=item B<--width> I<width>

=item B<-h> I<height>

=item B<--height> I<height>

Sets the window width and height.

=item B<-f>

=item B<--fullscreen>

Attempts to switch into a true fullscreen mode, if possible. The window size
parameters are ignored.

=item B<-a>

=item B<--automode> I<time>

Automatically switches textures after a specified number of seconds has passed.
I<time> can be fractional and the decimal separator is always the period.

=item B<-t> I<module>

=item B<--texswitcher> I<module>

Selects the texture switching module to use. Default is B<Simple>.

See below for available texture switchers and their descriptions.

=item B<-T> I<option>=I<value>

=item B<--texswitcher-options> I<option>=I<value>

Passes an option I<option> with the value I<value> to the selected texture
switcher. The available options depend on the texture switcher used.

This option can be given multiple times to set multiple options.

=item B<-r> I<resolution>

=item B<--texture-resolution> I<resolution>

Changes the texture resolution. I<resolution> must be a power of two. The default
value is 64.

=item B<-s> I<scale>

=item B<--texture-scale> I<scale>

Changes the texture scale factor, which is 50 by default, to I<scale>. Smaller
numbers result in larger boxes drawn on-screen, larger numbers draw smaller
boxes.

=item B<--debug-show-planes>

Zooms out of the normal view so you can see how Dizzy creates the animation.

=item B<--debug-show-messages>

Display some messages not only on standard output, but also in the GL window.

=back

=head1 TEXTURE SWITCHERS

=head2 Simple

A simple texture switcher. It just sets the new texture when it is told to do
so. It takes no options.

=head2 Blend

A texture switcher that crossfades between textures to generate a smooth
transition. It takes one option:

=over

=item B<blend>=I<duration>

Sets the duration of a blend to I<duration> seconds. The value can be fractional
(the decimal separator is always a period).  The default value is 2.

Note that you have to add the time you specify here to the automode time, so if
you want the transition to take two seconds and every image to stay for five
seconds, you set the duration to 2 and automode to 7 (not 5).

=back

=head1 KEYBOARD COMMANDS

=over

=item Cursor left

Select previous available texture.

=item Cursor right

Select next available texture.

=item Escape

=item q

Exit Dizzy.

=back

=cut