#!/usr/bin/env perl # # testmenu.pl # # Copyright (C) 2005 David J. Goehrig # # ------------------------------------------------------------------------------ # # This library is free software; you can redistribute it and/or # modify it under the terms of the GNU Lesser General Public # License as published by the Free Software Foundation; either # version 2.1 of the License, or (at your option) any later version. # # This library is distributed in the hope that it will be useful, # but WITHOUT ANY WARRANTY; without even the implied warranty of # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU # Lesser General Public License for more details. # # You should have received a copy of the GNU Lesser General Public # License along with this library; if not, write to the Free Software # Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA # # ------------------------------------------------------------------------------ # # Please feel free to send questions, suggestions or improvements to: # # David J. Goehrig # dgoehrig@cpan.org # use SDL; use SDL::App; use SDL::Surface; use SDL::Rect; use SDL::Event; use MIME::Base64 qw/ decode_base64 /; write_pngs(); my $menu = new SDL::Surface -name => '/tmp/menu.png'; my $app = new SDL::App -w => $menu->width(), -h => $menu->height(), -resizeable => 1; my $hilight = new SDL::Surface -name => '/tmp/highlight.png'; my %menu = ( 'start' => [ 115, 30, 160, 40 ], 'help' => [ 120, 100, 120, 40 ], 'giveup' => [ 120, 230, 120, 40 ], 'spawnserver' => [ 115, 170, 165, 40 ], 'credits' => [ 115, 285, 160, 40 ], ); my $needblit; sub drawMenu { my ($a,$dx,$dy,$no,$hi,%m) = @_; for (keys %m) { my ($x,$y,$w,$h) = @{$m{$_}}; next unless $dx >= $x && $dx <= $x+$w && $dy >= $y && $dy <= $y+$h; unless ($needblit) { my $rect = new SDL::Rect -w => $w, -h => $h, -x => $x, -y => $y; $hi->blit($rect,$a,$rect); $needblit = 1; } return $_; } $no->blit(NULL,$a,NULL) if $needblit; $needblit = 0; return 0; } sub help { print STDERR < sub { my ($e) = @_; drawMenu($app, $e->motion_x(), $e->motion_y(), $menu, $hilight, %menu); }, SDL_MOUSEBUTTONUP() => sub { my ($e) = @_; my $routine = drawMenu($app, $e->motion_x(), $e->motion_y(), $menu, $hilight, %menu); &{$routine} if ($routine); }, SDL_QUIT() => sub { exit(0); }, SDL_KEYDOWN() => sub { my ($e) = @_; exit(0) if ($e->key_sym() == SDLK_ESCAPE); }, ); $menu->blit(NULL,$app,NULL); $app->sync(); $app->loop(\%events); sub write_pngs { $png1 = decode_base64 < /tmp/highlight.png" or die "$!\n"; print FP $png1; close FP; open FP, "> /tmp/menu.png" or die "$!\n"; print FP $png2; close FP; }