Browse Source

Platform: WindowlessCglApplication cleanup.

Using std::unique_ptr instead of raw pointer, consistent private
variable naming, better error checks, improved error messages, replaced
C-style casts with constructor casts.

Doing this blindly, hopefully I didn't break anything :)
pull/87/merge
Vladimír Vondruš 11 years ago
parent
commit
d54be041c4
  1. 73
      src/Magnum/Platform/WindowlessCglApplication.cpp
  2. 8
      src/Magnum/Platform/WindowlessCglApplication.h

73
src/Magnum/Platform/WindowlessCglApplication.cpp

@ -31,18 +31,19 @@
#include <Corrade/Utility/Debug.h> #include <Corrade/Utility/Debug.h>
#include "Magnum/Platform/Context.h" #include "Magnum/Platform/Context.h"
#include "Magnum/Version.h"
namespace Magnum { namespace Platform { namespace Magnum { namespace Platform {
#ifndef DOXYGEN_GENERATING_OUTPUT #ifndef DOXYGEN_GENERATING_OUTPUT
WindowlessCglApplication::WindowlessCglApplication(const Arguments& arguments): WindowlessCglApplication(arguments, Configuration{}) {} WindowlessCglApplication::WindowlessCglApplication(const Arguments& arguments): WindowlessCglApplication{arguments, Configuration{}} {}
#endif #endif
WindowlessCglApplication::WindowlessCglApplication(const Arguments& arguments, const Configuration& configuration): WindowlessCglApplication(arguments, nullptr) { WindowlessCglApplication::WindowlessCglApplication(const Arguments& arguments, const Configuration& configuration): WindowlessCglApplication{arguments, nullptr} {
createContext(configuration); createContext(configuration);
} }
WindowlessCglApplication::WindowlessCglApplication(const Arguments&, std::nullptr_t): c(nullptr) {} WindowlessCglApplication::WindowlessCglApplication(const Arguments&, std::nullptr_t) {}
void WindowlessCglApplication::createContext() { createContext({}); } void WindowlessCglApplication::createContext() { createContext({}); }
@ -51,77 +52,67 @@ void WindowlessCglApplication::createContext(const Configuration& configuration)
} }
bool WindowlessCglApplication::tryCreateContext(const Configuration&) { bool WindowlessCglApplication::tryCreateContext(const Configuration&) {
CORRADE_ASSERT(!c, "Platform::WindowlessCglApplication::tryCreateContext(): context already created", false); CORRADE_ASSERT(!_context, "Platform::WindowlessCglApplication::tryCreateContext(): context already created", false);
/* Check version */ /* Check version */
int nPix = 0;
GLint major, minor; GLint major, minor;
CGLGetVersion(&major, &minor); CGLGetVersion(&major, &minor);
CGLError cglError; if(version(major, minor) < Version::GL210) {
if(major == 2 && minor < 1) {
Error() << "Platform::WindowlessCglApplication::tryCreateContext(): OpenGL version 2.1 or greater is required"; Error() << "Platform::WindowlessCglApplication::tryCreateContext(): OpenGL version 2.1 or greater is required";
return false; return false;
} }
int formatCount;
CGLPixelFormatAttribute pfAttributesGL_3_2[4] = { CGLPixelFormatAttribute attributes32[] = {
kCGLPFAAccelerated, kCGLPFAAccelerated,
kCGLPFAOpenGLProfile, kCGLPFAOpenGLProfile,
(CGLPixelFormatAttribute) kCGLOGLPVersion_3_2_Core, CGLPixelFormatAttribute(kCGLOGLPVersion_3_2_Core),
(CGLPixelFormatAttribute) 0 CGLPixelFormatAttribute(0)
}; };
if(CGLChoosePixelFormat(attributes32, &_pixelFormat, &formatCount) != kCGLNoError) {
Error() << "Platform::WindowlessCglApplication::tryCreateContext(): cannot choose pixel format for GL 3.2, falling back to 3.0";
cglError = CGLChoosePixelFormat(pfAttributesGL_3_2,&pixelFormat,&nPix); CGLPixelFormatAttribute attributes30[] = {
if(cglError == kCGLBadPixelFormat) {
Error() << "Platform::WindowlessCglApplication::tryCreateContext(): OpenGL version 3.2 has failed trying GL version 3.0";
CGLPixelFormatAttribute pfAttributesGL3[4] = {
kCGLPFAAccelerated, kCGLPFAAccelerated,
kCGLPFAOpenGLProfile, kCGLPFAOpenGLProfile,
(CGLPixelFormatAttribute) kCGLOGLPVersion_GL3_Core, CGLPixelFormatAttribute(kCGLOGLPVersion_GL3_Core),
(CGLPixelFormatAttribute) 0 CGLPixelFormatAttribute(0)
}; };
if(CGLChoosePixelFormat(attributes30, &_pixelFormat, &formatCount) != kCGLNoError) {
Error() << "Platform::WindowlessCglApplication::tryCreateContext(): cannot choose pixel format for GL 3.0, falling back to 2.1";
cglError = CGLChoosePixelFormat(pfAttributesGL3,&pixelFormat,&nPix); CGLPixelFormatAttribute attributes21[] = {
if(cglError == kCGLBadPixelFormat) {
Error() << "Platform::WindowlessCglApplication::tryCreateContext(): OpenGL version 3.0 has failed trying GL version Legacy";
CGLPixelFormatAttribute pfAttributesLegacy[4] = {
kCGLPFAAccelerated, kCGLPFAAccelerated,
kCGLPFAOpenGLProfile, kCGLPFAOpenGLProfile,
(CGLPixelFormatAttribute) kCGLOGLPVersion_Legacy, CGLPixelFormatAttribute(kCGLOGLPVersion_Legacy),
(CGLPixelFormatAttribute) 0 CGLPixelFormatAttribute(0)
}; };
if(CGLChoosePixelFormat(attributes21, &_pixelFormat, &formatCount) != kCGLNoError) {
cglError = CGLChoosePixelFormat(pfAttributesLegacy,&pixelFormat,&nPix); Error() << "Platform::WindowlessCglApplication::tryCreateContext(): cannot choose pixel format";
if(cglError == kCGLBadPixelFormat)
{
Error() << "Platform::WindowlessCglApplication::tryCreateContext(): Context could not be created";
return false; return false;
} }
} }
} }
cglError = CGLCreateContext(pixelFormat, NULL, &context); if(CGLCreateContext(_pixelFormat, nullptr, &_glContext) != kCGLNoError) {
if(cglError == kCGLBadContext) {
Error() << "Platform::WindowlessCglApplication::tryCreateContext(): cannot create context"; Error() << "Platform::WindowlessCglApplication::tryCreateContext(): cannot create context";
return false; return false;
} }
cglError = CGLSetCurrentContext(context); if(CGLSetCurrentContext(_glContext) != kCGLNoError) {
c = new Platform::Context; Error() << "Platform::WindowlessCglApplication::tryCreateContext(): cannot make context current";
return false;
}
_context.reset(new Platform::Context);
return true; return true;
} }
WindowlessCglApplication::~WindowlessCglApplication() { WindowlessCglApplication::~WindowlessCglApplication() {
delete c; _context.reset();
CGLDestroyContext(context); CGLDestroyContext(_glContext);
CGLDestroyPixelFormat(pixelFormat); CGLDestroyPixelFormat(_pixelFormat);
} }
}} }}

8
src/Magnum/Platform/WindowlessCglApplication.h

@ -31,6 +31,8 @@
* @brief Class @ref Magnum::Platform::WindowlessCglApplication, macro @ref MAGNUM_WINDOWLESSCGLAPPLICATION_MAIN() * @brief Class @ref Magnum::Platform::WindowlessCglApplication, macro @ref MAGNUM_WINDOWLESSCGLAPPLICATION_MAIN()
*/ */
#include <memory>
#include "Magnum/OpenGL.h" #include "Magnum/OpenGL.h"
#include <OpenGL/OpenGL.h> #include <OpenGL/OpenGL.h>
#include <OpenGL/CGLTypes.h> #include <OpenGL/CGLTypes.h>
@ -151,10 +153,10 @@ class WindowlessCglApplication {
bool tryCreateContext(const Configuration& configuration); bool tryCreateContext(const Configuration& configuration);
private: private:
CGLContextObj context; CGLContextObj _glContext;
CGLPixelFormatObj pixelFormat; CGLPixelFormatObj _pixelFormat;
Platform::Context* c; std::unique_ptr<Platform::Context> _context;
}; };
/** /**

Loading…
Cancel
Save