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().
Window create_window (const int width, const int height, const int major=3, const int minor=2, const int samples=0)
 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.
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 24 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:413
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: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
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:225

◆ 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:100
void clear_button_event()
desactive l'evenement.
Definition window.cpp:104
SDL_TextInputEvent text_event()
renvoie le dernier evenement. saisie de texte.
Definition window.cpp:65
void clear_key_event()
desactive l'evenement.
Definition window.cpp:58
SDL_KeyboardEvent key_event()
renvoie le dernier evenement. touche speciales.
Definition window.cpp:54
void clear_text_event()
desactive l'evenement.
Definition window.cpp:69
void clear_wheel_event()
desactive l'evenement.
Definition window.cpp:114
SDL_MouseWheelEvent wheel_event()
renvoie le dernier evenement. etat de la molette de la souris / glisser sur le pad.
Definition window.cpp:110

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

◆ create_window()

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

creation d'une fenetre pour l'application.

Definition at line 249 of file window.cpp.

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

315{
316 SDL_StopTextInput();
317 SDL_DestroyWindow(window);
318}

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

345{
346 if(window == nullptr)
347 return nullptr;
348
349 Context context= SDL_GL_CreateContext(window);
350 if(context == nullptr)
351 {
352 printf("[error] creating openGL context.\n");
353 return nullptr;
354 }
355
356 if(SDL_GL_SetSwapInterval(-1) != 0)
357 printf("[warning] can't set adaptive vsync...\n");
358
359 if(SDL_GL_GetSwapInterval() != -1)
360 {
361 printf("vsync ON\n");
362 SDL_GL_SetSwapInterval(1);
363 }
364 else
365 printf("adaptive vsync ON\n");
366
367 {
368 int n= 0;
369 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &n);
370 if(n > 1)
371 printf("MSAA %d samples\n", n);
372 }
373
374 //
375 app_start= std::chrono::high_resolution_clock::now();
376
377#ifndef NO_GLAD
378 // initialise les extensions opengl
379 gladLoadGL();
380
381 // purge les erreurs opengl generees par glew !
382 while(glGetError() != GL_NO_ERROR) {;}
383
384#ifndef GK_RELEASE
385 // configure l'affichage des messages d'erreurs opengl, si l'extension est disponible
386 // inclut dans openGL 4.3, mais pas dispo sur mac...
387 if(GLAD_GL_ARB_debug_output)
388 {
389 printf("debug output enabled...\n");
390 // selectionne tous les messages
391 glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
392 // desactive les messages du compilateur de shaders
393 glDebugMessageControlARB(GL_DEBUG_SOURCE_SHADER_COMPILER, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE);
394
395 glDebugMessageCallbackARB(debug_print, NULL);
396 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
397 }
398#endif
399#endif
400
401 return context;
402}

◆ release_context()

void release_context ( Context context)

detruit le contexte openGL.

Definition at line 404 of file window.cpp.

405{
406 SDL_GL_DeleteContext(context);
407}

◆ 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()

int window_msaa ( )

renvoie le nombre de samples MSAA.

Definition at line 32 of file window.cpp.

33{
34 int n= 0;
35 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &n);
36 return n;
37}

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

41{
42 SDL_Scancode code= SDL_GetScancodeFromKey(key);
43 assert((size_t) code < key_states.size());
44 return (int) key_states[code];
45}

◆ clear_key_state()

void clear_key_state ( const SDL_Keycode key)

desactive une touche du clavier.

Definition at line 46 of file window.cpp.

47{
48 SDL_Scancode code= SDL_GetScancodeFromKey(key);
49 assert((size_t) code < key_states.size());
50 key_states[code]= 0;
51}

◆ key_event()

SDL_KeyboardEvent key_event ( )

renvoie le dernier evenement. touche speciales.

Definition at line 54 of file window.cpp.

55{
56 return last_key;
57}

◆ clear_key_event()

void clear_key_event ( )

desactive l'evenement.

Definition at line 58 of file window.cpp.

59{
60 last_key.type= 0;
61 last_key.keysym.sym= 0;
62}

◆ button_event()

SDL_MouseButtonEvent button_event ( )

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

Definition at line 100 of file window.cpp.

101{
102 return last_button;
103}

◆ clear_button_event()

void clear_button_event ( )

desactive l'evenement.

Definition at line 104 of file window.cpp.

105{
106 last_button.state= 0;
107}

◆ wheel_event()

SDL_MouseWheelEvent wheel_event ( )

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

Definition at line 110 of file window.cpp.

111{
112 return last_wheel;
113}

◆ clear_wheel_event()

void clear_wheel_event ( )

desactive l'evenement.

Definition at line 114 of file window.cpp.

115{
116 last_wheel.x= 0;
117 last_wheel.y= 0;
118}

◆ text_event()

SDL_TextInputEvent text_event ( )

renvoie le dernier evenement. saisie de texte.

Definition at line 65 of file window.cpp.

66{
67 return last_text;
68}

◆ clear_text_event()

void clear_text_event ( )

desactive l'evenement.

Definition at line 69 of file window.cpp.

70{
71 last_text.text[0]= 0;
72}

◆ global_time()

float global_time ( )

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

Definition at line 126 of file window.cpp.

127{
128 std::chrono::high_resolution_clock::time_point now= std::chrono::high_resolution_clock::now();
129 last_delta= float(std::chrono::duration_cast<std::chrono::microseconds>(now - last_time).count()) / float(1000);
130 last_time= now;
131
132 return float(std::chrono::duration_cast<std::chrono::microseconds>(now - app_start).count()) / float(1000);
133}

◆ delta_time()

float delta_time ( )

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

Definition at line 135 of file window.cpp.

136{
137// pas super utile, a virer ?
138 return last_delta;
139}

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

146{
147 // configure openGL
148 glViewport(0, 0, width, height);
149
150 // run
151 while(events(window))
152 {
153 // dessiner
154 if(draw() < 1)
155 stop= 1; // fermer l'application si draw() renvoie 0 ou -1...
156
157 // presenter le resultat
158 SDL_GL_SwapWindow(window);
159 }
160
161 return 0;
162}
int events(Window window)
fonction interne de gestion d'evenements.
Definition window.cpp:168

◆ last_event_count()

int last_event_count ( )

Definition at line 165 of file window.cpp.

165{ return event_count; }

◆ events()

int events ( Window window)

fonction interne de gestion d'evenements.

Definition at line 168 of file window.cpp.

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

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

414{
415 if(exists(filename))
416 return filename;
417
418 if(path.empty())
419 {
420 // recupere la variable d'environnement, si elle existe
421 const char *envbase= std::getenv("GKIT_BASE_PATH");
422 if(envbase != nullptr)
423 {
424 path= std::string(envbase);
425 if(!path.empty() && path[path.size() -1] != '/')
426 {
427 path.append("/"); // force un /, si necessaire
428 printf("[base path] %s\n", path.c_str());
429 }
430 }
431 }
432
433 if(path.empty())
434 {
435 char *base= SDL_GetBasePath();
436 printf("[base path] %s\n", base);
437 path= base;
438 SDL_free(base);
439 }
440
441 smartpath= path + filename;
442 if(exists(smartpath.c_str()))
443 return smartpath.c_str();
444
445 smartpath= path + "../" + filename;
446 if(exists(smartpath.c_str()))
447 return smartpath.c_str();
448
449 return filename; // echec, fichier pas trouve, renvoie quand meme le fichier original.
450 // (permet au moins d'afficher l'erreur fichier non trouve dans l'application)
451}

◆ drop_event()

const char * drop_event ( )

drag/drop, renvoie le dernier fichier.

Definition at line 80 of file window.cpp.

81{
82 if(last_drops.empty())
83 return nullptr;
84 else
85 return last_drops.back().c_str();
86}

◆ clear_drop_event()

void clear_drop_event ( )

desactive drag/drop.

Definition at line 88 of file window.cpp.

89{
90 last_drops.clear();
91}

◆ drop_events()

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

drag/drop. recupere tous les fichiers.

Definition at line 75 of file window.cpp.

76{
77 return last_drops;
78}

◆ clear_drop_events()

void clear_drop_events ( )

desactive drag/drop.

Definition at line 93 of file window.cpp.

94{
95 last_drops.clear();
96}