Browse Source

Move function pointer loading into platform-specific class.

This way it will be possible decouple the main library from
platform-specific GLX/WGL/CGL/EGL... API.
pull/68/head
Vladimír Vondruš 12 years ago
parent
commit
6c766df568
  1. 16
      doc/platform.dox
  2. 14
      src/Magnum/Context.cpp
  3. 28
      src/Magnum/Context.h
  4. 4
      src/Magnum/Platform/AbstractXApplication.cpp
  5. 3
      src/Magnum/Platform/AbstractXApplication.h
  6. 4
      src/Magnum/Platform/AndroidApplication.cpp
  7. 2
      src/Magnum/Platform/AndroidApplication.h
  8. 1
      src/Magnum/Platform/CMakeLists.txt
  9. 79
      src/Magnum/Platform/Context.h
  10. 4
      src/Magnum/Platform/GlutApplication.cpp
  11. 2
      src/Magnum/Platform/GlutApplication.h
  12. 2
      src/Magnum/Platform/NaClApplication.cpp
  13. 2
      src/Magnum/Platform/NaClApplication.h
  14. 1
      src/Magnum/Platform/Platform.h
  15. 4
      src/Magnum/Platform/Sdl2Application.cpp
  16. 2
      src/Magnum/Platform/Sdl2Application.h
  17. 4
      src/Magnum/Platform/WindowlessCglApplication.cpp
  18. 2
      src/Magnum/Platform/WindowlessCglApplication.h
  19. 4
      src/Magnum/Platform/WindowlessGlxApplication.cpp
  20. 3
      src/Magnum/Platform/WindowlessGlxApplication.h
  21. 4
      src/Magnum/Platform/WindowlessNaClApplication.cpp
  22. 2
      src/Magnum/Platform/WindowlessNaClApplication.h
  23. 4
      src/Magnum/Platform/WindowlessWglApplication.cpp
  24. 2
      src/Magnum/Platform/WindowlessWglApplication.h

16
doc/platform.dox

@ -263,25 +263,25 @@ MyApplication::MyApplication(int& argc, char** argv): Platform::Application(argc
@section platform-custom Using custom platform toolkits @section platform-custom Using custom platform toolkits
In case you want to use some not-yet-supported toolkit or you don't want to use In case you want to use some not-yet-supported toolkit or you don't want to use
the wrappers in @ref Platform namespace, you can initialize %Magnum manually. the application wrappers in @ref Platform namespace, you can initialize %Magnum
All you need is to create OpenGL context and then create instance of manually. First create OpenGL context and then create instance of
@ref Context class, which will take care of proper initialization and feature @ref Platform::Context class, which will take care of proper initialization and
detection. The instance must be alive for whole application lifetime. Example feature detection. The instance must be alive for whole application lifetime.
`main()` function with manual initialization: Example `main()` function with manual initialization:
@code @code
int main(int argc, char** argv) { int main(int argc, char** argv) {
// Create OpenGL context ... // Create OpenGL context ...
{ {
// Initialize Magnum // Initialize Magnum
Context context; Platform::Context context;
// open window, enter main loop... // Open window, enter main loop ...
// Magnum context gets destroyed // Magnum context gets destroyed
} }
// delete OpenGL context ... // Delete OpenGL context ...
return 0; return 0;
} }

14
src/Magnum/Context.cpp

@ -294,17 +294,9 @@ const std::vector<Extension>& Extension::extensions(Version version) {
Context* Context::_current = nullptr; Context* Context::_current = nullptr;
Context::Context() { Context::Context(void functionLoader()) {
#ifndef MAGNUM_TARGET_GLES /* Load GL function pointers */
/* Init glLoadGen. Ignore functions that failed to load (described by if(functionLoader) functionLoader();
`ogl_LOAD_SUCCEEDED + n` return code), as we requested the latest OpenGL
with many vendor extensions and there won't ever be a driver supporting
everything possible. */
if(ogl_LoadFunctions() == ogl_LOAD_FAILED) {
Error() << "ExtensionWrangler: cannot initialize glLoadGen";
std::exit(64);
}
#endif
/* Get version */ /* Get version */
#if defined(MAGNUM_TARGET_GLES) && !defined(MAGNUM_TARGET_GLES2) #if defined(MAGNUM_TARGET_GLES) && !defined(MAGNUM_TARGET_GLES2)

28
src/Magnum/Context.h

@ -42,9 +42,8 @@
namespace Magnum { namespace Magnum {
namespace Implementation { namespace Implementation { struct State; }
struct State; namespace Platform { class Context; }
}
/** /**
@brief Run-time information about OpenGL extension @brief Run-time information about OpenGL extension
@ -88,11 +87,15 @@ class MAGNUM_EXPORT Extension {
Provides access to version and extension information. Instance available Provides access to version and extension information. Instance available
through @ref Context::current() is automatically created during construction of through @ref Context::current() is automatically created during construction of
*Application classes in @ref Platform namespace. You can safely assume that the `*Application` classes in @ref Platform namespace. You can safely assume that
instance is available during whole lifetime of *Application object. See the instance is available during whole lifetime of `*Application` object. It's
@ref platform documentation for more information about engine setup. also possible to create the context without using any `*Application` class
using @ref Platform::Context subclass, see @ref platform documentation for more
information.
*/ */
class MAGNUM_EXPORT Context { class MAGNUM_EXPORT Context {
friend class Platform::Context;
public: public:
/** /**
* @brief %Context flag * @brief %Context flag
@ -199,17 +202,6 @@ class MAGNUM_EXPORT Context {
*/ */
typedef Containers::EnumSet<DetectedDriver> DetectedDrivers; typedef Containers::EnumSet<DetectedDriver> DetectedDrivers;
/**
* @brief Constructor
*
* Does initial setup, detects available features and enables them
* throughout the engine.
* @see @fn_gl{Get} with @def_gl{MAJOR_VERSION}, @def_gl{MINOR_VERSION},
* @def_gl{CONTEXT_FLAGS}, @def_gl{NUM_EXTENSIONS},
* @fn_gl{GetString} with @def_gl{EXTENSIONS}
*/
explicit Context();
/** @brief Copying is not allowed */ /** @brief Copying is not allowed */
Context(const Context&) = delete; Context(const Context&) = delete;
@ -478,6 +470,8 @@ class MAGNUM_EXPORT Context {
private: private:
static Context* _current; static Context* _current;
explicit Context(void functionLoader());
MAGNUM_LOCAL void setupDriverWorkarounds(); MAGNUM_LOCAL void setupDriverWorkarounds();
Version _version; Version _version;

4
src/Magnum/Platform/AbstractXApplication.cpp

@ -27,7 +27,7 @@
#include <Corrade/Utility/utilities.h> #include <Corrade/Utility/utilities.h>
#include "Magnum/Context.h" #include "Magnum/Platform/Context.h"
#include "Magnum/Version.h" #include "Magnum/Version.h"
#include "Implementation/AbstractContextHandler.h" #include "Implementation/AbstractContextHandler.h"
@ -95,7 +95,7 @@ bool AbstractXApplication::tryCreateContext(const Configuration& configuration)
/* Set OpenGL context as current */ /* Set OpenGL context as current */
contextHandler->makeCurrent(); contextHandler->makeCurrent();
c = new Context; c = new Platform::Context;
return true; return true;
} }

3
src/Magnum/Platform/AbstractXApplication.h

@ -41,6 +41,7 @@
#include "Magnum/Magnum.h" #include "Magnum/Magnum.h"
#include "Magnum/Math/Vector2.h" #include "Magnum/Math/Vector2.h"
#include "Magnum/Platform/Platform.h"
namespace Magnum { namespace Magnum {
@ -183,7 +184,7 @@ class AbstractXApplication {
Implementation::AbstractContextHandler<Configuration, Display*, VisualID, Window>* contextHandler; Implementation::AbstractContextHandler<Configuration, Display*, VisualID, Window>* contextHandler;
Context* c; Platform::Context* c;
/** @todo Get this from the created window */ /** @todo Get this from the created window */
Vector2i viewportSize; Vector2i viewportSize;

4
src/Magnum/Platform/AndroidApplication.cpp

@ -28,7 +28,7 @@
#include <Corrade/Utility/AndroidStreamBuffer.h> #include <Corrade/Utility/AndroidStreamBuffer.h>
#include <Corrade/Utility/Debug.h> #include <Corrade/Utility/Debug.h>
#include "Magnum/Context.h" #include "Magnum/Platform/Context.h"
#include "Implementation/Egl.h" #include "Implementation/Egl.h"
@ -136,7 +136,7 @@ bool AndroidApplication::tryCreateContext(const Configuration& configuration) {
/* Make the context current */ /* Make the context current */
CORRADE_INTERNAL_ASSERT_OUTPUT(eglMakeCurrent(_display, _surface, _surface, _context)); CORRADE_INTERNAL_ASSERT_OUTPUT(eglMakeCurrent(_display, _surface, _surface, _context));
_c.reset(new Context); _c.reset(new Platform::Context);
return true; return true;
} }

2
src/Magnum/Platform/AndroidApplication.h

@ -306,7 +306,7 @@ class AndroidApplication {
EGLSurface _surface; EGLSurface _surface;
EGLContext _context; EGLContext _context;
std::unique_ptr<Context> _c; std::unique_ptr<Platform::Context> _c;
std::unique_ptr<LogOutput> _logOutput; std::unique_ptr<LogOutput> _logOutput;
CORRADE_ENUMSET_FRIEND_OPERATORS(Flags) CORRADE_ENUMSET_FRIEND_OPERATORS(Flags)

1
src/Magnum/Platform/CMakeLists.txt

@ -25,6 +25,7 @@
# Headers # Headers
set(MagnumPlatform_HEADERS set(MagnumPlatform_HEADERS
Context.h
Platform.h Platform.h
Screen.h Screen.h
ScreenedApplication.h ScreenedApplication.h

79
src/Magnum/Platform/Context.h

@ -0,0 +1,79 @@
#ifndef Magnum_Platform_Context_h
#define Magnum_Platform_Context_h
/*
This file is part of Magnum.
Copyright © 2010, 2011, 2012, 2013, 2014
Vladimír Vondruš <mosra@centrum.cz>
Permission is hereby granted, free of charge, to any person obtaining a
copy of this software and associated documentation files (the "Software"),
to deal in the Software without restriction, including without limitation
the rights to use, copy, modify, merge, publish, distribute, sublicense,
and/or sell copies of the Software, and to permit persons to whom the
Software is furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included
in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
DEALINGS IN THE SOFTWARE.
*/
#include <Corrade/Utility/Debug.h>
#include "Magnum/Context.h"
#include "Magnum/OpenGL.h"
namespace Magnum { namespace Platform {
/**
@brief Platform-specific context
In most cases not needed to be used directly as the initialization is done
automatically in `*Application` classes. See @ref platform for more
information.
*/
class Context: public Magnum::Context {
public:
/**
* @brief Constructor
*
* Does initial setup, loads OpenGL function pointers using
* platform-specific API, detects available features and enables them
* throughout the engine.
* @see @fn_gl{Get} with @def_gl{MAJOR_VERSION}, @def_gl{MINOR_VERSION},
* @def_gl{CONTEXT_FLAGS}, @def_gl{NUM_EXTENSIONS},
* @fn_gl{GetString} with @def_gl{EXTENSIONS}
*/
explicit Context():
#ifndef MAGNUM_TARGET_GLES
Magnum::Context{functionLoader} {}
#else
Magnum::Context{nullptr} {}
#endif
private:
#ifndef MAGNUM_TARGET_GLES
void functionLoader() {
/* Init glLoadGen. Ignore functions that failed to load (described
by `ogl_LOAD_SUCCEEDED + n` return code), as we requested the
latest OpenGL with many vendor extensions and there won't ever
everything possible. */
if(ogl_LoadFunctions() == ogl_LOAD_FAILED) {
Error() << "ExtensionWrangler: cannot initialize glLoadGen";
std::exit(64);
}
}
#endif
};
}}
#endif

4
src/Magnum/Platform/GlutApplication.cpp

@ -27,8 +27,8 @@
#include <tuple> #include <tuple>
#include "Magnum/Context.h"
#include "Magnum/Version.h" #include "Magnum/Version.h"
#include "Magnum/Platform/Context.h"
#include "Magnum/Platform/ScreenedApplication.hpp" #include "Magnum/Platform/ScreenedApplication.hpp"
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
@ -96,7 +96,7 @@ bool GlutApplication::tryCreateContext(const Configuration& configuration) {
glutMotionFunc(staticMouseMoveEvent); glutMotionFunc(staticMouseMoveEvent);
glutDisplayFunc(staticDrawEvent); glutDisplayFunc(staticDrawEvent);
c = new Context; c = new Platform::Context;
return true; return true;
} }

2
src/Magnum/Platform/GlutApplication.h

@ -267,7 +267,7 @@ class GlutApplication {
static GlutApplication* instance; static GlutApplication* instance;
Context* c; Platform::Context* c;
}; };
/** /**

2
src/Magnum/Platform/NaClApplication.cpp

@ -114,7 +114,7 @@ bool NaClApplication::tryCreateContext(const Configuration& configuration) {
RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE|PP_INPUTEVENT_CLASS_WHEEL); RequestInputEvents(PP_INPUTEVENT_CLASS_MOUSE|PP_INPUTEVENT_CLASS_WHEEL);
RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD); RequestFilteringInputEvents(PP_INPUTEVENT_CLASS_KEYBOARD);
c = new Context; c = new Platform::Context;
return true; return true;
} }

2
src/Magnum/Platform/NaClApplication.h

@ -379,7 +379,7 @@ class NaClApplication: public pp::Instance, public pp::Graphics3DClient, public
pp::Graphics3D* graphics; pp::Graphics3D* graphics;
pp::Fullscreen* fullscreen; pp::Fullscreen* fullscreen;
Context* c; Platform::Context* c;
Vector2i viewportSize; Vector2i viewportSize;
Flags flags; Flags flags;

1
src/Magnum/Platform/Platform.h

@ -33,6 +33,7 @@ namespace Magnum { namespace Platform {
template<class> class BasicScreen; template<class> class BasicScreen;
template<class> class BasicScreenedApplication; template<class> class BasicScreenedApplication;
class Context;
}} }}

4
src/Magnum/Platform/Sdl2Application.cpp

@ -31,7 +31,7 @@
#include <emscripten/emscripten.h> #include <emscripten/emscripten.h>
#endif #endif
#include "Magnum/Context.h" #include "Magnum/Platform/Context.h"
#include "Magnum/Version.h" #include "Magnum/Version.h"
#include "Magnum/Platform/ScreenedApplication.hpp" #include "Magnum/Platform/ScreenedApplication.hpp"
@ -198,7 +198,7 @@ bool Sdl2Application::tryCreateContext(const Configuration& configuration) {
context = SDL_SetVideoMode(configuration.size().x(), configuration.size().y(), 24, SDL_OPENGL|SDL_HWSURFACE|SDL_DOUBLEBUF); context = SDL_SetVideoMode(configuration.size().x(), configuration.size().y(), 24, SDL_OPENGL|SDL_HWSURFACE|SDL_DOUBLEBUF);
#endif #endif
c = new Context; c = new Platform::Context;
return true; return true;
} }

2
src/Magnum/Platform/Sdl2Application.h

@ -406,7 +406,7 @@ class Sdl2Application {
SDL_Surface* context; SDL_Surface* context;
#endif #endif
Context* c; Platform::Context* c;
Flags flags; Flags flags;
}; };

4
src/Magnum/Platform/WindowlessCglApplication.cpp

@ -30,7 +30,7 @@
#include <Corrade/Utility/Assert.h> #include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/Debug.h> #include <Corrade/Utility/Debug.h>
#include "Magnum/Context.h" #include "Magnum/Platform/Context.h"
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
@ -113,7 +113,7 @@ bool WindowlessCglApplication::tryCreateContext(const Configuration&) {
} }
cglError = CGLSetCurrentContext(context); cglError = CGLSetCurrentContext(context);
c = new Context; c = new Platform::Context;
return true; return true;
} }

2
src/Magnum/Platform/WindowlessCglApplication.h

@ -151,7 +151,7 @@ class WindowlessCglApplication {
CGLContextObj context; CGLContextObj context;
CGLPixelFormatObj pixelFormat; CGLPixelFormatObj pixelFormat;
Context* c; Platform::Context* c;
}; };
/** /**

4
src/Magnum/Platform/WindowlessGlxApplication.cpp

@ -28,7 +28,7 @@
#include <Corrade/Utility/Assert.h> #include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/Debug.h> #include <Corrade/Utility/Debug.h>
#include "Magnum/Context.h" #include "Magnum/Platform/Context.h"
#define None 0L // redef Xlib nonsense #define None 0L // redef Xlib nonsense
@ -111,7 +111,7 @@ bool WindowlessGlxApplication::tryCreateContext(const Configuration&) {
return false; return false;
} }
c = new Context; c = new Platform::Context;
return true; return true;
} }

3
src/Magnum/Platform/WindowlessGlxApplication.h

@ -40,6 +40,7 @@
#undef Status #undef Status
#include "Magnum/Magnum.h" #include "Magnum/Magnum.h"
#include "Magnum/Platform/Context.h"
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
@ -157,7 +158,7 @@ class WindowlessGlxApplication {
GLXContext context; GLXContext context;
GLXPbuffer pbuffer; GLXPbuffer pbuffer;
Context* c; Platform::Context* c;
}; };
/** /**

4
src/Magnum/Platform/WindowlessNaClApplication.cpp

@ -30,7 +30,7 @@
#include <Corrade/Utility/Assert.h> #include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/NaClStreamBuffer.h> #include <Corrade/Utility/NaClStreamBuffer.h>
#include "Magnum/Context.h" #include "Magnum/Platform/Context.h"
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
@ -97,7 +97,7 @@ bool WindowlessNaClApplication::tryCreateContext(const Configuration&) {
glSetCurrentContextPPAPI(graphics->pp_resource()); glSetCurrentContextPPAPI(graphics->pp_resource());
c = new Context; c = new Platform::Context;
return true; return true;
} }

2
src/Magnum/Platform/WindowlessNaClApplication.h

@ -171,7 +171,7 @@ class WindowlessNaClApplication: public pp::Instance, public pp::Graphics3DClien
bool Init(std::uint32_t, const char*, const char*) override; bool Init(std::uint32_t, const char*, const char*) override;
pp::Graphics3D* graphics; pp::Graphics3D* graphics;
Context* c; Platform::Context* c;
ConsoleDebugOutput* debugOutput; ConsoleDebugOutput* debugOutput;
}; };

4
src/Magnum/Platform/WindowlessWglApplication.cpp

@ -29,7 +29,7 @@
#include <Corrade/Utility/Assert.h> #include <Corrade/Utility/Assert.h>
#include <Corrade/Utility/Debug.h> #include <Corrade/Utility/Debug.h>
#include "Magnum/Context.h" #include "Magnum/Platform/Context.h"
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
@ -114,7 +114,7 @@ bool WindowlessWglApplication::tryCreateContext(const Configuration&) {
return false; return false;
} }
_c = new Context; _c = new Platform::Context;
return true; return true;
} }

2
src/Magnum/Platform/WindowlessWglApplication.h

@ -154,7 +154,7 @@ class WindowlessWglApplication {
HDC _deviceContext; HDC _deviceContext;
HGLRC _renderingContext; HGLRC _renderingContext;
Context* _c; Platform::Context* _c;
}; };
/** /**

Loading…
Cancel
Save