| Games-Risk documentation | Contained in the Games-Risk distribution. |
Games::Risk::Resources - utility module to load bundled resources
version 3.103040
use Games::Risk::Resources qw{ image };
my $image = image('actexit16');
This module is a focal point to access all resources bundled with
Games::Risk. Indeed, instead of each package to reinvent its loading
mechanism, this package provides handy functions to do that.
Moreover, by loading all the images at the same location, it will ensure that they are not loaded twice, cutting memory eating.
Games::Risk::Resources deals with various resources bundled within
the distribution. It doesn't export anything by default, but the
following subs are available for your import pleasure.
The images used for the GUI are bundled and loaded as Tk::Photo of
$poe_main_window.
Return the Tk image called $name. It can be directly used within Tk.
Map resources are playable maps, to allow more playing fun.
Return the absolute path of the map $name.
Return the names of all the maps bundled with Games::Risk.
Jerome Quelin
This software is Copyright (c) 2008 by Jerome Quelin.
This is free software, licensed under:
The GNU General Public License, Version 3, June 2007
| Games-Risk documentation | Contained in the Games-Risk distribution. |
# # This file is part of Games-Risk # # This software is Copyright (c) 2008 by Jerome Quelin. # # This is free software, licensed under: # # The GNU General Public License, Version 3, June 2007 # use 5.010; use strict; use warnings; package Games::Risk::Resources; BEGIN { $Games::Risk::Resources::VERSION = '3.103040'; } # ABSTRACT: utility module to load bundled resources use POE qw{ Loop::Tk }; use File::Basename qw{ basename }; use File::ShareDir qw{ dist_dir }; use File::Spec::Functions; use FindBin qw{ $Bin }; use Path::Class; use Readonly; use Tk; use Tk::JPEG; use Tk::PNG; use base qw{ Exporter }; our @EXPORT_OK = qw{ image map_path maps $SHAREDIR }; my (%images, %maps); Readonly our $SHAREDIR => _find_sharedir(); #-- # SUBROUTINES # -- public subs # # my $img = image( $name ); # # return the Tk image called $name. # sub image { return $images{ $_[0] }; } # # my $path = map_path( $name ); # # return the absolute path of the map $name. # sub map_path { my ($map) = @_; return $maps{$map}; } # # my @maps = maps(); # # return the names of all the maps bundled with GR. # sub maps { my @maps = sort keys %maps; return @maps; } # -- private subs # # _find_maps( $dirname ); # # find all maps bundled with the package. # sub _find_maps { my ($dirname) = @_; my $glob = catfile($dirname, 'maps', '*.map'); %maps = map { ( basename($_,qw{.map}) => $_ ) } glob $glob; } sub _find_sharedir { my $root = dir($Bin)->parent; return $root->subdir('share') if -f $root->file('dist.ini'); return dir( dist_dir( 'Games-Risk' ) ); } # # _load_images( $dirname ); # # load images from $dirname/images/*.png # sub _load_images { my ($dirname) = @_; my $glob = catfile($dirname, 'images', '*.png'); foreach my $path ( glob $glob ) { my $name = basename( $path, qw{.png} ); $images{$name} = $poe_main_window->Photo(-file => $path); } } # # _load_tk_icons( $dirname ); # # load tk icons from $dirname/images/tk_icons. # code & artwork taken from Tk::ToolBar # sub _load_tk_icons { my ($dirname) = @_; my $path = catfile($dirname, 'images', 'tk_icons'); open my $fh, '<', $path or die "can't open '$path': $!"; while (<$fh>) { chomp; last if /^#/; # skip rest of file my ($name, $data) = (split /:/)[0, 4]; $images{$name} = $poe_main_window->Photo(-data => $data); } close $fh; } #-- # INITIALIZATION # FIXME: all of this is ugly and should go away _load_tk_icons($SHAREDIR); _load_images($SHAREDIR); _find_maps($SHAREDIR); 1;
__END__