Module: MiniGL::Res
- Defined in:
- lib/minigl/global.rb
Overview
This class is responsible for resource management. It keeps references to
all loaded resources until a call to clear is made. Resources can be
loaded as global, so that their references won't be removed even when
clear is called.
It also provides an easier syntax for loading resources, assuming a particular folder structure. All resources must be inside subdirectories of a 'data' directory, so that you will only need to specify the type of resource being loaded and the file name (either as string or as symbol). There are default extensions for each type of resource, so the extension must be specified only if the file is in a format other than the default.
Class Attribute Summary collapse
-
.font_dir ⇒ Object
Gets the current path to font files (under
prefix). -
.img_dir ⇒ Object
Gets the current path to image files (under
prefix). -
.prefix ⇒ Object
Get the current prefix for searching data files.
-
.retro_images ⇒ Object
Gets or sets a flag that indicates whether images will be loaded with the 'retro' option set (see
Gosu::Imagefor details), when this option is not specified in a 'Res.img' or 'Res.imgs' call. -
.separator ⇒ Object
Gets or sets the character that is currently being used in the
idparameter of the loading methods as a folder separator. -
.song_dir ⇒ Object
Gets the current path to song files (under
prefix). -
.sound_dir ⇒ Object
Gets the current path to sound files (under
prefix). -
.tileset_dir ⇒ Object
Gets the current path to tileset files (under
prefix).
Class Method Summary collapse
-
.clear ⇒ Object
Releases the memory used by all non-global resources.
-
.font(id, size, global = true, ext = '.ttf') ⇒ Object
Returns a
Gosu::Fontobject. -
.img(id, global = false, tileable = false, ext = '.png', retro = nil) ⇒ Object
Returns a
Gosu::Imageobject. -
.imgs(id, sprite_cols, sprite_rows, global = false, ext = '.png', retro = nil, tileable = false) ⇒ Object
Returns an array of
Gosu::Imageobjects, using the image as a spritesheet. -
.initialize ⇒ Object
This is called by
GameWindow.initialize. -
.song(id, global = false, ext = '.ogg') ⇒ Object
Returns a
Gosu::Songobject. -
.sound(id, global = false, ext = '.wav') ⇒ Object
Returns a
Gosu::Sampleobject. -
.tileset(id, tile_width = 32, tile_height = 32, global = false, ext = '.png', retro = nil) ⇒ Object
Returns an array of
Gosu::Imageobjects, using the image as a tileset.
Class Attribute Details
.font_dir ⇒ Object
Gets the current path to font files (under prefix). Default is 'font'.
560 561 562 |
# File 'lib/minigl/global.rb', line 560 def font_dir @font_dir end |
.img_dir ⇒ Object
Gets the current path to image files (under prefix). Default is 'img'.
547 548 549 |
# File 'lib/minigl/global.rb', line 547 def img_dir @img_dir end |
.prefix ⇒ Object
Get the current prefix for searching data files. This is the directory under which 'img', 'sound', 'song', etc. folders are located.
544 545 546 |
# File 'lib/minigl/global.rb', line 544 def prefix @prefix end |
.retro_images ⇒ Object
Gets or sets a flag that indicates whether images will be loaded with
the 'retro' option set (see Gosu::Image for details), when this
option is not specified in a 'Res.img' or 'Res.imgs' call.
573 574 575 |
# File 'lib/minigl/global.rb', line 573 def retro_images @retro_images end |
.separator ⇒ Object
Gets or sets the character that is currently being used in the id
parameter of the loading methods as a folder separator. Default is '_'.
Note that if you want to use symbols to specify paths, this separator
should be a valid character in a Ruby symbol. On the other hand, if you
want to use only slashes in Strings, you can specify a 'weird' character
that won't appear in any file name.
568 569 570 |
# File 'lib/minigl/global.rb', line 568 def separator @separator end |
.song_dir ⇒ Object
Gets the current path to song files (under prefix). Default is 'song'.
557 558 559 |
# File 'lib/minigl/global.rb', line 557 def song_dir @song_dir end |
.sound_dir ⇒ Object
Gets the current path to sound files (under prefix). Default is 'sound'.
554 555 556 |
# File 'lib/minigl/global.rb', line 554 def sound_dir @sound_dir end |
.tileset_dir ⇒ Object
Gets the current path to tileset files (under prefix). Default is
'tileset'.
551 552 553 |
# File 'lib/minigl/global.rb', line 551 def tileset_dir @tileset_dir end |
Class Method Details
.clear ⇒ Object
Releases the memory used by all non-global resources.
787 788 789 790 791 792 793 |
# File 'lib/minigl/global.rb', line 787 def clear @imgs.clear @tilesets.clear @sounds.clear @songs.clear @fonts.clear end |
.font(id, size, global = true, ext = '.ttf') ⇒ Object
Returns a Gosu::Font object. Fonts are needed to draw text
and used by MiniGL elements like buttons, text fields and TextHelper
objects.
Parameters:
[id] A string or symbol representing the path to the song. It must be
specified the same way as in img, but the base directory is
+prefix+/+font_dir+.
[size] The size of the font, in pixels. This will correspond,
approximately, to the height of the tallest character when drawn.
[global] Set to true if you want to keep the font in memory until the
game execution is finished. If false, the font will be released
when you call clear.
[ext] The extension of the file being loaded. Specify only if it is
other than ".ttf".
777 778 779 780 781 782 783 784 |
# File 'lib/minigl/global.rb', line 777 def font(id, size, global = true, ext = '.ttf') a = global ? @global_fonts : @fonts id_size = "#{id}_#{size}" return a[id_size] if a[id_size] s = @prefix + @font_dir + id.to_s.split(@separator).join('/') + ext font = Gosu::Font.new size, name: s a[id_size] = font end |
.img(id, global = false, tileable = false, ext = '.png', retro = nil) ⇒ Object
Returns a Gosu::Image object.
Parameters:
[id] A string or symbol representing the path to the image. If the file
is inside +prefix+/+img_dir+, only the file name is needed. If it's
inside a subdirectory of +prefix+/+img_dir+, the id must be
prefixed by each subdirectory name followed by separator. Example:
to load 'data/img/sprite/1.png', with the default values of prefix,
img_dir and separator, provide :sprite_1 or "sprite_1".
[global] Set to true if you want to keep the image in memory until the
game execution is finished. If false, the image will be
released when you call clear.
[tileable] Whether the image should be loaded in tileable mode, which is
proper for images that will be used as a tile, i.e., that
will be drawn repeated times, side by side, forming a
continuous composition.
[ext] The extension of the file being loaded. Specify only if it is
other than '.png'.
[retro] Whether the image should be loaded with the 'retro' option set
(see Gosu::Image for details). If the value is omitted, the
Res.retro_images value will be used.
658 659 660 661 662 663 664 665 |
# File 'lib/minigl/global.rb', line 658 def img(id, global = false, tileable = false, ext = '.png', retro = nil) a = global ? @global_imgs : @imgs return a[id] if a[id] s = @prefix + @img_dir + id.to_s.split(@separator).join('/') + ext retro = Res.retro_images if retro.nil? img = Gosu::Image.new s, tileable: tileable, retro: retro a[id] = img end |
.imgs(id, sprite_cols, sprite_rows, global = false, ext = '.png', retro = nil, tileable = false) ⇒ Object
Returns an array of Gosu::Image objects, using the image as
a spritesheet. The image with index 0 will be the top left sprite, and
the following indices raise first from left to right and then from top
to bottom.
Parameters:
[id] A string or symbol representing the path to the image. See img
for details.
[sprite_cols] Number of columns in the spritesheet.
[sprite_rows] Number of rows in the spritesheet.
[global] Set to true if you want to keep the image in memory until the
game execution is finished. If false, the image will be
released when you call clear.
[ext] The extension of the file being loaded. Specify only if it is
other than ".png".
[retro] Whether the image should be loaded with the 'retro' option set
(see Gosu::Image for details). If the value is omitted, the
Res.retro_images value will be used.
685 686 687 688 689 690 691 692 |
# File 'lib/minigl/global.rb', line 685 def imgs(id, sprite_cols, sprite_rows, global = false, ext = '.png', retro = nil, tileable = false) a = global ? @global_imgs : @imgs return a[id] if a[id] s = @prefix + @img_dir + id.to_s.split(@separator).join('/') + ext retro = Res.retro_images if retro.nil? imgs = Gosu::Image.load_tiles s, -sprite_cols, -sprite_rows, tileable: tileable, retro: retro a[id] = imgs end |
.initialize ⇒ Object
This is called by GameWindow.initialize. Don't call it
explicitly.
577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 |
# File 'lib/minigl/global.rb', line 577 def initialize @imgs = {} @global_imgs = {} @tilesets = {} @global_tilesets = {} @sounds = {} @global_sounds = {} @songs = {} @global_songs = {} @fonts = {} @global_fonts = {} @prefix = File.(File.dirname($0)) + '/data/' @img_dir = 'img/' @tileset_dir = 'tileset/' @sound_dir = 'sound/' @song_dir = 'song/' @font_dir = 'font/' @separator = '_' @retro_images = false end |
.song(id, global = false, ext = '.ogg') ⇒ Object
Returns a Gosu::Song object. This should be used for the
background musics of your game.
Parameters:
[id] A string or symbol representing the path to the song. It must be
specified the same way as in img, but the base directory is
+prefix+/+song_dir+.
[global] Set to true if you want to keep the song in memory until the
game execution is finished. If false, the song will be released
when you call clear.
[ext] The extension of the file being loaded. Specify only if it is
other than ".ogg".
754 755 756 757 758 759 760 |
# File 'lib/minigl/global.rb', line 754 def song(id, global = false, ext = '.ogg') a = global ? @global_songs : @songs return a[id] if a[id] s = @prefix + @song_dir + id.to_s.split(@separator).join('/') + ext song = Gosu::Song.new s a[id] = song end |
.sound(id, global = false, ext = '.wav') ⇒ Object
Returns a Gosu::Sample object. This should be used for
simple and short sound effects.
Parameters:
[id] A string or symbol representing the path to the sound. It must be
specified the same way as in img, but the base directory is
+prefix+/+sound_dir+.
[global] Set to true if you want to keep the sound in memory until the
game execution is finished. If false, the sound will be
released when you call clear.
[ext] The extension of the file being loaded. Specify only if it is
other than ".wav".
734 735 736 737 738 739 740 |
# File 'lib/minigl/global.rb', line 734 def sound(id, global = false, ext = '.wav') a = global ? @global_sounds : @sounds return a[id] if a[id] s = @prefix + @sound_dir + id.to_s.split(@separator).join('/') + ext sound = Gosu::Sample.new s a[id] = sound end |
.tileset(id, tile_width = 32, tile_height = 32, global = false, ext = '.png', retro = nil) ⇒ Object
Returns an array of Gosu::Image objects, using the image as
a tileset. Works the same as imgs, except you must provide the tile
size instead of the number of columns and rows, and that the images will
be loaded as tileable.
Parameters:
[id] A string or symbol representing the path to the image. It must be
specified the same way as in img, but the base directory is
+prefix+/+tileset_dir+.
[tile_width] Width of each tile, in pixels.
[tile_height] Height of each tile, in pixels.
[global] Set to true if you want to keep the image in memory until the
game execution is finished. If false, the image will be
released when you call clear.
[ext] The extension of the file being loaded. Specify only if it is
other than ".png".
[retro] Whether the image should be loaded with the 'retro' option set
(see Gosu::Image for details). If the value is omitted, the
Res.retro_images value will be used.
713 714 715 716 717 718 719 720 |
# File 'lib/minigl/global.rb', line 713 def tileset(id, tile_width = 32, tile_height = 32, global = false, ext = '.png', retro = nil) a = global ? @global_tilesets : @tilesets return a[id] if a[id] s = @prefix + @tileset_dir + id.to_s.split(@separator).join('/') + ext retro = Res.retro_images if retro.nil? tileset = Gosu::Image.load_tiles s, tile_width, tile_height, tileable: true, retro: retro a[id] = tileset end |