Mercurial > public > algo-animator
changeset 4:035d3880da04
render text with number of elements on screen
author | Dennis C. M. <dennis@denniscm.com> |
---|---|
date | Mon, 12 Jun 2023 23:10:08 +0100 |
parents | e4003f606e07 |
children | 6be2faa7ed6e |
files | 34 README.md main.c utils.c utils.h |
diffstat | 5 files changed, 44 insertions(+), 74 deletions(-) [+] |
line wrap: on
line diff
--- a/34 Mon Jun 12 20:13:04 2023 +0100 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,55 +0,0 @@ -#include <stdio.h> -#include <GL/glut.h> - - -#define HEIGHT 1080 -#define WIDTH 1920 - - -void setup() { - - // Set background dark - glClearColor(0.0, 0.0, 0.0, 1.0); - - // Set point color and size to 1 pixel - glColor3f(0.0, 1.0, 0.0); - glPointSize(5.0); - - // Matrix projection - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - - - // Set the coordinates to be used with the viewport - gluOrtho2D(0, WIDTH, HEIGHT, 0); - - - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); -} - - -void display() { - glClear(GL_COLOR_BUFFER_BIT); - glBegin(GL_QUADS); - glVertex2f(100.0f, 100.0f); // Bottom-left vertex of first rectangle - glVertex2f(200.0f, 100.0f); // Bottom-right vertex of first rectangle - glVertex2f(200.0f, 200.0f); // Top-right vertex of first rectangle - glVertex2f(100.0f, 200.0f); // Top-left vertex of first rectangle - glEnd(); - glFlush(); -} - - -int main(int argc, char** argv) { - - glutInit(&argc, argv); - glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); - glutInitWindowSize(WIDTH, HEIGHT); - glutCreateWindow("OpenGL Window"); - setup(); - glutDisplayFunc(display); - glutMainLoop(); - - return 0; -}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/README.md Mon Jun 12 23:10:08 2023 +0100 @@ -0,0 +1,6 @@ +# algo-animator + +## Compile and run +```bash +gcc main.c utils.c -lglut -lGL -lGLU -lm && ./a.out +```
--- a/main.c Mon Jun 12 20:13:04 2023 +0100 +++ b/main.c Mon Jun 12 23:10:08 2023 +0100 @@ -1,5 +1,4 @@ -#include <stdio.h> -#include <GL/glut.h> +#include "utils.h" #define HEIGHT 1080 @@ -27,29 +26,28 @@ void display() { glClear(GL_COLOR_BUFFER_BIT); glBegin(GL_QUADS); - /* - glVertex2f(100.0, 100.0); // Top left - glVertex2f(150.0, 100.0); // Top right - glVertex2f(150.0, 300.0); // Bottom right - glVertex2f(100.0, 300.0); // Bottom left - glVertex2f(160.0, 100.0); - glVertex2f(210.0, 100.0); - glVertex2f(210.0, 300.0); - glVertex2d(160.0, 300.0); - */ - + float x = 1; float rect_width = 5.0; - float space = 10.0; + float space = 5.0; + int max_rects = floor((WIDTH - rect_width) / (rect_width + space)) + x; + + int rect_counter = 0; + while (rect_counter < max_rects) { + glVertex2f(x, 100.0); + glVertex2f(x + rect_width, 100.0); + glVertex2f(x + rect_width, HEIGHT - 300); + glVertex2d(x, HEIGHT - 300); - for (float pos_x = 100.0; pos_x < 500.0; pos_x += 10.0) { - glVertex2f(pos_x, 100.0); - glVertex2f(pos_x + rect_width, 100.0); - glVertex2f(pos_x + rect_width, 300.0); - glVertex2d(pos_x, 300.0); + x += rect_width + space; + rect_counter++; } glEnd(); + + char text[256]; + sprintf(text, "Number of elements: %i", rect_counter); + render_text(text, 20.0, HEIGHT - 200); glFlush(); }
--- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/utils.c Mon Jun 12 23:10:08 2023 +0100 @@ -0,0 +1,10 @@ +#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); + } +}