1. Technology
You can opt-out at any time. Please refer to our privacy policy for contact information.

Getting Gaming With Rubygame

By

The easiest way to load an image into a surface is to set up Rubygame's autoloading. When autoloading is enabled, Rubygame will search a number of user-defined directories for the files you want to load. When the files are loaded into memory, they stay in memory until the end of the program, so future references to the same image will not require the image be reloaded from the hard drive.

Setting up autoloading is easy. Simply assign a list of directories to Surface.autoload_dirs. Once that's done, you can use the index operator on the Surface class itself to load images into surfaces. To get the directory your program is in (which may or may not be the directory from which the program was actually run), you can use the special variable __FILE__.

 # Set up automatic loading of images and sounds
 resources_dir = File.dirname( __FILE__ ) + "/data"
 Surface.autoload_dirs = [ resources_dir ]
 
  1. About.com
  2. Technology
  3. Ruby
  4. Tutorials
  5. Game Programming
  6. Setting up Autoloading

©2014 About.com. All rights reserved.