From 90cb8eb71f1d1d345156364bce568fc5cf3a5cca Mon Sep 17 00:00:00 2001
From: "tanguy.cavagna" <tanguy.cavagna@etu.hesge.ch>
Date: Wed, 13 Apr 2022 14:50:43 +0200
Subject: [PATCH] Revert opengl for master only

---
 charge/charge.c |  78 +----------------------------------
 charge/charge.h |  11 +----
 draw/draw.c     |  19 ---------
 draw/draw.h     |   5 +--
 main.c          | 107 +++++++++++++++++++++++-------------------------
 makefile        |   2 +-
 6 files changed, 56 insertions(+), 166 deletions(-)

diff --git a/charge/charge.c b/charge/charge.c
index 2345c40..caf9f82 100644
--- a/charge/charge.c
+++ b/charge/charge.c
@@ -85,29 +85,6 @@ bool field_line_segment(struct gfx_context_t *ctxt, charge_t *charges,
     return true;
 }
 
-bool field_line_segment_gl(charge_t *charges, int num_charges, double dx,
-                           double x0, double x1, double y0, double y1, int w,
-                           int h, vec2 old, vec2 p, bool side) {
-    while (pos_contrain_in_universe(p, x0, x1, y0, y1) &&
-           pos_not_too_close(p, charges, num_charges)) {
-        coordinates_t old_coord =
-            position_to_coordinates(w, h, x0, x1, y0, y1, old);
-        coordinates_t pi_coord =
-            position_to_coordinates(w, h, x0, x1, y0, y1, p);
-
-        glBegin(GL_LINES);
-        glColor3f(255, 255, 255);
-        glVertex2f(pi_coord.row, pi_coord.column);
-        glVertex2f(old_coord.row, old_coord.column);
-        glEnd();
-
-        old = p; // Save current point
-        compute_p_next(charges, num_charges, EPS, dx, &p, side);
-    }
-
-    return true;
-}
-
 void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges,
                      int num_charges, double dx, vec2 pos0, double x0,
                      double x1, double y0, double y1) {
@@ -123,21 +100,6 @@ void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges,
     }
 }
 
-void draw_field_line_gl(charge_t *charges, int num_charges, double dx,
-                        vec2 pos0, double x0, double x1, double y0, double y1,
-                        int w, int h) {
-    vec2 p = pos0;
-    vec2 old = p;
-
-    bool ended = false;
-
-    for (int side = 0; side <= 1; side++) {
-        old = pos0; // Reset to start from the same point for both sides
-        field_line_segment_gl(charges, num_charges, dx, x0, x1, y0, y1, w, h,
-                              old, p, (bool)side);
-    }
-}
-
 void draw_charges(struct gfx_context_t *ctxt, charge_t *charges,
                   int num_charges, double x0, double x1, double y0, double y1) {
     // Center coordinates
@@ -179,45 +141,7 @@ void draw_charges(struct gfx_context_t *ctxt, charge_t *charges,
     }
 }
 
-void draw_charges_GL(charge_t *charges, int num_charges, double x0, double x1,
-                     double y0, double y1, int w, int h) {
-    // Center coordinates
-    coordinates_t c;
-
-    // Charges coordinates
-    coordinates_t ch0;
-    coordinates_t ch1;
-
-    int chargePadding = CHARGE_RADIUS / 2;
-
-    // Draw charges
-    for (int i = 0; i < num_charges; i++) {
-        coordinates_t chCoord =
-            position_to_coordinates(w, h, x0, x1, y0, y1, charges[i].pos);
-
-        // Draw sign according to charge value
-        if (charges[i].q < 0) {
-            glBegin(GL_LINES);
-            glColor3f(0, 0, 255);
-            glVertex2f(chCoord.row - chargePadding, chCoord.column);
-            glVertex2f(chCoord.row + chargePadding, chCoord.column);
-            glEnd();
-        } else {
-            glBegin(GL_LINES);
-            glColor3f(255, 0, 0);
-            glVertex2f(chCoord.row, chCoord.column - chargePadding);
-            glVertex2f(chCoord.row, chCoord.column + chargePadding);
-            glVertex2f(chCoord.row - chargePadding, chCoord.column);
-            glVertex2f(chCoord.row + chargePadding, chCoord.column);
-            glEnd();
-        }
-
-        gl_draw_circle(chCoord, CHARGE_RADIUS, charges[i].q < 0 ? 0 : 255, 0,
-                       charges[i].q < 0 ? 255 : 0);
-    }
-}
-
-void generate_points(vec2 points[], int nb_points, int w, int h) {
+void generate_points(vec2 points[], int nb_points) {
     double x = 0;
     double y = 0;
 
diff --git a/charge/charge.h b/charge/charge.h
index a7b74b3..2d92da0 100644
--- a/charge/charge.h
+++ b/charge/charge.h
@@ -18,8 +18,6 @@
 #include "../gfx/gfx.h"
 #include "../utilities/utils.h"
 #include "../vec2/vec2.h"
-#include <GL/freeglut.h>
-#include <GL/gl.h>
 #include <stdbool.h>
 #include <stdlib.h>
 
@@ -100,10 +98,6 @@ void draw_field_line(struct gfx_context_t *ctxt, charge_t *charges,
                      int num_charges, double dx, vec2 pos0, double x0,
                      double x1, double y0, double y1);
 
-void draw_field_line_gl(charge_t *charges, int num_charges, double dx,
-                        vec2 pos0, double x0, double x1, double y0, double y1,
-                        int w, int h);
-
 /**
  * @brief Draw all charges
  *        A circle with a minus sign for negative charges
@@ -120,15 +114,12 @@ void draw_field_line_gl(charge_t *charges, int num_charges, double dx,
 void draw_charges(struct gfx_context_t *ctxt, charge_t *charges,
                   int num_charges, double x0, double x1, double y0, double y1);
 
-void draw_charges_GL(charge_t *charges, int num_charges, double x0, double x1,
-                     double y0, double y1, int w, int h);
-
 /**
  * @brief Generate a given amount of points
  *
  * @param points Points array
  * @param nb_points Number of point to generate
  */
-void generate_points(vec2 points[], int nb_points, int w, int h);
+void generate_points(vec2 points[], int nb_points);
 
 #endif // _CHARGE_H_
diff --git a/draw/draw.c b/draw/draw.c
index ef23e2e..7d8a86b 100644
--- a/draw/draw.c
+++ b/draw/draw.c
@@ -118,22 +118,3 @@ void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r,
         }
     }
 }
-<<<<<<< HEAD
-=======
-
-void gl_draw_circle(coordinates_t c, int rad, int r, int g, int b) {
-    int i;
-    int lineAmount = 100; //# of triangles used to draw circle
-
-    // GLfloat radius = 0.8f; //radius
-    GLfloat twicePi = 2.0f * 3.141592;
-
-    glBegin(GL_LINE_LOOP);
-    glColor3f(r, g, b);
-    for (i = 0; i <= lineAmount; i++) {
-        glVertex2f(c.row + (rad * cos(i * twicePi / lineAmount)),
-                   c.column + (rad * sin(i * twicePi / lineAmount)));
-    }
-    glEnd();
-}
->>>>>>> 438b2433ab31076f95d3a015859fa4f7594aa04f
diff --git a/draw/draw.h b/draw/draw.h
index 53eb733..53adfa4 100644
--- a/draw/draw.h
+++ b/draw/draw.h
@@ -13,11 +13,10 @@
 #define _DRAW_H_
 
 #include "../gfx/gfx.h"
-#include <GL/freeglut.h>
-#include <GL/gl.h>
 #include <stdint.h>
 #include <stdio.h>
 #include <stdlib.h>
+
 typedef struct {
     uint32_t row;
     uint32_t column;
@@ -54,6 +53,4 @@ void gfx_draw_line(struct gfx_context_t *ctxt, coordinates_t p0,
 void gfx_draw_circle(struct gfx_context_t *ctxt, coordinates_t c, int r,
                      uint32_t color);
 
-void gl_draw_circle(coordinates_t c, int radius, int r, int g, int b);
-
 #endif // _DRAW_H_
diff --git a/main.c b/main.c
index 0cf1d53..9e8ffc9 100644
--- a/main.c
+++ b/main.c
@@ -10,8 +10,6 @@
  */
 #include "charge/charge.h"
 #include "gfx/gfx.h"
-#include <GL/freeglut.h>
-#include <GL/glut.h>
 #include <math.h>
 #include <stdbool.h>
 #include <stdio.h>
@@ -24,84 +22,83 @@
 #define x1 1 // Maximal x of the universe
 #define y0 0 // Minimal y of the universe
 #define y1 1 // Maximal y of the universe
-#define NB_CHARGES 5
+#define NB_CHARGES 3
 #define NB_POINTS 200
 #define DELTA (1 / sqrt(pow(WINDOW_WIDTH, 2) + pow(WINDOW_HEIGHT, 2)))
 
-int charge_count = NB_CHARGES;
-vec2 pos[30];
-double chs[30];
-
-void display() { // Display function will draw the image.
-    glClearColor(0, 0, 0, 1);
-    glClear(GL_COLOR_BUFFER_BIT);
-
+void redraw_field(struct gfx_context_t *ctxt, int charge_count,
+                  vec2 pos[charge_count], double chs[charge_count]) {
     // Generate all the charges
     charge_t charges[charge_count];
     for (int i = 0; i < charge_count; i++)
         charges[i] = charge_create(chs[i], pos[i]);
 
-    draw_charges_GL(charges, charge_count, x0, x1, y0, y1, WINDOW_WIDTH,
-                    WINDOW_HEIGHT);
+    draw_charges(ctxt, charges, charge_count, x0, x1, y0, y1);
 
     // Generate all the points
     vec2 points[NB_POINTS];
-    generate_points(points, NB_POINTS, WINDOW_WIDTH, WINDOW_HEIGHT);
+    generate_points(points, NB_POINTS);
     for (int i = 0; i < NB_POINTS; i++) {
-        draw_field_line_gl(charges, charge_count, DELTA, points[i], x0, x1, y0,
-                           y1, WINDOW_WIDTH, WINDOW_HEIGHT);
-    }
-
-    glutSwapBuffers();
-}
-
-/**
- * @brief Event that retrieves actions performed with the mouse.
- *
- * @param button
- * @param state
- * @param x
- * @param y
- */
-void handle_mouse_input(int button, int state, int x, int y) {
-    if (state == GLUT_UP)
-        return;
-
-    if (button == GLUT_LEFT_BUTTON) {
-        charge_count += 1;
-        pos[charge_count - 1] =
-            vec2_create((double)y / WINDOW_WIDTH, (double)x / WINDOW_HEIGHT);
-        chs[charge_count - 1] = rand_one() / (rand_one() * 10 - 5);
+        coordinates_t c = position_to_coordinates(WINDOW_WIDTH, WINDOW_HEIGHT,
+                                                  x0, x1, y0, y1, points[i]);
+        draw_field_line(ctxt, charges, charge_count, DELTA, points[i], x0, x1,
+                        y0, y1);
     }
-
-    glutPostRedisplay();
 }
 
-int main(int argc, char **argv) {
+int main(void) {
     srand(time(NULL));
 
+    int charge_count = NB_CHARGES;
+    vec2 pos[30];
+    double chs[30];
+
     for (int i = 0; i < charge_count; i++) {
         chs[i] = rand_one() / (rand_one() * 10 - 5);
         pos[i] = vec2_create(rand_one(), rand_one());
     }
 
-    glutInit(&argc, argv);
-    glutSetOption(GLUT_MULTISAMPLE, 16);
-    glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGBA | GLUT_DEPTH |
-                        GLUT_MULTISAMPLE);
-    glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
-    int window = glutCreateWindow("GL RGB Triangle");
+    // GFX initialization
+    struct gfx_context_t *ctxt =
+        gfx_create("draw", WINDOW_WIDTH, WINDOW_HEIGHT);
+    if (!ctxt) {
+        fprintf(stderr, "Graphics initialization failed !\n");
+        return EXIT_FAILURE;
+    }
+
+    redraw_field(ctxt, charge_count, pos, chs);
+
+    bool running = true;
+    // GFX Draw loop
+    while (running) {
+        SDL_Event event = gfx_event();
 
-    glClearColor(0, 0, 0, 1);
-    glMatrixMode(GL_PROJECTION);
-    glLoadIdentity();
-    gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
+        switch (event.type) {
+        case SDL_KEYDOWN:
+            if (event.key.keysym.sym == SDLK_ESCAPE)
+                running = false;
+            break;
 
-    glutDisplayFunc(display);
-    glutMouseFunc(handle_mouse_input);
+        case SDL_MOUSEBUTTONDOWN:
+            if (event.button.button == SDL_BUTTON_LEFT) {
+                charge_count += 1;
+                gfx_clear(ctxt, 0x000000);
+                int m_x, m_y;
+                SDL_GetMouseState(&m_x, &m_y);
+
+                pos[charge_count - 1] = vec2_create(
+                    (double)m_x / WINDOW_WIDTH, (double)m_y / WINDOW_HEIGHT);
+                chs[charge_count - 1] = rand_one() / (rand_one() * 10 - 5);
+
+                redraw_field(ctxt, charge_count, pos, chs);
+            }
+            break;
+        }
+
+        gfx_present(ctxt);
+    }
 
-    glutMainLoop();
+    gfx_destroy(ctxt);
 
-    glutDestroyWindow(window);
     return EXIT_SUCCESS;
 }
diff --git a/makefile b/makefile
index 22e9f56..972e713 100644
--- a/makefile
+++ b/makefile
@@ -3,7 +3,7 @@
 CC = gcc
 CFLAGS = -Wall -Wextra -std=gnu11 -g -fsanitize=address -fsanitize=leak
 LDFLAGS = -lm
-LIBS = -lSDL2 -lGL -lGLU -lglut
+LIBS = -lSDL2
 VPATH:=vec2 gfx
 
 BIN = bin
-- 
GitLab