Mercurial > public > algo-animator
annotate main.c @ 9:b7da0083b706
removing struct?
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Fri, 23 Jun 2023 18:26:31 +0100 |
parents | f7af7255705e |
children | f4e0c266321a |
rev | line source |
---|---|
8 | 1 #include <stdio.h> |
9 | 2 #include <stdlib.h> |
7 | 3 #include <GL/glut.h> |
9 | 4 #include <math.h> |
0 | 5 |
6 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
7 #define WINDOW_HEIGHT 1080 |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
8 #define WINDOW_WIDTH 1920 |
7 | 9 #define RECT_WIDTH 5 |
10 #define SPACE 5 | |
11 | |
12 | |
9 | 13 // Globals |
14 char* algos[] = {"Bubble sort", "Selection sort", "Insertion sort", "Quick sort"}; | |
15 | |
16 int selected_algo = 0; | |
17 int n_algos = sizeof(algos) / sizeof(algos[0]); | |
18 int n_rectangles; | |
19 | |
7 | 20 struct Rectangle { |
21 int width; | |
22 int height; | |
23 int x; | |
24 }; | |
25 | |
26 struct Rectangle* rectangles; | |
0 | 27 |
28 | |
9 | 29 // Algos |
30 void bubble_sort() { | |
31 for (int i = 0; i < n_rectangles; i++) { | |
32 | |
33 struct Rectangle current = rectangles[i]; | |
34 struct Rectangle next = rectangles[i + 1]; | |
35 | |
36 printf("Current pos: %i\n", current.x); | |
37 printf("Next pos: %i\n", next.x); | |
38 | |
39 // Place largest element at the end of the array | |
40 if (current.height > next.height) { | |
41 rectangles[i + 1].x = current.x; | |
42 rectangles[i].x = next.x; | |
43 } | |
44 } | |
45 } | |
46 | |
47 | |
48 // Utils | |
49 int random_int(int min, int max) { | |
50 return rand() % ((max - min) + 1) + min; | |
51 } | |
52 | |
53 | |
54 void algo_selector(int direction) { | |
55 int selection = selected_algo + direction; | |
56 int lower = 0; | |
57 int upper = (sizeof(algos) / sizeof(algos[0])) - 1; | |
58 | |
59 if (selection >= lower && selection <= upper) { | |
60 selected_algo = selection; | |
61 } | |
62 } | |
63 | |
64 // GL and GLUT | |
0 | 65 void setup() { |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
66 |
0 | 67 // Set background dark |
68 glClearColor(0.0, 0.0, 0.0, 1.0); | |
69 | |
70 // Set point color and size to 1 pixel | |
71 glColor3f(0.0, 1.0, 0.0); | |
72 glPointSize(5.0); | |
73 | |
74 // Matrix projection and reset with identity | |
75 glMatrixMode(GL_PROJECTION); | |
76 glLoadIdentity(); | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
77 |
0 | 78 // Set the coordinates to be used with the viewport |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
79 gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0); |
8 | 80 |
81 } | |
82 | |
83 | |
84 void render_text(int x, int y, char* text) { | |
85 glRasterPos2f(x, y); | |
86 | |
87 for (const char *c = text; *c; ++c) { | |
88 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); | |
89 } | |
0 | 90 } |
91 | |
92 | |
93 void display() { | |
94 glClear(GL_COLOR_BUFFER_BIT); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
95 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
96 |
7 | 97 for (int i = 0; i < n_rectangles; i++) { |
9 | 98 glVertex2f(rectangles[i].x, WINDOW_HEIGHT - 200); |
99 glVertex2f(rectangles[i].x + rectangles[i].width, WINDOW_HEIGHT - 200); | |
7 | 100 glVertex2f(rectangles[i].x + rectangles[i].width, rectangles[i].height); |
101 glVertex2d(rectangles[i].x, rectangles[i].height); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
102 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
103 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
104 glEnd(); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
105 |
7 | 106 // Render text |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
107 char text[256]; |
9 | 108 |
109 sprintf(text, "Algorithm: %s", algos[selected_algo]); | |
110 render_text(20.0, WINDOW_HEIGHT - 130, text); | |
8 | 111 |
9 | 112 sprintf(text, "Number of elements: %i", n_rectangles); |
113 render_text(20.0, WINDOW_HEIGHT - 100, text); | |
114 | |
115 render_text(WINDOW_WIDTH - 500, WINDOW_HEIGHT - 130, "Press 'a' or 's' to select an algorithm"); | |
116 render_text(WINDOW_WIDTH - 500, WINDOW_HEIGHT - 100, "Press 'enter' to run the algorithm"); | |
8 | 117 |
118 glutSwapBuffers(); | |
119 glFlush(); | |
120 } | |
121 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
122 |
8 | 123 void idle() { |
124 glutPostRedisplay(); | |
125 } | |
126 | |
127 | |
128 void keyboard(unsigned char key, int x, int y) { | |
129 | |
9 | 130 // S |
131 if (key == 115) { | |
132 algo_selector(1); | |
133 } | |
134 | |
135 // A | |
136 if (key == 97) { | |
137 algo_selector(-1); | |
138 } | |
139 | |
140 // Enter | |
8 | 141 if (key == 13) { |
9 | 142 bubble_sort(); |
7 | 143 } |
0 | 144 } |
145 | |
146 | |
147 int main(int argc, char** argv) { | |
7 | 148 n_rectangles = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; |
149 rectangles = malloc(n_rectangles * sizeof(struct Rectangle)); | |
150 | |
151 int x_pos = 1; | |
152 | |
153 int i = 0; | |
154 while (i < n_rectangles) { | |
155 rectangles[i].width = RECT_WIDTH; | |
9 | 156 rectangles[i].height = random_int(100, WINDOW_HEIGHT - 200); |
7 | 157 rectangles[i].x = x_pos; |
158 | |
159 x_pos += RECT_WIDTH + SPACE; | |
160 i++; | |
161 } | |
162 | |
0 | 163 glutInit(&argc, argv); |
8 | 164 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
165 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
0 | 166 glutCreateWindow("OpenGL Window"); |
167 setup(); | |
168 glutDisplayFunc(display); | |
8 | 169 glutKeyboardFunc(keyboard); |
170 glutIdleFunc(idle); | |
0 | 171 glutMainLoop(); |
172 | |
7 | 173 free(rectangles); |
174 | |
0 | 175 return 0; |
176 } | |
7 | 177 |
178 |