comparison main.c @ 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
comparison
equal deleted inserted replaced
7:f159eec07daf 8:f7af7255705e
1 #include "utils.h" 1 #include "utils.h"
2 #include <stdio.h>
2 #include <GL/glut.h> 3 #include <GL/glut.h>
3 #include <stdio.h>
4 #include <stdlib.h>
5 4
6 5
7 #define WINDOW_HEIGHT 1080 6 #define WINDOW_HEIGHT 1080
8 #define WINDOW_WIDTH 1920 7 #define WINDOW_WIDTH 1920
9 #define RECT_WIDTH 5 8 #define RECT_WIDTH 5
17 }; 16 };
18 17
19 // Globals 18 // Globals
20 struct Rectangle* rectangles; 19 struct Rectangle* rectangles;
21 int n_rectangles; 20 int n_rectangles;
21 int test_counter = 0;
22 22
23 23
24 void setup() { 24 void setup() {
25 25
26 // Set background dark 26 // Set background dark
34 glMatrixMode(GL_PROJECTION); 34 glMatrixMode(GL_PROJECTION);
35 glLoadIdentity(); 35 glLoadIdentity();
36 36
37 // Set the coordinates to be used with the viewport 37 // Set the coordinates to be used with the viewport
38 gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0); 38 gluOrtho2D(0, WINDOW_WIDTH, WINDOW_HEIGHT, 0);
39
40 }
41
42
43 void render_text(int x, int y, char* text) {
44 glRasterPos2f(x, y);
45
46 for (const char *c = text; *c; ++c) {
47 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c);
48 }
39 } 49 }
40 50
41 51
42 void display() { 52 void display() {
43 glClear(GL_COLOR_BUFFER_BIT); 53 glClear(GL_COLOR_BUFFER_BIT);
53 glEnd(); 63 glEnd();
54 64
55 // Render text 65 // Render text
56 char text[256]; 66 char text[256];
57 sprintf(text, "Number of elements: %i", n_rectangles); 67 sprintf(text, "Number of elements: %i", n_rectangles);
58 glRasterPos2f(20.0, WINDOW_HEIGHT - 50); 68 render_text(40.0, WINDOW_HEIGHT - 50, text);
59 69
60 for (const char *c = text; *c; ++c) { 70 sprintf(text, "Test counter: %i", test_counter);
61 glutBitmapCharacter(GLUT_BITMAP_TIMES_ROMAN_24, *c); 71 render_text(540.0, WINDOW_HEIGHT - 50, text);
72
73 glutSwapBuffers();
74 glFlush();
75 }
76
77
78 void idle() {
79 glutPostRedisplay();
80 }
81
82
83 void keyboard(unsigned char key, int x, int y) {
84
85 // 13 is the ASCII value of ENTER key
86 if (key == 13) {
87 test_counter++;
62 } 88 }
63
64 glFlush();
65 } 89 }
66 90
67 91
68 int main(int argc, char** argv) { 92 int main(int argc, char** argv) {
69 n_rectangles = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1; 93 n_rectangles = floor((WINDOW_WIDTH - RECT_WIDTH) / (RECT_WIDTH + SPACE)) + 1;
80 x_pos += RECT_WIDTH + SPACE; 104 x_pos += RECT_WIDTH + SPACE;
81 i++; 105 i++;
82 } 106 }
83 107
84 glutInit(&argc, argv); 108 glutInit(&argc, argv);
85 glutInitDisplayMode(GLUT_SINGLE | GLUT_RGB); 109 glutInitDisplayMode(GLUT_DOUBLE | GLUT_RGB);
86 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT); 110 glutInitWindowSize(WINDOW_WIDTH, WINDOW_HEIGHT);
87 glutCreateWindow("OpenGL Window"); 111 glutCreateWindow("OpenGL Window");
88 setup(); 112 setup();
89 glutDisplayFunc(display); 113 glutDisplayFunc(display);
114 glutKeyboardFunc(keyboard);
115 glutIdleFunc(idle);
90 glutMainLoop(); 116 glutMainLoop();
91 117
92 free(rectangles); 118 free(rectangles);
93 119
94 return 0; 120 return 0;