Skip to content
Snippets Groups Projects
Commit 6479a3ae authored by Florian Burgener's avatar Florian Burgener
Browse files

Free memory

parent fe882640
Branches
No related tags found
No related merge requests found
...@@ -8,7 +8,7 @@ ...@@ -8,7 +8,7 @@
const int32_t PREVIOUS_POSITIONS_LENGTH = 200; const int32_t PREVIOUS_POSITIONS_LENGTH = 200;
CelestialObject *celestial_object_create(char *name, double mass, double semi_major_axis, double eccentricity, uint32_t drawing_disc_radius, uint32_t drawing_color) { CelestialObject *celestial_object_create(char *name, double mass, double semi_major_axis, double eccentricity, int32_t drawing_disc_radius, int32_t drawing_color) {
CelestialObject *object = (CelestialObject *)malloc(sizeof(CelestialObject)); CelestialObject *object = (CelestialObject *)malloc(sizeof(CelestialObject));
strcpy(object->name, name); strcpy(object->name, name);
...@@ -30,7 +30,12 @@ CelestialObject *celestial_object_create(char *name, double mass, double semi_ma ...@@ -30,7 +30,12 @@ CelestialObject *celestial_object_create(char *name, double mass, double semi_ma
return object; return object;
} }
uint32_t get_zoomed_drawing_disc_radius(CelestialObject *object, double zoom_factor) { void celestial_object_destroy(CelestialObject *object) {
free(object->previous_positions);
free(object);
}
int32_t get_zoomed_drawing_disc_radius(CelestialObject *object, double zoom_factor) {
if (zoom_factor < 1) { if (zoom_factor < 1) {
return object->drawing_disc_radius * zoom_factor; return object->drawing_disc_radius * zoom_factor;
} }
...@@ -39,7 +44,7 @@ uint32_t get_zoomed_drawing_disc_radius(CelestialObject *object, double zoom_fac ...@@ -39,7 +44,7 @@ uint32_t get_zoomed_drawing_disc_radius(CelestialObject *object, double zoom_fac
} }
void celestial_object_draw(CelestialObject *object, Vector2 reference_frame, double zoom_factor) { void celestial_object_draw(CelestialObject *object, Vector2 reference_frame, double zoom_factor) {
uint32_t color = object->drawing_color; int32_t color = object->drawing_color;
glColor3ub((color & 0xFF0000) >> 16, (color & 0x00FF00) >> 8, (color & 0x0000FF) >> 0); glColor3ub((color & 0xFF0000) >> 16, (color & 0x00FF00) >> 8, (color & 0x0000FF) >> 0);
Vector2 scaled_position = scale_position(zoom_factor, object->current_position, reference_frame); Vector2 scaled_position = scale_position(zoom_factor, object->current_position, reference_frame);
...@@ -57,6 +62,6 @@ void celestial_object_draw_name(CelestialObject *object, Vector2 reference_frame ...@@ -57,6 +62,6 @@ void celestial_object_draw_name(CelestialObject *object, Vector2 reference_frame
return; return;
} }
uint32_t zoomed_drawing_disc_radius = get_zoomed_drawing_disc_radius(object, zoom_factor); int32_t zoomed_drawing_disc_radius = get_zoomed_drawing_disc_radius(object, zoom_factor);
draw_text(object->name, vector2_add(scaled_position, vector2_create(zoomed_drawing_disc_radius + 5, -10))); draw_text(object->name, vector2_add(scaled_position, vector2_create(zoomed_drawing_disc_radius + 5, -10)));
} }
...@@ -10,13 +10,14 @@ typedef struct CelestialObject { ...@@ -10,13 +10,14 @@ typedef struct CelestialObject {
Vector2 current_position; Vector2 current_position;
double semi_major_axis; double semi_major_axis;
double eccentricity; double eccentricity;
uint32_t drawing_disc_radius; int32_t drawing_disc_radius;
uint32_t drawing_color; int32_t drawing_color;
Vector2 *previous_positions; Vector2 *previous_positions;
int32_t previous_positions_length; int32_t previous_positions_length;
} CelestialObject; } CelestialObject;
CelestialObject *celestial_object_create(char *name, double mass, double semi_major_axis, double eccentricity, uint32_t drawing_disc_radius, uint32_t drawing_color); CelestialObject *celestial_object_create(char *name, double mass, double semi_major_axis, double eccentricity, int32_t drawing_disc_radius, int32_t drawing_color);
void celestial_object_destroy(CelestialObject *object);
void celestial_object_draw(CelestialObject *object, Vector2 reference_frame, double zoom_factor); void celestial_object_draw(CelestialObject *object, Vector2 reference_frame, double zoom_factor);
void celestial_object_draw_name(CelestialObject *object, Vector2 reference_frame, double zoom_factor); void celestial_object_draw_name(CelestialObject *object, Vector2 reference_frame, double zoom_factor);
......
...@@ -63,6 +63,15 @@ PlanetarySystem *planetary_system_create() { ...@@ -63,6 +63,15 @@ PlanetarySystem *planetary_system_create() {
return system; return system;
} }
void planetary_system_destroy(PlanetarySystem *planetary_system) {
for (int32_t i = 0; i < planetary_system->objects_length; i += 1) {
celestial_object_destroy(planetary_system->objects[i]);
}
free(planetary_system->objects);
free(planetary_system);
}
CelestialObject *planetary_system_get_star(PlanetarySystem *planetary_system) { CelestialObject *planetary_system_get_star(PlanetarySystem *planetary_system) {
return planetary_system->objects[0]; return planetary_system->objects[0];
} }
......
...@@ -18,6 +18,7 @@ typedef struct PlanetarySystem { ...@@ -18,6 +18,7 @@ typedef struct PlanetarySystem {
} PlanetarySystem; } PlanetarySystem;
PlanetarySystem *planetary_system_create(); PlanetarySystem *planetary_system_create();
void planetary_system_destroy(PlanetarySystem *planetary_system);
void planetary_system_update(PlanetarySystem *planetary_system, double interval); void planetary_system_update(PlanetarySystem *planetary_system, double interval);
Vector2 planetary_system_get_reference_frame(PlanetarySystem *system); Vector2 planetary_system_get_reference_frame(PlanetarySystem *system);
void planetary_system_draw(PlanetarySystem *planetary_system); void planetary_system_draw(PlanetarySystem *planetary_system);
......
...@@ -8,9 +8,9 @@ ...@@ -8,9 +8,9 @@
#include "PlanetarySystem.h" #include "PlanetarySystem.h"
#include "Vector2.h" #include "Vector2.h"
void draw_disc(Vector2 position, uint32_t radius) { void draw_disc(Vector2 position, int32_t radius) {
glBegin(GL_POLYGON); glBegin(GL_POLYGON);
for (uint32_t i = 0; i < 360; i += 1) { for (int32_t i = 0; i < 360; i += 1) {
double theta = i * 3.1415 / 180; double theta = i * 3.1415 / 180;
double x = position.x + radius * cos(theta); double x = position.x + radius * cos(theta);
double y = position.y + radius * sin(theta); double y = position.y + radius * sin(theta);
......
...@@ -5,7 +5,7 @@ ...@@ -5,7 +5,7 @@
#include "Vector2.h" #include "Vector2.h"
void draw_disc(Vector2 position, uint32_t radius); void draw_disc(Vector2 position, int32_t radius);
void draw_line(Vector2 a, Vector2 b); void draw_line(Vector2 a, Vector2 b);
void draw_scaled_lines(Vector2 *points, int32_t points_length, Vector2 reference_frame, double zoom_factor); void draw_scaled_lines(Vector2 *points, int32_t points_length, Vector2 reference_frame, double zoom_factor);
void draw_text(char *text, Vector2 position); void draw_text(char *text, Vector2 position);
......
...@@ -25,11 +25,14 @@ const double ZOOM_MULTIPLIER = 1.1; ...@@ -25,11 +25,14 @@ const double ZOOM_MULTIPLIER = 1.1;
const int32_t DEFAULT_SIMULATION_SPEED_IN_DAYS = 10; const int32_t DEFAULT_SIMULATION_SPEED_IN_DAYS = 10;
// Maximum speed of the simulation in days. // Maximum speed of the simulation in days.
const int32_t SIMULATION_MAXIMUM_SPEED_IN_DAYS = 500; const int32_t SIMULATION_MAXIMUM_SPEED_IN_DAYS = 500;
// Time that elapses in the planetary system per second. // Time that elapses by default in the planetary system per second, in seconds.
const int32_t DEFAULT_SIMULATION_SPEED_IN_SECONDS = DEFAULT_SIMULATION_SPEED_IN_DAYS * ONE_DAY_IN_SECONDS; const int32_t DEFAULT_SIMULATION_SPEED_IN_SECONDS = DEFAULT_SIMULATION_SPEED_IN_DAYS * ONE_DAY_IN_SECONDS;
// Maximum time that elapses in the planetary system at each call of the planetary_system_update function, this value must be small enough for the planetary system to work. // Maximum time that elapses in the planetary system at each call of the planetary_system_update function, this value must be small enough for the planetary system to work.
const int32_t PLANETARY_SYSTEM_INTERVAL = 100; const int32_t PLANETARY_SYSTEM_INTERVAL = 100;
const unsigned char KEYBOARD_ESCAPE_KEY = 27;
const unsigned char KEYBOARD_T_KEY = 't';
// https://stackoverflow.com/questions/3417837/what-is-the-best-way-to-suppress-a-unused-variable-x-warning // https://stackoverflow.com/questions/3417837/what-is-the-best-way-to-suppress-a-unused-variable-x-warning
#ifdef UNUSED #ifdef UNUSED
#elif defined(__GNUC__) #elif defined(__GNUC__)
...@@ -78,7 +81,7 @@ void draw() { ...@@ -78,7 +81,7 @@ void draw() {
glClear(GL_COLOR_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT);
char title[100]; char title[100];
double elapsed_time = (get_current_time() - start_time) / ONE_SECOND_IN_MICROSECONDS; double elapsed_time = (double)(get_current_time() - start_time) / ONE_SECOND_IN_MICROSECONDS;
sprintf(title, "Solar System (%.2lf)", elapsed_time); sprintf(title, "Solar System (%.2lf)", elapsed_time);
glutSetWindowTitle(title); glutSetWindowTitle(title);
...@@ -97,12 +100,12 @@ void draw_timer() { ...@@ -97,12 +100,12 @@ void draw_timer() {
} }
void handle_keyboard_input(unsigned char key, int UNUSED(x), int UNUSED(y)) { void handle_keyboard_input(unsigned char key, int UNUSED(x), int UNUSED(y)) {
if (key == 27) { if (key == KEYBOARD_ESCAPE_KEY) {
// TODO : Free glutLeaveMainLoop();
exit(0); return;
} }
if (key == 116) { if (key == KEYBOARD_T_KEY) {
planetary_system->show_names = !planetary_system->show_names; planetary_system->show_names = !planetary_system->show_names;
} }
} }
...@@ -157,7 +160,7 @@ int main(int argc, char *argv[]) { ...@@ -157,7 +160,7 @@ int main(int argc, char *argv[]) {
glutSetOption(GLUT_MULTISAMPLE, 16); glutSetOption(GLUT_MULTISAMPLE, 16);
glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE); glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT); glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
glutCreateWindow(WINDOW_NAME); int window = glutCreateWindow(WINDOW_NAME);
glClearColor(0.0, 0.0, 0.0, 1.0); glClearColor(0.0, 0.0, 0.0, 1.0);
glMatrixMode(GL_PROJECTION); glMatrixMode(GL_PROJECTION);
...@@ -177,5 +180,8 @@ int main(int argc, char *argv[]) { ...@@ -177,5 +180,8 @@ int main(int argc, char *argv[]) {
glutMainLoop(); glutMainLoop();
glutDestroyWindow(window);
planetary_system_destroy(planetary_system);
return EXIT_SUCCESS; return EXIT_SUCCESS;
} }
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment