Mercurial > public > algo-animator
annotate main.c @ 6:40a8bdbe2005
Refactor to array of structures
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Tue, 13 Jun 2023 18:37:23 +0100 |
parents | 6be2faa7ed6e |
children | f159eec07daf |
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" |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
2 #include <stdio.h> |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
3 #include <stdlib.h> |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
4 #include <math.h> |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
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 |
0 | 10 |
11 | |
12 void setup() { | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
13 |
0 | 14 // Set background dark |
15 glClearColor(0.0, 0.0, 0.0, 1.0); | |
16 | |
17 // Set point color and size to 1 pixel | |
18 glColor3f(0.0, 1.0, 0.0); | |
19 glPointSize(5.0); | |
20 | |
21 // Matrix projection and reset with identity | |
22 glMatrixMode(GL_PROJECTION); | |
23 glLoadIdentity(); | |
2
ea3c427d922d
draw a weird filled rectangle
Dennis C. M. <dennis@denniscm.com>
parents:
0
diff
changeset
|
24 |
0 | 25 // 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
|
26 gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0); |
0 | 27 } |
28 | |
29 | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
30 struct Rectangle { |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
31 int width; |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
32 int height; |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
33 int x_position; |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
34 }; |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
35 |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
36 |
0 | 37 void display() { |
38 glClear(GL_COLOR_BUFFER_BIT); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
39 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
40 |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
41 float x_position = 1; |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
42 float rect_width = 5.0; |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
43 float space = 5.0; |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
44 |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
45 // Compute max number of rectangles to fit the windows |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
46 int max_rects = floor((WINDOW_WIDTH - rect_width) / (rect_width + space)) + x_position; |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
47 |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
48 struct Rectangle *rectangles = malloc(max_rects * sizeof(struct Rectangle)); |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
49 int rect_counter = 0; |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
50 |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
51 while (rect_counter < max_rects) { |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
52 struct Rectangle rectangle; |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
53 rectangle.width = rect_width; |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
54 rectangle.height = random_int(100, WINDOW_HEIGHT - 100); |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
55 rectangle.x_position = x_position; |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
56 rectangles[rect_counter] = rectangle; |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
57 |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
58 draw_rectangle( |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
59 rectangle.x_position, |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
60 rectangle.height, |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
61 rectangle.width, |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
62 WINDOW_HEIGHT |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
63 ); |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
64 |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
65 x_position += rect_width + space; |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
66 rect_counter++; |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
67 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
68 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
69 glEnd(); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
70 |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
71 char text[256]; |
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
72 sprintf(text, "Number of elements: %i", rect_counter); |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
73 render_text(text, 20.0, WINDOW_HEIGHT - 50); |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
74 |
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
75 free(rectangles); |
0 | 76 glFlush(); |
77 } | |
78 | |
79 | |
80 int main(int argc, char** argv) { | |
81 glutInit(&argc, argv); | |
82 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
6
40a8bdbe2005
Refactor to array of structures
Dennis C. M. <dennis@denniscm.com>
parents:
5
diff
changeset
|
83 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); |
0 | 84 glutCreateWindow("OpenGL Window"); |
85 setup(); | |
86 glutDisplayFunc(display); | |
87 glutMainLoop(); | |
88 | |
89 return 0; | |
90 } |