mirror of https://github.com/mosra/magnum.git
Browse Source
Finally something that can be used for: * Unit tests * Resource compilation * Benchmarks * ... I also desperately need something similar for Windows (and Mac OS, too).pull/7/head
5 changed files with 226 additions and 2 deletions
@ -0,0 +1,90 @@ |
|||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "WindowlessGlxApplication.h" |
||||||
|
|
||||||
|
#include <Utility/Debug.h> |
||||||
|
|
||||||
|
#include "Context.h" |
||||||
|
|
||||||
|
#define None 0L // redef Xlib nonsense
|
||||||
|
|
||||||
|
namespace Magnum { namespace Platform { |
||||||
|
|
||||||
|
WindowlessGlxApplication::WindowlessGlxApplication(int&, char**) { |
||||||
|
display = XOpenDisplay(nullptr); |
||||||
|
|
||||||
|
/* Check version */ |
||||||
|
int major, minor; |
||||||
|
glXQueryVersion(display, &major, &minor); |
||||||
|
if(major == 1 && minor < 4) { |
||||||
|
Error() << "WindowlessGlxApplication: GLX version 1.4 or greater is required."; |
||||||
|
std::exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
/* Choose config */ |
||||||
|
int configCount = 0; |
||||||
|
static const int fbAttributes[] = { None }; |
||||||
|
GLXFBConfig* configs = glXChooseFBConfig(display, DefaultScreen(display), fbAttributes, &configCount); |
||||||
|
if(!configCount) { |
||||||
|
Error() << "WindowlessGlxApplication: no supported framebuffer configuration found."; |
||||||
|
std::exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
GLint contextAttributes[] = { |
||||||
|
#ifdef MAGNUM_TARGET_GLES |
||||||
|
GLX_CONTEXT_MAJOR_VERSION_ARB, 2, |
||||||
|
GLX_CONTEXT_MINOR_VERSION_ARB, 0, |
||||||
|
GLX_CONTEXT_PROFILE_MASK_ARB, GLX_CONTEXT_ES2_PROFILE_BIT_EXT, |
||||||
|
#endif |
||||||
|
0 |
||||||
|
}; |
||||||
|
|
||||||
|
/** @todo Use some extension wrangler for this, not GLEW, as it apparently needs context to create context, yo dawg wtf. */ |
||||||
|
PFNGLXCREATECONTEXTATTRIBSARBPROC glXCreateContextAttribsARB = (PFNGLXCREATECONTEXTATTRIBSARBPROC) glXGetProcAddress((const GLubyte*)"glXCreateContextAttribsARB"); |
||||||
|
context = glXCreateContextAttribsARB(display, configs[0], 0, True, contextAttributes); |
||||||
|
if(!context) { |
||||||
|
Error() << "WindowlessGlxApplication: cannot create context."; |
||||||
|
std::exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
/* Create pbuffer */ |
||||||
|
int pbufferAttributes[] = { |
||||||
|
GLX_PBUFFER_WIDTH, 32, |
||||||
|
GLX_PBUFFER_HEIGHT, 32, |
||||||
|
None |
||||||
|
}; |
||||||
|
pbuffer = glXCreatePbuffer(display, configs[0], pbufferAttributes); |
||||||
|
|
||||||
|
XFree(configs); |
||||||
|
|
||||||
|
/* Set OpenGL context as current */ |
||||||
|
if(!glXMakeContextCurrent(display, pbuffer, pbuffer, context)) { |
||||||
|
Error() << "WindowlessGlxApplication: cannot make context current"; |
||||||
|
std::exit(1); |
||||||
|
} |
||||||
|
|
||||||
|
/* Initialize extension wrangler */ |
||||||
|
ExtensionWrangler::initialize(ExtensionWrangler::ExperimentalFeatures::Enable); |
||||||
|
|
||||||
|
c = new Context; |
||||||
|
} |
||||||
|
|
||||||
|
WindowlessGlxApplication::~WindowlessGlxApplication() { |
||||||
|
glXMakeCurrent(display, None, nullptr); |
||||||
|
glXDestroyContext(display, context); |
||||||
|
} |
||||||
|
|
||||||
|
}} |
||||||
@ -0,0 +1,107 @@ |
|||||||
|
#ifndef Magnum_Platform_WindowlessGlxApplication_h |
||||||
|
#define Magnum_Platform_WindowlessGlxApplication_h |
||||||
|
/*
|
||||||
|
Copyright © 2010, 2011, 2012 Vladimír Vondruš <mosra@centrum.cz> |
||||||
|
|
||||||
|
This file is part of Magnum. |
||||||
|
|
||||||
|
Magnum is free software: you can redistribute it and/or modify |
||||||
|
it under the terms of the GNU Lesser General Public License version 3 |
||||||
|
only, as published by the Free Software Foundation. |
||||||
|
|
||||||
|
Magnum is distributed in the hope that it will be useful, |
||||||
|
but WITHOUT ANY WARRANTY; without even the implied warranty of |
||||||
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
||||||
|
GNU Lesser General Public License version 3 for more details. |
||||||
|
*/ |
||||||
|
|
||||||
|
/** @file
|
||||||
|
* @brief Class Magnum::Platform::WindowlessGlxApplication |
||||||
|
*/ |
||||||
|
|
||||||
|
#include "Magnum.h" |
||||||
|
#include <GL/glx.h> |
||||||
|
#include <X11/Xlib.h> |
||||||
|
#include <X11/Xutil.h> |
||||||
|
/* undef Xlib nonsense to avoid conflicts */ |
||||||
|
#undef None |
||||||
|
#undef Always |
||||||
|
|
||||||
|
#include "AbstractContextHandler.h" |
||||||
|
|
||||||
|
namespace Magnum { namespace Platform { |
||||||
|
|
||||||
|
/**
|
||||||
|
@brief Windowless GLX application |
||||||
|
|
||||||
|
@section WindowlessGlxApplication-usage Usage |
||||||
|
|
||||||
|
Place your code into exec(). The subclass can be then used directly in |
||||||
|
`main()` - see convenience macro MAGNUM_WINDOWLESSGLXAPPLICATION_MAIN(). |
||||||
|
@code |
||||||
|
class MyApplication: public Magnum::Platform::WindowlessGlxApplication { |
||||||
|
// implement required methods...
|
||||||
|
}; |
||||||
|
MAGNUM_WINDOWLESSGLXAPPLICATION_MAIN(MyApplication) |
||||||
|
@endcode |
||||||
|
*/ |
||||||
|
class WindowlessGlxApplication { |
||||||
|
public: |
||||||
|
/**
|
||||||
|
* @brief Constructor |
||||||
|
* @param argc Count of arguments of `main()` function |
||||||
|
* @param argv Arguments of `main()` function |
||||||
|
* |
||||||
|
* Creates window with double-buffered OpenGL 3.2 core context or |
||||||
|
* OpenGL ES 2.0 context, if targetting OpenGL ES. |
||||||
|
*/ |
||||||
|
WindowlessGlxApplication(int& argc, char** argv); |
||||||
|
|
||||||
|
~WindowlessGlxApplication(); |
||||||
|
|
||||||
|
/**
|
||||||
|
* @brief Execute application |
||||||
|
* @return Value for returning from `main()`. |
||||||
|
*/ |
||||||
|
virtual int exec() = 0; |
||||||
|
|
||||||
|
private: |
||||||
|
Display* display; |
||||||
|
GLXContext context; |
||||||
|
GLXPbuffer pbuffer; |
||||||
|
|
||||||
|
Context* c; |
||||||
|
}; |
||||||
|
|
||||||
|
/** @hideinitializer
|
||||||
|
@brief Entry point for windowless GLX application |
||||||
|
@param className Class name |
||||||
|
|
||||||
|
Can be used as equivalent to the following code to achieve better portability, |
||||||
|
see @ref portability-applications for more information. |
||||||
|
@code |
||||||
|
int main(int argc, char** argv) { |
||||||
|
className app(argc, argv); |
||||||
|
return app.exec(); |
||||||
|
} |
||||||
|
@endcode |
||||||
|
When no other application header is included this macro is also aliased to |
||||||
|
`MAGNUM_APPLICATION_MAIN()`. |
||||||
|
*/ |
||||||
|
#define MAGNUM_WINDOWLESSGLXAPPLICATION_MAIN(className) \ |
||||||
|
int main(int argc, char** argv) { \
|
||||||
|
className app(argc, argv); \
|
||||||
|
return app.exec(); \
|
||||||
|
} |
||||||
|
|
||||||
|
#ifndef DOXYGEN_GENERATING_OUTPUT |
||||||
|
#ifndef MAGNUM_APPLICATION_MAIN |
||||||
|
#define MAGNUM_APPLICATION_MAIN(className) MAGNUM_WINDOWLESSGLXAPPLICATION_MAIN(className) |
||||||
|
#else |
||||||
|
#undef MAGNUM_APPLICATION_MAIN |
||||||
|
#endif |
||||||
|
#endif |
||||||
|
|
||||||
|
}} |
||||||
|
|
||||||
|
#endif |
||||||
Loading…
Reference in new issue