We can apply this functionality to animation as well. Since D3DXSprite is used to draw single- or multi-frame sprites, you can use the same transformation to rotate and scale a sprite regardless of whether it’s animated. Figure 4.5 shows a sprite sheet containing frames from an animated space rock or asteroid.
This program uses the same transforms that were applied to the fatship sprite in the previous example; the difference now is that we’re dealing with an animated sprite. What’s the difference? As far as Direct3D is concerned, there is none. Our Sprite::draw() method handles single “static” sprites as well as sprites with animation.
Let’s give animation with rotation and scaling a try. Figure 4.6 shows the output from the RotateAnimDemo program, with the code listing to follow.
#include "..EngineAdvanced2D.h" using namespace Advanced2D; Sprite *asteroid; bool game_preload() { g_engine->setAppTitle("SPRITE ANIMATE/ROTATE/SCALE DEMO"); g_engine->setFullscreen(false); g_engine->setScreenWidth(800); g_engine->setScreenHeight(600); g_engine->setColorDepth(32); return true; } bool game_init(HWND) { //load sprite asteroid = new Sprite(); asteroid->loadImage("asteroid.tga"); asteroid->setTotalFrames(64); asteroid->setColumns(8); asteroid->setSize(60,60); asteroid->setFrameTimer(30); return true; } void game_update() { static float scale = 0.005f; float r,s; //set position asteroid->setPosition(400,300); //set rotation asteroid->setRotation(timeGetTime()/600.0f); //set scale s = asteroid->getScale() + scale; if (s < 0.25 || s > 5.0f) scale *= -1; asteroid->setScale(s); //exit when escape key is pressed if (KEY_DOWN(VK_ESCAPE)) g_engine->Close(); } void game_end() { delete asteroid; } void game_render3d() { g_engine->ClearScene(D3DCOLOR_XRGB(0,0,80)); } void game_render2d() { asteroid->animate(); asteroid->draw(); }