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

Files

file  app.h
 
file  text.h
 
file  widgets.h
 
file  window.h
 

Classes

class  App
 classe application. More...
 
struct  Text
 
struct  Widgets
 

Typedefs

typedef SDL_Window * Window
 
typedef SDL_GLContext Context
 

Functions

 App::App (const int width, const int height, const int major=3, const int minor=3)
 constructeur, dimensions de la fenetre et version d'openGL. More...
 
virtual int App::init ()=0
 a deriver pour creer les objets openGL. More...
 
virtual int App::quit ()=0
 a deriver pour detruire les objets openGL. More...
 
virtual int App::update (const float time, const float delta)
 a deriver et redefinir pour animer les objets en fonction du temps. More...
 
virtual int App::render ()=0
 a deriver pour afficher les objets. More...
 
int App::run ()
 execution de l'application. More...
 
float App::global_time ()
 renvoie le temps ecoule depuis le lancement de l'application, en millisecondes. More...
 
float App::delta_time ()
 renvoie le temps ecoule depuis la derniere frame, en millisecondes. More...
 
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...
 
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)
 creation d'une fenetre pour l'application. More...
 
void release_window (Window w)
 destruction de la fenetre. More...
 
Context create_context (Window window, const int major=3, const int minor=2)
 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 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...
 
int run (Window window, int(*draw)(void))
 fonction principale. gestion des evenements et appel de la fonction draw de l'application. More...
 
int events (Window window)
 fonction interne de gestion d'evenements. More...
 

Variables

Window App::m_window
 
Context App::m_context
 
unsigned int App::m_time
 
unsigned int App::m_delta
 

Detailed Description

Function Documentation

App::App ( const int  width,
const int  height,
const int  major = 3,
const int  minor = 3 
)

constructeur, dimensions de la fenetre et version d'openGL.

Definition at line 6 of file app.cpp.

7  : m_window(nullptr), m_context(nullptr)
8 {
9  m_window= create_window(width, height);
10  m_context= create_context(m_window, major, minor);
11 }
Context create_context(Window window, const int major, const int minor)
cree et configure un contexte opengl
Definition: window.cpp:252
Window create_window(const int w, const int h)
creation d'une fenetre pour l'application.
Definition: window.cpp:186
virtual int App::init ( )
pure virtual

a deriver pour creer les objets openGL.

Implemented in TP, TP, DrawInstanceBuffer, Framebuffer, TP, DrawInstanceID, TP, and TP.

virtual int App::quit ( )
pure virtual

a deriver pour detruire les objets openGL.

Implemented in Framebuffer, DrawInstanceBuffer, TP, DrawInstanceID, TP, TP, TP, and TP.

virtual int App::update ( const float  time,
const float  delta 
)
inlinevirtual

a deriver et redefinir pour animer les objets en fonction du temps.

Reimplemented in Framebuffer, DrawInstanceBuffer, TP, TP, DrawInstanceID, TP, and TP.

Definition at line 33 of file app.h.

33 { return 0; }
virtual int App::render ( )
pure virtual

a deriver pour afficher les objets.

Implemented in Framebuffer, DrawInstanceBuffer, TP, TP, DrawInstanceID, TP, TP, and TP.

int App::run ( )

execution de l'application.

Definition at line 21 of file app.cpp.

22 {
23  if(init() < 0)
24  return -1;
25 
26  // configure openGL
27  glViewport(0, 0, window_width(), window_height());
28 
29  // utiliser SDL_GetPerformanceCounter() / SDL_GetPerformanceFrequency() si la precision n'est pas suffisante
30  m_time= SDL_GetTicks();
31  while(events(m_window))
32  {
33  m_delta= SDL_GetTicks() - m_time;
34  if(update(global_time(), delta_time()) < 0)
35  break;
36  if(render() < 1)
37  break;
38 
39  m_time= SDL_GetTicks();
40 
41  // presenter le resultat
42  SDL_GL_SwapWindow(m_window);
43  }
44 
45  if(quit() < 0)
46  return -1;
47  return 0;
48 }
virtual int quit()=0
a deriver pour detruire les objets openGL.
int events(Window window)
fonction interne de gestion d'evenements.
Definition: window.cpp:112
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.
Definition: app.cpp:50
int window_width()
renvoie la largeur de la fenetre de l'application.
Definition: window.cpp:14
virtual int init()=0
a deriver pour creer les objets openGL.
virtual int update(const float time, const float delta)
a deriver et redefinir pour animer les objets en fonction du temps.
Definition: app.h:33
int window_height()
renvoie la hauteur de la fenetre de l'application.
Definition: window.cpp:18
virtual int render()=0
a deriver pour afficher les objets.
float delta_time()
renvoie le temps ecoule depuis la derniere frame, en millisecondes.
Definition: app.cpp:55
float App::global_time ( )

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

Definition at line 50 of file app.cpp.

51 {
52  return (float) m_time;
53 }
float App::delta_time ( )

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

Definition at line 55 of file app.cpp.

56 {
57  return (float) m_delta;
58 }
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("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("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_STATIC_DRAW);
60 
61  return text;
62 }
representation d'une couleur (rgba) transparente ou opaque.
Definition: color.h:13
GLuint make_texture(const int unit, const Image &im, const GLenum texel_type)
Definition: texture.cpp:25
GLuint program
shader pour afficher le texte.
Definition: text.h:68
Color White()
utilitaire. renvoie une couleur blanche.
Definition: color.cpp:10
int program_print_errors(const GLuint program)
affiche les erreurs de compilation.
Definition: program.cpp:330
Color color
couleur du texte.
Definition: text.h:66
GLuint read_program(const char *filename, const char *definitions)
Definition: program.cpp:150
Definition: text.h:61
GLuint font
texture contenant les caracteres.
Definition: text.h:67
Image read_image(const char *filename)
Definition: image_io.cpp:16
representation d'une image.
Definition: image.h:18
void clear(Text &text)
efface le contenu de la console.
Definition: text.cpp:72
GLuint ubo
uniform buffer object, pour transferrer le texte a afficher
Definition: text.h:70
GLuint vao
vertex array object.
Definition: text.h:69
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 }
GLuint program
shader pour afficher le texte.
Definition: text.h:68
GLuint font
texture contenant les caracteres.
Definition: text.h:67
int release_program(const GLuint program)
detruit les shaders et le program.
Definition: program.cpp:157
GLuint ubo
uniform buffer object, pour transferrer le texte a afficher
Definition: text.h:70
GLuint vao
vertex array object.
Definition: text.h:69
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 }
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 }
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
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 }
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
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 }
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
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 }
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
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 }
Color color
couleur du texte.
Definition: text.h:66
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 unsigned int v)
affecte une valeur a un uniform du shader program. uint.
Definition: uniforms.cpp:68
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 filtrages...
Definition: uniforms.cpp:118
GLuint program
shader pour afficher le texte.
Definition: text.h:68
Color color
couleur du texte.
Definition: text.h:66
GLuint font
texture contenant les caracteres.
Definition: text.h:67
GLuint ubo
uniform buffer object, pour transferrer le texte a afficher
Definition: text.h:70
GLuint vao
vertex array object.
Definition: text.h:69
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 }
int focus
focus
Definition: widgets.h:73
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
int py
placement du prochain widget
Definition: widgets.h:71
int fy
position du focus
Definition: widgets.h:74
int mb
click
Definition: widgets.h:76
int wy
scroll
Definition: widgets.h:78
int my
position du click
Definition: widgets.h:77
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
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_TextInputEvent text_event()
renvoie le dernier evenement. saisie de texte.
Definition: window.cpp:49
SDL_MouseButtonEvent button_event()
renvoie le dernier evenement. etat des boutons de la souris.
Definition: window.cpp:69
void clear_wheel_event()
desactive l'evenement.
Definition: window.cpp:83
void clear_key_event()
desactive l'evenement.
Definition: window.cpp:42
SDL_MouseWheelEvent wheel_event()
renvoie le dernier evenement. etat de la molette de la souris / glisser sur le pad.
Definition: window.cpp:79
void clear(Text &text)
efface le contenu de la console.
Definition: text.cpp:72
void clear_button_event()
desactive l'evenement.
Definition: window.cpp:73
void clear_text_event()
desactive l'evenement.
Definition: window.cpp:53
SDL_KeyboardEvent key_event()
renvoie le dernier evenement. touche speciales.
Definition: window.cpp:38
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 }
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:92
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
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 }
Definition: widgets.cpp:92
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
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 }
Definition: widgets.cpp:92
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
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 232 of file widgets.cpp.

233 {
234  Rect r= place(w, 128, height);
235 
236  if(overlap(r, w.mx, w.my))
237  {
238  if(w.wy != 0)
239  begin_line= begin_line - w.wy;
240  if(w.key == SDLK_PAGEUP)
241  begin_line= begin_line - height;
242  if(w.key == SDLK_PAGEDOWN)
243  begin_line= begin_line + height;
244  if(w.key == SDLK_SPACE)
245  begin_line= begin_line + height / 2;
246  if(w.key == SDLK_UP)
247  begin_line= begin_line - 1;
248  if(w.key == SDLK_DOWN)
249  begin_line= begin_line + 1;
250  }
251 
252  // compter les lignes
253  int n= 1;
254  for(int i= 0; text[i] != 0; i++)
255  if(text[i] == '\n')
256  n++;
257  // ligne de depart, pout que tout le texte reste affiche
258  if(begin_line + height > n)
259  begin_line= n - height;
260  if(begin_line < 1)
261  begin_line= 1;
262  // retrouve le debut de la ligne
263  int line= 1;
264  int offset= 0;
265  for(int i= 0; text[i] != 0; i++)
266  {
267  if(text[i] == '\n')
268  {
269  line++;
270  if(line == begin_line)
271  offset= i;
272  }
273  }
274  // affiche le texte
275  print(w.console, r.x, r.y, text + offset);
276 }
Text console
affichage des elements de l'interface.
Definition: widgets.h:70
int key
touche
Definition: widgets.h:80
int wy
scroll
Definition: widgets.h:78
void begin_line(Widgets &w)
place les prochains elements sur une nouvelle ligne.
Definition: widgets.cpp:129
int my
position du click
Definition: widgets.h:77
Definition: widgets.cpp:92
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
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);
}

Definition at line 278 of file widgets.cpp.

279 {
280  assert(text_size > 1);
281  int size= std::min((int) strlen(text), text_size -2);
282  Rect r= place(w, text_size -1);
283 
284  // focus
285  bool change= false;
286  if(w.mb > 0)
287  {
288  if(overlap(r, w.mx, w.my))
289  {
290  w.focus= 1;
291  w.fx= w.mx;
292  w.fy= w.my;
293 
294  if(w.fx >= r.x + size)
295  w.fx= r.x + size;
296  }
297 
298  else
299  change= (w.focus > 0); // click en dehors de la zone editable
300  }
301 
302  // edition
303  bool focus= overlap(r, w.fx, w.fy);
304  if(focus && w.key > 0)
305  {
306  int c= w.fx - r.x;
307  assert(c < text_size -1);
308 
309  if(w.key == SDLK_BACKSPACE)
310  {
311  w.fx--; // curseur a gauche
312  for(int i= c -1; i >= 0 && i+1 < text_size; i++) text[i]= text[i+1];
313  }
314  else if(w.key == SDLK_DELETE)
315  {
316  // curseur ne bouge pas
317  for(int i= c; i+1 < text_size; i++) text[i]= text[i+1];
318  }
319  else if(w.key == SDLK_LEFT)
320  {
321  w.fx--; // curseur a gauche
322  }
323  else if(w.key == SDLK_RIGHT)
324  {
325  w.fx++; // curseur a droite
326  }
327  else if(w.key == SDLK_RETURN)
328  {
329  w.focus= 0;
330  change= true;
331  }
332  else
333  {
334  w.fx++; // curseur a droite
335  for(int i= text_size -1; i > c; i--) text[i]= text[i -1];
336  text[c]= w.key;
337 
338  if(size < text_size - 2)
339  size++;
340  text[size+1]= 0;
341  }
342 
343  // verifier que le curseur reste dans la zone editable
344  if(w.fx < r.x)
345  w.fx= r.x;
346  if(w.fx >= r.x + size)
347  w.fx= r.x + size;
348  }
349 
350  int i= 0;
351  char tmp[128];
352  for(; text[i] != 0; i++) tmp[i]= text[i]; // copie les caracteres
353  for(; i < text_size; i++) tmp[i]= ' '; // complete avec des espaces
354  tmp[text_size -1]= 0; // termine avec un 0
355 
356  print_background(w.console, r.x, r.y, tmp);
357  if(focus)
358  print_background(w.console, w.fx, w.fy, text[w.fx - r.x], 1);
359 
360  return change;
361 }
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
Definition: widgets.cpp:92
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 }
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
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
Definition: widgets.cpp:92
void label(Widgets &w, const char *format,...)
cree un texte. meme fonctionnement que printf().
Definition: widgets.cpp:142
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
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 }
void end ( Widgets widgets)

termine la description des elements de l'interface graphique.

Definition at line 363 of file widgets.cpp.

364 {
365  return;
366 }
void default_color ( Widgets widgets,
const Color color 
)

choisit une couleur par defaut pour le texte.

Definition at line 368 of file widgets.cpp.

369 {
370  default_color(w.console, color);
371 }
void default_color(Widgets &w, const Color &color)
choisit une couleur par defaut pour le texte.
Definition: widgets.cpp:368
void draw ( Widgets widgets,
const int  width,
const int  height 
)

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

Definition at line 373 of file widgets.cpp.

374 {
375  draw(w.console, width, height);
376 }
void draw(Widgets &w, const int width, const int height)
affiche les elements decrits entre begin() et end().
Definition: widgets.cpp:373
Window create_window ( const int  width,
const int  height 
)

creation d'une fenetre pour l'application.

Definition at line 186 of file window.cpp.

187 {
188  // init sdl
189  if(SDL_Init(SDL_INIT_EVERYTHING | SDL_INIT_NOPARACHUTE ) < 0)
190  {
191  printf("[error] SDL_Init() failed:\n%s\n", SDL_GetError());
192  return NULL;
193  }
194 
195  // enregistre le destructeur de sdl
196  atexit(SDL_Quit);
197 
198  // creer la fenetre
199  Window window= SDL_CreateWindow("gKit",
200  SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, w, h,
201  SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE);
202  if(window == NULL)
203  {
204  printf("[error] SDL_CreateWindow() failed.\n");
205  return NULL;
206  }
207 
208  // recupere l'etat du clavier
209  int keys;
210  const unsigned char *state= SDL_GetKeyboardState(&keys);
211  key_states.assign(state, state + keys);
212 
213  SDL_SetWindowDisplayMode(window, NULL);
214  SDL_StartTextInput();
215 
216  // conserve les dimensions de la fenetre
217  SDL_GetWindowSize(window, &width, &height);
218 
219  return window;
220 }
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
void release_window ( Window  w)

destruction de la fenetre.

Definition at line 222 of file window.cpp.

223 {
224  SDL_StopTextInput();
225  SDL_DestroyWindow(window);
226 }
Context create_context ( Window  window,
const int  major,
const int  minor 
)

cree et configure un contexte opengl.

cree et configure un contexte opengl.

Definition at line 252 of file window.cpp.

253 {
254  if(window == NULL)
255  return NULL;
256 
257  // configure la creation du contexte opengl core profile, debug profile
258  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
259  SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
260 #ifndef GK_RELEASE
261  SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
262 #endif
263  SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
264 
265  SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 15);
266  SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
267 
268  Context context= SDL_GL_CreateContext(window);
269  if(context == NULL)
270  {
271  printf("[error] creating openGL context.\n");
272  return NULL;
273  }
274 
275  SDL_GL_SetSwapInterval(1);
276 
277 #ifndef NO_GLEW
278  // initialise les extensions opengl
279  glewExperimental= 1;
280  GLenum err= glewInit();
281  if(err != GLEW_OK)
282  {
283  printf("[error] loading extensions\n%s\n", glewGetErrorString(err));
284  SDL_GL_DeleteContext(context);
285  return NULL;
286  }
287 
288  // purge les erreurs opengl generees par glew !
289  while(glGetError() != GL_NO_ERROR) {;}
290 
291 #ifndef GK_RELEASE
292  // configure l'affichage des messages d'erreurs opengl, si l'extension est disponible
293  if(GLEW_ARB_debug_output)
294  {
295  printf("debug output enabled...\n");
296  glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
297 // glDebugMessageCallbackARB(debug, NULL);
298  glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
299  }
300 #endif
301 #endif
302 
303  return context;
304 }
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
void release_context ( Context  context)

detruit le contexte openGL.

Definition at line 306 of file window.cpp.

307 {
308  SDL_GL_DeleteContext(context);
309 }
int window_width ( )

renvoie la largeur de la fenetre de l'application.

Definition at line 14 of file window.cpp.

15 {
16  return width;
17 }
int window_height ( )

renvoie la hauteur de la fenetre de l'application.

Definition at line 18 of file window.cpp.

19 {
20  return height;
21 }
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 24 of file window.cpp.

25 {
26  SDL_Scancode code= SDL_GetScancodeFromKey(key);
27  assert((size_t) code < key_states.size());
28  return (int) key_states[code];
29 }
void clear_key_state ( const SDL_Keycode  key)

desactive une touche du clavier.

Definition at line 30 of file window.cpp.

31 {
32  SDL_Scancode code= SDL_GetScancodeFromKey(key);
33  assert((size_t) code < key_states.size());
34  key_states[code]= 0;
35 }
SDL_KeyboardEvent key_event ( )

renvoie le dernier evenement. touche speciales.

Definition at line 38 of file window.cpp.

39 {
40  return last_key;
41 }
void clear_key_event ( )

desactive l'evenement.

Definition at line 42 of file window.cpp.

43 {
44  last_key.type= 0;
45  last_key.keysym.sym= 0;
46 }
SDL_MouseButtonEvent button_event ( )

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

Definition at line 69 of file window.cpp.

70 {
71  return last_button;
72 }
void clear_button_event ( )

desactive l'evenement.

Definition at line 73 of file window.cpp.

74 {
75  last_button.state= 0;
76 }
SDL_MouseWheelEvent wheel_event ( )

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

Definition at line 79 of file window.cpp.

80 {
81  return last_wheel;
82 }
void clear_wheel_event ( )

desactive l'evenement.

Definition at line 83 of file window.cpp.

84 {
85  last_wheel.x= 0;
86  last_wheel.y= 0;
87 }
SDL_TextInputEvent text_event ( )

renvoie le dernier evenement. saisie de texte.

Definition at line 49 of file window.cpp.

50 {
51  return last_text;
52 }
void clear_text_event ( )

desactive l'evenement.

Definition at line 53 of file window.cpp.

54 {
55  last_text.text[0]= 0;
56 }
int run ( Window  window,
int(*)(void)  draw 
)

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

int events ( Window  window)

fonction interne de gestion d'evenements.

Definition at line 112 of file window.cpp.

113 {
114  // gestion des evenements
115  SDL_Event event;
116  while(SDL_PollEvent(&event))
117  {
118  switch(event.type)
119  {
120  case SDL_WINDOWEVENT:
121  // redimensionner la fenetre...
122  if(event.window.event == SDL_WINDOWEVENT_RESIZED)
123  {
124  // conserve les dimensions de la fenetre
125  width= event.window.data1;
126  height= event.window.data2;
127  SDL_SetWindowSize(window, width, height);
128 
129  // ... et le viewport opengl
130  glViewport(0, 0, width, height);
131  }
132  break;
133 
134  case SDL_DROPFILE:
135  last_drop.assign(event.drop.file);
136  SDL_free(event.drop.file);
137  break;
138 
139  case SDL_TEXTINPUT:
140  // conserver le dernier caractere
141  last_text= event.text;
142  break;
143 
144  case SDL_KEYDOWN:
145  // modifier l'etat du clavier
146  if((size_t) event.key.keysym.scancode < key_states.size())
147  {
148  key_states[event.key.keysym.scancode]= 1;
149  last_key= event.key; // conserver le dernier evenement
150  }
151 
152  // fermer l'application
153  if(event.key.keysym.sym == SDLK_ESCAPE)
154  stop= 1;
155  break;
156 
157  case SDL_KEYUP:
158  // modifier l'etat du clavier
159  if((size_t) event.key.keysym.scancode < key_states.size())
160  {
161  key_states[event.key.keysym.scancode]= 0;
162  last_key= event.key; // conserver le dernier evenement
163  }
164  break;
165 
166  case SDL_MOUSEBUTTONDOWN:
167  case SDL_MOUSEBUTTONUP:
168  last_button= event.button;
169  break;
170 
171  case SDL_MOUSEWHEEL:
172  last_wheel= event.wheel;
173  break;
174 
175  case SDL_QUIT:
176  stop= 1; // fermer l'application
177  break;
178  }
179  }
180 
181  return 1 - stop;
182 }