# HG changeset patch # User Dennis C. M. # Date 1687510897 -3600 # Node ID f159eec07daf18e1198b86cc246fcfffe58ca103 # Parent 40a8bdbe20052dac0316a8d62bf49845aa705405 refactor diff -r 40a8bdbe2005 -r f159eec07daf algos.h --- a/algos.h Tue Jun 13 18:37:23 2023 +0100 +++ b/algos.h Fri Jun 23 10:01:37 2023 +0100 @@ -0,0 +1,5 @@ +#ifndef ALGOS_H +#define ALGOS_H + + +#endif diff -r 40a8bdbe2005 -r f159eec07daf main.c --- a/main.c Tue Jun 13 18:37:23 2023 +0100 +++ b/main.c Fri Jun 23 10:01:37 2023 +0100 @@ -1,12 +1,24 @@ #include "utils.h" +#include #include #include -#include -#include #define WINDOW_HEIGHT 1080 #define WINDOW_WIDTH 1920 +#define RECT_WIDTH 5 +#define SPACE 5 + + +struct Rectangle { + int width; + int height; + int x; +}; + +// Globals +struct Rectangle* rectangles; +int n_rectangles; void setup() { @@ -27,57 +39,48 @@ } -struct Rectangle { - int width; - int height; - int x_position; -}; - - void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); - float x_position = 1; - float rect_width = 5.0; - float space = 5.0; - - // Compute max number of rectangles to fit the windows - int max_rects = floor((WINDOW_WIDTH - rect_width) / (rect_width + space)) + x_position; - - struct Rectangle *rectangles = malloc(max_rects * sizeof(struct Rectangle)); - int rect_counter = 0; - - while (rect_counter < max_rects) { - struct Rectangle rectangle; - rectangle.width = rect_width; - rectangle.height = random_int(100, WINDOW_HEIGHT - 100); - rectangle.x_position = x_position; - rectangles[rect_counter] = rectangle; - - draw_rectangle( - rectangle.x_position, - rectangle.height, - rectangle.width, - WINDOW_HEIGHT - ); - - x_position += rect_width + space; - rect_counter++; + for (int i = 0; i < n_rectangles; i++) { + glVertex2f(rectangles[i].x, WINDOW_HEIGHT - 100); + glVertex2f(rectangles[i].x + rectangles[i].width, WINDOW_HEIGHT - 100); + glVertex2f(rectangles[i].x + rectangles[i].width, rectangles[i].height); + glVertex2d(rectangles[i].x, rectangles[i].height); } glEnd(); + // Render text char text[256]; - sprintf(text, "Number of elements: %i", rect_counter); - render_text(text, 20.0, WINDOW_HEIGHT - 50); + sprintf(text, "Number of elements: %i", n_rectangles); + glRasterPos2f(20.0, WINDOW_HEIGHT - 50); - free(rectangles); + for (const char *c = text; *c; ++c) { + glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); + } + glFlush(); } int main(int argc, char** argv) { + n_rectangles = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; + rectangles = malloc(n_rectangles * sizeof(struct Rectangle)); + + int x_pos = 1; + + int i = 0; + while (i < n_rectangles) { + rectangles[i].width = RECT_WIDTH; + rectangles[i].height = random_int(100, WINDOW_HEIGHT - 100); + rectangles[i].x = x_pos; + + x_pos += RECT_WIDTH + SPACE; + i++; + } + glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); @@ -86,5 +89,9 @@ glutDisplayFunc(display); glutMainLoop(); + free(rectangles); + return 0; } + + diff -r 40a8bdbe2005 -r f159eec07daf utils.c --- a/utils.c Tue Jun 13 18:37:23 2023 +0100 +++ b/utils.c Fri Jun 23 10:01:37 2023 +0100 @@ -1,23 +1,6 @@ #include "utils.h" -void render_text(const char *text, float x, float y) { - glRasterPos2f(x, y); - - for (const char *c = text; *c; ++c) { - glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); - } -} - - int random_int(int min, int max) { return rand() % ((max - min) + 1) + min; } - - -void draw_rectangle(int x_position, int rect_height, int rect_width, int window_height) { - glVertex2f(x_position, window_height - 100); - glVertex2f(x_position + rect_width, window_height - 100); - glVertex2f(x_position + rect_width, rect_height); - glVertex2d(x_position, rect_height); -} diff -r 40a8bdbe2005 -r f159eec07daf utils.h --- a/utils.h Tue Jun 13 18:37:23 2023 +0100 +++ b/utils.h Fri Jun 23 10:01:37 2023 +0100 @@ -2,11 +2,11 @@ #define UTILS_H -#include +#include +#include -void render_text(const char *text, float x, float y); + int random_int(int min, int max); -void draw_rectangle(int x_position, int rect_height, int rect_width, int window_height); #endif