Browse Source

Platform: Sdl2Application cleanup.

Using std::unique_ptr instead of raw pointer, consistent private
variable naming.
pull/87/merge
Vladimír Vondruš 11 years ago
parent
commit
59477d04f8
  1. 64
      src/Magnum/Platform/Sdl2Application.cpp
  2. 17
      src/Magnum/Platform/Sdl2Application.h

64
src/Magnum/Platform/Sdl2Application.cpp

@ -56,9 +56,9 @@ Sdl2Application::InputEvent::Modifiers fixedModifiers(Uint16 mod) {
} }
#ifdef CORRADE_TARGET_EMSCRIPTEN #ifdef CORRADE_TARGET_EMSCRIPTEN
Sdl2Application* Sdl2Application::instance = nullptr; Sdl2Application* Sdl2Application::_instance = nullptr;
void Sdl2Application::staticMainLoop() { void Sdl2Application::staticMainLoop() {
instance->mainLoop(); _instance->mainLoop();
} }
#endif #endif
@ -70,10 +70,10 @@ Sdl2Application::Sdl2Application(const Arguments& arguments, const Configuration
createContext(configuration); createContext(configuration);
} }
Sdl2Application::Sdl2Application(const Arguments&, std::nullptr_t): context(nullptr), flags(Flag::Redraw) { Sdl2Application::Sdl2Application(const Arguments&, std::nullptr_t): _glContext{nullptr}, _flags{Flag::Redraw} {
#ifdef CORRADE_TARGET_EMSCRIPTEN #ifdef CORRADE_TARGET_EMSCRIPTEN
CORRADE_ASSERT(!instance, "Platform::Sdl2Application::Sdl2Application(): the instance is already created", ); CORRADE_ASSERT(!_instance, "Platform::Sdl2Application::Sdl2Application(): the instance is already created", );
instance = this; _instance = this;
#endif #endif
if(SDL_Init(SDL_INIT_VIDEO) < 0) { if(SDL_Init(SDL_INIT_VIDEO) < 0) {
@ -89,7 +89,7 @@ void Sdl2Application::createContext(const Configuration& configuration) {
} }
bool Sdl2Application::tryCreateContext(const Configuration& configuration) { bool Sdl2Application::tryCreateContext(const Configuration& configuration) {
CORRADE_ASSERT(!context, "Platform::Sdl2Application::tryCreateContext(): context already created", false); CORRADE_ASSERT(!_glContext, "Platform::Sdl2Application::tryCreateContext(): context already created", false);
/* Enable double buffering and 24bt depth buffer */ /* Enable double buffering and 24bt depth buffer */
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
@ -156,7 +156,7 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) {
} }
/* Create window */ /* Create window */
if(!(window = SDL_CreateWindow(configuration.title().data(), if(!(_window = SDL_CreateWindow(configuration.title().data(),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
configuration.size().x(), configuration.size().y(), configuration.size().x(), configuration.size().y(),
SDL_WINDOW_OPENGL|windowFlags))) SDL_WINDOW_OPENGL|windowFlags)))
@ -166,20 +166,20 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) {
} }
/* Create context */ /* Create context */
context = SDL_GL_CreateContext(window); _glContext = SDL_GL_CreateContext(_window);
#ifndef MAGNUM_TARGET_GLES #ifndef MAGNUM_TARGET_GLES
/* Fall back to (forward compatible) GL 2.1, if core context creation fails /* Fall back to (forward compatible) GL 2.1, if core context creation fails
and if version is not user-specified */ and if version is not user-specified */
if(configuration.version() == Version::None && !context) { if(configuration.version() == Version::None && !_glContext) {
Warning() << "Platform::Sdl2Application::tryCreateContext(): cannot create core context:" << SDL_GetError() << "(falling back to compatibility context)"; Warning() << "Platform::Sdl2Application::tryCreateContext(): cannot create core context:" << SDL_GetError() << "(falling back to compatibility context)";
SDL_DestroyWindow(window); SDL_DestroyWindow(_window);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 2);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1); SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 1);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY); SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_COMPATIBILITY);
if(!(window = SDL_CreateWindow(configuration.title().data(), if(!(_window = SDL_CreateWindow(configuration.title().data(),
SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED,
configuration.size().x(), configuration.size().y(), configuration.size().x(), configuration.size().y(),
SDL_WINDOW_OPENGL|windowFlags))) SDL_WINDOW_OPENGL|windowFlags)))
@ -191,47 +191,47 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) {
#endif #endif
/* Cannot create context (or fallback compatibility context on desktop) */ /* Cannot create context (or fallback compatibility context on desktop) */
if(!context) { if(!_glContext) {
Error() << "Platform::Sdl2Application::tryCreateContext(): cannot create context:" << SDL_GetError(); Error() << "Platform::Sdl2Application::tryCreateContext(): cannot create context:" << SDL_GetError();
SDL_DestroyWindow(window); SDL_DestroyWindow(_window);
window = nullptr; _window = nullptr;
return false; return false;
} }
#else #else
/* Emscripten-specific initialization */ /* Emscripten-specific initialization */
context = SDL_SetVideoMode(configuration.size().x(), configuration.size().y(), 24, SDL_OPENGL|SDL_HWSURFACE|SDL_DOUBLEBUF); _glContext = SDL_SetVideoMode(configuration.size().x(), configuration.size().y(), 24, SDL_OPENGL|SDL_HWSURFACE|SDL_DOUBLEBUF);
#endif #endif
c = new Platform::Context; _context.reset(new Platform::Context);
return true; return true;
} }
void Sdl2Application::swapBuffers() { void Sdl2Application::swapBuffers() {
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
SDL_GL_SwapWindow(window); SDL_GL_SwapWindow(_window);
#else #else
SDL_Flip(context); SDL_Flip(_glContext);
#endif #endif
} }
Sdl2Application::~Sdl2Application() { Sdl2Application::~Sdl2Application() {
delete c; _context.reset();
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
SDL_GL_DeleteContext(context); SDL_GL_DeleteContext(_glContext);
SDL_DestroyWindow(window); SDL_DestroyWindow(_window);
#else #else
SDL_FreeSurface(context); SDL_FreeSurface(_glContext);
CORRADE_INTERNAL_ASSERT(instance == this); CORRADE_INTERNAL_ASSERT(_instance == this);
instance = nullptr; _instance = nullptr;
#endif #endif
SDL_Quit(); SDL_Quit();
} }
int Sdl2Application::exec() { int Sdl2Application::exec() {
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
while(!(flags & Flag::Exit)) mainLoop(); while(!(_flags & Flag::Exit)) mainLoop();
#else #else
emscripten_set_main_loop(staticMainLoop, 0, true); emscripten_set_main_loop(staticMainLoop, 0, true);
#endif #endif
@ -240,7 +240,7 @@ int Sdl2Application::exec() {
void Sdl2Application::exit() { void Sdl2Application::exit() {
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
flags |= Flag::Exit; _flags |= Flag::Exit;
#else #else
emscripten_cancel_main_loop(); emscripten_cancel_main_loop();
#endif #endif
@ -255,10 +255,10 @@ void Sdl2Application::mainLoop() {
switch(event.window.event) { switch(event.window.event) {
case SDL_WINDOWEVENT_RESIZED: case SDL_WINDOWEVENT_RESIZED:
viewportEvent({event.window.data1, event.window.data2}); viewportEvent({event.window.data1, event.window.data2});
flags |= Flag::Redraw; _flags |= Flag::Redraw;
break; break;
case SDL_WINDOWEVENT_EXPOSED: case SDL_WINDOWEVENT_EXPOSED:
flags |= Flag::Redraw; _flags |= Flag::Redraw;
break; break;
} break; } break;
@ -288,7 +288,7 @@ void Sdl2Application::mainLoop() {
case SDL_QUIT: case SDL_QUIT:
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
flags |= Flag::Exit; _flags |= Flag::Exit;
#else #else
emscripten_cancel_main_loop(); emscripten_cancel_main_loop();
#endif #endif
@ -296,8 +296,8 @@ void Sdl2Application::mainLoop() {
} }
} }
if(flags & Flag::Redraw) { if(_flags & Flag::Redraw) {
flags &= ~Flag::Redraw; _flags &= ~Flag::Redraw;
drawEvent(); drawEvent();
return; return;
} }
@ -310,7 +310,7 @@ void Sdl2Application::mainLoop() {
void Sdl2Application::setMouseLocked(bool enabled) { void Sdl2Application::setMouseLocked(bool enabled) {
/** @todo Implement this in Emscripten */ /** @todo Implement this in Emscripten */
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
SDL_SetWindowGrab(window, enabled ? SDL_TRUE : SDL_FALSE); SDL_SetWindowGrab(_window, enabled ? SDL_TRUE : SDL_FALSE);
SDL_SetRelativeMouseMode(enabled ? SDL_TRUE : SDL_FALSE); SDL_SetRelativeMouseMode(enabled ? SDL_TRUE : SDL_FALSE);
#else #else
CORRADE_ASSERT(false, "Sdl2Application::setMouseLocked(): not implemented", ); CORRADE_ASSERT(false, "Sdl2Application::setMouseLocked(): not implemented", );

17
src/Magnum/Platform/Sdl2Application.h

@ -29,8 +29,9 @@
* @brief Class @ref Magnum::Platform::Sdl2Application, macro @ref MAGNUM_SDL2APPLICATION_MAIN() * @brief Class @ref Magnum::Platform::Sdl2Application, macro @ref MAGNUM_SDL2APPLICATION_MAIN()
*/ */
#include <Corrade/Containers/EnumSet.h> #include <memory>
#include <Corrade/Corrade.h> #include <Corrade/Corrade.h>
#include <Corrade/Containers/EnumSet.h>
#include "Magnum/Magnum.h" #include "Magnum/Magnum.h"
#include "Magnum/Math/Vector2.h" #include "Magnum/Math/Vector2.h"
@ -285,7 +286,7 @@ class Sdl2Application {
* in the next iteration. You can call it from @ref drawEvent() itself * in the next iteration. You can call it from @ref drawEvent() itself
* to redraw immediately without waiting for user input. * to redraw immediately without waiting for user input.
*/ */
void redraw() { flags |= Flag::Redraw; } void redraw() { _flags |= Flag::Redraw; }
#ifdef DOXYGEN_GENERATING_OUTPUT #ifdef DOXYGEN_GENERATING_OUTPUT
protected: protected:
@ -396,22 +397,22 @@ class Sdl2Application {
CORRADE_ENUMSET_FRIEND_OPERATORS(Flags) CORRADE_ENUMSET_FRIEND_OPERATORS(Flags)
#ifdef CORRADE_TARGET_EMSCRIPTEN #ifdef CORRADE_TARGET_EMSCRIPTEN
static Sdl2Application* instance; static Sdl2Application* _instance;
static void staticMainLoop(); static void staticMainLoop();
#endif #endif
void mainLoop(); void mainLoop();
#ifndef CORRADE_TARGET_EMSCRIPTEN #ifndef CORRADE_TARGET_EMSCRIPTEN
SDL_Window* window; SDL_Window* _window;
SDL_GLContext context; SDL_GLContext _glContext;
#else #else
SDL_Surface* context; SDL_Surface* _glContext;
#endif #endif
Platform::Context* c; std::unique_ptr<Platform::Context> _context;
Flags flags; Flags _flags;
}; };
CORRADE_ENUMSET_OPERATORS(Sdl2Application::Flags) CORRADE_ENUMSET_OPERATORS(Sdl2Application::Flags)

Loading…
Cancel
Save