Class: AGL::Res

Inherits:
Object
  • Object
show all
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 Method Summary collapse

Class Method Details

.clearObject

Releases the memory used by all non-global resources.



468
469
470
471
472
473
474
# File 'lib/minigl/global.rb', line 468

def self.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 ‘data/font’.

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”.



458
459
460
461
462
463
464
465
# File 'lib/minigl/global.rb', line 458

def self.font id, size, global = true, ext = ".ttf"
	if global; a = @@global_fonts; else; a = @@fonts; end
	id_size = "#{id}_#{size}"
	return a[id_size] if a[id_size]
	s = "data/font/" + id.to_s.split('_').join('/') + ext
	font = Gosu::Font.new Game.window, s, size
	a[id_size] = font
end

.img(id, global = false, tileable = false, ext = ".png") ⇒ Object

Returns a Gosu::Image object.

Parameters:

id

A string or symbol representing the path to the image. If the file is inside ‘data/img’, only the file name is needed. If it’s inside a subdirectory, the id must be prefixed by each subdirectory name followed by an underscore. Example: to load ‘data/img/sprite/1.png’, 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”.



348
349
350
351
352
353
354
# File 'lib/minigl/global.rb', line 348

def self.img id, global = false, tileable = false, ext = ".png"
	if global; a = @@global_imgs; else; a = @@imgs; end
	return a[id] if a[id]
	s = "data/img/" + id.to_s.split('_').join('/') + ext
	img = Gosu::Image.new Game.window, s, tileable
	a[id] = img
end

.imgs(id, sprite_cols, sprite_rows, global = false, ext = ".png") ⇒ 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”.



371
372
373
374
375
376
377
# File 'lib/minigl/global.rb', line 371

def self.imgs id, sprite_cols, sprite_rows, global = false, ext = ".png"
	if global; a = @@global_imgs; else; a = @@imgs; end
	return a[id] if a[id]
	s = "data/img/" + id.to_s.split('_').join('/') + ext
	imgs = Gosu::Image.load_tiles Game.window, s, -sprite_cols, -sprite_rows, false
	a[id] = imgs
end

.initializeObject

This is called by Game.initialize. Don’t call it explicitly.



318
319
320
321
322
323
324
325
326
327
328
329
# File 'lib/minigl/global.rb', line 318

def self.initialize
	@@imgs = Hash.new
	@@global_imgs = Hash.new
	@@tilesets = Hash.new
	@@global_tilesets = Hash.new
	@@sounds = Hash.new
	@@global_sounds = Hash.new
	@@songs = Hash.new
	@@global_songs = Hash.new
	@@fonts = Hash.new
	@@global_fonts = Hash.new
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 ‘data/song’.

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”.



435
436
437
438
439
440
441
# File 'lib/minigl/global.rb', line 435

def self.song id, global = false, ext = ".ogg"
	if global; a = @@global_songs; else; a = @@songs; end
	return a[id] if a[id]
	s = "data/song/" + id.to_s.split('_').join('/') + ext
	song = Gosu::Song.new Game.window, 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 ‘data/sound’.

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”.



415
416
417
418
419
420
421
# File 'lib/minigl/global.rb', line 415

def self.sound id, global = false, ext = ".wav"
	if global; a = @@global_sounds; else; a = @@sounds; end
	return a[id] if a[id]
	s = "data/sound/" + id.to_s.split('_').join('/') + ext
	sound = Gosu::Sample.new Game.window, s
	a[id] = sound
end

.tileset(id, tile_width = 32, tile_height = 32, global = false, ext = ".png") ⇒ 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 ‘data/tileset’.

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”.



395
396
397
398
399
400
401
# File 'lib/minigl/global.rb', line 395

def self.tileset id, tile_width = 32, tile_height = 32, global = false, ext = ".png"
	if global; a = @@global_tilesets; else; a = @@tilesets; end
	return a[id] if a[id]
	s = "data/tileset/" + id.to_s.split('_').join('/') + ext
	tileset = Gosu::Image.load_tiles Game.window, s, tile_width, tile_height, true
	a[id] = tileset
end