From dcfc336fd9c38ec76777bbf7d43b4480f0a7cdf9 Mon Sep 17 00:00:00 2001
From: Florian Burgener <florian.burgener@etu.hesge.ch>
Date: Mon, 13 Dec 2021 03:18:40 +0100
Subject: [PATCH] Add simulation speed control

---
 PlanetarySystem.c |  2 +-
 main.c            | 32 +++++++++++++++++++++++++++++---
 2 files changed, 30 insertions(+), 4 deletions(-)

diff --git a/PlanetarySystem.c b/PlanetarySystem.c
index 80591e9..7da4fb0 100644
--- a/PlanetarySystem.c
+++ b/PlanetarySystem.c
@@ -146,7 +146,7 @@ void planetary_system_draw(PlanetarySystem *planetary_system) {
 
         uint32_t color = object->drawing_color;
         glColor3ub((color & 0xFF0000) >> 16, (color & 0x00FF00) >> 8, (color & 0x0000FF) >> 0);
-        draw_disc(scaled_position, object->drawing_disc_radius);
+        draw_disc(scaled_position, object->drawing_disc_radius * planetary_system->zoom_factor);
 
         if (i != 5) {
             glLineWidth(4.0);
diff --git a/main.c b/main.c
index d2af39b..0f19312 100644
--- a/main.c
+++ b/main.c
@@ -2,6 +2,7 @@
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 #include <time.h>
 
 #include "PlanetarySystem.h"
@@ -23,8 +24,9 @@
 #endif
 
 PlanetarySystem *planetary_system;
-uint32_t elapsed_time = 0;
-uint32_t true_time_shift = 0;
+int32_t elapsed_time = 0;
+int32_t true_time_shift = 0;
+int32_t time_elasping_per_second = TIME_ELASPING_PER_SECOND;
 
 void draw_timer() {
     glutPostRedisplay();
@@ -36,7 +38,7 @@ void update_timer() {
         true_time_shift = time(NULL) % 1000;
     elapsed_time += 1;
 
-    for (uint32_t i = 0; i < TIME_ELASPING_PER_SECOND / REFRESH_RATE / TIME_ELASPING_PER_UPDATE; i += 1) {
+    for (int32_t i = 0; i < time_elasping_per_second / REFRESH_RATE / TIME_ELASPING_PER_UPDATE; i += 1) {
         planetary_system_update(planetary_system, TIME_ELASPING_PER_UPDATE);
     }
 
@@ -55,6 +57,14 @@ 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]);
+    }
+
     glFlush();
 }
 
@@ -77,6 +87,22 @@ void handle_special_keyboard_input(int key, int UNUSED(x), int UNUSED(y)) {
         planetary_system->reference_frame_object_index += 1;
         planetary_system->reference_frame_object_index %= planetary_system->objects_length;
     }
+
+    if (key == GLUT_KEY_UP) {
+        time_elasping_per_second += 86400;
+
+        if (time_elasping_per_second > 86400 * 300) {
+            time_elasping_per_second = 86400 * 300;
+        }
+    }
+
+    if (key == GLUT_KEY_DOWN) {
+        time_elasping_per_second -= 86400;
+
+        if (time_elasping_per_second < 0) {
+            time_elasping_per_second = 0;
+        }
+    }
 }
 
 void handle_mouse_input(int button, int state, int UNUSED(x), int UNUSED(y)) {
-- 
GitLab