PolyAGLCore.cpp 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252
  1. /*
  2. Copyright (C) 2011 by Ivan Safrin
  3. Permission is hereby granted, free of charge, to any person obtaining a copy
  4. of this software and associated documentation files (the "Software"), to deal
  5. in the Software without restriction, including without limitation the rights
  6. to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. copies of the Software, and to permit persons to whom the Software is
  8. furnished to do so, subject to the following conditions:
  9. The above copyright notice and this permission notice shall be included in
  10. all copies or substantial portions of the Software.
  11. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  12. IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  13. FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  14. AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  15. LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  16. OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  17. THE SOFTWARE.
  18. */
  19. #include "PolyAGLCore.h"
  20. #include <iostream>
  21. using namespace Polycode;
  22. long getThreadID() {
  23. return (long)pthread_self();
  24. }
  25. AGLCore::AGLCore(WindowRef window, Polycode::Rectangle *clippingArea, int xRes, int yRes, bool fullScreen,int aaLevel, int frameRate) : Core(xRes, yRes, fullScreen,aaLevel, frameRate) {
  26. eventMutex = createMutex();
  27. mainWindow = window;
  28. initTime = mach_absolute_time();
  29. lockMutex(CoreServices::getRenderMutex());
  30. int numBuffers = 0;
  31. if(aaLevel > 0) {
  32. numBuffers = 1;
  33. }
  34. AGLPixelFormat pf;
  35. printf("AA: %d\n", aaLevel);
  36. if(aaLevel > 0) {
  37. GLint attrib[] = { AGL_RGBA,
  38. AGL_DEPTH_SIZE, 32,
  39. AGL_RED_SIZE, 8,
  40. AGL_GREEN_SIZE, 8,
  41. AGL_BLUE_SIZE, 8,
  42. AGL_ALPHA_SIZE, 8,
  43. AGL_DOUBLEBUFFER,
  44. AGL_SAMPLE_BUFFERS_ARB, numBuffers,
  45. AGL_SAMPLES_ARB, aaLevel,
  46. AGL_SUPERSAMPLE,
  47. AGL_NO_RECOVERY,
  48. AGL_NONE };
  49. pf = aglChoosePixelFormat (NULL, 0, attrib);
  50. } else {
  51. GLint attrib[] = { AGL_RGBA,
  52. AGL_DEPTH_SIZE, 32,
  53. AGL_RED_SIZE, 8,
  54. AGL_GREEN_SIZE, 8,
  55. AGL_BLUE_SIZE, 8,
  56. AGL_ALPHA_SIZE, 8,
  57. AGL_DOUBLEBUFFER,
  58. AGL_SAMPLE_BUFFERS_ARB, 0,
  59. AGL_SAMPLES_ARB, 0,
  60. AGL_SUPERSAMPLE,
  61. AGL_NO_RECOVERY,
  62. AGL_NONE };
  63. pf = aglChoosePixelFormat (NULL, 0, attrib);
  64. }
  65. aglContext = NULL;
  66. aglContext = aglCreateContext (pf, NULL);
  67. if (aglContext) {
  68. aglSetWindowRef(aglContext,window);
  69. aglSetCurrentContext (NULL);
  70. aglSetCurrentContext (aglContext);
  71. } else {
  72. Logger::log("ERROR CREATING AGL CONTEXT!\n");
  73. }
  74. aglDestroyPixelFormat (pf);
  75. if(clippingArea) {
  76. const GLint bufferRect[4] = {clippingArea->x, clippingArea->y, clippingArea->w, clippingArea->h};
  77. aglSetInteger(aglContext, AGL_BUFFER_RECT, bufferRect);
  78. aglEnable(aglContext, AGL_BUFFER_RECT);
  79. }
  80. renderer = new OpenGLRenderer();
  81. if(aaLevel > 0) {
  82. glEnable (GL_MULTISAMPLE_ARB);
  83. glHint (GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
  84. }
  85. services->setRenderer(renderer);
  86. renderer->Resize(xRes, yRes);
  87. unlockMutex(CoreServices::getRenderMutex());
  88. // setVideoMode(xRes, yRes, fullScreen, aaLevel);
  89. }
  90. void AGLCore::setVideoMode(int xRes, int yRes, bool fullScreen, int aaLevel) {
  91. lockMutex(CoreServices::getRenderMutex());
  92. dispatchEvent(new Event(), EVENT_CORE_RESIZE);
  93. aglSetCurrentContext (NULL);
  94. aglSetCurrentContext (aglContext);
  95. const GLint bufferRect[4] = {0, 0, xRes, yRes};
  96. aglSetInteger(aglContext, AGL_BUFFER_RECT, bufferRect);
  97. aglEnable(aglContext, AGL_BUFFER_RECT);
  98. if(fullScreen) {
  99. aglEnable (aglContext, AGL_FS_CAPTURE_SINGLE);
  100. aglSetFullScreen(aglContext, xRes, yRes, 0, 0);
  101. } else {
  102. }
  103. renderer->Resize(xRes, yRes);
  104. if(aaLevel > 0) {
  105. glEnable (GL_MULTISAMPLE_ARB);
  106. glHint (GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
  107. }
  108. unlockMutex(CoreServices::getRenderMutex());
  109. CoreServices::getInstance()->getMaterialManager()->reloadProgramsAndTextures();
  110. }
  111. vector<Poly::Rectangle> AGLCore::getVideoModes() {
  112. vector<Poly::Rectangle> retVector;
  113. return retVector;
  114. }
  115. AGLCore::~AGLCore() {
  116. Logger::log("Destroying AGL core...\n");
  117. aglDestroyContext(aglContext);
  118. }
  119. void *AGLThreadFunc(void *data) {
  120. Threaded *target = static_cast<Threaded*>(data);
  121. target->runThread();
  122. return NULL;
  123. }
  124. void AGLCore::createThread(Threaded *target) {
  125. pthread_t thread;
  126. pthread_create( &thread, NULL, AGLThreadFunc, (void*)target);
  127. }
  128. void AGLCore::lockMutex(CoreMutex *mutex) {
  129. PosixMutex *m = (PosixMutex*) mutex;
  130. pthread_mutex_lock(&m->pMutex);
  131. }
  132. void AGLCore::unlockMutex(CoreMutex *mutex) {
  133. PosixMutex *m = (PosixMutex*) mutex;
  134. pthread_mutex_unlock(&m->pMutex);
  135. }
  136. CoreMutex *AGLCore::createMutex() {
  137. PosixMutex *mutex = new PosixMutex();
  138. pthread_mutex_init(&mutex->pMutex, NULL);
  139. return mutex;
  140. }
  141. unsigned int AGLCore::getTicks() {
  142. uint64_t time = mach_absolute_time();
  143. double conversion = 0.0;
  144. mach_timebase_info_data_t info;
  145. mach_timebase_info( &info );
  146. conversion = 1e-9 * (double) info.numer / (double) info.denom;
  147. return (((double)(time - initTime)) * conversion) * 1000.0f;
  148. }
  149. void AGLCore::enableMouse(bool newval) {
  150. Core::enableMouse(newval);
  151. }
  152. void AGLCore::checkEvents() {
  153. lockMutex(eventMutex);
  154. OSXEvent event;
  155. for(int i=0; i < osxEvents.size(); i++) {
  156. event = osxEvents[i];
  157. switch(event.eventGroup) {
  158. case OSXEvent::INPUT_EVENT:
  159. switch(event.eventCode) {
  160. case InputEvent::EVENT_MOUSEMOVE:
  161. input->setDeltaPosition(lastMouseX - event.mouseX, lastMouseY - event.mouseY);
  162. lastMouseX = event.mouseX;
  163. lastMouseY = event.mouseY;
  164. input->setMousePosition(event.mouseX, event.mouseY, getTicks());
  165. break;
  166. case InputEvent::EVENT_MOUSEDOWN:
  167. input->setMouseButtonState(event.mouseButton, true, getTicks());
  168. break;
  169. case InputEvent::EVENT_MOUSEUP:
  170. input->setMouseButtonState(event.mouseButton, false, getTicks());
  171. break;
  172. case InputEvent::EVENT_KEYDOWN:
  173. input->setKeyState(event.keyCode, (char)event.unicodeChar, true, getTicks());
  174. break;
  175. case InputEvent::EVENT_KEYUP:
  176. input->setKeyState(event.keyCode, (char)event.unicodeChar, false, getTicks());
  177. break;
  178. }
  179. break;
  180. }
  181. }
  182. osxEvents.clear();
  183. unlockMutex(eventMutex);
  184. }
  185. bool AGLCore::Update() {
  186. if(!running)
  187. return false;
  188. lockMutex(CoreServices::getRenderMutex());
  189. checkEvents();
  190. // aglSetCurrentContext(NULL);
  191. // aglSetCurrentContext(aglContext);
  192. renderer->BeginRender();
  193. updateCore();
  194. renderer->EndRender();
  195. aglSwapBuffers(aglContext);
  196. // aglSetCurrentContext(NULL);
  197. unlockMutex(CoreServices::getRenderMutex());
  198. doSleep();
  199. return running;
  200. }