• Animating two spries independently with DirectX
    2 replies, posted
Can someone help me? I'm going through a DirectX book, this is the "on your own" part of the Drawing and Animating Sprites chapter. Here is my code. Sprite Draw and Animate Functions: [cpp] void Sprite_Draw_Frame(LPDIRECT3DTEXTURE9 texture, int destx, int desty, int framenum, int framew, int frameh, int columns) { D3DXVECTOR3 position((float)destx, (float)desty, 0); D3DCOLOR white = D3DCOLOR_XRGB(255, 255, 255); RECT rect; rect.left = (framenum % columns) * framew; rect.top = (framenum / columns) * frameh; rect.right = rect.left + framew; rect.bottom = rect.top + frameh; spriteobj->Draw(texture, &rect, NULL, &position, white); } void Sprite_Animate(int &frame, int startframe, int endframe, int direction, int &starttime, int delay) { if((int)GetTickCount() > starttime + delay) { starttime = GetTickCount(); frame += direction; if(frame > endframe) frame = startframe; if(frame < startframe) frame = endframe; } }[/cpp] Rendering function: [cpp]void Game_Run(HWND window) { //make sure the Direct3D device is valid if (!d3ddev) return; //update input devices DirectInput_Update(); //clear the scene d3ddev->Clear(0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,100), 1.0f, 0); //start rendering if (d3ddev->BeginScene()) { spriteobj->Begin(D3DXSPRITE_ALPHABLEND); Sprite_Animate(frame, 0, 29, 1, starttime, 30); Sprite_Draw_Frame(explosion, 200, 200, frame, 128, 128, 6); //stop rendering spriteobj->End(); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); }[/cpp] 1. The Animate Sprite project in this chapter does show how animation works, but it's kind of a boring demo. Modify the program so that it generates more than one explosion sprite at a time. I have successfully accomplished that. [cpp] if (d3ddev->BeginScene()) { spriteobj->Begin(D3DXSPRITE_ALPHABLEND); Sprite_Animate(frame, 0, 29, 1, starttime, 30); Sprite_Draw_Frame(explosion, 200, 200, frame, 128, 128, 6); Sprite_Draw_Frame(explosion1, 500, 500, frame, 128, 128, 6); //stop rendering spriteobj->End(); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); }[/cpp] It [I]does[/I] render two, but obviously at the same time, speed, and relative to each other. I want to know how to make two [I]separate[/I] sprites, which leads on to the next example: 2. Building on the previous example, now modify the project so that each explosion is animated at a random animation rate. Can someone help me?
starttime variable is being updated to the last time a frame was rendered, and the frame counter will increase only if starttime+delay time has passed. it means if you use two separated frame counters, you can make them grow at different speed just by changing their delay values. For example: [CODE] if (d3ddev->BeginScene()) { spriteobj->Begin(D3DXSPRITE_ALPHABLEND); Sprite_Animate(frame, 0, 29, 1, starttime, 30); Sprite_Animate(frame1, 0, 29, 1, starttime1, 10); Sprite_Draw_Frame(explosion, 200, 200, frame, 128, 128, 6); Sprite_Draw_Frame(explosion1, 500, 500, frame1, 128, 128, 6); //stop rendering spriteobj->End(); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); } [/CODE] If you want, you can also increase the 'direction' value, so you'll skip some frames, making the animation go faster, but maybe bad looking.
[QUOTE=pepin180;37945738]starttime variable is being updated to the last time a frame was rendered, and the frame counter will increase only if starttime+delay time has passed. it means if you use two separated frame counters, you can make them grow at different speed just by changing their delay values. For example: [CODE] if (d3ddev->BeginScene()) { spriteobj->Begin(D3DXSPRITE_ALPHABLEND); Sprite_Animate(frame, 0, 29, 1, starttime, 30); Sprite_Animate(frame1, 0, 29, 1, starttime1, 10); Sprite_Draw_Frame(explosion, 200, 200, frame, 128, 128, 6); Sprite_Draw_Frame(explosion1, 500, 500, frame1, 128, 128, 6); //stop rendering spriteobj->End(); d3ddev->EndScene(); d3ddev->Present(NULL, NULL, NULL, NULL); } [/CODE] If you want, you can also increase the 'direction' value, so you'll skip some frames, making the animation go faster, but maybe bad looking.[/QUOTE] I tried a method like that before (using those same variables even!) but I'll try your method. Thanks. [editline]7th October 2012[/editline] Haha, thanks a lot man! I think I was about to solve it, too, as I had, literally, those same variables used in your code. Much appreciated :)
Sorry, you need to Log In to post a reply to this thread.