Setting up Autoloading
Now that the main loop for Rubygame is up and running, it's time to put something meaningful on the screen. To do this, you can load images into surfaces. A surface is nothing more than an image in memory. These surfaces can be painted (or "blitted", short for block image transfer) onto other surfaces. and any surface can be painted onto any other surface, including the screen, which itself is a special type of surface.
Loading images into surfaces and painting them onto the screen is the primary way of displaying things on the screen in Rubygame.
Before getting started, take a moment to download the example code with the image file required for this article.
Loading Images
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__.
Once autoloading is set up, you can load images on the Surface class itself with the index operator. For example, to load the image file background.png, you could say surface = Surface['background.png']. This is a great little shortcut Rubygame sets up for you, and it makes loading images a snap.
Loaded images should be stored in a variable. Be sure to keep the variable around if you wish to keep any changes you make to the surface object.
If the variable goes out of scope or there are no more references to it, the image will still be in memory but any changes you made to it will not be there.
Also note that surfaces can only be loaded after the Screen object has been created. Rubygame needs to know the properties of the screen (bit depth, etc) in order to properly load surfaces. Trying to load a surface before the screen has been created will raise an exception.
Once you have a Surface object with an image, and a screen object to show it on, you can start drawing things to the screen. Drawing one surface to another is called "blitting," which, if you'll remember, is short for block image transfer. The word "blit" comes from the early days of PC game programming, and the name just stuck. You can think of blitting as painting or drawing--it's all the same thing.
The blit method takes two arguments. The first argument is the target surface to blit to, which, in this case, is the screen. The second argument is the coordinates on the target surface to blit to. Since this is a background image, the top-left corner (or coordinates 0,0) will do just fine. Because the background image is the same size as the screen, it will cover the entire screen. Though the blit method can take more arguments (for example, you can blit only part of a surface onto another surface), you don't need to worry about them right now.
The image used was taken from NASA's Astronomy Picture of the Day. Many images there are public domain and can be used in your games. See the "About Image Permissions" section of this page for more information about which images from APOD you can use without special permission.
Only relevant portions of the code have been displayed here in the article. To get the full picture and examine the code in its complete, working state, download the example code for this article. Included in the zip archive is the background image needed to run the program, as well as the complete program code.
Loading and Displaying Images
Now that the main loop for Rubygame is up and running, it's time to put something meaningful on the screen. To do this, you can load images into surfaces. A surface is nothing more than an image in memory. These surfaces can be painted (or "blitted", short for block image transfer) onto other surfaces. and any surface can be painted onto any other surface, including the screen, which itself is a special type of surface.
Loading images into surfaces and painting them onto the screen is the primary way of displaying things on the screen in Rubygame.
Before getting started, take a moment to download the example code with the image file required for this article.
Loading Images
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 ]
Once autoloading is set up, you can load images on the Surface class itself with the index operator. For example, to load the image file background.png, you could say surface = Surface['background.png']. This is a great little shortcut Rubygame sets up for you, and it makes loading images a snap.
Loaded images should be stored in a variable. Be sure to keep the variable around if you wish to keep any changes you make to the surface object.
If the variable goes out of scope or there are no more references to it, the image will still be in memory but any changes you made to it will not be there.
Also note that surfaces can only be loaded after the Screen object has been created. Rubygame needs to know the properties of the screen (bit depth, etc) in order to properly load surfaces. Trying to load a surface before the screen has been created will raise an exception.
Once you have a Surface object with an image, and a screen object to show it on, you can start drawing things to the screen. Drawing one surface to another is called "blitting," which, if you'll remember, is short for block image transfer. The word "blit" comes from the early days of PC game programming, and the name just stuck. You can think of blitting as painting or drawing--it's all the same thing.
The blit method takes two arguments. The first argument is the target surface to blit to, which, in this case, is the screen. The second argument is the coordinates on the target surface to blit to. Since this is a background image, the top-left corner (or coordinates 0,0) will do just fine. Because the background image is the same size as the screen, it will cover the entire screen. Though the blit method can take more arguments (for example, you can blit only part of a surface onto another surface), you don't need to worry about them right now.
The image used was taken from NASA's Astronomy Picture of the Day. Many images there are public domain and can be used in your games. See the "About Image Permissions" section of this page for more information about which images from APOD you can use without special permission.
# Draw the background onto the screen background.blit( screen, [0,0] )# Show the screen on the monitor screen.update
Only relevant portions of the code have been displayed here in the article. To get the full picture and examine the code in its complete, working state, download the example code for this article. Included in the zip archive is the background image needed to run the program, as well as the complete program code.
SHARE