From 5ad07d3ff44779950e2394ce7228eee6128f6de0 Mon Sep 17 00:00:00 2001 From: Florian Burgener <florian.burgener@etu.hesge.ch> Date: Mon, 13 Dec 2021 14:42:20 +0100 Subject: [PATCH] Refactoring --- CelestialObject.c | 36 ++++++++++++++++++++-- CelestialObject.h | 5 ++- Makefile | 2 +- PlanetarySystem.c | 78 ++++++++++++++++------------------------------- PlanetarySystem.h | 2 +- drawing.c | 45 +++++++++++++++++++++++++++ drawing.h | 14 +++++++++ main.c | 7 ++--- 8 files changed, 127 insertions(+), 62 deletions(-) create mode 100644 drawing.c create mode 100644 drawing.h diff --git a/CelestialObject.c b/CelestialObject.c index 2d0086f..b94e792 100644 --- a/CelestialObject.c +++ b/CelestialObject.c @@ -1,13 +1,18 @@ #include "CelestialObject.h" +#include <GL/glut.h> #include <stdlib.h> +#include "PlanetarySystem.h" +#include "drawing.h" + #define G 6.67e-11 #define SUN_MASS 1.989e30 -CelestialObject *celestial_object_create(double mass, double semi_major_axis, double eccentricity, uint32_t drawing_disc_radius, uint32_t drawing_color) { - CelestialObject *object = (CelestialObject*)malloc(sizeof(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 *object = (CelestialObject *)malloc(sizeof(CelestialObject)); + object->name = name; object->mass = mass; object->previous_position = vector2_create_zero(); @@ -23,3 +28,30 @@ CelestialObject *celestial_object_create(double mass, double semi_major_axis, do return object; } + +void draw_celestial_object(PlanetarySystem *planetary_system, int32_t i, Vector2 reference_frame) { + CelestialObject *object = planetary_system->objects[i]; + Vector2 scaled_position = scale_position(planetary_system, object->current_position, reference_frame); + + uint32_t color = object->drawing_color; + glColor3ub((color & 0xFF0000) >> 16, (color & 0x00FF00) >> 8, (color & 0x0000FF) >> 0); + + if (planetary_system->zoom_factor < 1) + draw_disc(scaled_position, object->drawing_disc_radius * planetary_system->zoom_factor); + else + draw_disc(scaled_position, object->drawing_disc_radius); + + if (i != 5) { + glLineWidth(4.0); + + for (int32_t j = 0; j < object->points_length - 1; j += 1) { + Vector2 p1 = scale_position(planetary_system, object->points[j], reference_frame); + Vector2 p2 = scale_position(planetary_system, object->points[j + 1], reference_frame); + draw_line(p1, p2); + } + } + + if (i == 5 && planetary_system->zoom_factor < 36) + return; + draw_text(object->name, scaled_position); +} diff --git a/CelestialObject.h b/CelestialObject.h index 0b1b3a8..84de0d4 100644 --- a/CelestialObject.h +++ b/CelestialObject.h @@ -3,6 +3,7 @@ #include "Vector2.h" +typedef struct PlanetarySystem PlanetarySystem; typedef struct CelestialObject { double mass; Vector2 previous_position; @@ -13,8 +14,10 @@ typedef struct CelestialObject { uint32_t drawing_color; Vector2 *points; int32_t points_length; + char *name; } CelestialObject; -CelestialObject *celestial_object_create(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, uint32_t drawing_disc_radius, uint32_t drawing_color); +void draw_celestial_object(PlanetarySystem *planetary_system, int32_t i, Vector2 reference_frame); #endif diff --git a/Makefile b/Makefile index 602e97a..af52a68 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ LDFLAGS:=-lm -lGL -lGLU -lglut %.o: %.c $(HEADERS) $(CC) $(CFLAGS) -c $< -o $@ -$(TARGET): main.o Vector2.o CelestialObject.o PlanetarySystem.o +$(TARGET): main.o Vector2.o CelestialObject.o PlanetarySystem.o drawing.o $(CC) $(CFLAGS) -o $@ $^ $(LDFLAGS) clean: diff --git a/PlanetarySystem.c b/PlanetarySystem.c index f741fb6..24c0f35 100644 --- a/PlanetarySystem.c +++ b/PlanetarySystem.c @@ -3,9 +3,11 @@ #include <GL/glut.h> #include <math.h> #include <stdlib.h> +#include <string.h> #include "CelestialObject.h" #include "Vector2.h" +#include "drawing.h" const uint32_t SCREEN_WIDTH = 1000; const uint32_t SCREEN_HEIGHT = 1000; @@ -34,19 +36,34 @@ const double MARS_SEMI_MAJOR_AXIS = 227.943824 * 1E9; PlanetarySystem *planetary_system_create() { PlanetarySystem *planetary_system = (PlanetarySystem *)malloc(sizeof(PlanetarySystem)); - planetary_system->objects_length = 6; + planetary_system->objects_length = 9; planetary_system->objects = (CelestialObject **)malloc(sizeof(PlanetarySystem *) * planetary_system->objects_length); planetary_system->zoom_factor = 1; planetary_system->reference_frame_object_index = 0; - planetary_system->objects[0] = celestial_object_create(SUN_MASS, 0, 0, 50, 0xFFFFFF); - planetary_system->objects[1] = celestial_object_create(MERCURY_MASS, MERCURY_SEMI_MAJOR_AXIS, MERCURY_ECCENTRICITY, 10, 0xDBCECA); - planetary_system->objects[2] = celestial_object_create(VENUS_MASS, VENUS_SEMI_MAJOR_AXIS, VENUS_ECCENTRICITY, 20, 0x8B7D82); - planetary_system->objects[3] = celestial_object_create(EARTH_MASS, EARTH_SEMI_MAJOR_AXIS, EARTH_ECCENTRICITY, 20, 0x6b93d6); - planetary_system->objects[4] = celestial_object_create(MARS_MASS, MARS_SEMI_MAJOR_AXIS, MARS_ECCENTRICITY, 12, 0xBC2732); - planetary_system->objects[5] = celestial_object_create(7.34767309 * 1E22, 384.399 * 1E6, 0.0549, 5, 0x8A2BE2); + // Sun + planetary_system->objects[0] = celestial_object_create("", SUN_MASS, 0, 0, 50, 0xFFFFFF); + // Mercury + planetary_system->objects[1] = celestial_object_create("Mercury", MERCURY_MASS, MERCURY_SEMI_MAJOR_AXIS, MERCURY_ECCENTRICITY, 10, 0xDBCECA); + // Venus + planetary_system->objects[2] = celestial_object_create("Venus", VENUS_MASS, VENUS_SEMI_MAJOR_AXIS, VENUS_ECCENTRICITY, 20, 0x8B7D82); + // Earth + planetary_system->objects[3] = celestial_object_create("Earth", EARTH_MASS, EARTH_SEMI_MAJOR_AXIS, EARTH_ECCENTRICITY, 20, 0x6b93d6); + // Mars + planetary_system->objects[4] = celestial_object_create("Mars", MARS_MASS, MARS_SEMI_MAJOR_AXIS, MARS_ECCENTRICITY, 12, 0xBC2732); + + // Moon + planetary_system->objects[5] = celestial_object_create("Moon", 7.34767309 * 1E22, 384.399 * 1E6, 0.0549, 5, 0x7D7B67); planetary_system->objects[5]->current_position.x += planetary_system->objects[3]->current_position.x; + // Fake planets are all green. + // Fake Planet 1 + planetary_system->objects[6] = celestial_object_create("Naboo", 1000000, 800.598262 * 1E9, 0.8, 12, 0x005500); + // Fake Planet 2 (Fake Jupyter) + planetary_system->objects[7] = celestial_object_create("Jupitaire", 1898.6 * 1E10, 778.340821 * 1E9, 0.04839266, 12, 0x005500); + // Fake Planet 3 + planetary_system->objects[8] = celestial_object_create("Endor", 1E23, 100 * 1E9, 0.34, 12, 0x005500); + return planetary_system; } @@ -117,53 +134,10 @@ void planetary_system_update(PlanetarySystem *planetary_system, double interval) } } -Vector2 scale_position(PlanetarySystem *planetary_system, Vector2 object_position, Vector2 reference_frame_position) { - Vector2 unscaled_position = vector2_substract(object_position, reference_frame_position); - unscaled_position = vector2_multiply(unscaled_position, planetary_system->zoom_factor); - - Vector2 scaled_position = vector2_multiply(unscaled_position, 1.0 / (300 * 1E9)); - scaled_position = vector2_fit_canvas(scaled_position, SCREEN_WIDTH, SCREEN_HEIGHT); - return scaled_position; -} - -void draw_disc(Vector2 position, uint32_t radius) { - glBegin(GL_POLYGON); - for (uint32_t i = 0; i < 360; i += 1) { - double theta = i * 3.1415 / 180; - double x = position.x + radius * cos(theta); - double y = position.y + radius * sin(theta); - glVertex2f(x, y); - } - glEnd(); -} - void planetary_system_draw(PlanetarySystem *planetary_system) { - CelestialObject *reference_frame = planetary_system->objects[planetary_system->reference_frame_object_index]; + Vector2 reference_frame = planetary_system->objects[planetary_system->reference_frame_object_index]->current_position; for (uint32_t i = 0; i < planetary_system->objects_length; i += 1) { - CelestialObject *object = planetary_system->objects[i]; - Vector2 scaled_position = scale_position(planetary_system, object->current_position, reference_frame->current_position); - - uint32_t color = object->drawing_color; - glColor3ub((color & 0xFF0000) >> 16, (color & 0x00FF00) >> 8, (color & 0x0000FF) >> 0); - - if (planetary_system->zoom_factor < 1) { - draw_disc(scaled_position, object->drawing_disc_radius * planetary_system->zoom_factor); - } else { - draw_disc(scaled_position, object->drawing_disc_radius); - } - - if (i != 5) { - glLineWidth(4.0); - - for (int32_t j = 0; j < object->points_length - 1; j += 1) { - glBegin(GL_LINES); - Vector2 aaa = scale_position(planetary_system, object->points[j], reference_frame->current_position); - glVertex2f(aaa.x, aaa.y); - Vector2 bbb = scale_position(planetary_system, object->points[j + 1], reference_frame->current_position); - glVertex2f(bbb.x, bbb.y); - glEnd(); - } - } + draw_celestial_object(planetary_system, i, reference_frame); } } diff --git a/PlanetarySystem.h b/PlanetarySystem.h index 7c7f12c..9617542 100644 --- a/PlanetarySystem.h +++ b/PlanetarySystem.h @@ -8,7 +8,7 @@ const uint32_t SCREEN_WIDTH; const uint32_t SCREEN_HEIGHT; -typedef struct { +typedef struct PlanetarySystem { uint32_t objects_length; CelestialObject **objects; double interval; diff --git a/drawing.c b/drawing.c new file mode 100644 index 0000000..9b07362 --- /dev/null +++ b/drawing.c @@ -0,0 +1,45 @@ +#include "drawing.h" + +#include <GL/glut.h> +#include <math.h> +#include <stdint.h> +#include <string.h> + +#include "PlanetarySystem.h" +#include "Vector2.h" + +void draw_disc(Vector2 position, uint32_t radius) { + glBegin(GL_POLYGON); + for (uint32_t i = 0; i < 360; i += 1) { + double theta = i * 3.1415 / 180; + double x = position.x + radius * cos(theta); + double y = position.y + radius * sin(theta); + glVertex2f(x, y); + } + glEnd(); +} + +void draw_line(Vector2 a, Vector2 b) { + glBegin(GL_LINES); + glVertex2f(a.x, a.y); + glVertex2f(b.x, b.y); + glEnd(); +} + +void draw_text(char *text, Vector2 position) { + glColor3f(1.0, 1.0, 1.0); + glRasterPos2f(position.x + 8, position.y + 32); + + for (int32_t i = 0; i < (int32_t)strlen(text); i++) { + glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]); + } +} + +Vector2 scale_position(PlanetarySystem *planetary_system, Vector2 object_position, Vector2 reference_frame_position) { + Vector2 unscaled_position = vector2_substract(object_position, reference_frame_position); + unscaled_position = vector2_multiply(unscaled_position, planetary_system->zoom_factor); + + Vector2 scaled_position = vector2_multiply(unscaled_position, 1.0 / (300 * 1E9)); + scaled_position = vector2_fit_canvas(scaled_position, SCREEN_WIDTH, SCREEN_HEIGHT); + return scaled_position; +} diff --git a/drawing.h b/drawing.h new file mode 100644 index 0000000..447c2a2 --- /dev/null +++ b/drawing.h @@ -0,0 +1,14 @@ +#ifndef DRAWING_H +#define DRAWING_H + +#include <stdint.h> + +#include "PlanetarySystem.h" +#include "Vector2.h" + +void draw_disc(Vector2 position, uint32_t radius); +void draw_line(Vector2 a, Vector2 b); +void draw_text(char *text, Vector2 position); +Vector2 scale_position(PlanetarySystem *planetary_system, Vector2 object_position, Vector2 reference_frame_position); + +#endif diff --git a/main.c b/main.c index 0f19312..c5d2ffb 100644 --- a/main.c +++ b/main.c @@ -7,6 +7,7 @@ #include "PlanetarySystem.h" #include "Vector2.h" +#include "drawing.h" #define WINDOW_NAME "Solar System" #define REFRESH_RATE 200 @@ -57,13 +58,9 @@ void draw() { planetary_system_draw(planetary_system); - glColor3f(1.0, 1.0, 1.0); - glRasterPos2f(8, 32); char text[100]; sprintf(text, "Simulation Speed : %d days per second", time_elasping_per_second / 86400); - for (int32_t i = 0; i < (int32_t)strlen(text); i++) { - glutBitmapCharacter(GLUT_BITMAP_HELVETICA_18, text[i]); - } + draw_text(text, vector2_create(8, 32)); glFlush(); } -- GitLab