gKit2 light
Loading...
Searching...
No Matches
text.cpp
1
2#include <ctype.h>
3#include <cstdio>
4#include <cstdarg>
5
6#include "program.h"
7#include "uniforms.h"
8#include "image.h"
9#include "image_io.h"
10#include "texture.h"
11#include "text.h"
12#include "window.h"
13
15{
16 Text text;
17
18 // charge la fonte
19 Image font= read_image( smart_path("data/font.png") );
20
21 // modifie la transparence du caractere de fond
22 for(unsigned int y= 0; y < 16; y++)
23 for(unsigned int x= 0; x < 8; x++)
24 {
25 unsigned int starty= 16 *7;
26 unsigned int startx= 8 *2;
27 Color color= font(startx + x, starty + y);
28 color.a= 0.6f;
29 font(startx + x, starty + y)= color;
30 }
31
32 // cree le curseur
33 for(unsigned int y= 0; y < 16; y++)
34 for(unsigned int x= 0; x < 8; x++)
35 {
36 unsigned int starty= 16 *7;
37 unsigned int startx= 8 *1;
38 Color color= (x > 1) ? Color(1, 1, 1, 0.6f) : Color(1, 1, 1, 1);
39 font(startx + x, starty + y)= color;
40 }
41
42 text.font= make_texture(0, font);
43 //~ release_image(font);
44
45 text.color= White();
46
47 // shader
48 text.program= read_program( smart_path("data/shaders/text.glsl") );
50
51 // associe l'uniform buffer a l'entree 0 / binding 0
52 GLint index= glGetUniformBlockIndex(text.program, "textData");
53 glUniformBlockBinding(text.program, index, 0);
54
55 clear(text);
56 glGenVertexArrays(1, &text.vao);
57 glGenBuffers(1, &text.ubo);
58 glBindBuffer(GL_UNIFORM_BUFFER, text.ubo);
59 glBufferData(GL_UNIFORM_BUFFER, sizeof(text.buffer), text.buffer, GL_DYNAMIC_DRAW);
60
61 return text;
62}
63
64void release_text( Text& text )
65{
67 glDeleteVertexArrays(1, &text.vao);
68 glDeleteBuffers(1, &text.ubo);
69 glDeleteTextures(1, &text.font);
70}
71
72void clear( Text& text )
73{
74 for(int y= 0; y < 24; y++)
75 for(int x= 0; x < 128; x++)
76 text.buffer[y][x]= ' ';
77}
78
79
80static
81void print( Text& text, const int px, const int py, const int background, const char *message )
82{
83 int x= px;
84 int y= 23 - py; // premiere ligne en haut...
85
86 for(int i= 0; message[i] != 0; i++)
87 {
88 unsigned char c= message[i];
89 if(x >= 128 || c == '\n')
90 {
91 y--;
92 x= px;
93 }
94 if(c == '\n') continue; // ne pas afficher le \n
95
96 if(x < 0 || y < 0) break;
97 if(x >= 128 || y >= 24) break;
98
99 //~ if(!isprint(c)) c= ' ';
100 text.buffer[y][x]= (int) c | (background << 8);
101 x++;
102 }
103}
104
105
106void print_background( Text& text, const int px, const int py, const int background, const char c )
107{
108 int x= px;
109 int y= 23 - py; // premiere ligne en haut...
110 if(x < 0 || y < 0) return;
111 if(x >= 128 || y >= 24) return;
112 //~ if(!isprint(c)) return;
113
114 text.buffer[y][x]= (int) c | (background << 8);
115}
116
117void print_background( Text& text, const int px, const int py, const char *message )
118{
119 print(text, px, py, 2, message);
120}
121
122void print( Text& text, const int px, const int py, const char *message )
123{
124 print(text, px, py, 0, message);
125}
126
127void printf_background( Text& text, const int px, const int py, const char *format, ... )
128{
129 char tmp[24*128+1] = { 0 };
130
131 va_list args;
132 va_start(args, format);
133 vsnprintf(tmp, sizeof(tmp), format, args);
134 va_end(args);
135
136 tmp[24*128]= 0;
137 print(text, px, py, 2, tmp);
138}
139
140void printf( Text& text, const int px, const int py, const char *format, ... )
141{
142 char tmp[24*128+1] = { 0 };
143
144 va_list args;
145 va_start(args, format);
146 vsnprintf(tmp, sizeof(tmp), format, args);
147 va_end(args);
148
149 tmp[24*128]= 0;
150 print(text, px, py, 0, tmp);
151}
152
153void default_color( Text& text, const Color& color )
154{
155 text.color= color;
156}
157
158void draw( const Text& text, const int width, const int height )
159{
160 glBindVertexArray(text.vao);
161 glUseProgram(text.program);
162 program_use_texture(text.program, "font", 0, text.font);
163
164 program_uniform(text.program, "offset", height - 24*16);
165 program_uniform(text.program, "default_color", text.color);
166
167 // transfere le texte dans l'uniform buffer associe au binding 0, cf create_text()
168 glBindBufferBase(GL_UNIFORM_BUFFER, 0, text.ubo);
169 glBufferSubData(GL_UNIFORM_BUFFER, 0, sizeof(text.buffer), text.buffer);
170
171 glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
172 glEnable(GL_BLEND);
173 glDisable(GL_DEPTH_TEST);
174
175 glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);
176
177 glDisable(GL_BLEND);
178 glEnable(GL_DEPTH_TEST);
179}
180
representation d'une image.
Definition image.h:21
const char * smart_path(const char *filename)
renvoie le chemin(path) vers le fichier 'filename' apres l'avoir cherche dans un repertoire standard....
Definition window.cpp:413
void clear(Text &text)
efface le contenu de la console.
Definition text.cpp:72
void printf(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y. meme utilisation que printf().
Definition text.cpp:140
Text create_text()
cree une console. a detruire avec release_text( ).
Definition text.cpp:14
void draw(const Text &text, const int width, const int height)
dessine la console.
Definition text.cpp:158
void default_color(Text &text, const Color &color)
choisit une couleur par defaut pour le texte.
Definition text.cpp:153
void release_text(Text &text)
detruit une console.
Definition text.cpp:64
void print_background(Text &text, const int px, const int py, const int background, const char c)
affiche un caractere c sur un fond background.
Definition text.cpp:106
void printf_background(Text &text, const int px, const int py, const char *format,...)
affiche un texte a la position x, y sur un fond par defaut.
Definition text.cpp:127
Image read_image(const char *filename, const bool flipY)
Definition image_io.cpp:171
Color White()
utilitaire. renvoie une couleur blanche.
Definition color.cpp:23
GLuint make_texture(const int unit, const int width, const int height, const GLenum texel_type, const GLenum data_format, const GLenum data_type)
creation de textures filtrables / mipmaps
Definition texture.cpp:10
GLuint read_program(const char *filename, const char *definitions)
Definition program.cpp:218
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition program.cpp:446
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:225
void program_use_texture(const GLuint program, const char *uniform, const int unit, const GLuint texture, const GLuint sampler)
configure le pipeline et le shader program pour utiliser une texture, et des parametres de filtrage,...
Definition uniforms.cpp:198
representation d'une couleur (rgba) transparente ou opaque.
Definition color.h:14
Definition text.h:62
GLuint font
texture contenant les caracteres.
Definition text.h:67
GLuint vao
vertex array object.
Definition text.h:69
GLuint ubo
uniform buffer object, pour transferrer le texte a afficher
Definition text.h:70
GLuint program
shader pour afficher le texte.
Definition text.h:68
Color color
couleur du texte.
Definition text.h:66