Mercurial > public > algo-animator
changeset 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 |
files | algos.c algos.h main.c utils.c utils.h |
diffstat | 3 files changed, 41 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/main.c Tue Jun 13 17:23:52 2023 +0100 +++ b/main.c Tue Jun 13 18:37:23 2023 +0100 @@ -5,8 +5,8 @@ #include <time.h> -#define HEIGHT 1080 -#define WIDTH 1920 +#define WINDOW_HEIGHT 1080 +#define WINDOW_WIDTH 1920 void setup() { @@ -23,42 +23,56 @@ glLoadIdentity(); // Set the coordinates to be used with the viewport - gluOrtho2D(0, WIDTH, HEIGHT, 0); + gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0); } +struct Rectangle { + int width; + int height; + int x_position; +}; + + void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); - float x = 1; + 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((WIDTH - rect_width) / (rect_width + space)) + x; - - // Initialize empty array with same of `max_rects` - int *unsorted_array = (int*)malloc(max_rects * sizeof(int)); + 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) { - int height = random_int(100, HEIGHT - 100); + struct Rectangle rectangle; + rectangle.width = rect_width; + rectangle.height = random_int(100, WINDOW_HEIGHT - 100); + rectangle.x_position = x_position; + rectangles[rect_counter] = rectangle; - glVertex2f(x, HEIGHT - 100); - glVertex2f(x + rect_width, HEIGHT - 100); - glVertex2f(x + rect_width, height); - glVertex2d(x, height); + draw_rectangle( + rectangle.x_position, + rectangle.height, + rectangle.width, + WINDOW_HEIGHT + ); - x += rect_width + space; + x_position += rect_width + space; rect_counter++; } glEnd(); - + char text[256]; sprintf(text, "Number of elements: %i", rect_counter); - render_text(text, 20.0, HEIGHT - 50); + render_text(text, 20.0, WINDOW_HEIGHT - 50); + + free(rectangles); glFlush(); } @@ -66,7 +80,7 @@ int main(int argc, char** argv) { glutInit(&argc, argv); glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); - glutInitWindowSize(WIDTH, HEIGHT); + glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); glutCreateWindow("OpenGL Window"); setup(); glutDisplayFunc(display);
--- a/utils.c Tue Jun 13 17:23:52 2023 +0100 +++ b/utils.c Tue Jun 13 18:37:23 2023 +0100 @@ -13,3 +13,11 @@ 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); +}