comparison 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
comparison
equal deleted inserted replaced
5:6be2faa7ed6e 6:40a8bdbe2005
3 #include <stdlib.h> 3 #include <stdlib.h>
4 #include <math.h> 4 #include <math.h>
5 #include <time.h> 5 #include <time.h>
6 6
7 7
8 #define HEIGHT 1080 8 #define WINDOW_HEIGHT 1080
9 #define WIDTH 1920 9 #define WINDOW_WIDTH 1920
10 10
11 11
12 void setup() { 12 void setup() {
13 13
14 // Set background dark 14 // Set background dark
21 // Matrix projection and reset with identity 21 // Matrix projection and reset with identity
22 glMatrixMode(GL_PROJECTION); 22 glMatrixMode(GL_PROJECTION);
23 glLoadIdentity(); 23 glLoadIdentity();
24 24
25 // Set the coordinates to be used with the viewport 25 // Set the coordinates to be used with the viewport
26 gluOrtho2D(0, WIDTH, HEIGHT, 0); 26 gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
27 } 27 }
28
29
30 struct Rectangle {
31 int width;
32 int height;
33 int x_position;
34 };
28 35
29 36
30 void display() { 37 void display() {
31 glClear(GL_COLOR_BUFFER_BIT); 38 glClear(GL_COLOR_BUFFER_BIT);
32 glBegin(GL_QUADS); 39 glBegin(GL_QUADS);
33 40
34 float x = 1; 41 float x_position = 1;
35 float rect_width = 5.0; 42 float rect_width = 5.0;
36 float space = 5.0; 43 float space = 5.0;
37 44
38 // Compute max number of rectangles to fit the windows 45 // Compute max number of rectangles to fit the windows
39 int max_rects = floor((WIDTH - rect_width) / (rect_width + space)) + x; 46 int max_rects = floor((WINDOW_WIDTH - rect_width) / (rect_width + space)) + x_position;
40
41 // Initialize empty array with same of `max_rects`
42 int *unsorted_array = (int*)malloc(max_rects * sizeof(int));
43 47
48 struct Rectangle *rectangles = malloc(max_rects * sizeof(struct Rectangle));
44 int rect_counter = 0; 49 int rect_counter = 0;
50
45 while (rect_counter < max_rects) { 51 while (rect_counter < max_rects) {
46 int height = random_int(100, HEIGHT - 100); 52 struct Rectangle rectangle;
53 rectangle.width = rect_width;
54 rectangle.height = random_int(100, WINDOW_HEIGHT - 100);
55 rectangle.x_position = x_position;
56 rectangles[rect_counter] = rectangle;
47 57
48 glVertex2f(x, HEIGHT - 100); 58 draw_rectangle(
49 glVertex2f(x + rect_width, HEIGHT - 100); 59 rectangle.x_position,
50 glVertex2f(x + rect_width, height); 60 rectangle.height,
51 glVertex2d(x, height); 61 rectangle.width,
62 WINDOW_HEIGHT
63 );
52 64
53 x += rect_width + space; 65 x_position += rect_width + space;
54 rect_counter++; 66 rect_counter++;
55 } 67 }
56 68
57 glEnd(); 69 glEnd();
58 70
59 char text[256]; 71 char text[256];
60 sprintf(text, "Number of elements: %i", rect_counter); 72 sprintf(text, "Number of elements: %i", rect_counter);
61 render_text(text, 20.0, HEIGHT - 50); 73 render_text(text, 20.0, WINDOW_HEIGHT - 50);
74
75 free(rectangles);
62 glFlush(); 76 glFlush();
63 } 77 }
64 78
65 79
66 int main(int argc, char** argv) { 80 int main(int argc, char** argv) {
67 glutInit(&argc, argv); 81 glutInit(&argc, argv);
68 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 82 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
69 glutInitWindowSize(WIDTH, HEIGHT); 83 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
70 glutCreateWindow("OpenGL Window"); 84 glutCreateWindow("OpenGL Window");
71 setup(); 85 setup();
72 glutDisplayFunc(display); 86 glutDisplayFunc(display);
73 glutMainLoop(); 87 glutMainLoop();
74 88