Astrophysics/main.c

133 lines
3.7 KiB
C

#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL.h>
#include "argparser.h"
#include "yaml.h"
#include "gravity.h"
#include "motion.h"
#include "constantes.h"
#include "functions.h"
#include "sdl/projectile.h"
static void mainInitPlanet(const struct yamlFile *, struct object *, char *, double);
static void mainInitProjectile(const struct yamlFile *, struct projectile *, const double);
int main(int argc, char *argv[]) {
int res = -1;
int debug = FALSE;
FILE *f = NULL;
/* Create our structures */
struct yamlFile *yamlFile = malloc(sizeof(struct yamlFile));
struct constantes constantes;
struct object s_planet1;
struct projectile s_projectile;
int i;
/** CHECK ARGUMENTS **/
if (argc == 1)
return -1;
res = getArgs(argv[1]);
if (res == FALSE)
exit(1);
res = fileExist(argv[2]);
if (res == -1){
printf("File %s doesn't exist\n", argv[2]);
exit(1);
}
/* Check if debug */
if(argc == 4){
if(getArgsOptional(argv[3]) == TRUE){
debug = TRUE;
}
}
/** CONFIGURE VARIABLES **/
f = fopen(argv[2], "r");
// Stock in struct yamlFile the data
bufferingFiles(f, yamlFile);
fclose(f);
// Stock constants variables from YAML file
/* Get constantes variables */
constantes.grav = parseYamlFileDouble(yamlFile, "Constantes:grav");
// Init planet
mainInitPlanet(yamlFile, &s_planet1, "Planet1", constantes.grav);
// Initialise projectile
mainInitProjectile(yamlFile, &s_projectile, s_planet1.grav);
switch (argv[1][1]){
case 'p':
handleProjectile(&s_projectile, &s_planet1, debug);
break;
default:
break;
}
/* We destroy our multi-array */
for(i = 0; i < yamlFile->len; i++)
free(yamlFile->buf[i]);
free(yamlFile->buf);
free(yamlFile);
return 0;
}
static void mainInitPlanet(const struct yamlFile *yamlFile, struct object *s_planet, char *p, double grav){
/* Initialise caracteristique of planet */
char rayon[BUF_SIZE];
char masse[BUF_SIZE];
int res = 0;
res = sprintf(rayon, "%s:rayon", p);
if(res < 0){
printf("Failed to initialize planet: %s\n", p);
exit(1);
}
res = sprintf(masse, "%s:masse", p);
if(res < 0){
printf("Failed to initialize planet: %s\n", p);
exit(1);
}
s_planet->rayon = parseYamlFileDouble(yamlFile, rayon);
s_planet->masse = parseYamlFileDouble(yamlFile, masse);
// Calcul gravitational force in Newton
s_planet->grav = calcul_gravity_object(grav, s_planet->masse, s_planet->rayon);
}
/*
* This function initialize projectile's structure
*/
static void mainInitProjectile(const struct yamlFile *yamlFile, struct projectile *s_projectile, const double grav){
double v0 = 0.0;
double deg_to_rad = 0.0;
s_projectile->height = parseYamlFileInt(yamlFile, "Projectile:height");
s_projectile->width= parseYamlFileInt(yamlFile, "Projectile:width");
s_projectile->x = parseYamlFileInt(yamlFile, "Projectile:x");
s_projectile->y = parseYamlFileInt(yamlFile, "Projectile:y");
v0 = parseYamlFileDouble(yamlFile, "Projectile:v0");
s_projectile->masse = parseYamlFileInt(yamlFile, "Projectile:masse");
s_projectile->alpha = parseYamlFileDouble(yamlFile, "Projectile:angle");
deg_to_rad = degree_to_radian(s_projectile->alpha);
s_projectile->v0x = calculate_initial_speed(deg_to_rad, v0, 'x');
s_projectile->v0y = calculate_initial_speed(deg_to_rad, v0, 'y');
s_projectile->rect.x = s_projectile->x;
s_projectile->rect.y = s_projectile->y;
s_projectile->rect.w = s_projectile->height;
s_projectile->rect.h = s_projectile->width;
/***** Informations about the projectile *****/
// Calcul delta t
s_projectile->deltat = calculate_delta_t(s_projectile->v0y, grav);
// calcul distance in meter
s_projectile->distance = calculate_total_distance(s_projectile->v0x, s_projectile->deltat);
// Calcul height Max
s_projectile->hmax = calculate_height_max(0.0, s_projectile->v0y, grav);
}