PolyCore.cpp 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  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 "PolyCore.h"
  20. #include "PolyCoreInput.h"
  21. #include "PolyCoreServices.h"
  22. #ifdef _WINDOWS
  23. #include <windows.h>
  24. #else
  25. #include <unistd.h>
  26. #endif
  27. #include <time.h>
  28. namespace Polycode {
  29. TimeInfo::TimeInfo() {
  30. time_t rawtime;
  31. struct tm * timeinfo;
  32. time( &rawtime );
  33. timeinfo = localtime ( &rawtime );
  34. seconds = timeinfo->tm_sec;
  35. minutes = timeinfo->tm_min;
  36. hours = timeinfo->tm_hour;
  37. month = timeinfo->tm_mon;
  38. monthDay = timeinfo->tm_mday;
  39. weekDay = timeinfo->tm_wday;
  40. year = timeinfo->tm_year;
  41. yearDay = timeinfo->tm_yday;
  42. }
  43. Core::Core(int _xRes, int _yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex) : EventDispatcher() {
  44. int _hz;
  45. getScreenInfo(&defaultScreenWidth, &defaultScreenHeight, &_hz);
  46. services = CoreServices::getInstance();
  47. input = new CoreInput();
  48. services->setCore(this);
  49. fps = 0;
  50. timeLeftOver = 0.0;
  51. running = true;
  52. frames = 0;
  53. lastFrameTicks=0;
  54. lastFPSTicks=0;
  55. elapsed = 0;
  56. xRes = _xRes;
  57. yRes = _yRes;
  58. paused = false;
  59. pauseOnLoseFocus = false;
  60. if (fullScreen && !xRes && !yRes) {
  61. getScreenInfo(&xRes, &yRes, NULL);
  62. }
  63. mouseEnabled = true; mouseCaptured = false;
  64. lastSleepFrameTicks = 0;
  65. this->monitorIndex = monitorIndex;
  66. if(frameRate == 0)
  67. frameRate = 60;
  68. setFramerate(frameRate);
  69. threadedEventMutex = NULL;
  70. }
  71. int Core::getScreenWidth() {
  72. int width, height, hz;
  73. getScreenInfo(&width, &height, &hz);
  74. return width;
  75. }
  76. int Core::getScreenHeight() {
  77. int width, height, hz;
  78. getScreenInfo(&width, &height, &hz);
  79. return height;
  80. }
  81. void Core::setFramerate(int frameRate, int maxFixedCycles) {
  82. refreshInterval = 1000 / frameRate;
  83. fixedTimestep = 1.0 / ((double) frameRate);
  84. maxFixedElapsed = fixedTimestep * maxFixedCycles;
  85. }
  86. void Core::enableMouse(bool newval) {
  87. mouseEnabled = newval;
  88. }
  89. void Core::captureMouse(bool newval) {
  90. mouseCaptured = newval;
  91. }
  92. Number Core::getXRes() {
  93. return xRes;
  94. }
  95. Number Core::getYRes() {
  96. return yRes;
  97. }
  98. CoreInput *Core::getInput() {
  99. return input;
  100. }
  101. Core::~Core() {
  102. printf("Shutting down core");
  103. delete services;
  104. delete input;
  105. }
  106. void Core::Shutdown() {
  107. running = false;
  108. }
  109. String Core::getUserHomeDirectory() {
  110. return userHomeDirectory;
  111. }
  112. String Core::getDefaultWorkingDirectory() {
  113. return defaultWorkingDirectory;
  114. }
  115. Number Core::getElapsed() {
  116. return ((Number)elapsed)/1000.0f;
  117. }
  118. double Core::getTicksFloat() {
  119. return getTicks()/1000.0;
  120. }
  121. void Core::createThread(Threaded *target) {
  122. if(!threadedEventMutex) {
  123. threadedEventMutex = createMutex();
  124. }
  125. target->eventMutex = threadedEventMutex;
  126. target->core = this;
  127. lockMutex(threadedEventMutex);
  128. threads.push_back(target);
  129. unlockMutex(threadedEventMutex);
  130. }
  131. CoreMutex *Core::getEventMutex() {
  132. return eventMutex;
  133. }
  134. void Core::loseFocus() {
  135. if(pauseOnLoseFocus) {
  136. paused = true;
  137. }
  138. input->clearInput();
  139. dispatchEvent(new Event(), EVENT_LOST_FOCUS);
  140. }
  141. void Core::gainFocus() {
  142. if(pauseOnLoseFocus) {
  143. paused = false;
  144. }
  145. input->clearInput();
  146. dispatchEvent(new Event(), EVENT_GAINED_FOCUS);
  147. }
  148. void Core::removeThread(Threaded *thread) {
  149. if(threadedEventMutex){
  150. lockMutex(threadedEventMutex);
  151. for(int i=0; i < threads.size(); i++) {
  152. if(threads[i] == thread) {
  153. threads.erase(threads.begin() + i);
  154. return;
  155. }
  156. }
  157. unlockMutex(threadedEventMutex);
  158. }
  159. }
  160. bool Core::updateAndRender() {
  161. bool ret = Update();
  162. Render();
  163. return ret;
  164. }
  165. bool Core::fixedUpdate() {
  166. if(fixedElapsed < fixedTimestep) {
  167. return false;
  168. }
  169. services->fixedUpdate();
  170. fixedElapsed -= fixedTimestep;
  171. return true;
  172. }
  173. Number Core::getFixedTimestep() {
  174. return fixedTimestep;
  175. }
  176. bool Core::Update() {
  177. bool ret = systemUpdate();
  178. while(fixedUpdate()) {}
  179. return ret;
  180. }
  181. void Core::updateCore() {
  182. frames++;
  183. frameTicks = getTicks();
  184. elapsed = frameTicks - lastFrameTicks;
  185. if(elapsed > 1000)
  186. elapsed = 1000;
  187. timeLeftOver = fixedElapsed;
  188. fixedElapsed = (((Number)elapsed)/1000.0f) + timeLeftOver;
  189. if(fixedElapsed > maxFixedElapsed) {
  190. fixedElapsed = maxFixedElapsed;
  191. }
  192. services->Update(elapsed);
  193. if(frameTicks-lastFPSTicks >= 1000) {
  194. fps = frames;
  195. frames = 0;
  196. lastFPSTicks = frameTicks;
  197. }
  198. lastFrameTicks = frameTicks;
  199. if(threadedEventMutex){
  200. lockMutex(threadedEventMutex);
  201. std::vector<Threaded*>::iterator iter = threads.begin();
  202. while (iter != threads.end()) {
  203. for(int j=0; j < (*iter)->eventQueue.size(); j++) {
  204. Event *event = (*iter)->eventQueue[j];
  205. (*iter)->__dispatchEvent(event, event->getEventCode());
  206. if(event->deleteOnDispatch)
  207. delete event;
  208. }
  209. (*iter)->eventQueue.clear();
  210. if((*iter)->scheduledForRemoval) {
  211. iter = threads.erase(iter);
  212. } else {
  213. ++iter;
  214. }
  215. }
  216. unlockMutex(threadedEventMutex);
  217. }
  218. }
  219. void Core::doSleep() {
  220. unsigned int ticks = getTicks();
  221. unsigned int ticksSinceLastFrame = ticks - lastSleepFrameTicks;
  222. int sleepTimeMs = refreshInterval - ticksSinceLastFrame;
  223. if(sleepTimeMs > 0) {
  224. #ifdef _WINDOWS
  225. Sleep(sleepTimeMs);
  226. #else
  227. usleep(sleepTimeMs * 1000);
  228. #endif
  229. }
  230. lastSleepFrameTicks = getTicks();
  231. timeSleptMs = lastSleepFrameTicks - ticks;
  232. }
  233. Number Core::getFPS() {
  234. return fps;
  235. }
  236. CoreServices *Core::getServices() {
  237. return services;
  238. }
  239. }