gKit2 light
Files | Classes | Typedefs | Functions
utilitaires pour creer une application

Files

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

Detailed Description

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
void clear(Text &text)
efface le contenu de la console.
Definition: text.cpp:72
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:431
Color White()
utilitaire. renvoie une couleur blanche.
Definition: color.cpp:36
Image read_image(const char *filename)
Definition: image_io.cpp:18
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:25
GLuint read_program(const char *filename, const char *definitions)
Definition: program.cpp:204
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition: program.cpp:432
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:211

◆ 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 }
void print(Text &text, const int px, const int py, const char *message)
affiche un texte a la position x, y.
Definition: text.cpp:122

◆ 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_uniform(const GLuint program, const char *uniform, const std::vector< unsigned > &v)
affecte un tableau de valeurs a un uniform du shader program.
Definition: uniforms.cpp:94
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;
15  w.console= create_text();
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
int wy
scroll
Definition: widgets.h:78
int fy
position du focus
Definition: widgets.h:74
Text console
affichage des elements de l'interface.
Definition: widgets.h:70
int mb
click
Definition: widgets.h:76
int my
position du click
Definition: widgets.h:77
int focus
focus
Definition: widgets.h:73
int py
placement du prochain widget
Definition: widgets.h:71

◆ 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:102
void clear_button_event()
desactive l'evenement.
Definition: window.cpp:106
SDL_TextInputEvent text_event()
renvoie le dernier evenement. saisie de texte.
Definition: window.cpp:67
void clear_key_event()
desactive l'evenement.
Definition: window.cpp:60
SDL_KeyboardEvent key_event()
renvoie le dernier evenement. touche speciales.
Definition: window.cpp:56
void clear_text_event()
desactive l'evenement.
Definition: window.cpp:71
void clear_wheel_event()
desactive l'evenement.
Definition: window.cpp:116
SDL_MouseWheelEvent wheel_event()
renvoie le dernier evenement. etat de la molette de la souris / glisser sur le pad.
Definition: window.cpp:112

◆ 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 }
Definition: widgets.cpp:93

◆ 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)
288  begin_line= begin_line - 1;
289  if(w.key == SDLK_DOWN)
290  begin_line= begin_line + 1;
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
int key
touche
Definition: widgets.h:80

◆ 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);
draw(widgets, window_width(), window_height());
}
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:29
bool edit(Widgets &w, int text_size, char *text)
Definition: widgets.cpp:319
void end(Widgets &w)
termine la description des elements de l'interface graphique.
Definition: widgets.cpp:404
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition: window.cpp:25

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
Point min(const Point &a, const Point &b)
renvoie la plus petite composante de chaque point. x, y, z= min(a.x, b.x), min(a.y,...
Definition: vec.cpp:30

◆ 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 }
void label(Widgets &w, const char *format,...)
cree un texte. meme fonctionnement que printf().
Definition: widgets.cpp:142
bool value(Widgets &w, const char *label, int &value, const int value_min, const int value_max, const int value_step)
valeur editable par increment.
Definition: widgets.cpp:191

◆ 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 }
void draw(Widgets &w, const int width, const int height)
affiche les elements decrits entre begin() et end().
Definition: widgets.cpp:414

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

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

326 {
327  SDL_StopTextInput();
328  SDL_DestroyWindow(window);
329 }

◆ create_context()

Context create_context ( Window  window)

cree et configure un contexte opengl.

cree et configure un contexte opengl.

Definition at line 356 of file window.cpp.

357 {
358  if(window == nullptr)
359  return nullptr;
360 
361  Context context= SDL_GL_CreateContext(window);
362  if(context == nullptr)
363  {
364  printf("[error] creating openGL context.\n");
365  return nullptr;
366  }
367 
368  if(SDL_GL_SetSwapInterval(-1) != 0)
369  printf("[warning] can't set adaptive vsync...\n");
370 
371  if(SDL_GL_GetSwapInterval() != -1)
372  {
373  printf("vsync ON\n");
374  SDL_GL_SetSwapInterval(1);
375  }
376  else
377  printf("adaptive vsync ON\n");
378 
379  {
380  int n= 0;
381  SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &n);
382  if(n > 1)
383  printf("MSAA %d samples\n", n);
384  }
385 
386  //
387  app_start= std::chrono::high_resolution_clock::now();
388 
389 #ifndef NO_GLEW
390  // initialise les extensions opengl
391  glewExperimental= 1;
392  GLenum err= glewInit();
393  if(err != GLEW_OK)
394  {
395  printf("[error] loading extensions\n%s\n", glewGetErrorString(err));
396  SDL_GL_DeleteContext(context);
397  return nullptr;
398  }
399 
400  // purge les erreurs opengl generees par glew !
401  while(glGetError() != GL_NO_ERROR) {;}
402 
403 #ifndef GK_RELEASE
404  // configure l'affichage des messages d'erreurs opengl, si l'extension est disponible
405  if(GLEW_ARB_debug_output)
406  {
407  printf("debug output enabled...\n");
408  // selectionne tous les messages
409  glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
410  // desactive les messages du compilateur de shaders
411  glDebugMessageControlARB(GL_DEBUG_SOURCE_SHADER_COMPILER, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE);
412 
413  glDebugMessageCallbackARB(debug_print, NULL);
414  glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
415  }
416 #endif
417 #endif
418 
419  return context;
420 }

◆ release_context()

void release_context ( Context  context)

detruit le contexte openGL.

Definition at line 422 of file window.cpp.

423 {
424  SDL_GL_DeleteContext(context);
425 }

◆ window_width()

int window_width ( )

renvoie la largeur de la fenetre de l'application.

Definition at line 25 of file window.cpp.

26 {
27  return width;
28 }

◆ window_height()

int window_height ( )

renvoie la hauteur de la fenetre de l'application.

Definition at line 29 of file window.cpp.

30 {
31  return height;
32 }

◆ window_msaa()

int window_msaa ( )

renvoie le nombre de samples MSAA.

Definition at line 34 of file window.cpp.

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

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

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

◆ clear_key_state()

void clear_key_state ( const SDL_Keycode  key)

desactive une touche du clavier.

Definition at line 48 of file window.cpp.

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

◆ key_event()

SDL_KeyboardEvent key_event ( )

renvoie le dernier evenement. touche speciales.

Definition at line 56 of file window.cpp.

57 {
58  return last_key;
59 }

◆ clear_key_event()

void clear_key_event ( )

desactive l'evenement.

Definition at line 60 of file window.cpp.

61 {
62  last_key.type= 0;
63  last_key.keysym.sym= 0;
64 }

◆ button_event()

SDL_MouseButtonEvent button_event ( )

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

Definition at line 102 of file window.cpp.

103 {
104  return last_button;
105 }

◆ clear_button_event()

void clear_button_event ( )

desactive l'evenement.

Definition at line 106 of file window.cpp.

107 {
108  last_button.state= 0;
109 }

◆ wheel_event()

SDL_MouseWheelEvent wheel_event ( )

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

Definition at line 112 of file window.cpp.

113 {
114  return last_wheel;
115 }

◆ clear_wheel_event()

void clear_wheel_event ( )

desactive l'evenement.

Definition at line 116 of file window.cpp.

117 {
118  last_wheel.x= 0;
119  last_wheel.y= 0;
120 }

◆ text_event()

SDL_TextInputEvent text_event ( )

renvoie le dernier evenement. saisie de texte.

Definition at line 67 of file window.cpp.

68 {
69  return last_text;
70 }

◆ clear_text_event()

void clear_text_event ( )

desactive l'evenement.

Definition at line 71 of file window.cpp.

72 {
73  last_text.text[0]= 0;
74 }

◆ global_time()

float global_time ( )

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

Definition at line 128 of file window.cpp.

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

◆ delta_time()

float delta_time ( )

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

Definition at line 137 of file window.cpp.

138 {
139 // pas super utile, a virer ?
140  return last_delta;
141 }

◆ run()

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

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

◆ events()

int events ( Window  window)

fonction interne de gestion d'evenements.

Definition at line 170 of file window.cpp.

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

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

432 {
433  if(exists(filename))
434  return filename;
435 
436  if(path.empty())
437  {
438  // recupere la variable d'environnement, si elle existe
439  const char *envbase= std::getenv("GKIT_BASE_PATH");
440  if(envbase != nullptr)
441  {
442  path= std::string(envbase);
443  if(!path.empty() && path[path.size() -1] != '/')
444  {
445  path.append("/"); // force un /, si necessaire
446  printf("[base path] %s\n", path.c_str());
447  }
448  }
449  }
450 
451  if(path.empty())
452  {
453  char *base= SDL_GetBasePath();
454  printf("[base path] %s\n", base);
455  path= base;
456  SDL_free(base);
457  }
458 
459  smartpath= path + filename;
460  if(exists(smartpath.c_str()))
461  return smartpath.c_str();
462 
463  smartpath= path + "../" + filename;
464  if(exists(smartpath.c_str()))
465  return smartpath.c_str();
466 
467  return filename; // echec, fichier pas trouve, renvoie quand meme le fichier original.
468  // (permet au moins d'afficher l'erreur fichier non trouve dans l'application)
469 }

◆ drop_event()

const char* drop_event ( )

drag/drop, renvoie le dernier fichier.

Definition at line 82 of file window.cpp.

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

◆ clear_drop_event()

void clear_drop_event ( )

desactive drag/drop.

Definition at line 90 of file window.cpp.

91 {
92  last_drops.clear();
93 }

◆ drop_events()

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

drag/drop. recupere tous les fichiers.

Definition at line 77 of file window.cpp.

78 {
79  return last_drops;
80 }

◆ clear_drop_events()

void clear_drop_events ( )

desactive drag/drop.

Definition at line 95 of file window.cpp.

96 {
97  last_drops.clear();
98 }