I've been playing around with some code to do fireworks with the view to maybe using it in a slot machine game I'm developing which I plan to release as freeware.
Anyway I thought as I'm using PocketFrog to do it I'd post the code here for lots of lovely criticism
I can think of a few obvious improvements like using a struct to store the details of my "dots". It also uses a few floats and flop division which I could maybe get rid of.
Heres the source anyhow, you should be able to just c+p this into the IDE and run it so long as you have PocketFrog Header and Lib directories set up.
Header File
CPP File
I'm a relative newbie to C++ programming so any comments would be appreciated.
Also, an easy question I'm sure, how do I set the program icon for a C++ app?
--
Rich Woods
Anyway I thought as I'm using PocketFrog to do it I'd post the code here for lots of lovely criticism
Heres the source anyhow, you should be able to just c+p this into the IDE and run it so long as you have PocketFrog Header and Lib directories set up.
Header File
- Code:
#ifndef POCKETFROG_FWORK_TEST_H
#define POCKETFROG_FWORK_TEST_H
#include <PocketFrog/PocketFrog.h>
using namespace Frog;
class FWorkTest : public Game
{
public:
// Construction
FWorkTest();
// Framework callbacks
virtual bool GameInit();
virtual void GameEnd();
virtual void GameLoop();
private:
Point m_dots[512]; // Particles
int m_colour[512]; // Particle colour
int m_timer; // Time to next firework
float m_stepx[512];
float m_stepy[512];
float m_posx[512];
float m_posy[512];
int m_size[512];
int m_fade[512];
};
#endif
CPP File
- Code:
#include "fwork.h"
using namespace Frog;
CComModule _Module;
///////////////////
//
// Fireworks Test
//
///////////////////
FWorkTest::FWorkTest()
{
m_config.szWindowName = _T( "Firework Test" );
m_config.orientation = ORIENTATION_NORMAL;
}
bool FWorkTest::GameInit()
{
// Initialize random seed
srand( PocketPC::GetTickCount() );
for (int i = 0; i < 512; i++)
{
// Set all variables to initial values
m_colour[i] = 0;
m_dots[i].x = 0;
m_dots[i].y = 0;
m_stepx[i] = 0;
m_stepy[i] = 0;
m_posx[i] = 0;
m_posy[i] = 0;
m_size[i] = 0;
m_fade[i] = 0;
}
m_timer = 0;
return true;
}
void FWorkTest::GameEnd()
{
}
void FWorkTest::GameLoop()
{
int i, j, k;
Display* display = GetDisplay();
display->GetBackBuffer();
// start with a clear black background
display->Clear(Color( 0, 0, 0));
if (m_timer == 0)
{
// y offset for this batch of rockets (vertical speed)
float y = -.35 + (float (rand() % 100) / 1000);
// z stores qty of rockets in this group - float for use in float arithmetic later
float z = (rand() % 5) + 1;
// x offset calculated consecutively in a loop
for (int x = 0; x < z; x++)
{
// loop through dots to find the first free one
for (i = 0; i < 512; i++)
{
if (m_colour[i] == 0)
{
// rockets are a dull brown colour
m_colour[i] = Color( 64, 64, 0);
// set initial position
m_posx[i] = (display->GetWidth() / 2) - 1;
m_posy[i] = display->GetHeight() -1;
// calculate this rocket's x and y offsets
m_stepx[i] = (float (rand() % 20 + (x * 20)) / 1000) - (z / 100);
m_stepy[i] = y + (float (rand() % 10) / 1000);
break;
}
}
}
m_timer = 500;
}
m_timer--;
// loop through all the active dots
for (i = 0; i < 512; i++)
{
// dot not active == ignore
if (m_colour[i])
{
// dots of this colour are rockets
if (m_colour[i] == Color( 64, 64, 0))
{
// is rocket ready to explode?
if (m_stepy[i] > -.05 && (rand() % 50) == 0)
{
// exploded rockets become embers, coloured orange
m_colour[i] = Color( 128, 64, 32);
m_size[i] = 2;
// choose colour for this explosion
int newcol = Color( (rand() % 127) + 128, (rand() % 127) + 128, (rand() % 127) + 128);
// random factor to size of this explosion
int x = (rand() % 100);
// make 40 new dots active
for (j = 0; j < 40; j++)
{
// look for a free dot
for (k = 0; k < 512; k++)
{
// inactive so we'll use this one
if (m_colour[k] == 0)
{
// set to colour of explosion
m_colour[k] = newcol;
// set to position of explosion
m_posx[k] = m_posx[i];
m_posy[k] = m_posy[i];
// set offsets random in all directions
m_stepx[k] = (float (rand() % 50) / 100) - .25;
m_stepy[k] = (float (rand() % 50) / 100) - .25;
// set time taken for this dot to fade
m_fade[k] = (rand() % 55) + 200 + x;
// size of dot based on time left to fade
m_size[k] = (m_fade[k] / 64) + 1;
break;
}
}
}
}
else
// still looking at a rocket
// create a spark falling from it now and then
if (rand() % 10 == 0)
{
// look for a free dot
for (k = 0; k < 512; k++)
{
if (m_colour[k] == 0)
{
// sparks are orange
m_colour[k] = Color(255, 128, 64);
// set initial position at rocket
m_posx[k] = m_posx[i];
m_posy[k] = m_posy[i];
// initial move offsets are opposite to rocket with a slight random factor
m_stepx[k] = (0 - m_stepx[i]) + (float (rand() % 100) / 1000) - .05;
m_stepy[k] = (0 - m_stepy[i]) + (float (rand() % 100) / 1000) - .05;
// set time taken for this dot to fade
m_fade[k] = (rand() % 27) + 100;
// size of dot based on time left to fade
m_size[k] = (m_fade[k] / 64) + 1;
break;
}
}
}
}
else
// now deal with dots that don't represent rockets
// ignore embers, they don't fade
if (m_colour[i] != Color( 128, 64, 32))
{
// fade the dot by one
if (m_fade > 0)
{
m_fade[i] -= 1;
// resize the dot
m_size[i] = (m_fade[i] / 64) + 1;
if (m_fade[i] == 0)
{
// when the dot fades to nothing there's a 2% chance it will become an ember
// otherwise we set colour to 0 to free it for use again
if (rand() % 50 == 0) m_colour[i] = Color( 128, 64, 32);
else m_colour[i] = 0;
}
}
}
// apply some gravity
m_stepy[i] += .0002;
// move to new position by adding offsets
m_posy[i] += m_stepy[i];
m_posx[i] += m_stepx[i];
// set the integer position of the dot
m_dots[i].x = m_posx[i];
m_dots[i].y = m_posy[i];
// check on screen
if (m_dots[i].y < display->GetHeight() && m_dots[i].y >= 0 &&
m_dots[i].x < display->GetWidth() && m_dots[i].x >= 0)
{
// if its a rocket draw a line instead of a dot
if (m_colour[i] == Color( 64, 64, 0))
{
display->DrawLine(m_dots[i].x, m_dots[i].y,
m_dots[i].x + int (m_stepx[i] * 40), m_dots[i].y + int (m_stepy[i] * 40), m_colour[i]);
}
else
// draw a rect or set a pixel depending on size of dot
if (m_size[i] > 1)
{
display->FillRect(m_dots[i].x, m_dots[i].y,
m_dots[i].x + m_size[i], m_dots[i].y + m_size[i], m_colour[i]);
// tracer line from back
display->DrawLine(m_dots[i].x, m_dots[i].y,
m_dots[i].x - int (m_stepx[i] * 20), m_dots[i].y - int (m_stepy[i] * 20), m_colour[i]);
}
else
display->SetPixel(m_dots[i].x, m_dots[i].y, m_colour[i]);
}
else
// off screen, free it for use
{
m_colour[i] = 0;
}
}
}
// Update screen
display->Update();
}
//////////////////////////////////////////////////////////////////////////////
//
// WinMain - Entry point
//
//////////////////////////////////////////////////////////////////////////////
int WINAPI WinMain( HINSTANCE hInstance, HINSTANCE, LPTSTR, int )
{
_Module.Init( 0, hInstance );
FWorkTest game;
game.Run();
return 0;
}
I'm a relative newbie to C++ programming so any comments would be appreciated.
Also, an easy question I'm sure, how do I set the program icon for a C++ app?
--
Rich Woods