From 2149b27af97a2158dea0560532e06297289720a4 Mon Sep 17 00:00:00 2001
From: Florian Burgener <florian.brgnr@gmail.com>
Date: Sat, 18 Dec 2021 17:33:01 +0100
Subject: [PATCH] Anti-aliasing

---
 .gitignore        |  1 +
 Makefile          | 13 ++++++++++---
 PlanetarySystem.c |  6 +++---
 PlanetarySystem.h |  4 ++--
 Vector2.c         |  4 ++--
 main.c            |  7 ++++---
 6 files changed, 22 insertions(+), 13 deletions(-)

diff --git a/.gitignore b/.gitignore
index 95916b8..08922dc 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1,3 +1,4 @@
 *.o
 main
+main.exe
 .vscode
diff --git a/Makefile b/Makefile
index af52a68..25019ee 100644
--- a/Makefile
+++ b/Makefile
@@ -1,9 +1,16 @@
 TARGET = main
-CC:=gcc
+# CCFLAGS
+CC = gcc
 # CFLAGS:=-g -Ofast -Wall -Wextra -fsanitize=address -fsanitize=leak -std=gnu11
 # CFLAGS:=-fsanitize=address
-CFLAGS:=-g -O3 -Wall -Wextra -std=gnu11
-LDFLAGS:=-lm -lGL -lGLU -lglut
+CFLAGS = -g -O3 -Wall -Wextra -std=gnu11
+LDFLAGS = -lm
+
+ifeq ($(OS),Windows_NT)
+	LDFLAGS += -lopengl32 -lglu32 -lfreeglut
+else
+	LDFLAGS += -lGL -lGLU -lglut
+endif
 
 %.o: %.c $(HEADERS)
 	$(CC) $(CFLAGS) -c $< -o $@
diff --git a/PlanetarySystem.c b/PlanetarySystem.c
index 72efa14..15f48cd 100644
--- a/PlanetarySystem.c
+++ b/PlanetarySystem.c
@@ -10,8 +10,8 @@
 #include "Vector2.h"
 #include "drawing.h"
 
-const uint32_t SCREEN_WIDTH = 1000;
-const uint32_t SCREEN_HEIGHT = 1000;
+const uint32_t SCREEN_WIDTH = 800;
+const uint32_t SCREEN_HEIGHT = 800;
 
 const double G = 6.67430 * 1E-11;
 
@@ -112,7 +112,7 @@ void planetary_system_update(PlanetarySystem *planetary_system, double interval)
 
         int32_t length = object->points_length;
 
-        if (length > 0 && vector2_norm(vector2_substract(object->points[0], object->current_position)) < 1E9 * 1.5)
+        if (length > 0 && vector2_norm(vector2_substract(object->points[0], object->current_position)) < 1E9)
             continue;
         for (int32_t j = (length == 200) ? length - 1 : length; j >= 1; j -= 1) {
             object->points[j] = object->points[j - 1];
diff --git a/PlanetarySystem.h b/PlanetarySystem.h
index 4a5effd..2901953 100644
--- a/PlanetarySystem.h
+++ b/PlanetarySystem.h
@@ -5,8 +5,8 @@
 
 #include "CelestialObject.h"
 
-const uint32_t SCREEN_WIDTH;
-const uint32_t SCREEN_HEIGHT;
+extern const uint32_t SCREEN_WIDTH;
+extern const uint32_t SCREEN_HEIGHT;
 
 typedef struct PlanetarySystem {
     int32_t objects_length;
diff --git a/Vector2.c b/Vector2.c
index 0770586..459a285 100644
--- a/Vector2.c
+++ b/Vector2.c
@@ -51,8 +51,8 @@ bool vector2_is_similiar(Vector2 a, Vector2 b, double epsilon) {
 }
 
 Vector2 vector2_fit_canvas(Vector2 v, uint32_t width, uint32_t height) {
-    double x = round((width - 1) / 2.0 + v.x * (width - 1) / 2.0);
-    double y = round((height - 1) / 2.0 + v.y * (height - 1) / 2.0);
+    double x = width / 2.0 * (1 + v.x);
+    double y = height / 2.0 * (1 + v.y);
     return vector2_create(x, y);
 }
 
diff --git a/main.c b/main.c
index c5d2ffb..2847df6 100644
--- a/main.c
+++ b/main.c
@@ -52,7 +52,7 @@ void draw() {
 
     if (elapsed_time % REFRESH_RATE == 0 && elapsed_time != 0) {
         char title[100];
-        sprintf(title, "Solar System (%d : %ld)", elapsed_time / REFRESH_RATE, (time(NULL) - true_time_shift) % 1000);
+        sprintf(title, "Solar System (%d : %lld)", elapsed_time / REFRESH_RATE, (time(NULL) - true_time_shift) % 1000);
         glutSetWindowTitle(title);
     }
 
@@ -62,7 +62,7 @@ void draw() {
     sprintf(text, "Simulation Speed : %d days per second", time_elasping_per_second / 86400);
     draw_text(text, vector2_create(8, 32));
 
-    glFlush();
+    glutSwapBuffers();
 }
 
 void handle_keyboard_input(unsigned char key, int UNUSED(x), int UNUSED(y)) {
@@ -118,7 +118,8 @@ int main(int argc, char *argv[]) {
     planetary_system = planetary_system_create();
 
     glutInit(&argc, argv);
-    glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
+    glutSetOption(GLUT_MULTISAMPLE, 16);
+    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH | GLUT_MULTISAMPLE);
     glutInitWindowSize(SCREEN_WIDTH, SCREEN_HEIGHT);
     glutCreateWindow(WINDOW_NAME);
 
-- 
GitLab