first merge
[sdlgit/SDL_perl.git] / test / graywin.sdlpl
CommitLineData
bfd90409 1#!/usr/bin/env perl
2#
3# graywin.pl
4#
5# Copyright (C) 2005 David J. Goehrig <dgoehrig@cpan.org>
6#
7# ------------------------------------------------------------------------------
8#
9# This library is free software; you can redistribute it and/or
10# modify it under the terms of the GNU Lesser General Public
11# License as published by the Free Software Foundation; either
12# version 2.1 of the License, or (at your option) any later version.
13#
14# This library is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17# Lesser General Public License for more details.
18#
19# You should have received a copy of the GNU Lesser General Public
20# License along with this library; if not, write to the Free Software
21# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
22#
23# ------------------------------------------------------------------------------
24#
25# Please feel free to send questions, suggestions or improvements to:
26#
27# David J. Goehrig
28# dgoehrig@cpan.org
29#
30
31use SDL;
32use SDL::App;
33use SDL::Rect;
34use SDL::Event;
35use SDL::Color;
36
37my %options;
38$options{-flags} = SDL::SWSURFACE;
39$options{-title} = $0;
40$options{-width} ||= 640;
41$options{-height} ||= 480;
42$options{-depth} ||= $options{-bpp} || 8;
43
44my $app = new SDL::App %options;
45
46sub DrawBox {
47 my ($x,$y) = @_;
48
49 my ($w, $h) = ( int(rand(640)), int(rand(480)) );
50
51 my $rect = new SDL::Rect -width => $w, -height => $h,
52 -x => ($x - int($w/2)), -y => ($y - int($h/2));
53
54 my $color = new SDL::Color -r => rand(256), -g => rand(256), -b => rand(256);
55
56 $app->fill($rect,$color);
57 $app->update($rect);
58};
59
60$app->loop( {
61 SDL_MOUSEBUTTONDOWN() => sub {
62 my ($event) = @_;
63 DrawBox($event->button_x(),$event->button_y());
64 },
65 SDL_KEYDOWN() => sub {
66 my ($event) = @_;
67 $app->warp($options{-width}/2,$options{-height}/2)
68 if ($event->key_sym() == SDLK_SPACE);
69 $app->fullscreen()
70 if ($event->key_sym() == SDLK_f);
71 exit(0) if ($event->key_sym() == SDLK_ESCAPE);
72 },
73 SDL_QUIT() => sub { exit(0); }
74} );
75
76