gKit2 light
Loading...
Searching...
No Matches
utilitaires pour creer une application

Files

file  src/gKit/text.h
file  src/gKit/widgets.h
file  src/gKit/window.h

Classes

struct  Text
struct  Widgets

Typedefs

typedef SDL_Window * Window
typedef SDL_GLContext Context

Functions

Text create_text ()
 cree une console. a detruire avec release_text( ).
void release_text (Text &text)
 detruit une console.
void clear (Text &text)
 efface le contenu de la console.
void print_background (Text &text, const int x, const int y, const int background, const char c)
 affiche un caractere c sur un fond background.
void print_background (Text &text, const int x, const int y, const char *message)
 affiche un caractere c sur un fond par defaut.
void print (Text &text, const int x, const int y, const char *message)
 affiche un texte a la position x, y.
void printf_background (Text &text, const int x, const int y, const char *format,...)
 affiche un texte a la position x, y sur un fond par defaut.
void printf (Text &text, const int x, const int y, const char *format,...)
 affiche un texte a la position x, y. meme utilisation que printf().
void default_color (Text &text, const Color &color)
 choisit une couleur par defaut pour le texte.
void draw (const Text &text, const int width, const int height)
 dessine la console.
Widgets create_widgets ()
 cree une interface graphique. a detruire avec release_widgets( ).
void release_widgets (Widgets &widgets)
 detruit l'interface graphique.
void begin (Widgets &widgets)
 debut de la description des elements de l'interface graphique.
void begin_line (Widgets &widgets)
 place les prochains elements sur une nouvelle ligne.
void label (Widgets &widgets, const char *format,...)
 cree un texte. meme fonctionnement que printf().
bool button (Widgets &widgets, const char *text, int &status)
bool select (Widgets &widgets, const char *text, const int option, int &status)
void text_area (Widgets &w, const int height, const char *text, int &begin_line)
bool edit (Widgets &widgets, const int text_size, char *text)
bool value (Widgets &widgets, const char *label, int &value, const int value_min, const int value_max, const int value_step)
 valeur editable par increment.
bool value (Widgets &widgets, const char *label, float &value, const float value_min, const float value_max, const float value_step)
 valeur editable par increment.
void end_line (Widgets &widgets)
 termine la description des elements de la ligne.
void end (Widgets &widgets)
 termine la description des elements de l'interface graphique.
void default_color (Widgets &widgets, const Color &color)
 choisit une couleur par defaut pour le texte.
void draw (Widgets &widgets, const int width, const int height)
 affiche les elements decrits entre begin() et end().
void window_msaa (const int samples)
 option mssa. utiliser avant la creation de la fenetre avec create_window().
void window_srgb (const bool flag)
 option srgb. utiliser avant la creation de la fenetre avec create_window().
Window create_window (const int width, const int height, const int major=3, const int minor=2)
 creation d'une fenetre pour l'application.
void release_window (Window w)
 destruction de la fenetre.
Context create_context (Window window)
 cree et configure un contexte opengl.
void release_context (Context context)
 detruit le contexte openGL.
int window_width ()
 renvoie la largeur de la fenetre de l'application.
int window_height ()
 renvoie la hauteur de la fenetre de l'application.
int window_msaa ()
 renvoie le nombre de samples MSAA.
bool window_srgb ()
 renvoie le mode srgb de la fenetre de l'application.
int key_state (const SDL_Keycode key)
 renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
void clear_key_state (const SDL_Keycode key)
 desactive une touche du clavier.
SDL_KeyboardEvent key_event ()
 renvoie le dernier evenement. touche speciales.
void clear_key_event ()
 desactive l'evenement.
SDL_MouseButtonEvent button_event ()
 renvoie le dernier evenement. etat des boutons de la souris.
void clear_button_event ()
 desactive l'evenement.
SDL_MouseWheelEvent wheel_event ()
 renvoie le dernier evenement. etat de la molette de la souris / glisser sur le pad.
void clear_wheel_event ()
 desactive l'evenement.
SDL_TextInputEvent text_event ()
 renvoie le dernier evenement. saisie de texte.
void clear_text_event ()
 desactive l'evenement.
float global_time ()
 renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
float delta_time ()
 renvoie le temps ecoule depuis la derniere frame, en millisecondes.
int run (Window window, int(*draw)(void))
 fonction principale. gestion des evenements et appel de la fonction draw de l'application.
int last_event_count ()
int events (Window window)
 fonction interne de gestion d'evenements.
const char * smart_path (const char *filename)
 renvoie le chemin(path) vers le fichier 'filename' apres l'avoir cherche dans un repertoire standard...
const char * drop_event ()
 drag/drop, renvoie le dernier fichier.
void clear_drop_event ()
 desactive drag/drop.
const std::vector< std::string > & drop_events ()
 drag/drop. recupere tous les fichiers.
void clear_drop_events ()
 desactive drag/drop.

Detailed Description

Typedef Documentation

◆ Window

typedef SDL_Window* Window

Definition at line 17 of file window.h.

◆ Context

typedef SDL_GLContext Context

Definition at line 29 of file window.h.

Function Documentation

◆ create_text()

Text create_text ( )

cree une console. a detruire avec release_text( ).

Definition at line 14 of file text.cpp.

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}
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:448
void clear(Text &text)
efface le contenu de la console.
Definition text.cpp:72
Image read_image(const char *filename, const bool flipY)
Definition image_io.cpp:191
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:217
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
Definition program.cpp:461
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

◆ release_text()

void release_text ( Text & text)

detruit une console.

Definition at line 64 of file text.cpp.

65{
67 glDeleteVertexArrays(1, &text.vao);
68 glDeleteBuffers(1, &text.ubo);
69 glDeleteTextures(1, &text.font);
70}
int release_program(const GLuint program)
detruit les shaders et le program.
Definition program.cpp:240

◆ clear()

void clear ( Text & text)

efface le contenu de la console.

Definition at line 72 of file text.cpp.

73{
74 for(int y= 0; y < 24; y++)
75 for(int x= 0; x < 128; x++)
76 text.buffer[y][x]= ' ';
77}

◆ print_background() [1/2]

void print_background ( Text & text,
const int x,
const int y,
const int background,
const char c )

affiche un caractere c sur un fond background.

Definition at line 106 of file text.cpp.

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}

◆ print_background() [2/2]

void print_background ( Text & text,
const int x,
const int y,
const char * message )

affiche un caractere c sur un fond par defaut.

Definition at line 117 of file text.cpp.

118{
119 print(text, px, py, 2, message);
120}

◆ print()

void print ( Text & text,
const int x,
const int y,
const char * message )

affiche un texte a la position x, y.

Definition at line 122 of file text.cpp.

123{
124 print(text, px, py, 0, message);
125}

◆ printf_background()

void printf_background ( Text & text,
const int x,
const int y,
const char * format,
... )

affiche un texte a la position x, y sur un fond par defaut.

Definition at line 127 of file text.cpp.

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}

◆ printf()

void printf ( Text & text,
const int x,
const int y,
const char * format,
... )

affiche un texte a la position x, y. meme utilisation que printf().

Definition at line 140 of file text.cpp.

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}

◆ default_color() [1/2]

void default_color ( Text & text,
const Color & color )

choisit une couleur par defaut pour le texte.

Definition at line 153 of file text.cpp.

154{
155 text.color= color;
156}

◆ draw() [1/2]

void draw ( const Text & text,
const int width,
const int height )

dessine la console.

Definition at line 158 of file text.cpp.

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}
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

◆ create_widgets()

Widgets create_widgets ( )

cree une interface graphique. a detruire avec release_widgets( ).

Definition at line 12 of file widgets.cpp.

13{
14 Widgets w;
16 w.px= 0; w.py= 0;
17 w.focus= 0; w.fx= 0; w.fy= 0;
18 w.mb= 0; w.mx= 0; w.my= 0;
19 w.wx= 0; w.wy= 0;
20 return w;
21}
Text create_text()
cree une console. a detruire avec release_text( ).
Definition text.cpp:14
Text console
affichage des elements de l'interface.
Definition widgets.h:70

◆ release_widgets()

void release_widgets ( Widgets & widgets)

detruit l'interface graphique.

Definition at line 23 of file widgets.cpp.

24{
25 release_text(w.console);
26}
void release_text(Text &text)
detruit une console.
Definition text.cpp:64

◆ begin()

void begin ( Widgets & widgets)

debut de la description des elements de l'interface graphique.

Definition at line 29 of file widgets.cpp.

30{
31 clear(w.console);
32 w.px= 0;
33 w.py= 0;
34
35 SDL_MouseButtonEvent mouse= button_event();
36 w.mb= 0;
37 w.mx= mouse.x / 8;
38 w.my= mouse.y / 16;
39 if(mouse.state == SDL_PRESSED)
40 {
42 w.mb= 1;
43 }
44
45 SDL_MouseWheelEvent wheel= wheel_event();
46 w.wx= 0;
47 w.wy= 0;
48 if(wheel.x != 0 || wheel.y != 0)
49 {
51 w.wx= wheel.x;
52 w.wy= wheel.y;
53 }
54
55 SDL_KeyboardEvent key= key_event( );
56 w.key= 0;
57 w.mod= 0;
58 if(key.type == SDL_KEYDOWN)
59 {
61 w.key= key.keysym.sym;
62 w.mod= key.keysym.mod;
63
64 // filtre les touches speciales
65 switch(w.key)
66 {
67 case SDLK_SPACE:
68 case SDLK_BACKSPACE:
69 case SDLK_DELETE:
70 case SDLK_UP:
71 case SDLK_DOWN:
72 case SDLK_PAGEUP:
73 case SDLK_PAGEDOWN:
74 case SDLK_LEFT:
75 case SDLK_RIGHT:
76 case SDLK_RETURN:
77 break;
78 default:
79 w.key= 0;
80 }
81 }
82
83 SDL_TextInputEvent input= text_event();
84 if(input.text[0] != 0)
85 {
86 w.key= input.text[0];
88 }
89}
SDL_MouseButtonEvent button_event()
renvoie le dernier evenement. etat des boutons de la souris.
Definition window.cpp:113
void clear_button_event()
desactive l'evenement.
Definition window.cpp:117
SDL_TextInputEvent text_event()
renvoie le dernier evenement. saisie de texte.
Definition window.cpp:78
void clear_key_event()
desactive l'evenement.
Definition window.cpp:71
SDL_KeyboardEvent key_event()
renvoie le dernier evenement. touche speciales.
Definition window.cpp:67
void clear_text_event()
desactive l'evenement.
Definition window.cpp:82
void clear_wheel_event()
desactive l'evenement.
Definition window.cpp:127
SDL_MouseWheelEvent wheel_event()
renvoie le dernier evenement. etat de la molette de la souris / glisser sur le pad.
Definition window.cpp:123

◆ begin_line()

void begin_line ( Widgets & widgets)

place les prochains elements sur une nouvelle ligne.

Definition at line 129 of file widgets.cpp.

130{
131 // place le prochain widget sur une nouvelle ligne
132 w.px= 0;
133 w.py= w.py +1;
134}

◆ label()

void label ( Widgets & widgets,
const char * format,
... )

cree un texte. meme fonctionnement que printf().

Definition at line 142 of file widgets.cpp.

143{
144 char tmp[4096] = { 0 };
145
146 va_list args;
147 va_start(args, format);
148 vsnprintf(tmp, sizeof(tmp), format, args);
149 va_end(args);
150
151 Rect r= place(w, tmp);
152 print(w.console, r.x, r.y, tmp);
153}

◆ button()

bool button ( Widgets & widgets,
const char * text,
int & status )

cree un bouton. renvoie true si le bouton a change d'etat.

Parameters
textlegende du bouton,
statusetat du bouton, 1 selectionne, 0 non selectionne. doit etre initialise a 0 avant la premiere utilisation.

Definition at line 155 of file widgets.cpp.

156{
157 Rect r= place(w, (int) strlen(text) +2);
158
159 bool change= false;
160 if(w.mb > 0 && overlap(r, w.mx, w.my))
161 {
162 change= true;
163 status= (status + 1) % 2;
164 }
165
166 char tmp[128];
167 sprintf(tmp, "%c %s", (status != 0) ? 22 : 20, text); // strlen(text) + 2
168
169 print(w.console, r.x, r.y, tmp);
170 return change;
171}

◆ select()

bool select ( Widgets & widgets,
const char * text,
const int option,
int & status )

cree un radio bouton. selectionne une seule option parmis une liste. renvoie true si le bouton a change d'etat.

Parameters
textlegende du bouton,
optionposition du bouton dans le groupe,
statusoption/bouton selectionne. doit etre initialise avec l'option par defaut avant la premiere utilisation.

Definition at line 173 of file widgets.cpp.

174{
175 Rect r= place(w, (int) strlen(text) +2);
176
177 bool change= false;
178 if(w.mb > 0 && overlap(r, w.mx, w.my))
179 {
180 change= true;
181 status= option;
182 }
183
184 char tmp[128];
185 sprintf(tmp, "%c %s", (status == option) ? 4 : 3, text); // strlen(text) + 2
186
187 print(w.console, r.x, r.y, tmp);
188 return change;
189}

◆ text_area()

void text_area ( Widgets & w,
const int height,
const char * text,
int & begin_line )

cree une zone de texte scrollable.

Parameters
heightnombre de lignes.
textcontenu a afficher.
begin_linepremiere ligne du texte a afficher. doit etre initialise avant la premiere utilisation.

Definition at line 273 of file widgets.cpp.

274{
275 Rect r= place(w, 128, height);
276
277 if(overlap(r, w.mx, w.my))
278 {
279 if(w.wy != 0)
280 begin_line= begin_line - w.wy;
281 if(w.key == SDLK_PAGEUP)
282 begin_line= begin_line - height;
283 if(w.key == SDLK_PAGEDOWN)
284 begin_line= begin_line + height;
285 if(w.key == SDLK_SPACE)
286 begin_line= begin_line + height / 2;
287 if(w.key == SDLK_UP)
289 if(w.key == SDLK_DOWN)
291 }
292
293 // compter les lignes
294 int n= 1;
295 for(int i= 0; text[i] != 0; i++)
296 if(text[i] == '\n')
297 n++;
298 // ligne de depart, pout que tout le texte reste affiche
299 if(begin_line + height > n)
300 begin_line= n - height;
301 if(begin_line < 1)
302 begin_line= 1;
303 // retrouve le debut de la ligne
304 int line= 1;
305 int offset= 0;
306 for(int i= 0; text[i] != 0; i++)
307 {
308 if(text[i] == '\n')
309 {
310 line++;
311 if(line == begin_line)
312 offset= i;
313 }
314 }
315 // affiche le texte
316 print(w.console, r.x, r.y, text + offset);
317}
void begin_line(Widgets &w)
place les prochains elements sur une nouvelle ligne.
Definition widgets.cpp:129

◆ edit()

bool edit ( Widgets & widgets,
const int text_size,
char * text )

cree un champ texte editable. renvoie true si le contenu a change.

Parameters
text_sizetaille du champ texte et de la chaine text, zero inclus. si text_size fait 10 caracteres, on ne pourra saisir que 9 caracteres.
textcontenu du champ, doit etre initialise avant la premiere utilisation.
char valeur[32]= "";
int draw( )
{
begin(widgets);
edit(widgets, sizeof(valeur), valeur);
end(widgets);
}
void begin(Widgets &w)
debut de la description des elements de l'interface graphique.
Definition widgets.cpp:29
int window_height()
renvoie la hauteur de la fenetre de l'application.
Definition window.cpp:27
bool edit(Widgets &w, int text_size, char *text)
Definition widgets.cpp:319
void end(Widgets &widgets)
termine la description des elements de l'interface graphique.
Definition widgets.cpp:404
void draw(Widgets &widgets, const int width, const int height)
affiche les elements decrits entre begin() et end().
Definition widgets.cpp:414
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition window.cpp:23

Definition at line 319 of file widgets.cpp.

320{
321 assert(text_size > 1);
322 int size= std::min((int) strlen(text), text_size -2);
323 Rect r= place(w, text_size -1);
324
325 // focus
326 bool change= false;
327 if(w.mb > 0)
328 {
329 if(overlap(r, w.mx, w.my))
330 {
331 w.focus= 1;
332 w.fx= w.mx;
333 w.fy= w.my;
334
335 if(w.fx >= r.x + size)
336 w.fx= r.x + size;
337 }
338
339 else
340 change= (w.focus > 0); // click en dehors de la zone editable
341 }
342
343 // edition
344 bool focus= overlap(r, w.fx, w.fy);
345 if(focus && w.key > 0)
346 {
347 int c= w.fx - r.x;
348 assert(c < text_size -1);
349
350 if(w.key == SDLK_BACKSPACE)
351 {
352 w.fx--; // curseur a gauche
353 for(int i= c -1; i >= 0 && i+1 < text_size; i++) text[i]= text[i+1];
354 }
355 else if(w.key == SDLK_DELETE)
356 {
357 // curseur ne bouge pas
358 for(int i= c; i+1 < text_size; i++) text[i]= text[i+1];
359 }
360 else if(w.key == SDLK_LEFT)
361 {
362 w.fx--; // curseur a gauche
363 }
364 else if(w.key == SDLK_RIGHT)
365 {
366 w.fx++; // curseur a droite
367 }
368 else if(w.key == SDLK_RETURN)
369 {
370 w.focus= 0;
371 change= true;
372 }
373 else
374 {
375 w.fx++; // curseur a droite
376 for(int i= text_size -1; i > c; i--) text[i]= text[i -1];
377 text[c]= w.key;
378
379 if(size < text_size - 2)
380 size++;
381 text[size+1]= 0;
382 }
383
384 // verifier que le curseur reste dans la zone editable
385 if(w.fx < r.x)
386 w.fx= r.x;
387 if(w.fx >= r.x + size)
388 w.fx= r.x + size;
389 }
390
391 int i= 0;
392 char tmp[128];
393 for(; text[i] != 0; i++) tmp[i]= text[i]; // copie les caracteres
394 for(; i < text_size; i++) tmp[i]= ' '; // complete avec des espaces
395 tmp[text_size -1]= 0; // termine avec un 0
396
397 print_background(w.console, r.x, r.y, tmp);
398 if(focus)
399 print_background(w.console, w.fx, w.fy, text[w.fx - r.x], 1);
400
401 return change;
402}
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

◆ value() [1/2]

bool value ( Widgets & widgets,
const char * label,
int & value,
const int value_min,
const int value_max,
const int value_step )

valeur editable par increment.

Definition at line 191 of file widgets.cpp.

192{
193 char tmp[128];
194 sprintf(tmp, "%s: %4d", label, value);
195
196 Rect r= place(w, (int) strlen(tmp));
197
198 bool change= false;
199 if(w.mb > 0 && overlap(r, w.mx, w.my))
200 change= true;
201 if(overlap(r, w.mx, w.my))
202 {
203 if(w.wy != 0)
204 value= value + w.wy * value_step;
205 if(w.key == SDLK_UP)
206 value= value + value_step;
207 if(w.key == SDLK_PAGEUP)
208 value= value + value_step * 10;
209 if(w.key == SDLK_DOWN)
210 value= value - value_step;
211 if(w.key == SDLK_PAGEDOWN)
212 value= value - value_step * 10;
213
214 if(value < value_min)
215 value= value_min;
216 if(value > value_max)
217 value= value_max;
218
219 sprintf(tmp, "%s: ", label);
220 int l= (int) strlen(tmp);
221 print(w.console, r.x, r.y, tmp);
222
223 sprintf(tmp, "%4d", value);
224 print_background(w.console, r.x + l, r.y, tmp);
225 }
226 else
227 print(w.console, r.x, r.y, tmp);
228
229 return change;
230}

◆ value() [2/2]

bool value ( Widgets & widgets,
const char * label,
float & value,
const float value_min,
const float value_max,
const float value_step )

valeur editable par increment.

Definition at line 232 of file widgets.cpp.

233{
234 char tmp[128];
235 sprintf(tmp, "%s: %.6f", label, value);
236
237 Rect r= place(w, (int) strlen(tmp));
238
239 bool change= false;
240 if(w.mb > 0 && overlap(r, w.mx, w.my))
241 change= true;
242 if(overlap(r, w.mx, w.my))
243 {
244 if(w.wy != 0)
245 value= value + w.wy * value_step;
246 if(w.key == SDLK_UP)
247 value= value + value_step;
248 if(w.key == SDLK_PAGEUP)
249 value= value + value_step * 10;
250 if(w.key == SDLK_DOWN)
251 value= value - value_step;
252 if(w.key == SDLK_PAGEDOWN)
253 value= value - value_step * 10;
254
255 if(value < value_min)
256 value= value_min;
257 if(value > value_max)
258 value= value_max;
259
260 sprintf(tmp, "%s: ", label);
261 int l= (int) strlen(tmp);
262 print(w.console, r.x, r.y, tmp);
263
264 sprintf(tmp, "%.6f", value);
265 print_background(w.console, r.x + l, r.y, tmp);
266 }
267 else
268 print(w.console, r.x, r.y, tmp);
269
270 return change;
271}

◆ end_line()

void end_line ( Widgets & widgets)

termine la description des elements de la ligne.

Definition at line 136 of file widgets.cpp.

137{
138 return;
139}

◆ end()

void end ( Widgets & widgets)

termine la description des elements de l'interface graphique.

Definition at line 404 of file widgets.cpp.

405{
406 return;
407}

◆ default_color() [2/2]

void default_color ( Widgets & widgets,
const Color & color )

choisit une couleur par defaut pour le texte.

Definition at line 409 of file widgets.cpp.

410{
411 default_color(w.console, color);
412}
void default_color(Widgets &w, const Color &color)
choisit une couleur par defaut pour le texte.
Definition widgets.cpp:409

◆ draw() [2/2]

void draw ( Widgets & widgets,
const int width,
const int height )

affiche les elements decrits entre begin() et end().

Definition at line 414 of file widgets.cpp.

415{
416 draw(w.console, width, height);
417}

◆ window_msaa() [1/2]

void window_msaa ( const int samples)

option mssa. utiliser avant la creation de la fenetre avec create_window().

Definition at line 37 of file window.cpp.

38{
39 msaa= samples;
40}

◆ window_srgb() [1/2]

void window_srgb ( const bool flag)

option srgb. utiliser avant la creation de la fenetre avec create_window().

Definition at line 47 of file window.cpp.

48{
49 srgb= flag;
50}

◆ create_window()

Window create_window ( const int width,
const int height,
const int major = 3,
const int minor = 2 )

creation d'une fenetre pour l'application.

Definition at line 262 of file window.cpp.

263{
264 // init sdl
265 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0)
266 {
267 printf("[error] SDL_Init() failed:\n%s\n", SDL_GetError());
268 return nullptr;
269 }
270
271 // enregistre le destructeur de sdl
272 atexit(SDL_Quit);
273
274 // configuration openGL
275#ifndef GK_OPENGLES
276 printf("creating window(%d, %d) openGL %d.%d....\n", w, h, major, minor);
277
278 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
279 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
280#ifndef GK_RELEASE
281 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
282#endif
283 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
284
285 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
286 SDL_GL_SetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, srgb ? 1 : 0);
287 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
288
289 if(msaa > 1)
290 {
291 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
292 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, msaa);
293 }
294
295#else
296 printf("creating window(%d, %d) openGL ES 3.0...\n", w, h);
297
298 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
299 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 0);
300 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
301 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
302#endif
303
304 // creer la fenetre
305 Window window= SDL_CreateWindow("gKit",
306 SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h,
307 SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
308 if(window == nullptr)
309 {
310 printf("[error] SDL_CreateWindow() failed.\n");
311 return nullptr;
312 }
313
314 // recupere l'etat du clavier
315 int keys;
316 const unsigned char *state= SDL_GetKeyboardState(&keys);
317 key_states.assign(state, state + keys);
318
319 SDL_SetWindowDisplayMode(window, nullptr);
320 SDL_StartTextInput();
321
322 // conserve les dimensions de la fenetre
323 SDL_GetWindowSize(window, &width, &height);
324
325 return window;
326}
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

◆ release_window()

void release_window ( Window w)

destruction de la fenetre.

Definition at line 328 of file window.cpp.

329{
330 SDL_StopTextInput();
331 SDL_DestroyWindow(window);
332}

◆ create_context()

Context create_context ( Window window)

cree et configure un contexte opengl.

cree et configure un contexte opengl

cree et configure un contexte opengl.

Definition at line 358 of file window.cpp.

359{
360 if(window == nullptr)
361 return nullptr;
362
363 Context context= SDL_GL_CreateContext(window);
364 if(context == nullptr)
365 {
366 printf("[error] creating openGL context.\n");
367 return nullptr;
368 }
369
370 if(SDL_GL_SetSwapInterval(-1) != 0)
371 printf("[warning] can't set adaptive vsync...\n");
372
373 if(SDL_GL_GetSwapInterval() != -1)
374 {
375 printf("vsync ON\n");
376 SDL_GL_SetSwapInterval(1);
377 }
378 else
379 printf("adaptive vsync ON\n");
380
381 {
382 int n= 0;
383 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &n);
384 if(n > 1)
385 printf("MSAA %d samples\n", n);
386 msaa= n;
387 }
388
389 {
390 int bits= 0;
391 SDL_GL_GetAttribute(SDL_GL_DEPTH_SIZE, &bits);
392 if(bits > 0)
393 printf("Zbuffer %d bits\n", bits);
394 }
395
396 {
397 int flag= 0;
398 SDL_GL_GetAttribute(SDL_GL_FRAMEBUFFER_SRGB_CAPABLE, &flag);
399 if(flag)
400 printf("sRGB framebuffer ON\n");
401
402 srgb= flag;
403 }
404
405 //
406 app_start= std::chrono::high_resolution_clock::now();
407
408#ifndef NO_GLAD
409 // initialise les extensions opengl
410 gladLoadGL();
411
412 // purge les erreurs opengl generees par glew !
413 while(glGetError() != GL_NO_ERROR) {;}
414
415#ifndef GK_RELEASE
416 // configure l'affichage des messages d'erreurs opengl, si l'extension est disponible
417 // inclut dans openGL 4.3, mais pas dispo sur mac...
418 if(GLAD_GL_ARB_debug_output)
419 {
420 printf("debug output enabled...\n");
421 // selectionne tous les messages
422 glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
423 // desactive les messages du compilateur de shaders
424 glDebugMessageControlARB(GL_DEBUG_SOURCE_SHADER_COMPILER, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE);
425
426 glDebugMessageCallbackARB(debug_print, NULL);
427 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
428 }
429#endif
430#endif
431
432 if(srgb)
433 glEnable(GL_FRAMEBUFFER_SRGB);
434
435
436 return context;
437}

◆ release_context()

void release_context ( Context context)

detruit le contexte openGL.

Definition at line 439 of file window.cpp.

440{
441 SDL_GL_DeleteContext(context);
442}

◆ window_width()

int window_width ( )

renvoie la largeur de la fenetre de l'application.

Definition at line 23 of file window.cpp.

24{
25 return width;
26}

◆ window_height()

int window_height ( )

renvoie la hauteur de la fenetre de l'application.

Definition at line 27 of file window.cpp.

28{
29 return height;
30}

◆ window_msaa() [2/2]

int window_msaa ( )

renvoie le nombre de samples MSAA.

Definition at line 33 of file window.cpp.

34{
35 return msaa;
36}

◆ window_srgb() [2/2]

bool window_srgb ( )

renvoie le mode srgb de la fenetre de l'application.

Definition at line 43 of file window.cpp.

44{
45 return srgb;
46}

◆ key_state()

int key_state ( const SDL_Keycode key)

renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.

Definition at line 53 of file window.cpp.

54{
55 SDL_Scancode code= SDL_GetScancodeFromKey(key);
56 assert((size_t) code < key_states.size());
57 return (int) key_states[code];
58}

◆ clear_key_state()

void clear_key_state ( const SDL_Keycode key)

desactive une touche du clavier.

Definition at line 59 of file window.cpp.

60{
61 SDL_Scancode code= SDL_GetScancodeFromKey(key);
62 assert((size_t) code < key_states.size());
63 key_states[code]= 0;
64}

◆ key_event()

SDL_KeyboardEvent key_event ( )

renvoie le dernier evenement. touche speciales.

Definition at line 67 of file window.cpp.

68{
69 return last_key;
70}

◆ clear_key_event()

void clear_key_event ( )

desactive l'evenement.

Definition at line 71 of file window.cpp.

72{
73 last_key.type= 0;
74 last_key.keysym.sym= 0;
75}

◆ button_event()

SDL_MouseButtonEvent button_event ( )

renvoie le dernier evenement. etat des boutons de la souris.

Definition at line 113 of file window.cpp.

114{
115 return last_button;
116}

◆ clear_button_event()

void clear_button_event ( )

desactive l'evenement.

Definition at line 117 of file window.cpp.

118{
119 last_button.state= 0;
120}

◆ wheel_event()

SDL_MouseWheelEvent wheel_event ( )

renvoie le dernier evenement. etat de la molette de la souris / glisser sur le pad.

Definition at line 123 of file window.cpp.

124{
125 return last_wheel;
126}

◆ clear_wheel_event()

void clear_wheel_event ( )

desactive l'evenement.

Definition at line 127 of file window.cpp.

128{
129 last_wheel.x= 0;
130 last_wheel.y= 0;
131}

◆ text_event()

SDL_TextInputEvent text_event ( )

renvoie le dernier evenement. saisie de texte.

Definition at line 78 of file window.cpp.

79{
80 return last_text;
81}

◆ clear_text_event()

void clear_text_event ( )

desactive l'evenement.

Definition at line 82 of file window.cpp.

83{
84 last_text.text[0]= 0;
85}

◆ global_time()

float global_time ( )

renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.

Definition at line 139 of file window.cpp.

140{
141 std::chrono::high_resolution_clock::time_point now= std::chrono::high_resolution_clock::now();
142 last_delta= float(std::chrono::duration_cast<std::chrono::microseconds>(now - last_time).count()) / float(1000);
143 last_time= now;
144
145 return float(std::chrono::duration_cast<std::chrono::microseconds>(now - app_start).count()) / float(1000);
146}

◆ delta_time()

float delta_time ( )

renvoie le temps ecoule depuis la derniere frame, en millisecondes.

Definition at line 148 of file window.cpp.

149{
150// pas super utile, a virer ?
151 return last_delta;
152}

◆ run()

int run ( Window window,
int(* draw )() )

fonction principale. gestion des evenements et appel de la fonction draw de l'application.

boucle de gestion des evenements de l'application.

fonction principale. gestion des evenements et appel de la fonction draw de l'application.

Definition at line 158 of file window.cpp.

159{
160 // configure openGL
161 glViewport(0, 0, width, height);
162
163 // run
164 while(events(window))
165 {
166 // dessiner
167 if(draw() < 1)
168 stop= 1; // fermer l'application si draw() renvoie 0 ou -1...
169
170 // presenter le resultat
171 SDL_GL_SwapWindow(window);
172 }
173
174 return 0;
175}
int events(Window window)
fonction interne de gestion d'evenements.
Definition window.cpp:181

◆ last_event_count()

int last_event_count ( )

Definition at line 178 of file window.cpp.

178{ return event_count; }

◆ events()

int events ( Window window)

fonction interne de gestion d'evenements.

Definition at line 181 of file window.cpp.

182{
183 bool resize_event= false;
184
185 // gestion des evenements
186 SDL_Event event;
187 while(SDL_PollEvent(&event))
188 {
189 switch(event.type)
190 {
191 case SDL_WINDOWEVENT:
192 // redimensionner la fenetre...
193 if(event.window.event == SDL_WINDOWEVENT_RESIZED)
194 {
195 // traite l'evenement apres la boucle...
196 resize_event= true;
197
198 // conserve les proportions de la fenetre
199 width= event.window.data1;
200 height= event.window.data2;
201 }
202 break;
203
204 case SDL_DROPFILE:
205 //~ printf("drop file '%s'\n", event.drop.file);
206 last_drops.push_back(std::string(event.drop.file));
207 SDL_free(event.drop.file);
208 break;
209
210 case SDL_TEXTINPUT:
211 // conserver le dernier caractere
212 last_text= event.text;
213 break;
214
215 case SDL_KEYDOWN:
216 // modifier l'etat du clavier
217 if((size_t) event.key.keysym.scancode < key_states.size())
218 {
219 key_states[event.key.keysym.scancode]= 1;
220 last_key= event.key; // conserver le dernier evenement
221 }
222
223 // fermer l'application
224 if(event.key.keysym.sym == SDLK_ESCAPE)
225 stop= 1;
226 break;
227
228 case SDL_KEYUP:
229 // modifier l'etat du clavier
230 if((size_t) event.key.keysym.scancode < key_states.size())
231 {
232 key_states[event.key.keysym.scancode]= 0;
233 last_key= event.key; // conserver le dernier evenement
234 }
235 break;
236
237 case SDL_MOUSEBUTTONDOWN:
238 case SDL_MOUSEBUTTONUP:
239 last_button= event.button;
240 break;
241
242 case SDL_MOUSEWHEEL:
243 last_wheel= event.wheel;
244 break;
245
246 case SDL_QUIT:
247 stop= 1; // fermer l'application
248 break;
249 }
250 }
251
252 if(resize_event)
253 {
254 glViewport(0, 0, width, height);
255 }
256
257 return 1 - stop;
258}

◆ smart_path()

const char * smart_path ( const char * filename)

renvoie le chemin(path) vers le fichier 'filename' apres l'avoir cherche dans un repertoire standard...

Definition at line 448 of file window.cpp.

449{
450 if(exists(filename))
451 return filename;
452
453 if(path.empty())
454 {
455 // recupere la variable d'environnement, si elle existe
456 const char *envbase= std::getenv("GKIT_BASE_PATH");
457 if(envbase != nullptr)
458 {
459 path= std::string(envbase);
460 if(!path.empty() && path[path.size() -1] != '/')
461 {
462 path.append("/"); // force un /, si necessaire
463 printf("[base path] %s\n", path.c_str());
464 }
465 }
466 }
467
468 if(path.empty())
469 {
470 char *base= SDL_GetBasePath();
471 printf("[base path] %s\n", base);
472 path= base;
473 SDL_free(base);
474 }
475
476 smartpath= path + filename;
477 if(exists(smartpath.c_str()))
478 return smartpath.c_str();
479
480 smartpath= path + "../" + filename;
481 if(exists(smartpath.c_str()))
482 return smartpath.c_str();
483
484 return filename; // echec, fichier pas trouve, renvoie quand meme le fichier original.
485 // (permet au moins d'afficher l'erreur fichier non trouve dans l'application)
486}

◆ drop_event()

const char * drop_event ( )

drag/drop, renvoie le dernier fichier.

Definition at line 93 of file window.cpp.

94{
95 if(last_drops.empty())
96 return nullptr;
97 else
98 return last_drops.back().c_str();
99}

◆ clear_drop_event()

void clear_drop_event ( )

desactive drag/drop.

Definition at line 101 of file window.cpp.

102{
103 last_drops.clear();
104}

◆ drop_events()

const std::vector< std::string > & drop_events ( )

drag/drop. recupere tous les fichiers.

Definition at line 88 of file window.cpp.

89{
90 return last_drops;
91}

◆ clear_drop_events()

void clear_drop_events ( )

desactive drag/drop.

Definition at line 106 of file window.cpp.

107{
108 last_drops.clear();
109}