35 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &n);
39static std::vector<unsigned char> key_states;
42 SDL_Scancode code= SDL_GetScancodeFromKey(key);
43 assert((
size_t) code < key_states.size());
44 return (
int) key_states[code];
48 SDL_Scancode code= SDL_GetScancodeFromKey(key);
49 assert((
size_t) code < key_states.size());
53static SDL_KeyboardEvent last_key;
61 last_key.keysym.sym= 0;
64static SDL_TextInputEvent last_text;
74static std::vector<std::string> last_drops;
82 if(last_drops.empty())
85 return last_drops.back().c_str();
99static SDL_MouseButtonEvent last_button;
106 last_button.state= 0;
109static SDL_MouseWheelEvent last_wheel;
122static std::chrono::high_resolution_clock::time_point app_start= {};
123static std::chrono::high_resolution_clock::time_point last_time= {};
124static float last_delta= 0;
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);
132 return float(std::chrono::duration_cast<std::chrono::microseconds>(now - app_start).count()) / float(1000);
145int run( Window window,
int (*draw)() )
148 glViewport(0, 0, width, height);
158 SDL_GL_SwapWindow(window);
164static int event_count= 0;
165int last_event_count( ) {
return event_count; }
170 bool resize_event=
false;
174 while(SDL_PollEvent(&event))
178 case SDL_WINDOWEVENT:
180 if(event.window.event == SDL_WINDOWEVENT_RESIZED)
186 width=
event.window.data1;
187 height=
event.window.data2;
193 last_drops.push_back(std::string(event.drop.file));
194 SDL_free(event.drop.file);
199 last_text=
event.text;
204 if((
size_t) event.key.keysym.scancode < key_states.size())
206 key_states[
event.key.keysym.scancode]= 1;
211 if(event.key.keysym.sym == SDLK_ESCAPE)
217 if((
size_t) event.key.keysym.scancode < key_states.size())
219 key_states[
event.key.keysym.scancode]= 0;
224 case SDL_MOUSEBUTTONDOWN:
225 case SDL_MOUSEBUTTONUP:
226 last_button=
event.button;
230 last_wheel=
event.wheel;
241 glViewport(0, 0, width, height);
249Window
create_window(
const int w,
const int h,
const int major,
const int minor,
const int samples )
252 if(SDL_Init(SDL_INIT_VIDEO | SDL_INIT_EVENTS) < 0)
254 printf(
"[error] SDL_Init() failed:\n%s\n", SDL_GetError());
263 printf(
"creating window(%d, %d) openGL %d.%d, %d MSAA samples...\n", w, h, major, minor, samples);
265 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, major);
266 SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, minor);
268 SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
270 SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
272 SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
273 SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
277 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLEBUFFERS, 1);
278 SDL_GL_SetAttribute(SDL_GL_MULTISAMPLESAMPLES, samples);
282 printf(
"creating window(%d, %d) openGL ES 3.0...\n", w, h);
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);
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)
296 printf(
"[error] SDL_CreateWindow() failed.\n");
302 const unsigned char *state= SDL_GetKeyboardState(&keys);
303 key_states.assign(state, state + keys);
305 SDL_SetWindowDisplayMode(window,
nullptr);
306 SDL_StartTextInput();
309 SDL_GetWindowSize(window, &width, &height);
317 SDL_DestroyWindow(window);
325void DEBUGCALLBACK debug_print( GLenum source, GLenum type,
unsigned int id, GLenum severity, GLsizei length,
326 const char *message,
const void *userParam )
328 static std::set<std::string> log;
329 if(log.insert(message).second ==
false)
333 if(severity == GL_DEBUG_SEVERITY_HIGH)
334 printf(
"[openGL error]\n%s\n", message);
335 else if(severity == GL_DEBUG_SEVERITY_MEDIUM)
336 printf(
"[openGL warning]\n%s\n", message);
338 printf(
"[openGL message]\n%s\n", message);
346 if(window ==
nullptr)
349 Context context= SDL_GL_CreateContext(window);
350 if(context ==
nullptr)
352 printf(
"[error] creating openGL context.\n");
356 if(SDL_GL_SetSwapInterval(-1) != 0)
357 printf(
"[warning] can't set adaptive vsync...\n");
359 if(SDL_GL_GetSwapInterval() != -1)
362 SDL_GL_SetSwapInterval(1);
365 printf(
"adaptive vsync ON\n");
369 SDL_GL_GetAttribute(SDL_GL_MULTISAMPLESAMPLES, &n);
371 printf(
"MSAA %d samples\n", n);
375 app_start= std::chrono::high_resolution_clock::now();
382 while(glGetError() != GL_NO_ERROR) {;}
387 if(GLAD_GL_ARB_debug_output)
389 printf(
"debug output enabled...\n");
391 glDebugMessageControlARB(GL_DONT_CARE, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_TRUE);
393 glDebugMessageControlARB(GL_DEBUG_SOURCE_SHADER_COMPILER, GL_DONT_CARE, GL_DONT_CARE, 0, NULL, GL_FALSE);
395 glDebugMessageCallbackARB(debug_print, NULL);
396 glEnable(GL_DEBUG_OUTPUT_SYNCHRONOUS_ARB);
406 SDL_GL_DeleteContext(context);
410static std::string smartpath;
411static std::string path;
421 const char *envbase= std::getenv(
"GKIT_BASE_PATH");
422 if(envbase !=
nullptr)
424 path= std::string(envbase);
425 if(!path.empty() && path[path.size() -1] !=
'/')
428 printf(
"[base path] %s\n", path.c_str());
435 char *base= SDL_GetBasePath();
436 printf(
"[base path] %s\n", base);
441 smartpath= path + filename;
442 if(exists(smartpath.c_str()))
443 return smartpath.c_str();
445 smartpath= path +
"../" + filename;
446 if(exists(smartpath.c_str()))
447 return smartpath.c_str();
const char * smart_path(const char *filename)
renvoie le chemin(path) vers le fichier 'filename' apres l'avoir cherche dans un repertoire standard....
SDL_MouseButtonEvent button_event()
renvoie le dernier evenement. etat des boutons de la souris.
Context create_context(Window window)
cree et configure un contexte opengl
void clear_button_event()
desactive l'evenement.
void clear_drop_events()
desactive drag/drop.
int events(Window window)
fonction interne de gestion d'evenements.
const std::vector< std::string > & drop_events()
drag/drop. recupere tous les fichiers.
int window_height()
renvoie la hauteur de la fenetre de l'application.
SDL_TextInputEvent text_event()
renvoie le dernier evenement. saisie de texte.
void release_window(Window window)
destruction de la fenetre.
int run(Window window, int(*draw)())
boucle de gestion des evenements de l'application.
void clear_key_event()
desactive l'evenement.
SDL_KeyboardEvent key_event()
renvoie le dernier evenement. touche speciales.
void clear_key_state(const SDL_Keycode key)
desactive une touche du clavier.
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().
const char * drop_event()
drag/drop, renvoie le dernier fichier.
Window create_window(const int w, const int h, const int major, const int minor, const int samples)
creation d'une fenetre pour l'application.
void clear_drop_event()
desactive drag/drop.
void clear_text_event()
desactive l'evenement.
void release_context(Context context)
detruit le contexte openGL.
void clear_wheel_event()
desactive l'evenement.
int key_state(const SDL_Keycode key)
renvoie l'etat d'une touche du clavier. cf la doc SDL2 pour les codes.
int window_msaa()
renvoie le nombre de samples MSAA.
SDL_MouseWheelEvent wheel_event()
renvoie le dernier evenement. etat de la molette de la souris / glisser sur le pad.
int window_width()
renvoie la largeur de la fenetre de l'application.
float delta_time()
renvoie le temps ecoule depuis la derniere frame, en millisecondes.
float global_time()
renvoie le temps ecoule depuis le lancement de l'application, en millisecondes.