changeset 8:f7af7255705e

text flickering fixed
author Dennis C. M. <dennis@denniscm.com>
date Fri, 23 Jun 2023 16:46:43 +0100
parents f159eec07daf
children b7da0083b706
files main.c utils.c
diffstat 2 files changed, 35 insertions(+), 8 deletions(-) [+]
line wrap: on
line diff
--- a/main.c	Fri Jun 23 10:01:37 2023 +0100
+++ b/main.c	Fri Jun 23 16:46:43 2023 +0100
@@ -1,7 +1,6 @@
 #include "utils.h"
+#include <stdio.h>
 #include <GL/glut.h>
-#include <stdio.h>
-#include <stdlib.h>
 
 
 #define WINDOW_HEIGHT 1080
@@ -19,6 +18,7 @@
 // Globals
 struct Rectangle* rectangles;
 int n_rectangles;
+int test_counter = 0;
 
 
 void setup() {
@@ -36,6 +36,16 @@
 
 	// Set the coordinates to be used with the viewport
 	gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
+
+}
+
+
+void render_text(int x, int y, char* text) {
+	glRasterPos2f(x, y);
+
+	for (const char *c = text; *c; ++c) {
+		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
+	}
 }
 
 
@@ -55,13 +65,27 @@
 	// Render text
 	char text[256];
 	sprintf(text, "Number of elements: %i", n_rectangles);
-	glRasterPos2f(20.0, WINDOW_HEIGHT - 50);
+	render_text(40.0, WINDOW_HEIGHT - 50, text);
+
+	sprintf(text, "Test counter: %i", test_counter);
+	render_text(540.0, WINDOW_HEIGHT - 50, text);
+
+	glutSwapBuffers();
+	glFlush();
+}
+
 
-	for (const char *c = text; *c; ++c) {
-		glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
+void idle() {
+	glutPostRedisplay();
+}
+
+
+void keyboard(unsigned char key, int x, int y) {
+
+	// 13 is the ASCII value of ENTER key
+	if (key == 13) {
+		test_counter++;
 	}
-
-	glFlush();
 }
 
 
@@ -82,11 +106,13 @@
 	}
 
 	glutInit(&argc, argv);
-	glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB);
+	glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
 	glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
 	glutCreateWindow("OpenGL Window");
 	setup();
 	glutDisplayFunc(display);
+	glutKeyboardFunc(keyboard);
+	glutIdleFunc(idle);
 	glutMainLoop();
 
 	free(rectangles);
--- a/utils.c	Fri Jun 23 10:01:37 2023 +0100
+++ b/utils.c	Fri Jun 23 16:46:43 2023 +0100
@@ -4,3 +4,4 @@
 int random_int(int min, int max) {
 	return rand() % ((max - min) + 1) + min;
 }
+