PolyAGLCore.cpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239
  1. /*
  2. * PolyAGLCore.cpp
  3. * Poly
  4. *
  5. * Created by Ivan Safrin on 3/12/08.
  6. * Copyright 2008 __MyCompanyName__. All rights reserved.
  7. *
  8. */
  9. #include "PolyAGLCore.h"
  10. #include <iostream>
  11. using namespace Polycode;
  12. long getThreadID() {
  13. return (long)pthread_self();
  14. }
  15. AGLCore::AGLCore(WindowRef window, Polycode::Rectangle *clippingArea, int xRes, int yRes, bool fullScreen,int aaLevel, int frameRate) : Core(xRes, yRes, fullScreen,aaLevel, frameRate) {
  16. eventMutex = createMutex();
  17. mainWindow = window;
  18. initTime = mach_absolute_time();
  19. lockMutex(CoreServices::getRenderMutex());
  20. int numBuffers = 0;
  21. if(aaLevel > 0) {
  22. numBuffers = 1;
  23. }
  24. AGLPixelFormat pf;
  25. printf("AA: %d\n", aaLevel);
  26. if(aaLevel > 0) {
  27. GLint attrib[] = { AGL_RGBA,
  28. AGL_DEPTH_SIZE, 32,
  29. AGL_RED_SIZE, 8,
  30. AGL_GREEN_SIZE, 8,
  31. AGL_BLUE_SIZE, 8,
  32. AGL_ALPHA_SIZE, 8,
  33. AGL_DOUBLEBUFFER,
  34. AGL_SAMPLE_BUFFERS_ARB, numBuffers,
  35. AGL_SAMPLES_ARB, aaLevel,
  36. AGL_SUPERSAMPLE,
  37. AGL_NO_RECOVERY,
  38. AGL_NONE };
  39. pf = aglChoosePixelFormat (NULL, 0, attrib);
  40. } else {
  41. GLint attrib[] = { AGL_RGBA,
  42. AGL_DEPTH_SIZE, 32,
  43. AGL_RED_SIZE, 8,
  44. AGL_GREEN_SIZE, 8,
  45. AGL_BLUE_SIZE, 8,
  46. AGL_ALPHA_SIZE, 8,
  47. AGL_DOUBLEBUFFER,
  48. AGL_SAMPLE_BUFFERS_ARB, 0,
  49. AGL_SAMPLES_ARB, 0,
  50. AGL_SUPERSAMPLE,
  51. AGL_NO_RECOVERY,
  52. AGL_NONE };
  53. pf = aglChoosePixelFormat (NULL, 0, attrib);
  54. }
  55. aglContext = NULL;
  56. aglContext = aglCreateContext (pf, NULL);
  57. if (aglContext) {
  58. aglSetWindowRef(aglContext,window);
  59. aglSetCurrentContext (NULL);
  60. aglSetCurrentContext (aglContext);
  61. } else {
  62. Logger::log("ERROR CREATING AGL CONTEXT!\n");
  63. }
  64. aglDestroyPixelFormat (pf);
  65. if(clippingArea) {
  66. const GLint bufferRect[4] = {clippingArea->x, clippingArea->y, clippingArea->w, clippingArea->h};
  67. aglSetInteger(aglContext, AGL_BUFFER_RECT, bufferRect);
  68. aglEnable(aglContext, AGL_BUFFER_RECT);
  69. }
  70. renderer = new OpenGLRenderer();
  71. if(aaLevel > 0) {
  72. glEnable (GL_MULTISAMPLE_ARB);
  73. glHint (GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
  74. }
  75. services->setRenderer(renderer);
  76. renderer->Resize(xRes, yRes);
  77. unlockMutex(CoreServices::getRenderMutex());
  78. // setVideoMode(xRes, yRes, fullScreen, aaLevel);
  79. }
  80. void AGLCore::setVideoMode(int xRes, int yRes, bool fullScreen, int aaLevel) {
  81. lockMutex(CoreServices::getRenderMutex());
  82. dispatchEvent(new Event(), EVENT_CORE_RESIZE);
  83. aglSetCurrentContext (NULL);
  84. aglSetCurrentContext (aglContext);
  85. const GLint bufferRect[4] = {0, 0, xRes, yRes};
  86. aglSetInteger(aglContext, AGL_BUFFER_RECT, bufferRect);
  87. aglEnable(aglContext, AGL_BUFFER_RECT);
  88. if(fullScreen) {
  89. aglEnable (aglContext, AGL_FS_CAPTURE_SINGLE);
  90. aglSetFullScreen(aglContext, xRes, yRes, 0, 0);
  91. } else {
  92. }
  93. renderer->Resize(xRes, yRes);
  94. if(aaLevel > 0) {
  95. glEnable (GL_MULTISAMPLE_ARB);
  96. glHint (GL_MULTISAMPLE_FILTER_HINT_NV, GL_NICEST);
  97. }
  98. unlockMutex(CoreServices::getRenderMutex());
  99. CoreServices::getInstance()->getMaterialManager()->reloadProgramsAndTextures();
  100. }
  101. vector<Poly::Rectangle> AGLCore::getVideoModes() {
  102. vector<Poly::Rectangle> retVector;
  103. return retVector;
  104. }
  105. AGLCore::~AGLCore() {
  106. Logger::log("Destroying AGL core...\n");
  107. aglDestroyContext(aglContext);
  108. }
  109. void *AGLThreadFunc(void *data) {
  110. Threaded *target = static_cast<Threaded*>(data);
  111. target->runThread();
  112. return NULL;
  113. }
  114. void AGLCore::createThread(Threaded *target) {
  115. pthread_t thread;
  116. pthread_create( &thread, NULL, AGLThreadFunc, (void*)target);
  117. }
  118. void AGLCore::lockMutex(CoreMutex *mutex) {
  119. PosixMutex *m = (PosixMutex*) mutex;
  120. pthread_mutex_lock(&m->pMutex);
  121. }
  122. void AGLCore::unlockMutex(CoreMutex *mutex) {
  123. PosixMutex *m = (PosixMutex*) mutex;
  124. pthread_mutex_unlock(&m->pMutex);
  125. }
  126. CoreMutex *AGLCore::createMutex() {
  127. PosixMutex *mutex = new PosixMutex();
  128. pthread_mutex_init(&mutex->pMutex, NULL);
  129. return mutex;
  130. }
  131. unsigned int AGLCore::getTicks() {
  132. uint64_t time = mach_absolute_time();
  133. double conversion = 0.0;
  134. mach_timebase_info_data_t info;
  135. mach_timebase_info( &info );
  136. conversion = 1e-9 * (double) info.numer / (double) info.denom;
  137. return (((double)(time - initTime)) * conversion) * 1000.0f;
  138. }
  139. void AGLCore::enableMouse(bool newval) {
  140. Core::enableMouse(newval);
  141. }
  142. void AGLCore::checkEvents() {
  143. lockMutex(eventMutex);
  144. OSXEvent event;
  145. for(int i=0; i < osxEvents.size(); i++) {
  146. event = osxEvents[i];
  147. switch(event.eventGroup) {
  148. case OSXEvent::INPUT_EVENT:
  149. switch(event.eventCode) {
  150. case InputEvent::EVENT_MOUSEMOVE:
  151. input->setDeltaPosition(lastMouseX - event.mouseX, lastMouseY - event.mouseY);
  152. lastMouseX = event.mouseX;
  153. lastMouseY = event.mouseY;
  154. input->setMousePosition(event.mouseX, event.mouseY, getTicks());
  155. break;
  156. case InputEvent::EVENT_MOUSEDOWN:
  157. input->setMouseButtonState(event.mouseButton, true, getTicks());
  158. break;
  159. case InputEvent::EVENT_MOUSEUP:
  160. input->setMouseButtonState(event.mouseButton, false, getTicks());
  161. break;
  162. case InputEvent::EVENT_KEYDOWN:
  163. input->setKeyState(event.keyCode, (char)event.unicodeChar, true, getTicks());
  164. break;
  165. case InputEvent::EVENT_KEYUP:
  166. input->setKeyState(event.keyCode, (char)event.unicodeChar, false, getTicks());
  167. break;
  168. }
  169. break;
  170. }
  171. }
  172. osxEvents.clear();
  173. unlockMutex(eventMutex);
  174. }
  175. bool AGLCore::Update() {
  176. if(!running)
  177. return false;
  178. lockMutex(CoreServices::getRenderMutex());
  179. checkEvents();
  180. // aglSetCurrentContext(NULL);
  181. // aglSetCurrentContext(aglContext);
  182. renderer->BeginRender();
  183. updateCore();
  184. renderer->EndRender();
  185. aglSwapBuffers(aglContext);
  186. // aglSetCurrentContext(NULL);
  187. unlockMutex(CoreServices::getRenderMutex());
  188. doSleep();
  189. return running;
  190. }