BsMacOSContext.mm 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123
  1. //********************************** Banshee Engine (www.banshee3d.com) **************************************************//
  2. //**************** Copyright (c) 2016 Marko Pintera ([email protected]). All rights reserved. **********************//
  3. #include "MacOS/BsMacOSContext.h"
  4. #include "MacOS/BsMacOSGLSupport.h"
  5. #import <AppKit/AppKit.h>
  6. namespace bs::ct
  7. {
  8. struct MacOSContext::Pimpl
  9. {
  10. NSOpenGLContext* context = nil;
  11. bool dirty = true;
  12. };
  13. MacOSContext::MacOSContext(bool depthStencil, UINT32 msaaCount)
  14. { @autoreleasepool {
  15. NSOpenGLPixelFormatAttribute attributes[] =
  16. {
  17. NSOpenGLPFAOpenGLProfile, NSOpenGLProfileVersion4_1Core,
  18. NSOpenGLPFADoubleBuffer,
  19. NSOpenGLPFAAccelerated,
  20. NSOpenGLPFADepthSize, depthStencil ? 24U : 0U,
  21. NSOpenGLPFAStencilSize, depthStencil ? 8U : 0U,
  22. NSOpenGLPFASampleBuffers, msaaCount > 1 ? 1U : 0U,
  23. };
  24. UINT32 attrIdx = 0;
  25. attributes[attrIdx++] = NSOpenGLPFAOpenGLProfile;
  26. attributes[attrIdx++] = NSOpenGLProfileVersion4_1Core;
  27. attributes[attrIdx++] = NSOpenGLPFADoubleBuffer;
  28. attributes[attrIdx++] = NSOpenGLPFAAccelerated;
  29. attributes[attrIdx++] = NSOpenGLPFAColorSize;
  30. attributes[attrIdx++] = 32;
  31. if(depthStencil)
  32. {
  33. attributes[attrIdx++] = NSOpenGLPFADepthSize;
  34. attributes[attrIdx++] = 24;
  35. attributes[attrIdx++] = NSOpenGLPFAStencilSize;
  36. attributes[attrIdx++] = 8;
  37. }
  38. if(msaaCount > 1)
  39. {
  40. attributes[attrIdx++] = NSOpenGLPFAMultisample;
  41. attributes[attrIdx++] = NSOpenGLPFASampleBuffers;
  42. attributes[attrIdx++] = 1;
  43. attributes[attrIdx++] = NSOpenGLPFASamples;
  44. attributes[attrIdx++] = msaaCount;
  45. attributes[attrIdx++] = NSOpenGLPFANoRecovery;
  46. }
  47. attributes[attrIdx] = 0;
  48. NSOpenGLPixelFormat* pixelFormat = [[NSOpenGLPixelFormat alloc] initWithAttributes:attributes];
  49. m = bs_new<Pimpl>();
  50. m->context = [[NSOpenGLContext alloc] initWithFormat:pixelFormat shareContext:nil];
  51. markAsDirty();
  52. }}
  53. MacOSContext::~MacOSContext()
  54. {
  55. bs_delete(m);
  56. }
  57. void MacOSContext::setCurrent(const RenderWindow& renderWindow)
  58. {
  59. NSWindow* window;
  60. renderWindow.getCustomAttribute("WINDOW", &window);
  61. [m->context setView:[window contentView]];
  62. [m->context makeCurrentContext];
  63. [m->context update];
  64. m->dirty = false;
  65. }
  66. void MacOSContext::endCurrent()
  67. {
  68. [m->context setView:nil];
  69. [m->context clearDrawable];
  70. [NSOpenGLContext clearCurrentContext];
  71. }
  72. void MacOSContext::releaseContext()
  73. {
  74. m->context = nil;
  75. }
  76. void MacOSContext::markAsDirty()
  77. {
  78. m->dirty = true;
  79. }
  80. void MacOSContext::updateIfDirty()
  81. {
  82. if(m->dirty)
  83. {
  84. [m->context update];
  85. m->dirty = false;
  86. }
  87. }
  88. void MacOSContext::setVSync(int interval)
  89. {
  90. if(interval < 0)
  91. interval = 0;
  92. [m->context setValues:&interval forParameter:NSOpenGLCPSwapInterval];
  93. }
  94. void MacOSContext::swapBuffers()
  95. {
  96. [m->context flushBuffer];
  97. updateIfDirty();
  98. }
  99. }