PolyCore.cpp 5.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243
  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. services = CoreServices::getInstance();
  45. input = new CoreInput();
  46. services->setCore(this);
  47. fps = 0;
  48. running = true;
  49. frames = 0;
  50. lastFrameTicks=0;
  51. lastFPSTicks=0;
  52. elapsed = 0;
  53. xRes = _xRes;
  54. yRes = _yRes;
  55. paused = false;
  56. pauseOnLoseFocus = false;
  57. if (fullScreen && !xRes && !yRes) {
  58. getScreenInfo(&xRes, &yRes, NULL);
  59. }
  60. mouseEnabled = true;
  61. lastSleepFrameTicks = 0;
  62. this->monitorIndex = monitorIndex;
  63. if(frameRate == 0)
  64. frameRate = 60;
  65. refreshInterval = 1000 / frameRate;
  66. threadedEventMutex = NULL;
  67. }
  68. void Core::enableMouse(bool newval) {
  69. mouseEnabled = newval;
  70. }
  71. int Core::getNumVideoModes() {
  72. return numVideoModes;
  73. }
  74. Number Core::getXRes() {
  75. return xRes;
  76. }
  77. Number Core::getYRes() {
  78. return yRes;
  79. }
  80. CoreInput *Core::getInput() {
  81. return input;
  82. }
  83. Core::~Core() {
  84. printf("Shutting down core");
  85. delete services;
  86. }
  87. void Core::Shutdown() {
  88. running = false;
  89. }
  90. String Core::getUserHomeDirectory() {
  91. return userHomeDirectory;
  92. }
  93. String Core::getDefaultWorkingDirectory() {
  94. return defaultWorkingDirectory;
  95. }
  96. Number Core::getElapsed() {
  97. return ((Number)elapsed)/1000.0f;
  98. }
  99. Number Core::getTicksFloat() {
  100. return ((Number)getTicks())/1000.0f;
  101. }
  102. void Core::setVideoModeIndex(int index, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel) {
  103. std::vector<Rectangle> resList = getVideoModes();
  104. if(index >= resList.size())
  105. return;
  106. setVideoMode(resList[index].w, resList[index].h, fullScreen, vSync, aaLevel, anisotropyLevel);
  107. }
  108. void Core::createThread(Threaded *target) {
  109. if(!threadedEventMutex) {
  110. threadedEventMutex = createMutex();
  111. }
  112. target->eventMutex = threadedEventMutex;
  113. target->core = this;
  114. lockMutex(threadedEventMutex);
  115. threads.push_back(target);
  116. unlockMutex(threadedEventMutex);
  117. }
  118. CoreMutex *Core::getEventMutex() {
  119. return eventMutex;
  120. }
  121. void Core::loseFocus() {
  122. if(pauseOnLoseFocus) {
  123. paused = true;
  124. }
  125. input->clearInput();
  126. dispatchEvent(new Event(), EVENT_LOST_FOCUS);
  127. }
  128. void Core::gainFocus() {
  129. if(pauseOnLoseFocus) {
  130. paused = false;
  131. }
  132. input->clearInput();
  133. dispatchEvent(new Event(), EVENT_GAINED_FOCUS);
  134. }
  135. void Core::removeThread(Threaded *thread) {
  136. if(threadedEventMutex){
  137. lockMutex(threadedEventMutex);
  138. for(int i=0; i < threads.size(); i++) {
  139. if(threads[i] == thread) {
  140. threads.erase(threads.begin() + i);
  141. return;
  142. }
  143. }
  144. unlockMutex(threadedEventMutex);
  145. }
  146. }
  147. void Core::updateCore() {
  148. frames++;
  149. frameTicks = getTicks();
  150. elapsed = frameTicks - lastFrameTicks;
  151. if(elapsed > 1000)
  152. elapsed = 1000;
  153. services->Update(elapsed, !paused);
  154. if(frameTicks-lastFPSTicks >= 1000) {
  155. fps = frames;
  156. frames = 0;
  157. lastFPSTicks = frameTicks;
  158. }
  159. lastFrameTicks = frameTicks;
  160. if(threadedEventMutex){
  161. lockMutex(threadedEventMutex);
  162. std::vector<Threaded*>::iterator iter = threads.begin();
  163. while (iter != threads.end()) {
  164. for(int j=0; j < (*iter)->eventQueue.size(); j++) {
  165. Event *event = (*iter)->eventQueue[j];
  166. (*iter)->__dispatchEvent(event, event->getEventCode());
  167. if(event->deleteOnDispatch)
  168. delete event;
  169. }
  170. (*iter)->eventQueue.clear();
  171. if((*iter)->scheduledForRemoval) {
  172. iter = threads.erase(iter);
  173. } else {
  174. ++iter;
  175. }
  176. }
  177. unlockMutex(threadedEventMutex);
  178. }
  179. }
  180. void Core::doSleep() {
  181. unsigned int ticks = getTicks();
  182. unsigned int ticksSinceLastFrame = ticks - lastSleepFrameTicks;
  183. if(ticksSinceLastFrame <= refreshInterval)
  184. #ifdef _WINDOWS
  185. Sleep((refreshInterval - ticksSinceLastFrame));
  186. #else
  187. usleep((refreshInterval - ticksSinceLastFrame) * 1000);
  188. #endif
  189. lastSleepFrameTicks = ticks;
  190. }
  191. Number Core::getFPS() {
  192. return fps;
  193. }
  194. CoreServices *Core::getServices() {
  195. return services;
  196. }
  197. }