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