Mercurial > public > algo-animator
annotate main.c @ 5:6be2faa7ed6e
rectangles with variable height
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Tue, 13 Jun 2023 17:23:52 +0100 |
parents | 035d3880da04 |
children | 40a8bdbe2005 |
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 | |
8 #define HEIGHT 1080 | |
9 #define WIDTH 1920 | |
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 |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
26 gluOrtho2D(0, WIDTH, HEIGHT, 0); |
0 | 27 } |
28 | |
29 | |
30 void display() { | |
31 glClear(GL_COLOR_BUFFER_BIT); | |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
32 glBegin(GL_QUADS); |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
33 |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
34 float x = 1; |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
35 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
|
36 float space = 5.0; |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
37 |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
38 // Compute max number of rectangles to fit the windows |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
39 int max_rects = floor((WIDTH - rect_width) / (rect_width + space)) + x; |
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
40 |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
41 // Initialize empty array with same of `max_rects` |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
42 int *unsorted_array = (int*)malloc(max_rects * sizeof(int)); |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
43 |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
44 int rect_counter = 0; |
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
45 while (rect_counter < max_rects) { |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
46 int height = random_int(100, HEIGHT - 100); |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
47 |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
48 glVertex2f(x, HEIGHT - 100); |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
49 glVertex2f(x + rect_width, HEIGHT - 100); |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
50 glVertex2f(x + rect_width, height); |
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
51 glVertex2d(x, height); |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
52 |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
53 x += rect_width + space; |
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
54 rect_counter++; |
3
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
55 } |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
56 |
e4003f606e07
draw multiple rectangles with loop
Dennis C. M. <dennis@denniscm.com>
parents:
2
diff
changeset
|
57 glEnd(); |
4
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
58 |
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
59 char text[256]; |
035d3880da04
render text with number of elements on screen
Dennis C. M. <dennis@denniscm.com>
parents:
3
diff
changeset
|
60 sprintf(text, "Number of elements: %i", rect_counter); |
5
6be2faa7ed6e
rectangles with variable height
Dennis C. M. <dennis@denniscm.com>
parents:
4
diff
changeset
|
61 render_text(text, 20.0, HEIGHT - 50); |
0 | 62 glFlush(); |
63 } | |
64 | |
65 | |
66 int main(int argc, char** argv) { | |
67 glutInit(&argc, argv); | |
68 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); | |
69 glutInitWindowSize(WIDTH, HEIGHT); | |
70 glutCreateWindow("OpenGL Window"); | |
71 setup(); | |
72 glutDisplayFunc(display); | |
73 glutMainLoop(); | |
74 | |
75 return 0; | |
76 } |