Looking at the sprite functionality at a lower level, you can tell the sprite renderer (D3DXSprite) what color you want to use for the color key; our Sprite class defines magenta (with an RGB of 255, 0, 255) as the default transparent color key. Figure 3.4 shows the sprite we’re using in the upcoming demo. This example program is called ColorkeyDemo and is ready to go on the CD-ROM.
AdviceThe rocket ship featured here was modeled by UAT graduate Nathan Cox, who—after a stint with EA on Medal of Honor: Airborne—now works for Nihilistic Software. |
#include "..EngineAdvanced2D.h" using namespace Advanced2D; Sprite *sprite; bool game_preload() { g_engine->setAppTitle("SPRITE COLOR KEY DEMO"); g_engine->setFullscreen(false); g_engine->setScreenWidth(640); g_engine->setScreenHeight(480); g_engine->setColorDepth(32); return 1; } bool game_init(HWND) { //load sprite sprite = new Sprite(); sprite->loadImage("fatship_colorkeyed.bmp"); return true; } void game_update() { //exit when escape key is pressed if (KEY_DOWN(VK_ESCAPE)) g_engine->Close(); } void game_end() { delete sprite; } void game_render3d() { g_engine->ClearScene(D3DCOLOR_XRGB(0,0,80)); g_engine->SetIdentity(); } void game_render2d() { //calculate center of screen int cx = g_engine->getScreenWidth() / 2; int cy = g_engine->getScreenHeight() / 2; //calculate center of sprite int sx = sprite->getWidth() / 2; int sy = sprite->getHeight() / 2; //draw sprite centered sprite->setPosition(cx-sx,cy-sy); sprite->draw(); }
I’ve called this program ColorkeyDemo, and it is available on the CD. Figure 3.5 shows the output from the code just listed.