PolyCocoaCore.mm 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430
  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 "PolyCocoaCore.h"
  20. #include <iostream>
  21. using namespace Polycode;
  22. long getThreadID() {
  23. return (long)pthread_self();
  24. }
  25. CocoaCore::CocoaCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate) {
  26. eventMutex = createMutex();
  27. // NSLog(@"BUNDLE: %@", [[NSBundle mainBundle] bundlePath]);
  28. chdir([[[[NSBundle mainBundle] bundlePath] stringByAppendingString:@"/Contents/Resources"] UTF8String]);
  29. NSOpenGLPixelFormatAttribute attrs[32];
  30. int atindx = 0;
  31. attrs[atindx++] = NSOpenGLPFADoubleBuffer;
  32. attrs[atindx++] = NSOpenGLPFADepthSize;
  33. attrs[atindx++] = 32;
  34. if(aaLevel > 0) {
  35. attrs[atindx++] = NSOpenGLPFASampleBuffers;
  36. attrs[atindx++] = 1;
  37. attrs[atindx++] = NSOpenGLPFASamples;
  38. attrs[atindx++] = aaLevel;
  39. attrs[atindx++] = NSOpenGLPFAMultisample;
  40. }
  41. attrs[atindx++] = NSOpenGLPFANoRecovery;
  42. if(fullScreen) {
  43. // attrs[atindx++] = NSOpenGLPFAFullScreen;
  44. // attrs[atindx++] = NSOpenGLPFAScreenMask;
  45. // attrs[atindx++] = CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay);
  46. }
  47. attrs[atindx++] = NSOpenGLPFAAccelerated;
  48. attrs[atindx++] = nil;
  49. /*
  50. NSOpenGLPixelFormatAttribute attrs[] =
  51. {
  52. NSOpenGLPFADoubleBuffer,
  53. NSOpenGLPFADepthSize, 16,
  54. // NSOpenGLPFAFullScreen,
  55. // NSOpenGLPFAScreenMask,
  56. // CGDisplayIDToOpenGLDisplayMask(kCGDirectMainDisplay),
  57. NSOpenGLPFASampleBuffers, 1,
  58. NSOpenGLPFASamples, aaLevel,
  59. NSOpenGLPFANoRecovery,
  60. // NSOpenGLPFAWindow,
  61. NSOpenGLPFAMultisample,
  62. // NSOpenGLPFAAccelerated,
  63. // NSOpenGLPFAAccumSize, 0,
  64. nil
  65. };
  66. */
  67. // [view lockContext];
  68. [view setCore:this];
  69. NSOpenGLPixelFormat *format = [[NSOpenGLPixelFormat alloc] initWithAttributes:attrs];
  70. if(!format) {
  71. NSLog(@"Error creating pixel format!\n");
  72. }
  73. context = [[NSOpenGLContext alloc] initWithFormat: format shareContext:nil];
  74. if (context == nil) {
  75. NSLog(@"Failed to create open gl context");
  76. }
  77. if(false) {
  78. // [view enterFullScreenMode:[NSScreen mainScreen] withOptions:0];
  79. // [view removeFromSuperview];
  80. // [[view window] orderOut:nil];
  81. // CGDisplayCapture (kCGDirectMainDisplay ) ;
  82. // CGDisplaySwitchToMode (kCGDirectMainDisplay, CGDisplayBestModeForParameters (kCGDirectMainDisplay, 32, xRes, yRes, NULL) );
  83. // [context makeCurrentContext];
  84. // [context setFullScreen];
  85. // [context flushBuffer];
  86. // CGDisplayCapture (kCGDirectMainDisplay ) ;
  87. // [context clearDrawable];
  88. // [context release];
  89. } else {
  90. [view clearGLContext];
  91. [view setOpenGLContext:context];
  92. [context setView: view];
  93. }
  94. if(fullScreen) {
  95. // [view enterFullScreenMode:[NSScreen mainScreen] withOptions:0];
  96. }
  97. glView = view;
  98. initTime = mach_absolute_time();
  99. // while(![view isContextReady]) {
  100. // }
  101. renderer = new OpenGLRenderer();
  102. services->setRenderer(renderer);
  103. //[view unlockContext];
  104. setVideoMode(xRes,yRes,fullScreen, vSync, aaLevel, anisotropyLevel);
  105. CoreServices::getInstance()->installModule(new GLSLShaderModule());
  106. }
  107. void CocoaCore::copyStringToClipboard(const String& str) {
  108. NSPasteboard *pb = [NSPasteboard generalPasteboard];
  109. NSArray *types = [NSArray arrayWithObjects:NSStringPboardType, nil];
  110. [pb declareTypes:types owner:glView];
  111. //NSString *nsstr = [NSString stringWithCharacters: (unichar*) str.c_str() length: str.length()];
  112. char* data = (char*)str.c_str();
  113. unsigned size = str.size() * sizeof(char);
  114. NSString* nsstr = [[[NSString alloc] initWithBytes:data length:size encoding:NSUTF32LittleEndianStringEncoding] autorelease];
  115. [pb setString: nsstr forType:NSStringPboardType];
  116. }
  117. String CocoaCore::getClipboardString() {
  118. }
  119. void CocoaCore::setVideoMode(int xRes, int yRes, bool fullScreen, bool vSync, int aaLeve, int anisotropyLevel) {
  120. this->xRes = xRes;
  121. this->yRes = yRes;
  122. this->fullScreen = fullScreen;
  123. this->aaLevel = aaLevel;
  124. renderer->setAnisotropyAmount(anisotropyLevel);
  125. renderer->Resize(xRes, yRes);
  126. // CoreServices::getInstance()->getMaterialManager()->reloadProgramsAndTextures();
  127. dispatchEvent(new Event(), EVENT_CORE_RESIZE);
  128. // NSRect visibleFrame = [[NSScreen mainScreen] visibleFrame];
  129. // NSRect frame = NSMakeRect([[glView window] frame].origin.x, [[glView window] frame].origin.y, xRes, yRes);
  130. // frame.origin.x = (visibleFrame.size.width - frame.size.width) * 0.5;
  131. // frame.origin.y = (visibleFrame.size.height - frame.size.height) * (9.0/10.0);
  132. // [[glView window] setFrame: frame display: YES animate: NO];
  133. // if(!fullScreen) {
  134. [[glView window] setContentSize: NSMakeSize(xRes, yRes)];
  135. // } else {
  136. // CGDisplaySwitchToMode (kCGDirectMainDisplay, CGDisplayBestModeForParameters (kCGDirectMainDisplay, 32, xRes, yRes, NULL) );
  137. // }
  138. if(fullScreen) {
  139. CGDisplaySwitchToMode (kCGDirectMainDisplay, CGDisplayBestModeForParameters (kCGDirectMainDisplay, 32, xRes, yRes, NULL) );
  140. [glView enterFullScreenMode:[NSScreen mainScreen] withOptions:[NSDictionary dictionaryWithObjectsAndKeys:
  141. nil]];
  142. }
  143. GLint sync = 0;
  144. if(vSync) {
  145. sync =1 ;
  146. }
  147. [context setValues:&sync forParameter:NSOpenGLCPSwapInterval];
  148. /*
  149. if(aaLevel > 0) {
  150. glEnable( GL_MULTISAMPLE_ARB );
  151. } else {
  152. glDisable( GL_MULTISAMPLE_ARB );
  153. }
  154. */
  155. }
  156. void CocoaCore::resizeTo(int xRes, int yRes) {
  157. this->xRes = xRes;
  158. this->yRes = yRes;
  159. renderer->Resize(xRes, yRes);
  160. dispatchEvent(new Event(), EVENT_CORE_RESIZE);
  161. }
  162. vector<Polycode::Rectangle> CocoaCore::getVideoModes() {
  163. vector<Polycode::Rectangle> retVector;
  164. return retVector;
  165. }
  166. CocoaCore::~CocoaCore() {
  167. if(fullScreen) {
  168. [glView exitFullScreenModeWithOptions:nil];
  169. }
  170. [glView clearGLContext];
  171. [context release];
  172. }
  173. void *ManagedThreadFunc(void *data) {
  174. Threaded *target = static_cast<Threaded*>(data);
  175. target->runThread();
  176. return NULL;
  177. }
  178. void CocoaCore::createThread(Threaded *target) {
  179. pthread_t thread;
  180. pthread_create( &thread, NULL, ManagedThreadFunc, (void*)target);
  181. }
  182. void CocoaCore::lockMutex(CoreMutex *mutex) {
  183. PosixMutex *m = (PosixMutex*) mutex;
  184. pthread_mutex_lock(&m->pMutex);
  185. }
  186. void CocoaCore::unlockMutex(CoreMutex *mutex) {
  187. PosixMutex *m = (PosixMutex*) mutex;
  188. pthread_mutex_unlock(&m->pMutex);
  189. }
  190. CoreMutex *CocoaCore::createMutex() {
  191. PosixMutex *mutex = new PosixMutex();
  192. pthread_mutex_init(&mutex->pMutex, NULL);
  193. return mutex;
  194. }
  195. unsigned int CocoaCore::getTicks() {
  196. uint64_t time = mach_absolute_time();
  197. double conversion = 0.0;
  198. mach_timebase_info_data_t info;
  199. mach_timebase_info( &info );
  200. conversion = 1e-9 * (double) info.numer / (double) info.denom;
  201. return (((double)(time - initTime)) * conversion) * 1000.0f;
  202. }
  203. void CocoaCore::enableMouse(bool newval) {
  204. if(newval)
  205. CGDisplayShowCursor(kCGDirectMainDisplay);
  206. else
  207. CGDisplayHideCursor(kCGDirectMainDisplay);
  208. Core::enableMouse(newval);
  209. }
  210. void CocoaCore::setCursor(int cursorType) {
  211. NSCursor *newCursor;
  212. switch(cursorType) {
  213. case CURSOR_TEXT:
  214. newCursor = [NSCursor IBeamCursor];
  215. break;
  216. case CURSOR_POINTER:
  217. newCursor = [NSCursor pointingHandCursor];
  218. break;
  219. case CURSOR_CROSSHAIR:
  220. newCursor = [NSCursor crosshairCursor];
  221. break;
  222. case CURSOR_RESIZE_LEFT_RIGHT:
  223. newCursor = [NSCursor resizeLeftRightCursor];
  224. break;
  225. case CURSOR_RESIZE_UP_DOWN:
  226. newCursor = [NSCursor resizeUpDownCursor];
  227. break;
  228. default:
  229. newCursor = [NSCursor arrowCursor];
  230. break;
  231. }
  232. [glView setCurrentCursor:newCursor];
  233. [glView resetCursorRects];
  234. [[glView window] invalidateCursorRectsForView: glView];
  235. }
  236. void CocoaCore::checkEvents() {
  237. lockMutex(eventMutex);
  238. CocoaEvent event;
  239. for(int i=0; i < cocoaEvents.size(); i++) {
  240. event = cocoaEvents[i];
  241. switch(event.eventGroup) {
  242. case CocoaEvent::INPUT_EVENT:
  243. switch(event.eventCode) {
  244. case InputEvent::EVENT_MOUSEMOVE:
  245. input->setDeltaPosition(lastMouseX - event.mouseX, lastMouseY - event.mouseY);
  246. lastMouseX = event.mouseX;
  247. lastMouseY = event.mouseY;
  248. input->setMousePosition(event.mouseX, event.mouseY, getTicks());
  249. break;
  250. case InputEvent::EVENT_MOUSEDOWN:
  251. input->setMouseButtonState(event.mouseButton, true, getTicks());
  252. break;
  253. case InputEvent::EVENT_MOUSEWHEEL_UP:
  254. input->mouseWheelUp(getTicks());
  255. break;
  256. case InputEvent::EVENT_MOUSEWHEEL_DOWN:
  257. input->mouseWheelDown(getTicks());
  258. break;
  259. case InputEvent::EVENT_MOUSEUP:
  260. input->setMouseButtonState(event.mouseButton, false, getTicks());
  261. break;
  262. case InputEvent::EVENT_KEYDOWN:
  263. input->setKeyState(event.keyCode, event.unicodeChar, true, getTicks());
  264. break;
  265. case InputEvent::EVENT_KEYUP:
  266. input->setKeyState(event.keyCode, event.unicodeChar, false, getTicks());
  267. break;
  268. }
  269. break;
  270. }
  271. }
  272. cocoaEvents.clear();
  273. unlockMutex(eventMutex);
  274. }
  275. void CocoaCore::createFolder(const String& folderPath) {
  276. [[NSFileManager defaultManager] createDirectoryAtPath:[NSString stringWithUTF8String: folderPath.c_str()] withIntermediateDirectories:YES attributes:nil error:nil];
  277. }
  278. void CocoaCore::copyDiskItem(const String& itemPath, const String& destItemPath) {
  279. [[NSFileManager defaultManager] copyItemAtPath: [NSString stringWithUTF8String: itemPath.c_str()] toPath: [NSString stringWithUTF8String: destItemPath.c_str()] error: nil];
  280. }
  281. void CocoaCore::moveDiskItem(const String& itemPath, const String& destItemPath) {
  282. [[NSFileManager defaultManager] moveItemAtPath: [NSString stringWithUTF8String: itemPath.c_str()] toPath: [NSString stringWithUTF8String: destItemPath.c_str()] error: nil];
  283. }
  284. void CocoaCore::removeDiskItem(const String& itemPath) {
  285. [[NSFileManager defaultManager] removeItemAtPath: [NSString stringWithUTF8String: itemPath.c_str()] error:nil];
  286. }
  287. String CocoaCore::openFolderPicker() {
  288. NSOpenPanel *attachmentPanel = [NSOpenPanel openPanel];
  289. [attachmentPanel setCanChooseFiles:NO];
  290. [attachmentPanel setCanCreateDirectories: YES];
  291. [attachmentPanel setCanChooseDirectories:YES];
  292. if ( [attachmentPanel runModalForDirectory:nil file:nil] == NSOKButton )
  293. {
  294. // files and directories selected.
  295. NSArray* files = [attachmentPanel filenames];
  296. NSString* fileName = [files objectAtIndex:0];
  297. return [fileName UTF8String];
  298. } else {
  299. return [@"" UTF8String];
  300. }
  301. }
  302. vector<String> CocoaCore::openFilePicker(vector<CoreFileExtension> extensions, bool allowMultiple) {
  303. vector<String> retVector;
  304. NSOpenPanel *attachmentPanel = [NSOpenPanel openPanel];
  305. [attachmentPanel setCanChooseFiles:YES];
  306. [attachmentPanel setCanCreateDirectories: YES];
  307. [attachmentPanel setCanChooseDirectories:NO];
  308. [attachmentPanel setAllowsMultipleSelection: allowMultiple];
  309. NSMutableArray *types = [[NSMutableArray alloc] init];
  310. for(int i=0; i < extensions.size(); i++) {
  311. CoreFileExtension extInfo = extensions[i];
  312. [types addObject: [NSString stringWithUTF8String: extInfo.extension.c_str()]];
  313. }
  314. if ( [attachmentPanel runModalForDirectory:nil file:nil types:types] == NSOKButton )
  315. {
  316. NSArray* files = [attachmentPanel filenames];
  317. NSString* fileName = [files objectAtIndex:0];
  318. retVector.push_back([fileName UTF8String]);
  319. } else {
  320. retVector.push_back("");
  321. }
  322. return retVector;
  323. }
  324. bool CocoaCore::Update() {
  325. if(!running)
  326. return false;
  327. lockMutex(CoreServices::getRenderMutex());
  328. checkEvents();
  329. renderer->BeginRender();
  330. updateCore();
  331. renderer->EndRender();
  332. [context flushBuffer];
  333. unlockMutex(CoreServices::getRenderMutex());
  334. doSleep();
  335. return running;
  336. }