Browse Source

Platform: GlutApplication cleanup.

Using std::unique_ptr instead of raw pointer, consistent private
variable naming.
pull/87/merge
Vladimír Vondruš 11 years ago
parent
commit
f10cf91f6b
  1. 28
      src/Magnum/Platform/GlutApplication.cpp
  2. 9
      src/Magnum/Platform/GlutApplication.h

28
src/Magnum/Platform/GlutApplication.cpp

@ -33,7 +33,7 @@
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
GlutApplication* GlutApplication::instance = nullptr; GlutApplication* GlutApplication::_instance = nullptr;
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
GlutApplication::GlutApplication(const Arguments& arguments): GlutApplication{arguments, Configuration{}} {} GlutApplication::GlutApplication(const Arguments& arguments): GlutApplication{arguments, Configuration{}} {}
@ -43,9 +43,9 @@ GlutApplication::GlutApplication(const Arguments& arguments, const Configuration
createContext(configuration); createContext(configuration);
} }
GlutApplication::GlutApplication(const Arguments& arguments, std::nullptr_t): c(nullptr) { GlutApplication::GlutApplication(const Arguments& arguments, std::nullptr_t) {
/* Save global instance */ /* Save global instance */
instance = this; _instance = this;
/* Init GLUT */ /* Init GLUT */
glutInit(&arguments.argc, arguments.argv); glutInit(&arguments.argc, arguments.argv);
@ -59,7 +59,7 @@ void GlutApplication::createContext(const Configuration& configuration) {
} }
bool GlutApplication::tryCreateContext(const Configuration& configuration) { bool GlutApplication::tryCreateContext(const Configuration& configuration) {
CORRADE_ASSERT(!c, "Platform::GlutApplication::tryCreateContext(): context already created", false); CORRADE_ASSERT(!_context, "Platform::GlutApplication::tryCreateContext(): context already created", false);
unsigned int flags = GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL; unsigned int flags = GLUT_DOUBLE|GLUT_RGBA|GLUT_DEPTH|GLUT_STENCIL;
@ -96,45 +96,43 @@ bool GlutApplication::tryCreateContext(const Configuration& configuration) {
glutMotionFunc(staticMouseMoveEvent); glutMotionFunc(staticMouseMoveEvent);
glutDisplayFunc(staticDrawEvent); glutDisplayFunc(staticDrawEvent);
c = new Platform::Context; _context.reset(new Platform::Context);
return true; return true;
} }
GlutApplication::~GlutApplication() { GlutApplication::~GlutApplication() = default;
delete c;
}
void GlutApplication::staticKeyPressEvent(unsigned char key, int x, int y) { void GlutApplication::staticKeyPressEvent(unsigned char key, int x, int y) {
KeyEvent e(static_cast<KeyEvent::Key>(key), {x, y}); KeyEvent e(static_cast<KeyEvent::Key>(key), {x, y});
instance->keyPressEvent(e); _instance->keyPressEvent(e);
} }
void GlutApplication::staticKeyReleaseEvent(unsigned char key, int x, int y) { void GlutApplication::staticKeyReleaseEvent(unsigned char key, int x, int y) {
KeyEvent e(static_cast<KeyEvent::Key>(key), {x, y}); KeyEvent e(static_cast<KeyEvent::Key>(key), {x, y});
instance->keyReleaseEvent(e); _instance->keyReleaseEvent(e);
} }
void GlutApplication::staticSpecialKeyPressEvent(int key, int x, int y){ void GlutApplication::staticSpecialKeyPressEvent(int key, int x, int y){
KeyEvent e(static_cast<KeyEvent::Key>(key << 16), {x, y}); KeyEvent e(static_cast<KeyEvent::Key>(key << 16), {x, y});
instance->keyPressEvent(e); _instance->keyPressEvent(e);
} }
void GlutApplication::staticSpecialKeyReleaseEvent(int key, int x, int y){ void GlutApplication::staticSpecialKeyReleaseEvent(int key, int x, int y){
KeyEvent e(static_cast<KeyEvent::Key>(key << 16), {x, y}); KeyEvent e(static_cast<KeyEvent::Key>(key << 16), {x, y});
instance->keyReleaseEvent(e); _instance->keyReleaseEvent(e);
} }
void GlutApplication::staticMouseEvent(int button, int state, int x, int y) { void GlutApplication::staticMouseEvent(int button, int state, int x, int y) {
MouseEvent e(static_cast<MouseEvent::Button>(button), {x, y}); MouseEvent e(static_cast<MouseEvent::Button>(button), {x, y});
if(state == GLUT_DOWN) if(state == GLUT_DOWN)
instance->mousePressEvent(e); _instance->mousePressEvent(e);
else else
instance->mouseReleaseEvent(e); _instance->mouseReleaseEvent(e);
} }
void GlutApplication::staticMouseMoveEvent(int x, int y) { void GlutApplication::staticMouseMoveEvent(int x, int y) {
MouseMoveEvent e({x, y}, MouseMoveEvent::Button::Left); MouseMoveEvent e({x, y}, MouseMoveEvent::Button::Left);
instance->mouseMoveEvent(e); _instance->mouseMoveEvent(e);
} }
void GlutApplication::viewportEvent(const Vector2i&) {} void GlutApplication::viewportEvent(const Vector2i&) {}

9
src/Magnum/Platform/GlutApplication.h

@ -29,6 +29,7 @@
* @brief Class @ref Magnum::Platform::GlutApplication, macro @ref MAGNUM_GLUTAPPLICATION_MAIN() * @brief Class @ref Magnum::Platform::GlutApplication, macro @ref MAGNUM_GLUTAPPLICATION_MAIN()
*/ */
#include <memory>
#include <string> #include <string>
#include "Magnum/Magnum.h" #include "Magnum/Magnum.h"
@ -246,7 +247,7 @@ class GlutApplication {
private: private:
static void staticViewportEvent(int x, int y) { static void staticViewportEvent(int x, int y) {
instance->viewportEvent({x, y}); _instance->viewportEvent({x, y});
} }
static void staticKeyPressEvent(unsigned char key, int x, int y); static void staticKeyPressEvent(unsigned char key, int x, int y);
@ -260,12 +261,12 @@ class GlutApplication {
static void staticMouseMoveEvent(int x, int y); static void staticMouseMoveEvent(int x, int y);
static void staticDrawEvent() { static void staticDrawEvent() {
instance->drawEvent(); _instance->drawEvent();
} }
static GlutApplication* instance; static GlutApplication* _instance;
Platform::Context* c; std::unique_ptr<Platform::Context> _context;
}; };
/** /**

Loading…
Cancel
Save