static SolarSystem *load_from_config(ALLEGRO_CONFIG *cfg) { SolarSystem *solsys; char **primary_names; const char *name; long num_bodies; ALLEGRO_CONFIG_SECTION *sec; int i; /* First pass: Determine the number of bodies in the file */ num_bodies = 0; sec = NULL; while ((name = get_next_config_section(cfg, &sec)) != NULL) { if (name[0] != '\0') num_bodies++; } if (num_bodies == 0) return NULL; /* Empty solarsystem */ solsys = ralloc_size(NULL, sizeof(SolarSystem) + num_bodies*sizeof(Body)); if (solsys == NULL) return NULL; solsys->num_bodies = num_bodies; primary_names = ralloc_array(solsys, char *, num_bodies); if (primary_names == NULL) { ralloc_free(solsys); return NULL; } /* Second pass: Load all celestial bodies */ i = 0; sec = NULL; while ((name = get_next_config_section(cfg, &sec)) != NULL && i < num_bodies) { if (name[0] == '\0') continue; solsys->body[i].ctx = solsys; if (!load_body(cfg, name, &solsys->body[i], &primary_names[i])) { log_err("Couldn't load body %s\n", name); ralloc_free(solsys); return NULL; } i++; } if (i < num_bodies) log_err("Internal consistency error\n"); /* Third pass: Connect each satellite body to its primary */ for (i = 0; i < num_bodies; i++) { Body *body = &solsys->body[i]; char *primary_name = primary_names[i]; if (primary_name == NULL) /* Independent body */ continue; /* Look for the primary */ body->primary = NULL; for (int j = 0; j < num_bodies; j++) { Body *body2 = &solsys->body[j]; if (strcmp(primary_name, body2->name) == 0) { body->primary = body2; break; } } if (body->primary == NULL) { log_err("Couldn't find %s's primary: %s\n", body->name, primary_name); ralloc_free(solsys); return NULL; } ralloc_free(primary_name); primary_name = NULL; /* Won't ever be used again */ body->primary->num_satellites++; body->primary->satellite = reralloc(solsys, body->primary->satellite, Body *, body->primary->num_satellites); if (body->primary->satellite == NULL) { log_err("Out of memory\n"); ralloc_free(solsys); return NULL; } body->primary->satellite[body->primary->num_satellites - 1] = body; body->orbit.epoch = 0; body->orbit.period = M_TWO_PI * sqrt(CUBE(body->orbit.SMa) / body->primary->grav_param); body->orbit.plane_orientation = quat_euler(RAD(body->orbit.LAN), RAD(body->orbit.Inc), RAD(body->orbit.APe)); } ralloc_free(primary_names); return solsys; }
int main(int argc, char *argv[]) { (void)argc; (void)argv; #ifdef __SSE__ printf("SSE "); #endif #ifdef __SSE2__ printf("SSE2 "); #endif #ifdef __SSE3__ printf("SSE3 "); #endif #ifdef __SSE4__ printf("SSE4 "); #endif #ifdef __SSE4_1__ printf("SSE4.1 "); #endif #ifdef __SSE4_2__ printf("SSE4.2 "); #endif #ifdef __AVX__ printf("AVX "); #endif #ifdef __FMA4__ printf("FMA4 "); #endif printf("\n"); printv(vec(1, 2, 3, 4)); printv(vzero()); printm(mzero()); printm(midentity()); vec4 a = { 1, 2, 3, 4 }, b = { 5, 6, 7, 8 }; printv(a); printv(b); printf("\nshuffles:\n"); printv(vshuffle(a, a, 0, 1, 2, 3)); printv(vshuffle(a, a, 3, 2, 1, 0)); printv(vshuffle(a, b, 0, 1, 0, 1)); printv(vshuffle(a, b, 2, 3, 2, 3)); printf("\ndot products:\n"); printv(vdot(a, b)); printv(vdot(b, a)); printv(vdot3(a, b)); printv(vdot3(b, a)); //vec4 blendmask = { 1, -1, 1, -1 }; //printv(vblend(x, y, blendmask)); vec4 x = { 1, 0, 0, 0 }, y = { 0, 1, 0, 0 }, z = { 0, 0, 1, 0 }, w = { 0, 0, 0, 1 }; printf("\ncross products:\n"); printv(vcross(x, y)); printv(vcross(y, x)); printv(vcross_scalar(x, y)); printv(vcross_scalar(y, x)); printf("\nquaternion products:\n"); printv(qprod(x, y)); printv(qprod(y, x)); printv(qprod_mad(x, y)); printv(qprod_mad(y, x)); printv(qprod_scalar(x, y)); printv(qprod_scalar(y, x)); printf("\nquaternion conjugates:\n"); printv(qconj(x)); printv(qconj(y)); printv(qconj(z)); printv(qconj(w)); printf("\nmat from quat:\n"); printm(quat_to_mat(w)); printm(quat_to_mat_mmul(w)); printm(quat_to_mat_scalar(w)); vec4 angles = { 0.0, 0.0, 0.0, 0.0 }; printf("\neuler to quaternion:\n"); printv(quat_euler(angles)); printv(quat_euler_scalar(angles)); printv(quat_euler_gems(angles)); printf("\neuler to matrix:\n"); printm(mat_euler(angles)); printm(mat_euler_scalar(angles)); printm(quat_to_mat(quat_euler(angles))); printf("\nperspective matrix:\n"); printm(mat_perspective_fovy(M_PI/4.0, 16.0/9.0, 0.1, 100.0)); printm(mat_perspective_fovy_inf_z(M_PI/4.0, 16.0/9.0, 0.1)); printm(mat_perspective_fovy_scalar(M_PI/4.0, 16.0/9.0, 0.1, 100.0)); printf("\northogonal matrix:\n"); printm(mat_ortho(-1.0, 1.0, -1.0, 1.0, -1.0, 1.0)); printm(mat_ortho(-1.0, 2.0, -1.0, 2.0, -1.0, 2.0)); printf("\ntranslate matrix:\n"); printm(mtranslate(a)); printf("\nscale matrix:\n"); printm(mscale(a)); return 0; }