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().
void window_resize (const bool flag)
 option resize. permet de creer une fenetre de taille fixe, ou pas.
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.
bool window_resize ()
 renvoie vrai si la fenetre peut etre redimensionnee.
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 30 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:456
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:214
int program_print_errors(const GLuint program, const char *filename)
affiche les erreurs de compilation.
Definition program.cpp:459
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)
Definition program.cpp:238

◆ 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:119
void clear_button_event()
desactive l'evenement.
Definition window.cpp:123
SDL_TextInputEvent text_event()
renvoie le dernier evenement. saisie de texte.
Definition window.cpp:88
void clear_key_event()
desactive l'evenement.
Definition window.cpp:81
SDL_KeyboardEvent key_event()
renvoie le dernier evenement. touche speciales.
Definition window.cpp:77
void clear_text_event()
desactive l'evenement.
Definition window.cpp:92
void clear_wheel_event()
desactive l'evenement.
Definition window.cpp:133
SDL_MouseWheelEvent wheel_event()
renvoie le dernier evenement. etat de la molette de la souris / glisser sur le pad.
Definition window.cpp:129

◆ 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 47 of file window.cpp.

48{
49 msaa= samples;
50}

◆ 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 57 of file window.cpp.

58{
59 srgb= flag;
60}

◆ window_resize() [1/2]

void window_resize ( const bool flag)

option resize. permet de creer une fenetre de taille fixe, ou pas.

Definition at line 33 of file window.cpp.

34{
35 resize= flag;
36}

◆ 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 270 of file window.cpp.

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

338{
339 SDL_StopTextInput();
340 SDL_DestroyWindow(window);
341}

◆ 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 367 of file window.cpp.

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

◆ release_context()

void release_context ( Context context)

detruit le contexte openGL.

Definition at line 447 of file window.cpp.

448{
449 SDL_GL_DeleteContext(context);
450}

◆ 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_resize() [2/2]

bool window_resize ( )

renvoie vrai si la fenetre peut etre redimensionnee.

Definition at line 37 of file window.cpp.

38{
39 return resize;
40}

◆ window_msaa() [2/2]

int window_msaa ( )

renvoie le nombre de samples MSAA.

Definition at line 43 of file window.cpp.

44{
45 return msaa;
46}

◆ window_srgb() [2/2]

bool window_srgb ( )

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

Definition at line 53 of file window.cpp.

54{
55 return srgb;
56}

◆ 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 63 of file window.cpp.

64{
65 SDL_Scancode code= SDL_GetScancodeFromKey(key);
66 assert((size_t) code < key_states.size());
67 return (int) key_states[code];
68}

◆ clear_key_state()

void clear_key_state ( const SDL_Keycode key)

desactive une touche du clavier.

Definition at line 69 of file window.cpp.

70{
71 SDL_Scancode code= SDL_GetScancodeFromKey(key);
72 assert((size_t) code < key_states.size());
73 key_states[code]= 0;
74}

◆ key_event()

SDL_KeyboardEvent key_event ( )

renvoie le dernier evenement. touche speciales.

Definition at line 77 of file window.cpp.

78{
79 return last_key;
80}

◆ clear_key_event()

void clear_key_event ( )

desactive l'evenement.

Definition at line 81 of file window.cpp.

82{
83 last_key.type= 0;
84 last_key.keysym.sym= 0;
85}

◆ button_event()

SDL_MouseButtonEvent button_event ( )

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

Definition at line 119 of file window.cpp.

120{
121 return last_button;
122}

◆ clear_button_event()

void clear_button_event ( )

desactive l'evenement.

Definition at line 123 of file window.cpp.

124{
125 last_button.state= 0;
126}

◆ wheel_event()

SDL_MouseWheelEvent wheel_event ( )

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

Definition at line 129 of file window.cpp.

130{
131 return last_wheel;
132}

◆ clear_wheel_event()

void clear_wheel_event ( )

desactive l'evenement.

Definition at line 133 of file window.cpp.

134{
135 last_wheel.x= 0;
136 last_wheel.y= 0;
137}

◆ text_event()

SDL_TextInputEvent text_event ( )

renvoie le dernier evenement. saisie de texte.

Definition at line 88 of file window.cpp.

89{
90 return last_text;
91}

◆ clear_text_event()

void clear_text_event ( )

desactive l'evenement.

Definition at line 92 of file window.cpp.

93{
94 last_text.text[0]= 0;
95}

◆ global_time()

float global_time ( )

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

Definition at line 145 of file window.cpp.

146{
147 std::chrono::high_resolution_clock::time_point now= std::chrono::high_resolution_clock::now();
148 last_delta= float(std::chrono::duration_cast<std::chrono::microseconds>(now - last_time).count()) / float(1000);
149 last_time= now;
150
151 return float(std::chrono::duration_cast<std::chrono::microseconds>(now - app_start).count()) / float(1000);
152}

◆ delta_time()

float delta_time ( )

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

Definition at line 154 of file window.cpp.

155{
156// pas super utile, a virer ?
157 return last_delta;
158}

◆ 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 164 of file window.cpp.

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

◆ last_event_count()

int last_event_count ( )

Definition at line 184 of file window.cpp.

185{
186 return event_count;
187}

◆ events()

int events ( Window window)

fonction interne de gestion d'evenements.

Definition at line 189 of file window.cpp.

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

◆ 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 456 of file window.cpp.

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

◆ drop_event()

const char * drop_event ( )

drag/drop, renvoie le dernier fichier.

Definition at line 102 of file window.cpp.

103{
104 if(last_drops.empty())
105 return nullptr;
106 else
107 return last_drops.back().c_str();
108}

◆ clear_drop_event()

void clear_drop_event ( )

desactive drag/drop.

Definition at line 109 of file window.cpp.

110{
111 last_drops.clear();
112}

◆ drop_events()

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

drag/drop. recupere tous les fichiers.

Definition at line 98 of file window.cpp.

99{
100 return last_drops;
101}

◆ clear_drop_events()

void clear_drop_events ( )

desactive drag/drop.

Definition at line 113 of file window.cpp.

114{
115 last_drops.clear();
116}