Framework used by examples to bring up a window and GL context via SDL2.
The example framework's sole purpose is to impement via SDL2, the boiler plate code to:
FastUIDraw is not tied to a particular windowing system at all. FastUIDraw can work with other frameworks (for example Qt) that handle window creation and event handling.
#include <iostream>
#include "demo_framework.hpp"
DemoRunner::
DemoRunner(void):
m_window(nullptr),
m_ctx(nullptr),
m_run_demo(true),
m_return_code(0),
m_demo(nullptr)
{
}
DemoRunner::
~DemoRunner()
{
if (m_window)
{
if (m_demo)
{
}
if (m_ctx)
{
SDL_GL_MakeCurrent(m_window, nullptr);
SDL_GL_DeleteContext(m_ctx);
}
SDL_ShowCursor(SDL_ENABLE);
SDL_SetWindowGrab(m_window, SDL_FALSE);
SDL_DestroyWindow(m_window);
SDL_Quit();
}
}
DemoRunner::
init_sdl(void)
{
if (SDL_Init(SDL_INIT_EVERYTHING) < 0)
{
std::cerr << "\nFailed on SDL_Init\n";
}
int window_width(800), window_height(600);
m_window = SDL_CreateWindow("",
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
window_width,
window_height,
SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);
if (m_window == nullptr)
{
std::cerr << "\nFailed on SDL_SetVideoMode\n";
}
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
#ifdef FASTUIDRAW_GL_USE_GLES
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_ES);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
for (int gl_minor = 2; gl_minor >= 0 && !m_ctx; --gl_minor)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, gl_minor);
m_ctx = SDL_GL_CreateContext(m_window);
}
}
#else
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 4);
for (int gl_minor = 6; gl_minor >= 0 && !m_ctx; --gl_minor)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, gl_minor);
m_ctx = SDL_GL_CreateContext(m_window);
}
if (!m_ctx)
{
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
m_ctx = SDL_GL_CreateContext(m_window);
}
}
#endif
if (m_ctx == nullptr)
{
std::cerr << "Unable to create GL context: " << SDL_GetError() << "\n";
}
SDL_GL_MakeCurrent(m_window, m_ctx);
}
void
DemoRunner::
handle_event(const SDL_Event &ev)
{
switch (ev.type)
{
case SDL_QUIT:
end_demo();
break;
case SDL_KEYUP:
switch (ev.key.keysym.sym)
{
case SDLK_ESCAPE:
end_demo();
break;
}
break;
}
m_demo->handle_event(ev);
}
void
DemoRunner::
event_loop(void)
{
while (m_run_demo)
{
m_demo->draw_frame();
SDL_GL_SwapWindow(m_window);
if (m_run_demo)
{
SDL_Event ev;
while(m_run_demo && SDL_PollEvent(&ev))
{
handle_event(ev);
}
}
}
}
Demo::
Demo(DemoRunner *runner, int argc, char **argv):
m_demo_runner(runner)
{
m_demo_runner->m_demo = this;
}
Demo::
window_dimensions(void)
{
SDL_GetWindowSize(m_demo_runner->m_window, &return_value.
x(), &return_value.
y());
return return_value;
}
void
Demo::
{
m_demo_runner->end_demo(return_code);
}