Mercurial > public > algo-animator
annotate main.c @ 8:f7af7255705e
text flickering fixed
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Fri, 23 Jun 2023 16:46:43 +0100 |
parents | f159eec07daf |
children | b7da0083b706 |
rev | line source |
---|---|
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
1 #include "utils.h" |
8 | 2 #include <stdio.h> |
7 | 3 #include <GL/glut.h> |
0 | 4 |
5 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
6 #define WINDOW_HEIGHT 1080 |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
7 #define WINDOW_WIDTH 1920 |
7 | 8 #define RECT_WIDTH 5 |
9 #define SPACE 5 | |
10 | |
11 | |
12 struct Rectangle { | |
13 int width; | |
14 int height; | |
15 int x; | |
16 }; | |
17 | |
18 // Globals | |
19 struct Rectangle* rectangles; | |
20 int n_rectangles; | |
8 | 21 int test_counter = 0; |
0 | 22 |
23 | |
24 void setup() { | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
25 |
0 | 26 // Set background dark |
27 glClearColor(0.0, 0.0, 0.0, 1.0); | |
28 | |
29 // Set point color and size to 1 pixel | |
30 glColor3f(0.0, 1.0, 0.0); | |
31 glPointSize(5.0); | |
32 | |
33 // Matrix projection and reset with identity | |
34 glMatrixMode(GL_PROJECTION); | |
35 glLoadIdentity(); | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
36 |
0 | 37 // 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
|
38 gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0); |
8 | 39 |
40 } | |
41 | |
42 | |
43 void render_text(int x, int y, char* text) { | |
44 glRasterPos2f(x, y); | |
45 | |
46 for (const char *c = text; *c; ++c) { | |
47 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); | |
48 } | |
0 | 49 } |
50 | |
51 | |
52 void display() { | |
53 glClear(GL_COLOR_BUFFER_BIT); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
54 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
55 |
7 | 56 for (int i = 0; i < n_rectangles; i++) { |
57 glVertex2f(rectangles[i].x, WINDOW_HEIGHT - 100); | |
58 glVertex2f(rectangles[i].x + rectangles[i].width, WINDOW_HEIGHT - 100); | |
59 glVertex2f(rectangles[i].x + rectangles[i].width, rectangles[i].height); | |
60 glVertex2d(rectangles[i].x, rectangles[i].height); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
61 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
62 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
63 glEnd(); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
64 |
7 | 65 // Render text |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
66 char text[256]; |
7 | 67 sprintf(text, "Number of elements: %i", n_rectangles); |
8 | 68 render_text(40.0, WINDOW_HEIGHT - 50, text); |
69 | |
70 sprintf(text, "Test counter: %i", test_counter); | |
71 render_text(540.0, WINDOW_HEIGHT - 50, text); | |
72 | |
73 glutSwapBuffers(); | |
74 glFlush(); | |
75 } | |
76 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
77 |
8 | 78 void idle() { |
79 glutPostRedisplay(); | |
80 } | |
81 | |
82 | |
83 void keyboard(unsigned char key, int x, int y) { | |
84 | |
85 // 13 is the ASCII value of ENTER key | |
86 if (key == 13) { | |
87 test_counter++; | |
7 | 88 } |
0 | 89 } |
90 | |
91 | |
92 int main(int argc, char** argv) { | |
7 | 93 n_rectangles = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; |
94 rectangles = malloc(n_rectangles * sizeof(struct Rectangle)); | |
95 | |
96 int x_pos = 1; | |
97 | |
98 int i = 0; | |
99 while (i < n_rectangles) { | |
100 rectangles[i].width = RECT_WIDTH; | |
101 rectangles[i].height = random_int(100, WINDOW_HEIGHT - 100); | |
102 rectangles[i].x = x_pos; | |
103 | |
104 x_pos += RECT_WIDTH + SPACE; | |
105 i++; | |
106 } | |
107 | |
0 | 108 glutInit(&argc, argv); |
8 | 109 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
110 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
0 | 111 glutCreateWindow("OpenGL Window"); |
112 setup(); | |
113 glutDisplayFunc(display); | |
8 | 114 glutKeyboardFunc(keyboard); |
115 glutIdleFunc(idle); | |
0 | 116 glutMainLoop(); |
117 | |
7 | 118 free(rectangles); |
119 | |
0 | 120 return 0; |
121 } | |
7 | 122 |
123 |