Mercurial > public > algo-animator
annotate main.c @ 7:f159eec07daf
refactor
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Fri, 23 Jun 2023 10:01:37 +0100 |
parents | 40a8bdbe2005 |
children | f7af7255705e |
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" |
7 | 2 #include <GL/glut.h> |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
3 #include <stdio.h> |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
4 #include <stdlib.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 | |
13 struct Rectangle { | |
14 int width; | |
15 int height; | |
16 int x; | |
17 }; | |
18 | |
19 // Globals | |
20 struct Rectangle* rectangles; | |
21 int n_rectangles; | |
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); |
0 | 39 } |
40 | |
41 | |
42 void display() { | |
43 glClear(GL_COLOR_BUFFER_BIT); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
44 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
45 |
7 | 46 for (int i = 0; i < n_rectangles; i++) { |
47 glVertex2f(rectangles[i].x, WINDOW_HEIGHT - 100); | |
48 glVertex2f(rectangles[i].x + rectangles[i].width, WINDOW_HEIGHT - 100); | |
49 glVertex2f(rectangles[i].x + rectangles[i].width, rectangles[i].height); | |
50 glVertex2d(rectangles[i].x, rectangles[i].height); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
51 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
52 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
53 glEnd(); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
54 |
7 | 55 // Render text |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
56 char text[256]; |
7 | 57 sprintf(text, "Number of elements: %i", n_rectangles); |
58 glRasterPos2f(20.0, WINDOW_HEIGHT - 50); | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
59 |
7 | 60 for (const char *c = text; *c; ++c) { |
61 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); | |
62 } | |
63 | |
0 | 64 glFlush(); |
65 } | |
66 | |
67 | |
68 int main(int argc, char** argv) { | |
7 | 69 n_rectangles = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; |
70 rectangles = malloc(n_rectangles * sizeof(struct Rectangle)); | |
71 | |
72 int x_pos = 1; | |
73 | |
74 int i = 0; | |
75 while (i < n_rectangles) { | |
76 rectangles[i].width = RECT_WIDTH; | |
77 rectangles[i].height = random_int(100, WINDOW_HEIGHT - 100); | |
78 rectangles[i].x = x_pos; | |
79 | |
80 x_pos += RECT_WIDTH + SPACE; | |
81 i++; | |
82 } | |
83 | |
0 | 84 glutInit(&argc, argv); |
85 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
86 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
0 | 87 glutCreateWindow("OpenGL Window"); |
88 setup(); | |
89 glutDisplayFunc(display); | |
90 glutMainLoop(); | |
91 | |
7 | 92 free(rectangles); |
93 | |
0 | 94 return 0; |
95 } | |
7 | 96 |
97 |