There was nuthin on TV and I didn't want to rotate a cube so I decided to create some bouncing balls. This took all of 10 minutes to complete. I was also fed up with reading up on programming documentation.
Nothing special but it does show how to use the <vector> STL library with Dark GDK.
If you copy and paste this source into your own project then don't forget to set:
Project-Properties / Configuration Properties / C/C++ / Code Generation / Runtime Library -> /MT (Not MTd)
#include "DarkGDK.h"
#include <vector>
using namespace std;
int free_obj()
{
int c = 1;
while (dbObjectExist(c)) c++;
return c;
}
struct BOBJ {
float gravity; // decrement the force of the object
float force; // current up down speed
float bounce; // percentage of force to change when bouncing
int id; // objects ID
};
vector<BOBJ> objs;
#define MAX_BOBJ 100
void add_BOBJ()
{
int size = objs.size();
if ( size >= MAX_BOBJ ) return;
objs.resize( size + 1 );
objs[size].gravity = 0.01f;
objs[size].force = 1.00f;
objs[size].bounce = (float)( rand() % 400 + 400 ) / 1000.0f;
objs[size].id = free_obj();
dbMakeObjectSphere(objs[size].id, 5);
dbPositionObject(objs[size].id, (float)(rand() % 100), 0.0f, (float)(rand() % 100));
dbColorObject(objs[size].id, (rand()%255<<16) + (rand()%255<<8) + rand()%255);
}
void process_BOBJ()
{
int curr = objs.size();
while ( curr > 0 )
{
curr--;
int o = objs[curr].id;
float x = dbObjectPositionX(o);
float y = dbObjectPositionY(o);
float z = dbObjectPositionZ(o);
y = y + objs[curr].force;
objs[curr].force = objs[curr].force - objs[curr].gravity;
if ( objs[curr].force < 0.0f && y < 0.0f )
{
// reverse the objects force and set the new bounce rate
objs[curr].force = -objs[curr].force * objs[curr].bounce;
// because I'm going backwards then it's safe to remove the item
if ( objs[curr].force < 0.1f )
{
dbDeleteObject(o);
objs.erase( objs.begin() + curr );
o = 0; // flag that it has gone
}
}
if ( o ) // still exists?
{
dbPositionObject(o, x,y,z);
}
}
}
void DarkGDK ( void )
{
dbSyncOn ( );
dbSyncRate ( 60 );
dbAutoCamOff();
dbPositionCamera(-30,30,-30);
dbPointCamera(100,0,100);
while ( LoopGDK ( ) )
{
add_BOBJ();
process_BOBJ();
dbSync ( );
}
return;
}
And it's been ages since a C++ snippet was posted...
Mental arithmetic? Me? (That's for computers) I can't subtract a fart from a plate of beans!
Warning! May contain Nuts!