macGLUtils.h 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //-----------------------------------------------------------------------------
  2. // Copyright (c) 2012 GarageGames, LLC
  3. //
  4. // Permission is hereby granted, free of charge, to any person obtaining a copy
  5. // of this software and associated documentation files (the "Software"), to
  6. // deal in the Software without restriction, including without limitation the
  7. // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  8. // sell copies of the Software, and to permit persons to whom the Software is
  9. // furnished to do so, subject to the following conditions:
  10. //
  11. // The above copyright notice and this permission notice shall be included in
  12. // all copies or substantial portions of the Software.
  13. //
  14. // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  15. // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  16. // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  17. // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  18. // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
  19. // FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
  20. // IN THE SOFTWARE.
  21. //-----------------------------------------------------------------------------
  22. #ifndef _MACGLUTILS_H_
  23. #define _MACGLUTILS_H_
  24. static Vector<NSOpenGLPixelFormatAttribute> _beginPixelFormatAttributesForDisplay(CGDirectDisplayID display)
  25. {
  26. Vector<NSOpenGLPixelFormatAttribute> attributes;
  27. attributes.reserve(16); // Most attribute lists won't exceed this
  28. attributes.push_back(NSOpenGLPFAScreenMask);
  29. attributes.push_back((NSOpenGLPixelFormatAttribute)CGDisplayIDToOpenGLDisplayMask(display));
  30. attributes.push_back(NSOpenGLPFANoRecovery);
  31. attributes.push_back(NSOpenGLPFADoubleBuffer);
  32. attributes.push_back(NSOpenGLPFAAccelerated);
  33. attributes.push_back(NSOpenGLPFAAuxBuffers);
  34. attributes.push_back((NSOpenGLPixelFormatAttribute)1);
  35. return attributes;
  36. }
  37. static void _addColorAlphaDepthStencilAttributes(Vector<NSOpenGLPixelFormatAttribute>& attributes, U32 color, U32 alpha, U32 depth, U32 stencil)
  38. {
  39. attributes.push_back(NSOpenGLPFAColorSize); attributes.push_back((NSOpenGLPixelFormatAttribute)color);
  40. attributes.push_back(NSOpenGLPFAAlphaSize); attributes.push_back((NSOpenGLPixelFormatAttribute)alpha);
  41. attributes.push_back(NSOpenGLPFADepthSize); attributes.push_back((NSOpenGLPixelFormatAttribute)depth);
  42. attributes.push_back(NSOpenGLPFAStencilSize); attributes.push_back((NSOpenGLPixelFormatAttribute)stencil);
  43. }
  44. static void _addFullscreenAttributes(Vector<NSOpenGLPixelFormatAttribute>& attributes)
  45. {
  46. attributes.push_back(NSOpenGLPFAFullScreen);
  47. }
  48. static void _endAttributeList(Vector<NSOpenGLPixelFormatAttribute>& attributes)
  49. {
  50. attributes.push_back((NSOpenGLPixelFormatAttribute)0);
  51. }
  52. static Vector<NSOpenGLPixelFormatAttribute> _createStandardPixelFormatAttributesForDisplay(CGDirectDisplayID display)
  53. {
  54. Vector<NSOpenGLPixelFormatAttribute> attributes = _beginPixelFormatAttributesForDisplay(display);
  55. _addColorAlphaDepthStencilAttributes(attributes, 24, 8, 24, 8);
  56. _endAttributeList(attributes);
  57. return attributes;
  58. }
  59. /// returns an opengl pixel format suitable for creating shared opengl contexts.
  60. static NSOpenGLPixelFormat* _createStandardPixelFormat()
  61. {
  62. Vector<NSOpenGLPixelFormatAttribute> attributes = _createStandardPixelFormatAttributesForDisplay(kCGDirectMainDisplay);
  63. NSOpenGLPixelFormat* fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes.address()];
  64. return fmt;
  65. }
  66. #endif