/src/Core_WindowEvents/MacOSXCocoa/RenderContextPimpl_MACOSX.mm

http://github.com/Akranar/daguerreo · Objective C++ · 133 lines · 113 code · 20 blank · 0 comment · 8 complexity · b6232699f7c365fd601b8f40efe3cb9e MD5 · raw file

  1. #include "RenderContextPimpl_MACOSX.h"
  2. #include "Cocoa_WindowData.h"
  3. #include "../WindowPixelFormat.h"
  4. #include "../RenderContext.h"
  5. #include <iostream>
  6. RenderContextPimpl::RenderContextPimpl(WindowData * window_data, const WindowPixelFormat * pixel_format)
  7. {
  8. bool use_multisample = pixel_format->GetSampleBuffers() > 0 && pixel_format->GetSamples() > 1;
  9. if (use_multisample)
  10. {
  11. SetAdvancedMultisamplingPixelFormat(pixel_format);
  12. if(!ns_opengl_pixelformat)
  13. {
  14. std::cout << "Multisampling Pixel Format Creation Failed." << std::endl;
  15. SetAdvancedPixelFormat(pixel_format);
  16. }
  17. }
  18. else
  19. {
  20. SetAdvancedPixelFormat(pixel_format);
  21. }
  22. if (!ns_opengl_pixelformat)
  23. {
  24. std::cout << "Advanced Pixel Format Creation Failed." << std::endl;
  25. SetBasicPixelFormat(pixel_format);
  26. }
  27. if (!ns_opengl_pixelformat)
  28. {
  29. std::cout << "Basic Pixel Format Creation Failed" << std::endl;
  30. throw 1;
  31. }
  32. ns_opengl_context = [[NSOpenGLContext alloc] initWithFormat:ns_opengl_pixelformat shareContext:nil];
  33. [ns_opengl_context makeCurrentContext];
  34. if (!ns_opengl_context)
  35. {
  36. std::cout << "Context Creation Failed." << std::endl;
  37. throw 1;
  38. }
  39. }
  40. RenderContextPimpl::RenderContextPimpl(WindowData * window_data, const RenderContext * render_context)
  41. {
  42. ns_opengl_pixelformat = [[NSOpenGLPixelFormat alloc] initWithCGLPixelFormatObj:[render_context->GetPimpl()->GetPixelFormatObject() CGLPixelFormatObj]];
  43. ns_opengl_context = [[NSOpenGLContext alloc] initWithFormat:ns_opengl_pixelformat shareContext:render_context->GetPimpl()->GetContextObject()];
  44. [ns_opengl_context makeCurrentContext];
  45. if (!ns_opengl_context)
  46. {
  47. std::cout << "Context Creation Failed." << std::endl;
  48. throw 1;
  49. }
  50. }
  51. RenderContextPimpl::~RenderContextPimpl()
  52. {
  53. [ns_opengl_context release];
  54. [ns_opengl_pixelformat release];
  55. }
  56. void RenderContextPimpl::SetAdvancedMultisamplingPixelFormat(const WindowPixelFormat * pixel_format)
  57. {
  58. NSOpenGLPixelFormatAttribute attributes_multisampling[] =
  59. {
  60. NSOpenGLPFAWindow,
  61. NSOpenGLPFAAccelerated,
  62. NSOpenGLPFADoubleBuffer,
  63. NSOpenGLPFANoRecovery,
  64. NSOpenGLPFAMinimumPolicy,
  65. NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
  66. NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetTotalColorBits(),
  67. NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetAlphaBits(),
  68. NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetDepthBits(),
  69. NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetStencilBits(),
  70. NSOpenGLPFAMultisample,
  71. NSOpenGLPFASampleBuffers, pixel_format->GetSampleBuffers(),
  72. NSOpenGLPFASamples, pixel_format->GetSamples(),
  73. NSOpenGLPFASampleAlpha,
  74. (NSOpenGLPixelFormatAttribute)nil
  75. };
  76. ns_opengl_pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes_multisampling];
  77. }
  78. void RenderContextPimpl::SetAdvancedPixelFormat(const WindowPixelFormat * pixel_format)
  79. {
  80. NSOpenGLPixelFormatAttribute attributes_advanced[] =
  81. {
  82. NSOpenGLPFAWindow,
  83. NSOpenGLPFAAccelerated,
  84. NSOpenGLPFADoubleBuffer,
  85. NSOpenGLPFANoRecovery,
  86. NSOpenGLPFAMinimumPolicy,
  87. NSOpenGLPFAScreenMask, CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
  88. NSOpenGLPFAColorSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetTotalColorBits(),
  89. NSOpenGLPFAAlphaSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetAlphaBits(),
  90. NSOpenGLPFADepthSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetDepthBits(),
  91. NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute) pixel_format->GetStencilBits(),
  92. (NSOpenGLPixelFormatAttribute)nil
  93. };
  94. ns_opengl_pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes_advanced];
  95. }
  96. void RenderContextPimpl::SetBasicPixelFormat(const WindowPixelFormat * pixel_format)
  97. {
  98. NSOpenGLPixelFormatAttribute attributes_basic [] =
  99. {
  100. NSOpenGLPFAWindow,
  101. NSOpenGLPFADoubleBuffer,
  102. NSOpenGLPFAColorSize,
  103. (NSOpenGLPixelFormatAttribute)32,
  104. NSOpenGLPFADepthSize,
  105. (NSOpenGLPixelFormatAttribute)16,
  106. NSOpenGLPFAStencilSize,
  107. (NSOpenGLPixelFormatAttribute)8,
  108. NSOpenGLPFAScreenMask,
  109. CGDisplayIDToOpenGLDisplayMask(CGMainDisplayID()),
  110. (NSOpenGLPixelFormatAttribute)nil
  111. };
  112. ns_opengl_pixelformat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes_basic];
  113. }