2019-07-21 14:45:50 +00:00
|
|
|
// Copyright 2019, Collabora, Ltd.
|
|
|
|
// SPDX-License-Identifier: BSL-1.0
|
|
|
|
/*!
|
|
|
|
* @file
|
|
|
|
* @brief SDL2 functions to drive the GUI.
|
|
|
|
* @author Jakob Bornecrantz <jakob@collabora.com>
|
|
|
|
* @ingroup gui
|
|
|
|
*/
|
|
|
|
|
2019-10-09 21:10:25 +00:00
|
|
|
#include "ogl/ogl_api.h"
|
2019-10-09 15:22:34 +00:00
|
|
|
#include "gui_sdl2.h"
|
2019-07-21 14:45:50 +00:00
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* Internal static functions
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
static void
|
2019-10-09 15:22:34 +00:00
|
|
|
sdl2_handle_keydown(struct sdl2_program *p, const SDL_Event *e)
|
2019-07-21 14:45:50 +00:00
|
|
|
{
|
|
|
|
switch (e->key.keysym.sym) {
|
2019-10-09 15:22:34 +00:00
|
|
|
case SDLK_ESCAPE: p->base.stopped = true; break;
|
2019-07-21 14:45:50 +00:00
|
|
|
default: break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
/*
|
|
|
|
*
|
|
|
|
* "Exported" functions.
|
|
|
|
*
|
|
|
|
*/
|
|
|
|
|
|
|
|
void
|
2019-10-09 15:22:34 +00:00
|
|
|
gui_sdl2_loop(struct sdl2_program *p)
|
2019-07-21 14:45:50 +00:00
|
|
|
{
|
|
|
|
SDL_Event event;
|
2019-10-09 15:22:34 +00:00
|
|
|
while (!p->base.stopped) {
|
2019-07-21 14:45:50 +00:00
|
|
|
if (SDL_WaitEvent(&event) == 0) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.type == SDL_QUIT) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (event.type == SDL_KEYDOWN) {
|
|
|
|
sdl2_handle_keydown(p, &event);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
int
|
2019-10-09 15:22:34 +00:00
|
|
|
gui_sdl2_init(struct sdl2_program *p)
|
2019-07-21 14:45:50 +00:00
|
|
|
{
|
|
|
|
if (SDL_Init(SDL_INIT_EVERYTHING) < 0) {
|
|
|
|
return -1;
|
|
|
|
}
|
2019-10-09 15:22:34 +00:00
|
|
|
p->sdl_initialized = true;
|
2019-07-21 14:45:50 +00:00
|
|
|
|
|
|
|
const char *title = "Monado! ☺";
|
|
|
|
int x = SDL_WINDOWPOS_UNDEFINED;
|
|
|
|
int y = SDL_WINDOWPOS_UNDEFINED;
|
|
|
|
int w = 1920;
|
|
|
|
int h = 1080;
|
|
|
|
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, 0);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
|
|
|
|
SDL_GL_CONTEXT_PROFILE_CORE);
|
2019-10-10 15:06:41 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS,
|
|
|
|
SDL_GL_CONTEXT_FORWARD_COMPATIBLE_FLAG);
|
2019-07-21 14:45:50 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
|
2019-10-10 15:06:41 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 2);
|
2019-07-21 14:45:50 +00:00
|
|
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
|
|
|
|
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
|
|
|
|
|
2019-12-03 16:04:24 +00:00
|
|
|
int window_flags = 0;
|
2019-07-21 14:45:50 +00:00
|
|
|
window_flags |= SDL_WINDOW_SHOWN;
|
|
|
|
window_flags |= SDL_WINDOW_OPENGL;
|
|
|
|
window_flags |= SDL_WINDOW_RESIZABLE;
|
|
|
|
window_flags |= SDL_WINDOW_ALLOW_HIGHDPI;
|
|
|
|
#if 0
|
|
|
|
window_flags |= SDL_WINDOW_MAXIMIZED;
|
|
|
|
#endif
|
|
|
|
|
2019-10-09 15:22:34 +00:00
|
|
|
|
2019-07-21 14:45:50 +00:00
|
|
|
p->win = SDL_CreateWindow(title, x, y, w, h, window_flags);
|
|
|
|
|
|
|
|
if (p->win == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
p->ctx = SDL_GL_CreateContext(p->win);
|
|
|
|
if (p->ctx == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_GL_MakeCurrent(p->win, p->ctx);
|
|
|
|
SDL_GL_SetSwapInterval(1); // Enable vsync
|
|
|
|
|
|
|
|
// Setup OpenGL bindings.
|
|
|
|
bool err = gladLoadGL((GLADloadfunc)SDL_GL_GetProcAddress) == 0;
|
|
|
|
if (err) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void
|
2019-10-09 15:22:34 +00:00
|
|
|
gui_sdl2_quit(struct sdl2_program *p)
|
2019-07-21 14:45:50 +00:00
|
|
|
{
|
|
|
|
if (p->ctx != NULL) {
|
|
|
|
SDL_GL_DeleteContext(p->ctx);
|
|
|
|
p->ctx = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p->win != NULL) {
|
|
|
|
SDL_DestroyWindow(p->win);
|
|
|
|
p->win = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
SDL_Quit();
|
2019-10-09 15:22:34 +00:00
|
|
|
p->sdl_initialized = false;
|
2019-07-21 14:45:50 +00:00
|
|
|
}
|