Commit 95443cbe authored by Stephen D's avatar Stephen D
Browse files

Command line options

parent 62120bfc
......@@ -3,6 +3,7 @@
#include <math.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include "qdbmp.h"
typedef struct RigidMass
......@@ -46,7 +47,7 @@ void processTick(RigidMass_t *body)
//END PHYSICS ENGINE
//thank you, stackoverflow
//stackoverflow stuff
/* reverse: reverse string s in place */
void reverse(char s[])
{
......@@ -78,17 +79,39 @@ void itoa(int n, char s[])
}
//END SO
//Data that needs to be saved and loaded
typedef struct config
{
int nStars;
int imgW, imgH;
int savePeriod;
int iteration;
char saveFile[255];
} config_t;
/* DEFAULT CONFIGURATION OPTIONS */
int nStars = 100;
int w = 1920;
int h = 1080;
int spawnSize = 500;
double maxComponentV = 10;
long m = 50000000000;
int a = 100;
int iteration = 0;
char s[255]; //save filename
char l[255]; //load filename
/* END DEFAULT CONFIGURATION OPTIONS */
//booleans are cool
typedef int bool;
#define true 1
#define false 0
/* CONFIGURATION OPTIONS */
#define numStars 100000
const int w = 1920; //image width
const int h = 1080; //image height
const int spawnSize = 500; //Centered on center of image
const double max_component_vel = 10;
const int num_iterations = 10000;
/* END CONFIGURATION OPTIONS */
//Allocate this array later!
RigidMass_t *stars;
RigidMass_t stars[numStars];
void generateStars()
{
int randAddonX = w/2 - spawnSize/2;
......@@ -100,7 +123,7 @@ void generateStars()
new.dvy = 0;
new.tick_time = 0.05;
for(int i = 0; i < numStars; i++)
for(int i = 0; i < nStars; i++)
{
new.x = rand() % spawnSize + randAddonX;
new.y = rand() % spawnSize + randAddonY;
......@@ -109,12 +132,12 @@ void generateStars()
if(abs(dx) <= abs(dy))
{
new.vx = (double)rand()/(double)(RAND_MAX/max_component_vel);
new.vx = (double)rand()/(double)(RAND_MAX/maxComponentV);
new.vy = -new.vx * dx / dy;
}
else
{
new.vy = (double)rand()/(double)(RAND_MAX/max_component_vel);
new.vy = (double)rand()/(double)(RAND_MAX/maxComponentV);
new.vx = -new.vy * dy / dx;
}
......@@ -131,9 +154,9 @@ void generateStars()
void iterate()
{
#pragma omp parallel for
for(int a = 0; a < numStars; a++)
for(int a = 0; a < nStars; a++)
{
for(int b = 0; b < numStars; b++)
for(int b = 0; b < nStars; b++)
{
//If stars are closer than 0.1 units apart, we don't calculate the force on each other
//this is important since we're stepping through time. If we end up with two stars ridiculously
......@@ -149,7 +172,7 @@ void iterate()
}
//'commit' the deltaV
for(int a = 0; a < numStars; a++)
for(int a = 0; a < nStars; a++)
{
processTick(stars + a);
//printf("%f, %f \r\n", stars[a].x, stars[a].y);
......@@ -159,7 +182,7 @@ void iterate()
BMP *out;
void addToImage()
{
for(int a = 0; a < numStars; a++)
for(int a = 0; a < nStars; a++)
{
BMP_SetPixelRGB(out, stars[a].x, stars[a].y, 0, 150, 0);
}
......@@ -181,21 +204,125 @@ void saveImage(double frame)
out = BMP_Create(w, h, 24);
}
int main()
void prnHelp(char *name)
{
//TODO: iterations per frame
//ticktime?
printf("Usage: %s [OPTION]\r\n", name);
printf("Available options:\r\n");
printf("N\t New simulation\r\n");
printf("n\t Number of stars\r\n");
printf("w WDTH\t Image width\r\n");
printf("h HGHT\t Image height\r\n");
printf("p SIZE\t Spawn size - Where new stars are put\r\n");
printf("v VEL\t Maximum component velocity of new stars\r\n");
printf("m MASS\t Mass of stars\r\n");
printf("s FILE\t Filename for autosaves\r\n");
printf("a PER\t How often to autosave, in iterations\r\n");
printf("L FILE\t Load previous simulation\r\n");
exit(0);
}
int main(int argc, char **argv)
{
printf("\t\tStephen's Gravitational Engine\r\n");
printf("\t\t\twww.scd31.com\r\n");
//Process commandline arguments
bool success = false;
bool new = false;
bool load = false;
strcpy(s, "sim"); //default autosave filename
char c;
while((c = getopt(argc, argv, "Nn:w:h:p:v:m:a:L:s:")) != -1)
{
success = true;
switch(c)
{
case 'N': new = true; break;
case 'n': nStars = atoi(optarg); break;
case 'w': w = atoi(optarg); break;
case 'h': h = atoi(optarg); break;
case 'p': spawnSize = atoi(optarg); break;
case 'v': maxComponentV = atof(optarg); break;
case 'm': m = atoi(optarg); break;
case 'a': a = atoi(optarg); break;
case 's':
if(strlen(optarg) >= 255)
{
printf("ERROR: Save filename too long.\r\n");
}
else strcpy(s, optarg);
break;
case 'L':
if(strlen(optarg) >= 255)
{
printf("ERROR: Load filename too long.\r\n");
}
else strcpy(l, optarg);
break;
default:
prnHelp(argv[0]);
}
}
if(!success)
{
prnHelp(argv[0]);
}
//Command validation
if(!new && !load)
{
printf("ERROR: Either start a new simulation or load an old one.\r\n");
exit(1);
}
if(new && load)
{
printf("ERROR: Cannot start a new simulation and load an old one at the same time!\r\n");
exit(1);
}
if(new)
{
printf("Starting a new simulation with the following parameters:\r\n");
printf("\t N = %d\r\n", nStars);
printf("\t W = %d, H = %d\r\n", w, h);
printf("\t spawn = %d\r\n", spawnSize);
printf("\t vComponentMax = %f\r\n", maxComponentV);
printf("\t m = %ld\r\n", m);
printf("\t Autosave: %s-ID.sgs every %d iterations\r\n", s, a);
//allocate array
stars = malloc(sizeof(RigidMass_t) * nStars);
generateStars();
}
else if(load) //technically redundant. can never be too sure
{
printf("Continuing %s...", l);
printf("%f stars loaded.", -1);
}
srand(time(NULL));
out = BMP_Create(w, h, 24);
generateStars();
for(int i = 0; i < num_iterations; i++)
//Runs until CTRL+C
//TODO: CTRL+C should force autosave
while(1)
{
iterate();
addToImage();
if (i % 10 == 0)
if (iteration % 10 == 0)
{
saveImage(i / 10);
saveImage(iteration / 10);
}
iteration++;
}
return 0;
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment