Mercurial > public > algo-animator
annotate main.c @ 11:aaecc9b7ca9c
testing glutTimerFunc
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Fri, 23 Jun 2023 23:08:34 +0100 |
parents | f4e0c266321a |
children | 9ba546527bc2 |
rev | line source |
---|---|
8 | 1 #include <stdio.h> |
9 | 2 #include <stdlib.h> |
7 | 3 #include <GL/glut.h> |
9 | 4 #include <math.h> |
10 | 5 #include <time.h> |
0 | 6 |
7 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
8 #define WINDOW_HEIGHT 1080 |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
9 #define WINDOW_WIDTH 1920 |
11 | 10 #define FPS 60 |
10 | 11 #define OFFSET 150 |
7 | 12 #define RECT_WIDTH 5 |
10 | 13 #define SPACE 1 |
7 | 14 |
15 | |
9 | 16 // Globals |
17 char* algos[] = {"Bubble sort", "Selection sort", "Insertion sort", "Quick sort"}; | |
18 int selected_algo = 0; | |
19 int n_algos = sizeof(algos) / sizeof(algos[0]); | |
20 | |
10 | 21 int* arr; |
22 int arr_size; | |
0 | 23 |
24 | |
9 | 25 // Algos |
11 | 26 int step = 0; |
27 int i = 0; | |
9 | 28 void bubble_sort() { |
11 | 29 if (i < arr_size - step - 1) { |
30 int current = arr[i]; | |
31 int next = arr[i + 1]; | |
9 | 32 |
11 | 33 if (current > next) { |
34 arr[i + 1] = current; | |
35 arr[i] = next; | |
36 } | |
9 | 37 |
11 | 38 i++; |
39 } else { | |
40 step++; | |
41 i = 0; | |
9 | 42 } |
43 } | |
44 | |
45 | |
10 | 46 void create_array() { |
47 arr_size = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; | |
48 arr = (int*)malloc(arr_size * sizeof(int)); | |
49 | |
50 // srand(time(NULL)); | |
51 | |
52 int min = OFFSET; | |
53 int max = WINDOW_HEIGHT - OFFSET; | |
54 | |
55 for (int i = 0; i < arr_size; i++) { | |
56 arr[i] = rand() % ((max - min) + 1) + min; | |
57 } | |
58 } | |
59 | |
60 | |
61 void print_array() { | |
62 for (int i = 0; i < arr_size; i++) { | |
63 printf("%d ", arr[i]); | |
64 } | |
9 | 65 } |
66 | |
67 | |
68 void algo_selector(int direction) { | |
69 int selection = selected_algo + direction; | |
70 int lower = 0; | |
71 int upper = (sizeof(algos) / sizeof(algos[0])) - 1; | |
72 | |
73 if (selection >= lower && selection <= upper) { | |
74 selected_algo = selection; | |
75 } | |
76 } | |
77 | |
78 // GL and GLUT | |
0 | 79 void setup() { |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
80 |
0 | 81 // Set background dark |
82 glClearColor(0.0, 0.0, 0.0, 1.0); | |
83 | |
84 // Set point color and size to 1 pixel | |
85 glColor3f(0.0, 1.0, 0.0); | |
86 glPointSize(5.0); | |
87 | |
88 // Matrix projection and reset with identity | |
89 glMatrixMode(GL_PROJECTION); | |
90 glLoadIdentity(); | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
91 |
10 | 92 /* |
93 * Creates projection matrix | |
94 * x increases from left to right (0 to WINDOW_WIDTH) | |
95 * y increases from bottom to top (0 to WINDOW_HEIGHT) | |
96 */ | |
97 gluOrtho2D(0, WINDOW_WIDTH, 0, WINDOW_HEIGHT); | |
8 | 98 |
99 } | |
100 | |
101 | |
102 void render_text(int x, int y, char* text) { | |
103 glRasterPos2f(x, y); | |
104 | |
105 for (const char *c = text; *c; ++c) { | |
106 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); | |
107 } | |
0 | 108 } |
109 | |
110 | |
111 void display() { | |
112 glClear(GL_COLOR_BUFFER_BIT); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
113 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
114 |
10 | 115 int x = 0; |
116 for (int i = 0; i < arr_size; i++) { | |
117 | |
118 // Bottom left | |
119 glVertex2f(x, OFFSET); | |
120 | |
121 // Top left | |
122 glVertex2f(x, arr[i]); | |
123 | |
124 // Top right | |
125 glVertex2f(x + RECT_WIDTH, arr[i]); | |
126 | |
127 // Bottom right | |
128 glVertex2f(x + RECT_WIDTH, OFFSET); | |
129 | |
130 x += RECT_WIDTH + SPACE; | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
131 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
132 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
133 glEnd(); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
134 |
7 | 135 // Render text |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
136 char text[256]; |
9 | 137 |
138 sprintf(text, "Algorithm: %s", algos[selected_algo]); | |
10 | 139 render_text(20, OFFSET - 50, text); |
8 | 140 |
10 | 141 sprintf(text, "Number of elements: %i", arr_size); |
142 render_text(20, OFFSET - 75, text); | |
9 | 143 |
10 | 144 render_text(WINDOW_WIDTH - 500, OFFSET - 50, "Press 'a' or 's' to select an algorithm"); |
145 render_text(WINDOW_WIDTH - 500, OFFSET - 75, "Press 'enter' to run the algorithm"); | |
146 render_text(WINDOW_WIDTH - 500, OFFSET - 100, "Press 'r' to reset array"); | |
8 | 147 |
148 glutSwapBuffers(); | |
149 } | |
150 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
151 |
11 | 152 void timer(int value) { |
153 bubble_sort(); | |
8 | 154 glutPostRedisplay(); |
11 | 155 glutTimerFunc(1000 / 800, timer, 0); |
8 | 156 } |
157 | |
158 | |
159 void keyboard(unsigned char key, int x, int y) { | |
160 | |
10 | 161 // s |
9 | 162 if (key == 115) { |
163 algo_selector(1); | |
164 } | |
165 | |
10 | 166 // r |
167 if (key == 114) { | |
168 create_array(); | |
169 } | |
170 | |
171 // a | |
9 | 172 if (key == 97) { |
173 algo_selector(-1); | |
174 } | |
175 | |
10 | 176 // enter |
8 | 177 if (key == 13) { |
10 | 178 printf("Before sorting: "); |
179 print_array(); | |
180 printf("\n\n"); | |
181 | |
9 | 182 bubble_sort(); |
10 | 183 |
184 printf("After sorting: "); | |
185 print_array(); | |
7 | 186 } |
0 | 187 } |
188 | |
189 | |
190 int main(int argc, char** argv) { | |
10 | 191 create_array(); |
7 | 192 |
0 | 193 glutInit(&argc, argv); |
8 | 194 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
195 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
0 | 196 glutCreateWindow("OpenGL Window"); |
197 setup(); | |
198 glutDisplayFunc(display); | |
8 | 199 glutKeyboardFunc(keyboard); |
11 | 200 glutTimerFunc(0, timer, 0); |
0 | 201 glutMainLoop(); |
202 | |
10 | 203 free(arr); |
7 | 204 |
0 | 205 return 0; |
206 } | |
7 | 207 |
208 |