12.07.2015 Views

Chapter Quiz

Chapter Quiz

Chapter Quiz

SHOW MORE
SHOW LESS
  • No tags were found...

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

256<strong>Chapter</strong> 8 ■Basic Sprite ProgrammingDrawing Translucent SpritesAllegro provides many special effects that you can apply to sprites, as you saw in the previoussections. The next technique is unusual enough to warrant a separate discussion.This section explains how to draw sprites with translucent alpha blending. Two more specialeffects (sprite lighting and Gouraud shading) are covered in the next chapter.Translucency is a degree of “see-through” that differs from transparency, which is entirelysee-through. Think of the glass in a window as being translucent, while an open windowis transparent. There is quite a bit of work involved in making a sprite translucent, and I’mnot entirely sure it’s necessary for a game to use this feature, which is most definitely adrain on the graphics hardware. Although a late-model video card can handle translucency,or alpha blending, with ease, there is still the issue of supporting older computersor those with non-standard video cards. As such, many 2D games have steered clear ofusing this feature. One of the problems with translucency in a software implementation isthat you must prepare both bitmaps before they will render with translucency. Somehardware solutions are likely available, but they are not provided for in Allegro.Translucency is provided by the draw_trans_sprite function.void draw_trans_sprite(BITMAP *bmp, BITMAP *sprite, int x, int y);Unfortunately, it’s not quite as cut-and-dried as this simple function makes it appear. Touse translucency, you have to use an alpha channel blender, and even the Allegro documentationis elusive in describing how this works. Suffice it to say, translucency is notsomething you would probably want to use in a game because it was really designed towork between just two bitmaps. You could use the same background image with multipleforeground sprites that are blended with the background using the alpha channel, but eachsprite must be adjusted pixel by pixel when the program starts. This is a special effect thatyou might find a use for, but I would advise against using it in the main loop of a game.Here is the source code for the TransSprite program, shown in Figure 8.12. I will explainhow it works after the listing.#include #include #include “allegro.h”int main(){int x, y, c, a;//initializeallegro_init();install_keyboard();

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!