#include "ptk.h" #include "KPTK.h" #include "KSound.h" #include "KMiscTools.h" #include "math.h" #include "time.h" KWindow *gameWindow ; KGraphic *surf_particles = NULL; KBatch *particleBatch ; #define MAX_PARTICLES 1024 struct sParticle { float x,y,speedx,speedy , alpha , alphafade; short color ; }; //our array of particles sParticle allParticles[MAX_PARTICLES] ; void initParticles( void ) ; void resetParticle( sParticle *partPtr ) ; void doBatching( void ) ; #ifdef WIN32 int WINAPI WinMain( HINSTANCE hInst, HINSTANCE hPreInst, LPSTR lpszCmdLine, int nCmdShow ) { #else int main( void ) { #endif initParticles( ) ; gameWindow = KPTK::createKWindow( K_DIRECTX ) ; gameWindow->createGameWindow( 800,600,-1,true,"PTK Batch Test" ); KInput::hidePointer( ) ; //allocate our batching buffer. particleBatch = KPTK::createKBatch( ) ; particleBatch->allocateBuffer( MAX_PARTICLES ) ; surf_particles = KPTK::createKGraphic( ) ; surf_particles->loadPicture( KMiscTools::makeFilePath( "particles.tga" ) , true, true ) ; surf_particles->setAlphaMode( 0 ) ; do { gameWindow->setClearColor( 0,0,0,1 ) ; gameWindow->setWorldView( 0,0,0,1,true) ; doBatching( ) ; gameWindow->flipBackBuffer( ) ; }while( gameWindow->isQuit() == false ) ; delete surf_particles ; delete particleBatch; delete gameWindow ; return 0 ; } void doBatching( void ) { long i ; sParticle *partPtr ; particleBatch->beginBatch( surf_particles ) ; //calculate our motion for ( i = 0 ; i < MAX_PARTICLES ; i++ ) { partPtr = &allParticles[i] ; partPtr->alpha -= partPtr->alphafade ; if ( partPtr->alpha <= 0 ) { resetParticle( partPtr ) ; continue ; } partPtr->x += partPtr->speedx ; partPtr->y += partPtr->speedy ; partPtr->speedy+=0.1f ; //add some gravity switch( partPtr->color ) { case 0 : particleBatch->blitAlphaRectFx( 55,81,97,117,partPtr->x, partPtr->y , 0,1,partPtr->alpha ) ; break ; case 1 : particleBatch->blitAlphaRectFx( 159,51,204,88,partPtr->x, partPtr->y , 0,1,partPtr->alpha ) ; break ; case 2 : particleBatch->blitAlphaRectFx( 34,184,85,227,partPtr->x, partPtr->y , 0,1,partPtr->alpha ) ; break ; case 3 : particleBatch->blitAlphaRectFx( 136,143,171,180,partPtr->x, partPtr->y , 0,1,partPtr->alpha ) ; break ; } } particleBatch->endBatch( ) ; } //resets a particle void resetParticle( sParticle *partPtr ) { partPtr->alpha = 1 ; partPtr->alphafade = ( rand( ) % 1000 ) ; partPtr->alphafade /= 20000.0f ; partPtr->x = 320+rand( ) % 30 ; partPtr->y =400+rand( ) % 10 ; partPtr->color = rand( ) % 4 ;; partPtr->speedx = (float)((rand( ) % 60) -30) / 10.0f ; partPtr->speedy = -(5+(rand( ) % 5)) ; } void initParticles( void ) { long i ; //clear our array of particles. memset( allParticles , sizeof( sParticle ) * MAX_PARTICLES , 0 ) ; for ( i = 0 ; i < MAX_PARTICLES ; i++ ) { resetParticle(&allParticles[i] ) ; } }