PolyUWPCore.cpp 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340
  1. /*
  2. Copyright (C) 2015 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 "polycode/core/PolyUWPCore.h"
  20. #include "polycode/core/PolyBasicFileProvider.h"
  21. #include <ppltasks.h>
  22. #include "polycode/core/PolyOpenGLGraphicsInterface.h"
  23. using namespace concurrency;
  24. using namespace Polycode;
  25. using namespace ABI::Windows::Foundation;
  26. using namespace ABI::Windows::Storage;
  27. using namespace Microsoft::WRL;
  28. using namespace Microsoft::WRL::Wrappers;
  29. void UWPCoreMutex::lock() {
  30. mutex.lock();
  31. }
  32. void UWPCoreMutex::unlock() {
  33. mutex.unlock();
  34. }
  35. UWPCore::UWPCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate, int monitorIndex, bool retinaSupport)
  36. : Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate, monitorIndex) {
  37. m_Window = view->window;
  38. m_eglWindow = view->eglWindow;
  39. mEglDisplay = view->mEglDisplay;
  40. mEglContext = view->mEglContext;
  41. mEglSurface = view->mEglSurface;
  42. LARGE_INTEGER li;
  43. QueryPerformanceFrequency(&li);
  44. pcFreq = double(li.QuadPart) / 1000.0;
  45. lastMouseY = 0;
  46. lastMouseX = 0;
  47. fileProviders.push_back(new BasicFileProvider());
  48. renderer = new Renderer();
  49. renderer->setBackingResolutionScale(1.0, 1.0);
  50. graphicsInterface = new OpenGLGraphicsInterface();
  51. renderer->setGraphicsInterface(this, graphicsInterface);
  52. services->setRenderer(renderer);
  53. setVideoMode(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, retinaSupport);
  54. services->getSoundManager()->setAudioInterface(new XAudio2AudioInterface());
  55. eventMutex = createMutex();
  56. }
  57. UWPCore::~UWPCore() {
  58. }
  59. void UWPCore::Render() {
  60. renderer->beginFrame();
  61. services->Render(Polycode::Rectangle(0, 0, xRes, yRes));
  62. renderer->endFrame();
  63. }
  64. void UWPCore::checkEvents() {
  65. eventMutex->lock();
  66. UWPEvent event;
  67. for (int i = 0; i < systemInputEvents.size(); i++) {
  68. event = systemInputEvents[i];
  69. switch (event.eventGroup) {
  70. case UWPEvent::INPUT_EVENT:
  71. switch (event.eventCode) {
  72. case InputEvent::EVENT_MOUSEMOVE:
  73. input->setDeltaPosition(lastMouseX - event.mouseX, lastMouseY - event.mouseY);
  74. lastMouseX = event.mouseX;
  75. lastMouseY = event.mouseY;
  76. input->setMousePosition(event.mouseX, event.mouseY, getTicks());
  77. break;
  78. case InputEvent::EVENT_MOUSEDOWN:
  79. input->mousePosition.x = event.mouseX;
  80. input->mousePosition.y = event.mouseY;
  81. input->setMouseButtonState(event.mouseButton, true, getTicks());
  82. break;
  83. case InputEvent::EVENT_MOUSEWHEEL_UP:
  84. input->mouseWheelUp(getTicks());
  85. break;
  86. case InputEvent::EVENT_MOUSEWHEEL_DOWN:
  87. input->mouseWheelDown(getTicks());
  88. break;
  89. case InputEvent::EVENT_MOUSEUP:
  90. input->setMouseButtonState(event.mouseButton, false, getTicks());
  91. break;
  92. case InputEvent::EVENT_KEYDOWN:
  93. if (!checkSpecialKeyEvents(event.keyCode))
  94. input->setKeyState(event.keyCode, event.unicodeChar, true, getTicks());
  95. break;
  96. case InputEvent::EVENT_KEYUP:
  97. input->setKeyState(event.keyCode, event.unicodeChar, false, getTicks());
  98. break;
  99. case InputEvent::EVENT_TOUCHES_BEGAN:
  100. input->touchesBegan(event.touch, event.touches, getTicks());
  101. break;
  102. case InputEvent::EVENT_TOUCHES_ENDED:
  103. input->touchesEnded(event.touch, event.touches, getTicks());
  104. break;
  105. case InputEvent::EVENT_TOUCHES_MOVED:
  106. input->touchesMoved(event.touch, event.touches, getTicks());
  107. break;
  108. }
  109. break;
  110. case UWPEvent::SYSTEM_FOCUS_EVENT:
  111. switch (event.eventCode) {
  112. case Core::EVENT_LOST_FOCUS:
  113. loseFocus();
  114. break;
  115. case Core::EVENT_GAINED_FOCUS:
  116. gainFocus();
  117. break;
  118. }
  119. break;
  120. }
  121. }
  122. systemInputEvents.clear();
  123. eventMutex->unlock();
  124. }
  125. bool UWPCore::systemUpdate() {
  126. if (!running) {
  127. return false;
  128. }
  129. doSleep();
  130. updateCore();
  131. checkEvents();
  132. return running;
  133. }
  134. void UWPCore::setCursor(int cursorType) {
  135. }
  136. void launchThread(Threaded *target) {
  137. target->runThread();
  138. target->scheduledForRemoval = true;
  139. }
  140. void UWPCore::createThread(Threaded * target) {
  141. Core::createThread(target);
  142. std::thread *thread = new std::thread(launchThread, target);
  143. }
  144. CoreMutex *UWPCore::createMutex() {
  145. UWPCoreMutex *mutex = new UWPCoreMutex();
  146. return mutex;
  147. }
  148. void UWPCore::copyStringToClipboard(const String& str) {
  149. }
  150. String UWPCore::getClipboardString() {
  151. return "";
  152. }
  153. void UWPCore::createFolder(const String& folderPath) {
  154. }
  155. void UWPCore::copyDiskItem(const String& itemPath, const String& destItemPath) {
  156. }
  157. void UWPCore::moveDiskItem(const String& itemPath, const String& destItemPath) {
  158. }
  159. void UWPCore::removeDiskItem(const String& itemPath) {
  160. }
  161. String UWPCore::openFolderPicker() {
  162. return "";
  163. }
  164. std::vector<String> UWPCore::openFilePicker(std::vector<CoreFileExtension> extensions, bool allowMultiple) {
  165. std::vector<String> ret;
  166. return ret;
  167. }
  168. String UWPCore::saveFilePicker(std::vector<CoreFileExtension> extensions) {
  169. return "";
  170. }
  171. void UWPCore::handleVideoModeChange(VideoModeChangeInfo *modeInfo) {
  172. if (eglMakeCurrent(mEglDisplay, mEglSurface, mEglSurface, mEglContext) == EGL_FALSE)
  173. {
  174. assert(false);
  175. }
  176. }
  177. void UWPCore::flushRenderContext() {
  178. eglSwapBuffers(mEglDisplay, mEglSurface);
  179. }
  180. void UWPCore::openURL(String url) {
  181. }
  182. unsigned int UWPCore::getTicks() {
  183. LARGE_INTEGER li;
  184. QueryPerformanceCounter(&li);
  185. return (unsigned int)(li.QuadPart / pcFreq);
  186. return 0;
  187. }
  188. String UWPCore::executeExternalCommand(String command, String args, String inDirectory) {
  189. return "";
  190. }
  191. void wtoc(char* Dest, const WCHAR* Source)
  192. {
  193. int i = 0;
  194. while (Source[i] != '\0') {
  195. Dest[i] = (char)Source[i];
  196. ++i;
  197. }
  198. Dest[i] = 0;
  199. }
  200. void ctow(WCHAR* Dest, const char* Source)
  201. {
  202. int i = 0;
  203. while (Source[i] != '\0') {
  204. Dest[i] = (WCHAR)Source[i];
  205. ++i;
  206. }
  207. Dest[i] = 0;
  208. }
  209. bool UWPCore::systemParseFolder(const Polycode::String& pathString, bool showHidden, std::vector<OSFileEntry> &targetVector) {
  210. WIN32_FIND_DATA findFileData;
  211. WCHAR curDir[4096];
  212. GetCurrentDirectory(4096, curDir);
  213. WCHAR tmp[4096];
  214. memset(tmp, 0, sizeof(WCHAR) * 4096);
  215. ctow(tmp, pathString.c_str());
  216. WIN32_FILE_ATTRIBUTE_DATA fileData;
  217. GetFileAttributesEx(tmp, GetFileExInfoStandard, &fileData);
  218. if (!(fileData.dwFileAttributes != INVALID_FILE_ATTRIBUTES &&
  219. (fileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY))) {
  220. return true;
  221. }
  222. SetCurrentDirectory(tmp);
  223. HANDLE hFind = FindFirstFileEx(L"*", FindExInfoStandard, &findFileData, FindExSearchNameMatch, NULL, 0);
  224. if (hFind == INVALID_HANDLE_VALUE) {
  225. SetCurrentDirectory(curDir);
  226. return true;
  227. }
  228. do {
  229. String fname(findFileData.cFileName);
  230. if ((fname.c_str()[0] != '.' || (fname.c_str()[0] == '.' && showHidden)) && fname != "..") {
  231. if (findFileData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
  232. targetVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FOLDER));
  233. }
  234. else {
  235. targetVector.push_back(OSFileEntry(pathString, fname, OSFileEntry::TYPE_FILE));
  236. }
  237. }
  238. } while (FindNextFile(hFind, &findFileData));
  239. FindClose(hFind);
  240. SetCurrentDirectory(curDir);
  241. return true;
  242. }
  243. void UWPCore::handleSystemEvent(UWPEvent systemEvent) {
  244. eventMutex->lock();
  245. systemInputEvents.push_back(systemEvent);
  246. eventMutex->unlock();
  247. }
  248. void Core::getScreenInfo(int *width, int *height, int *hz) {
  249. }
  250. void UWPCore::setDeviceSize(Number x, Number y) {
  251. deviceWidth = x;
  252. deviceHeight = y;
  253. renderer->setBackingResolutionScale(xRes/deviceWidth, yRes/deviceHeight);
  254. }
  255. Number UWPCore::getBackingXRes() {
  256. return deviceWidth;
  257. }
  258. Number UWPCore::getBackingYRes() {
  259. return deviceHeight;
  260. }