🛠️🐜 Antkeeper superbuild with dependencies included https://antkeeper.com
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

1169 lines
34 KiB

  1. #include "MacOpenGLWindow.h"
  2. #define GL_DO_NOT_WARN_IF_MULTI_GL_VERSION_HEADERS_INCLUDED
  3. #import <Cocoa/Cocoa.h>
  4. #include "OpenGLInclude.h"
  5. #include <stdlib.h>
  6. #include <stdio.h>
  7. #include <stddef.h>
  8. #include <string.h>
  9. enum
  10. {
  11. MY_MAC_ALTKEY=1,
  12. MY_MAC_SHIFTKEY=2,
  13. MY_MAC_CONTROL_KEY=4
  14. };
  15. /* report GL errors, if any, to stderr */
  16. static void checkError(const char *functionName)
  17. {
  18. GLenum error;
  19. while (( error = glGetError() ) != GL_NO_ERROR)
  20. {
  21. fprintf (stderr, "GL error 0x%X detected in %s\n", error, functionName);
  22. }
  23. }
  24. void dumpInfo(void)
  25. {
  26. printf ("Vendor: %s\n", glGetString (GL_VENDOR));
  27. printf ("Renderer: %s\n", glGetString (GL_RENDERER));
  28. printf ("Version: %s\n", glGetString (GL_VERSION));
  29. printf ("GLSL: %s\n", glGetString (GL_SHADING_LANGUAGE_VERSION));
  30. checkError ("dumpInfo");
  31. }
  32. // -------------------- View ------------------------
  33. @interface TestView : NSView
  34. {
  35. NSOpenGLContext* m_context;
  36. int m_lastWidth;
  37. int m_lastHeight;
  38. bool m_requestClose;
  39. b3ResizeCallback m_resizeCallback;
  40. }
  41. -(void)drawRect:(NSRect)rect;
  42. -(void) MakeContext:(int) openglVersion;
  43. -(void) MakeCurrent;
  44. -(float) GetWindowWidth;
  45. -(float) GetWindowHeight;
  46. -(BOOL) GetRequestClose;
  47. - (BOOL)windowShouldClose:(id)sender;
  48. -(void) setResizeCallback:(b3ResizeCallback) callback;
  49. -(b3ResizeCallback) getResizeCallback;
  50. -(NSOpenGLContext*) getContext;
  51. @end
  52. float loop;
  53. #define Pi 3.1415
  54. @implementation TestView
  55. - (BOOL)windowShouldClose:(id)sender
  56. {
  57. m_requestClose = true;
  58. return false;
  59. }
  60. -(BOOL) GetRequestClose
  61. {
  62. return m_requestClose;
  63. }
  64. -(float) GetWindowWidth
  65. {
  66. return m_lastWidth;
  67. }
  68. -(float) GetWindowHeight
  69. {
  70. return m_lastHeight;
  71. }
  72. -(b3ResizeCallback) getResizeCallback
  73. {
  74. return m_resizeCallback;
  75. }
  76. -(NSOpenGLContext*) getContext
  77. {
  78. return m_context;
  79. }
  80. -(void)setResizeCallback:(b3ResizeCallback)callback
  81. {
  82. m_resizeCallback = callback;
  83. }
  84. -(void)drawRect:(NSRect)rect
  85. {
  86. if (([self frame].size.width != m_lastWidth) || ([self frame].size.height != m_lastHeight))
  87. {
  88. m_lastWidth = [self frame].size.width;
  89. m_lastHeight = [self frame].size.height;
  90. // Only needed on resize:
  91. [m_context clearDrawable];
  92. // reshape([self frame].size.width, [self frame].size.height);
  93. float width = [self frame].size.width;
  94. float height = [self frame].size.height;
  95. // Get view dimensions in pixels
  96. // glViewport(0,0,10,10);
  97. if (m_resizeCallback)
  98. {
  99. (*m_resizeCallback)(width,height);
  100. }
  101. #ifndef NO_OPENGL3
  102. NSRect backingBounds = [self convertRectToBacking:[self bounds]];
  103. GLsizei backingPixelWidth = (GLsizei)(backingBounds.size.width),
  104. backingPixelHeight = (GLsizei)(backingBounds.size.height);
  105. // Set viewport
  106. glViewport(0, 0, backingPixelWidth, backingPixelHeight);
  107. #else
  108. glViewport(0,0,(GLsizei)width,(GLsizei)height);
  109. #endif
  110. }
  111. [m_context setView: self];
  112. [m_context makeCurrentContext];
  113. // Draw
  114. //display();
  115. [m_context flushBuffer];
  116. [NSOpenGLContext clearCurrentContext];
  117. loop = loop + 0.1;
  118. }
  119. -(void) MakeContext :(int) openglVersion
  120. {
  121. // NSWindow *w;
  122. NSOpenGLPixelFormat *fmt;
  123. m_requestClose = false;
  124. #ifndef NO_OPENGL3
  125. if (openglVersion==3)
  126. {
  127. NSOpenGLPixelFormatAttribute attrs[] =
  128. {
  129. NSOpenGLPFAOpenGLProfile,
  130. NSOpenGLProfileVersion3_2Core,
  131. NSOpenGLPFADoubleBuffer,
  132. NSOpenGLPFADepthSize, 32,
  133. NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)8,
  134. (NSOpenGLPixelFormatAttribute)0
  135. };
  136. // Init GL context
  137. fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: (NSOpenGLPixelFormatAttribute*)attrs];
  138. } else
  139. #endif
  140. {
  141. NSOpenGLPixelFormatAttribute attrs[] =
  142. {
  143. NSOpenGLPFADoubleBuffer,
  144. NSOpenGLPFADepthSize, 32,
  145. NSOpenGLPFAStencilSize, (NSOpenGLPixelFormatAttribute)8,
  146. (NSOpenGLPixelFormatAttribute)0
  147. };
  148. // Init GL context
  149. fmt = [[NSOpenGLPixelFormat alloc] initWithAttributes: (NSOpenGLPixelFormatAttribute*)attrs];
  150. }
  151. m_context = [[NSOpenGLContext alloc] initWithFormat: fmt shareContext: nil];
  152. [fmt release];
  153. [m_context makeCurrentContext];
  154. checkError("makeCurrentContext");
  155. }
  156. -(void) MakeCurrent
  157. {
  158. [m_context makeCurrentContext];
  159. }
  160. -(void)windowWillClose:(NSNotification *)note
  161. {
  162. [[NSApplication sharedApplication] terminate:self];
  163. }
  164. @end
  165. struct MacOpenGLWindowInternalData
  166. {
  167. MacOpenGLWindowInternalData()
  168. {
  169. m_myApp = 0;
  170. m_myview = 0;
  171. m_pool = 0;
  172. m_window = 0;
  173. m_width = -1;
  174. m_height = -1;
  175. m_exitRequested = false;
  176. }
  177. NSApplication* m_myApp;
  178. TestView* m_myview;
  179. NSAutoreleasePool* m_pool;
  180. NSWindow* m_window;
  181. int m_width;
  182. int m_height;
  183. bool m_exitRequested;
  184. };
  185. MacOpenGLWindow::MacOpenGLWindow()
  186. :m_internalData(0),
  187. m_mouseX(0),
  188. m_mouseY(0),
  189. m_modifierFlags(0),
  190. m_mouseMoveCallback(0),
  191. m_mouseButtonCallback(0),
  192. m_wheelCallback(0),
  193. m_keyboardCallback(0),
  194. m_retinaScaleFactor(1),
  195. m_allowRetina(true)
  196. {
  197. }
  198. MacOpenGLWindow::~MacOpenGLWindow()
  199. {
  200. if (m_internalData)
  201. closeWindow();
  202. }
  203. bool MacOpenGLWindow::isModifierKeyPressed(int key)
  204. {
  205. bool isPressed = false;
  206. switch (key)
  207. {
  208. case B3G_ALT:
  209. {
  210. isPressed = ((m_modifierFlags & MY_MAC_ALTKEY)!=0);
  211. break;
  212. };
  213. case B3G_SHIFT:
  214. {
  215. isPressed = ((m_modifierFlags & MY_MAC_SHIFTKEY)!=0);
  216. break;
  217. };
  218. case B3G_CONTROL:
  219. {
  220. isPressed = ((m_modifierFlags & MY_MAC_CONTROL_KEY )!=0);
  221. break;
  222. };
  223. default:
  224. {
  225. }
  226. };
  227. return isPressed;
  228. }
  229. float MacOpenGLWindow::getTimeInSeconds()
  230. {
  231. return 0.f;
  232. }
  233. void MacOpenGLWindow::setRenderCallback( b3RenderCallback renderCallback)
  234. {
  235. m_renderCallback = renderCallback;
  236. }
  237. void MacOpenGLWindow::setWindowTitle(const char* windowTitle)
  238. {
  239. [m_internalData->m_window setTitle:[NSString stringWithCString:windowTitle encoding:NSISOLatin1StringEncoding]] ;
  240. }
  241. void MacOpenGLWindow::createWindow(const b3gWindowConstructionInfo& ci)
  242. {
  243. if (m_internalData)
  244. closeWindow();
  245. int width = ci.m_width;
  246. int height = ci.m_height;
  247. const char* windowTitle = ci.m_title;
  248. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  249. m_internalData = new MacOpenGLWindowInternalData;
  250. m_internalData->m_width = width;
  251. m_internalData->m_height = height;
  252. m_internalData->m_pool = [NSAutoreleasePool new];
  253. m_internalData->m_myApp = [NSApplication sharedApplication];
  254. //myApp = [MyApp sharedApplication];
  255. //home();
  256. [NSApp setActivationPolicy:NSApplicationActivationPolicyRegular];
  257. id menubar = [[NSMenu new] autorelease];
  258. id appMenuItem = [[NSMenuItem new] autorelease];
  259. [menubar addItem:appMenuItem];
  260. [NSApp setMainMenu:menubar];
  261. id appMenu = [[NSMenu new] autorelease];
  262. id appName = [[NSProcessInfo processInfo] processName];
  263. id quitTitle = [@"Quit " stringByAppendingString:appName];
  264. id quitMenuItem = [[[NSMenuItem alloc] initWithTitle:quitTitle
  265. action:@selector(terminate:) keyEquivalent:@"q"] autorelease];
  266. [appMenu addItem:quitMenuItem];
  267. [appMenuItem setSubmenu:appMenu];
  268. NSMenuItem *fileMenuItem = [[NSMenuItem new] autorelease];
  269. NSMenu *fileMenu = [[NSMenu alloc] initWithTitle:@"File"];
  270. [fileMenuItem setSubmenu: fileMenu]; // was setMenu:
  271. NSMenuItem *newMenu = [[NSMenuItem alloc] initWithTitle:@"New" action:NULL keyEquivalent:@""];
  272. NSMenuItem *openMenu = [[NSMenuItem alloc] initWithTitle:@"Open" action:NULL keyEquivalent:@""];
  273. NSMenuItem *saveMenu = [[NSMenuItem alloc] initWithTitle:@"Save" action:NULL keyEquivalent:@""];
  274. [fileMenu addItem: newMenu];
  275. [fileMenu addItem: openMenu];
  276. [fileMenu addItem: saveMenu];
  277. [menubar addItem: fileMenuItem];
  278. // add Edit menu
  279. NSMenuItem *editMenuItem = [[NSMenuItem new] autorelease];
  280. NSMenu *menu = [[NSMenu allocWithZone:[NSMenu menuZone]]initWithTitle:@"Edit"];
  281. [editMenuItem setSubmenu: menu];
  282. NSMenuItem *copyItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]]initWithTitle:@"Copy" action:@selector(copy:) keyEquivalent:@"c"];
  283. [menu addItem:copyItem];
  284. [menubar addItem:editMenuItem];
  285. // [mainMenu setSubmenu:menu forItem:menuItem];
  286. //NSMenuItem *fileMenuItem = [[NSMenuItem alloc] initWithTitle: @"File"];
  287. /*[fileMenuItem setSubmenu: fileMenu]; // was setMenu:
  288. [fileMenuItem release];
  289. */
  290. /*NSMenu *newMenu;
  291. NSMenuItem *newItem;
  292. // Add the submenu
  293. newItem = [[NSMenuItem allocWithZone:[NSMenu menuZone]]
  294. initWithTitle:@"Flashy" action:NULL keyEquivalent:@""];
  295. newMenu = [[NSMenu allocWithZone:[NSMenu menuZone]]
  296. initWithTitle:@"Flashy"];
  297. [newItem setSubmenu:newMenu];
  298. [newMenu release];
  299. [[NSApp mainMenu] addItem:newItem];
  300. [newItem release];
  301. */
  302. NSRect frame = NSMakeRect(0., 0., width, height);
  303. m_internalData->m_window = [NSWindow alloc];
  304. [m_internalData->m_window initWithContentRect:frame
  305. styleMask:NSTitledWindowMask |NSResizableWindowMask| NSClosableWindowMask | NSMiniaturizableWindowMask
  306. backing:NSBackingStoreBuffered
  307. defer:false];
  308. [m_internalData->m_window setTitle:[NSString stringWithCString:windowTitle encoding:NSISOLatin1StringEncoding]] ;
  309. m_internalData->m_myview = [TestView alloc];
  310. [m_internalData->m_myview setResizeCallback:0];
  311. ///ci.m_resizeCallback];
  312. [m_internalData->m_myview initWithFrame: frame];
  313. // OpenGL init!
  314. [m_internalData->m_myview MakeContext : ci.m_openglVersion];
  315. // https://developer.apple.com/library/mac/#documentation/GraphicsAnimation/Conceptual/HighResolutionOSX/CapturingScreenContents/CapturingScreenContents.html#//apple_ref/doc/uid/TP40012302-CH10-SW1
  316. //support HighResolutionOSX for Retina Macbook
  317. if (ci.m_openglVersion>=3)
  318. {
  319. if (m_allowRetina)
  320. {
  321. [m_internalData->m_myview setWantsBestResolutionOpenGLSurface:YES];
  322. }
  323. }
  324. NSSize sz;
  325. sz.width = 1;
  326. sz.height = 1;
  327. // float newBackingScaleFactor = [m_internalData->m_window backingScaleFactor];
  328. dumpInfo();
  329. [m_internalData->m_window setContentView: m_internalData->m_myview];
  330. [m_internalData->m_window setDelegate:(id) m_internalData->m_myview];
  331. [m_internalData->m_window makeKeyAndOrderFront: nil];
  332. [m_internalData->m_myview MakeCurrent];
  333. m_internalData->m_width = m_internalData->m_myview.GetWindowWidth;
  334. m_internalData->m_height = m_internalData->m_myview.GetWindowHeight;
  335. [NSApp activateIgnoringOtherApps:YES];
  336. //[m_internalData->m_window setLevel:NSMainMenuWindowLevel];
  337. // [NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask];
  338. // [NSEvent addGlobalMonitorForEventsMatchingMask:NSMouseMovedMask handler:^(NSEvent *event)
  339. // {
  340. //[window setFrameOrigin:[NSEvent mouseLocation]];
  341. // NSPoint eventLocation = [m_internalData->m_window mouseLocationOutsideOfEventStream];
  342. // NSPoint eventLocation = [event locationInWindow];
  343. //NSPoint center = [m_internalData->m_myview convertPoint:eventLocation fromView:nil];
  344. // m_mouseX = center.x;
  345. // m_mouseY = [m_internalData->m_myview GetWindowHeight] - center.y;
  346. // printf("mouse coord = %f, %f\n",m_mouseX,m_mouseY);
  347. // if (m_mouseMoveCallback)
  348. // (*m_mouseMoveCallback)(m_mouseX,m_mouseY);
  349. // }];
  350. //see http://stackoverflow.com/questions/8238473/cant-get-nsmousemoved-events-from-nexteventmatchingmask-with-an-nsopenglview
  351. /* ProcessSerialNumber psn;
  352. GetCurrentProcess(&psn);
  353. TransformProcessType(&psn, kProcessTransformToForegroundApplication);
  354. SetFrontProcess(&psn);
  355. */
  356. #ifndef NO_OPENGL3
  357. m_retinaScaleFactor = [m_internalData->m_myview convertSizeToBacking:sz].width;
  358. #else
  359. m_retinaScaleFactor=1.f;
  360. #endif
  361. [m_internalData->m_myApp finishLaunching];
  362. [pool release];
  363. }
  364. void MacOpenGLWindow::runMainLoop()
  365. {
  366. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  367. // FILE* dump = fopen ("/Users/erwincoumans/yes.txt","wb");
  368. // fclose(dump);
  369. [pool release];
  370. }
  371. void MacOpenGLWindow::closeWindow()
  372. {
  373. delete m_internalData;
  374. m_internalData = 0;
  375. }
  376. extern float m_azi;
  377. extern float m_ele;
  378. extern float m_cameraDistance;
  379. /*
  380. * Summary:
  381. * Virtual keycodes
  382. *
  383. * Discussion:
  384. * These constants are the virtual keycodes defined originally in
  385. * Inside Mac Volume V, pg. V-191. They identify physical keys on a
  386. * keyboard. Those constants with "ANSI" in the name are labeled
  387. * according to the key position on an ANSI-standard US keyboard.
  388. * For example, kVK_ANSI_A indicates the virtual keycode for the key
  389. * with the letter 'A' in the US keyboard layout. Other keyboard
  390. * layouts may have the 'A' key label on a different physical key;
  391. * in this case, pressing 'A' will generate a different virtual
  392. * keycode.
  393. */
  394. enum {
  395. kVK_ANSI_A = 0x00,
  396. kVK_ANSI_S = 0x01,
  397. kVK_ANSI_D = 0x02,
  398. kVK_ANSI_F = 0x03,
  399. kVK_ANSI_H = 0x04,
  400. kVK_ANSI_G = 0x05,
  401. kVK_ANSI_Z = 0x06,
  402. kVK_ANSI_X = 0x07,
  403. kVK_ANSI_C = 0x08,
  404. kVK_ANSI_V = 0x09,
  405. kVK_ANSI_B = 0x0B,
  406. kVK_ANSI_Q = 0x0C,
  407. kVK_ANSI_W = 0x0D,
  408. kVK_ANSI_E = 0x0E,
  409. kVK_ANSI_R = 0x0F,
  410. kVK_ANSI_Y = 0x10,
  411. kVK_ANSI_T = 0x11,
  412. kVK_ANSI_1 = 0x12,
  413. kVK_ANSI_2 = 0x13,
  414. kVK_ANSI_3 = 0x14,
  415. kVK_ANSI_4 = 0x15,
  416. kVK_ANSI_6 = 0x16,
  417. kVK_ANSI_5 = 0x17,
  418. kVK_ANSI_Equal = 0x18,
  419. kVK_ANSI_9 = 0x19,
  420. kVK_ANSI_7 = 0x1A,
  421. kVK_ANSI_Minus = 0x1B,
  422. kVK_ANSI_8 = 0x1C,
  423. kVK_ANSI_0 = 0x1D,
  424. kVK_ANSI_RightBracket = 0x1E,
  425. kVK_ANSI_O = 0x1F,
  426. kVK_ANSI_U = 0x20,
  427. kVK_ANSI_LeftBracket = 0x21,
  428. kVK_ANSI_I = 0x22,
  429. kVK_ANSI_P = 0x23,
  430. kVK_ANSI_L = 0x25,
  431. kVK_ANSI_J = 0x26,
  432. kVK_ANSI_Quote = 0x27,
  433. kVK_ANSI_K = 0x28,
  434. kVK_ANSI_Semicolon = 0x29,
  435. kVK_ANSI_Backslash = 0x2A,
  436. kVK_ANSI_Comma = 0x2B,
  437. kVK_ANSI_Slash = 0x2C,
  438. kVK_ANSI_N = 0x2D,
  439. kVK_ANSI_M = 0x2E,
  440. kVK_ANSI_Period = 0x2F,
  441. kVK_ANSI_Grave = 0x32,
  442. kVK_ANSI_KeypadDecimal = 0x41,
  443. kVK_ANSI_KeypadMultiply = 0x43,
  444. kVK_ANSI_KeypadPlus = 0x45,
  445. kVK_ANSI_KeypadClear = 0x47,
  446. kVK_ANSI_KeypadDivide = 0x4B,
  447. kVK_ANSI_KeypadEnter = 0x4C,
  448. kVK_ANSI_KeypadMinus = 0x4E,
  449. kVK_ANSI_KeypadEquals = 0x51,
  450. kVK_ANSI_Keypad0 = 0x52,
  451. kVK_ANSI_Keypad1 = 0x53,
  452. kVK_ANSI_Keypad2 = 0x54,
  453. kVK_ANSI_Keypad3 = 0x55,
  454. kVK_ANSI_Keypad4 = 0x56,
  455. kVK_ANSI_Keypad5 = 0x57,
  456. kVK_ANSI_Keypad6 = 0x58,
  457. kVK_ANSI_Keypad7 = 0x59,
  458. kVK_ANSI_Keypad8 = 0x5B,
  459. kVK_ANSI_Keypad9 = 0x5C
  460. };
  461. /* keycodes for keys that are independent of keyboard layout*/
  462. enum {
  463. kVK_Return = 0x24,
  464. kVK_Tab = 0x30,
  465. kVK_Space = 0x31,
  466. kVK_Delete = 0x33,
  467. kVK_Escape = 0x35,
  468. kVK_Command = 0x37,
  469. kVK_Shift = 0x38,
  470. kVK_CapsLock = 0x39,
  471. kVK_Option = 0x3A,
  472. kVK_Control = 0x3B,
  473. kVK_RightShift = 0x3C,
  474. kVK_RightOption = 0x3D,
  475. kVK_RightControl = 0x3E,
  476. kVK_Function = 0x3F,
  477. kVK_F17 = 0x40,
  478. kVK_VolumeUp = 0x48,
  479. kVK_VolumeDown = 0x49,
  480. kVK_Mute = 0x4A,
  481. kVK_F18 = 0x4F,
  482. kVK_F19 = 0x50,
  483. kVK_F20 = 0x5A,
  484. kVK_F5 = 0x60,
  485. kVK_F6 = 0x61,
  486. kVK_F7 = 0x62,
  487. kVK_F3 = 0x63,
  488. kVK_F8 = 0x64,
  489. kVK_F9 = 0x65,
  490. kVK_F11 = 0x67,
  491. kVK_F13 = 0x69,
  492. kVK_F16 = 0x6A,
  493. kVK_F14 = 0x6B,
  494. kVK_F10 = 0x6D,
  495. kVK_F12 = 0x6F,
  496. kVK_F15 = 0x71,
  497. kVK_Help = 0x72,
  498. kVK_Home = 0x73,
  499. kVK_PageUp = 0x74,
  500. kVK_ForwardDelete = 0x75,
  501. kVK_F4 = 0x76,
  502. kVK_End = 0x77,
  503. kVK_F2 = 0x78,
  504. kVK_PageDown = 0x79,
  505. kVK_F1 = 0x7A,
  506. kVK_LeftArrow = 0x7B,
  507. kVK_RightArrow = 0x7C,
  508. kVK_DownArrow = 0x7D,
  509. kVK_UpArrow = 0x7E
  510. };
  511. /* ISO keyboards only*/
  512. enum {
  513. kVK_ISO_Section = 0x0A
  514. };
  515. /* JIS keyboards only*/
  516. enum {
  517. kVK_JIS_Yen = 0x5D,
  518. kVK_JIS_Underscore = 0x5E,
  519. kVK_JIS_KeypadComma = 0x5F,
  520. kVK_JIS_Eisu = 0x66,
  521. kVK_JIS_Kana = 0x68
  522. };
  523. int getAsciiCodeFromVirtualKeycode(int virtualKeyCode)
  524. {
  525. int keycode = 0xffffffff;
  526. switch (virtualKeyCode)
  527. {
  528. case kVK_ANSI_A : {keycode = 'a'; break;}
  529. case kVK_ANSI_B : {keycode = 'b'; break;}
  530. case kVK_ANSI_C : {keycode = 'c'; break;}
  531. case kVK_ANSI_D : {keycode = 'd';break;}
  532. case kVK_ANSI_E : {keycode = 'e'; break;}
  533. case kVK_ANSI_F : {keycode = 'f'; break;}
  534. case kVK_ANSI_G : {keycode = 'g'; break;}
  535. case kVK_ANSI_H : {keycode = 'h'; break;}
  536. case kVK_ANSI_I : {keycode = 'i'; break;}
  537. case kVK_ANSI_J : {keycode = 'j'; break;}
  538. case kVK_ANSI_K : {keycode = 'k'; break;}
  539. case kVK_ANSI_L : {keycode = 'l'; break;}
  540. case kVK_ANSI_M : {keycode = 'm'; break;}
  541. case kVK_ANSI_N : {keycode = 'n'; break;}
  542. case kVK_ANSI_O : {keycode = 'o'; break;}
  543. case kVK_ANSI_P : {keycode = 'p'; break;}
  544. case kVK_ANSI_Q : {keycode = 'q'; break;}
  545. case kVK_ANSI_R : {keycode = 'r'; break;}
  546. case kVK_ANSI_S : {keycode = 's';break;}
  547. case kVK_ANSI_T : {keycode = 't'; break;}
  548. case kVK_ANSI_U : {keycode = 'u'; break;}
  549. case kVK_ANSI_V : {keycode = 'v'; break;}
  550. case kVK_ANSI_W : {keycode = 'w'; break;}
  551. case kVK_ANSI_X : {keycode = 'x'; break;}
  552. case kVK_ANSI_Y : {keycode = 'y'; break;}
  553. case kVK_ANSI_Z : {keycode = 'z'; break;}
  554. case kVK_ANSI_1 : {keycode = '1'; break;}
  555. case kVK_ANSI_2 : {keycode = '2'; break;}
  556. case kVK_ANSI_3 : {keycode = '3'; break;}
  557. case kVK_ANSI_4 : {keycode = '4'; break;}
  558. case kVK_ANSI_5 : {keycode = '5'; break;}
  559. case kVK_ANSI_6 : {keycode = '6'; break;}
  560. case kVK_ANSI_7 : {keycode = '7'; break;}
  561. case kVK_ANSI_8 : {keycode = '8'; break;}
  562. case kVK_ANSI_9 : {keycode = '9'; break;}
  563. case kVK_ANSI_0 : {keycode = '0'; break;}
  564. case kVK_ANSI_Equal : {keycode = '='; break;}
  565. case kVK_ANSI_Minus : {keycode = '-'; break;}
  566. case kVK_Tab: {keycode = 9; break;}
  567. case kVK_Space: {keycode=' '; break;}
  568. case kVK_Escape: {keycode=27; break;}
  569. case kVK_Delete: {keycode=8; break;}
  570. case kVK_ForwardDelete: {keycode=B3G_INSERT; break;}
  571. case kVK_F1: {keycode = B3G_F1; break;}
  572. case kVK_F2: {keycode = B3G_F2; break;}
  573. case kVK_F3: {keycode = B3G_F3; break;}
  574. case kVK_F4: {keycode = B3G_F4; break;}
  575. case kVK_F5: {keycode = B3G_F5; break;}
  576. case kVK_F6: {keycode = B3G_F6; break;}
  577. case kVK_F7: {keycode = B3G_F7; break;}
  578. case kVK_F8: {keycode = B3G_F8; break;}
  579. case kVK_F9: {keycode = B3G_F9; break;}
  580. case kVK_F10: {keycode = B3G_F10; break;}
  581. case kVK_F11: {keycode = B3G_F11; break;}
  582. case kVK_F12: {keycode = B3G_F12; break;}
  583. case kVK_F13: {keycode = B3G_F13; break;}
  584. case kVK_F14: {keycode = B3G_F14; break;}
  585. case kVK_F15: {keycode = B3G_F15; break;}
  586. case kVK_LeftArrow: {keycode = B3G_LEFT_ARROW;break;}
  587. case kVK_RightArrow: {keycode = B3G_RIGHT_ARROW;break;}
  588. case kVK_UpArrow: {keycode = B3G_UP_ARROW;break;}
  589. case kVK_DownArrow: {keycode = B3G_DOWN_ARROW;break;}
  590. case kVK_PageUp :{keycode = B3G_PAGE_UP;break;}
  591. case kVK_PageDown :{keycode = B3G_PAGE_DOWN;break;}
  592. case kVK_End :{keycode = B3G_END;break;}
  593. case kVK_Home :{keycode = B3G_HOME;break;}
  594. case kVK_Control: {keycode = B3G_CONTROL;break;}
  595. case kVK_Option: {keycode = B3G_ALT;break;}
  596. case kVK_ANSI_RightBracket : {keycode = ']'; break;}
  597. case kVK_ANSI_LeftBracket : {keycode = '['; break;}
  598. case kVK_ANSI_Quote : {keycode = '\''; break;}
  599. case kVK_ANSI_Semicolon : {keycode = ';'; break;}
  600. case kVK_ANSI_Backslash : {keycode = '\\'; break;}
  601. case kVK_ANSI_Comma : {keycode = ','; break;}
  602. case kVK_ANSI_Slash : {keycode = '/'; break;}
  603. case kVK_ANSI_Period : {keycode = '.'; break;}
  604. case kVK_ANSI_Grave : {keycode = '`'; break;}
  605. case kVK_ANSI_KeypadDecimal : {keycode = '.'; break;}
  606. case kVK_ANSI_KeypadMultiply : {keycode = '*'; break;}
  607. case kVK_ANSI_KeypadPlus : {keycode = '+'; break;}
  608. case kVK_ANSI_KeypadClear : {keycode = '?'; break;}
  609. case kVK_ANSI_KeypadDivide : {keycode = '/'; break;}
  610. case kVK_ANSI_KeypadEnter : {keycode = B3G_RETURN; break;}
  611. case kVK_ANSI_KeypadMinus : {keycode = '-'; break;}
  612. case kVK_ANSI_KeypadEquals : {keycode = '='; break;}
  613. case kVK_ANSI_Keypad0 : {keycode = '0'; break;}
  614. case kVK_ANSI_Keypad1 : {keycode = '1'; break;}
  615. case kVK_ANSI_Keypad2 : {keycode = '2'; break;}
  616. case kVK_ANSI_Keypad3 : {keycode = '3'; break;}
  617. case kVK_ANSI_Keypad4 : {keycode = '4'; break;}
  618. case kVK_ANSI_Keypad5 : {keycode = '5'; break;}
  619. case kVK_ANSI_Keypad6 : {keycode = '6'; break;}
  620. case kVK_ANSI_Keypad7 : {keycode = '7'; break;}
  621. case kVK_ANSI_Keypad8 : {keycode = '8'; break;}
  622. case kVK_ANSI_Keypad9 : {keycode = '9'; break;}
  623. case kVK_Return:
  624. {
  625. keycode = B3G_RETURN; break;
  626. }
  627. default:
  628. {
  629. printf("unknown keycode\n");
  630. }
  631. }
  632. return keycode;
  633. }
  634. void MacOpenGLWindow::startRendering()
  635. {
  636. NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
  637. GLint err = glGetError();
  638. assert(err==GL_NO_ERROR);
  639. NSEvent *event = nil;
  640. bool handledEvent = false;
  641. do
  642. {
  643. [pool release];
  644. pool = [[NSAutoreleasePool alloc] init];
  645. event = [m_internalData->m_myApp
  646. nextEventMatchingMask:NSAnyEventMask
  647. untilDate:[NSDate distantPast]
  648. inMode:NSDefaultRunLoopMode
  649. // inMode:NSEventTrackingRunLoopMode
  650. dequeue:YES];
  651. //NSShiftKeyMask = 1 << 17,
  652. //NSControlKeyMask
  653. if ([event type] == NSFlagsChanged)
  654. {
  655. int modifiers = [event modifierFlags];
  656. if (m_keyboardCallback)
  657. {
  658. if ((modifiers & NSShiftKeyMask))
  659. {
  660. m_keyboardCallback(B3G_SHIFT,1);
  661. m_modifierFlags |= MY_MAC_SHIFTKEY;
  662. }else
  663. {
  664. if (m_modifierFlags& MY_MAC_SHIFTKEY)
  665. {
  666. m_keyboardCallback(B3G_SHIFT,0);
  667. m_modifierFlags &= ~MY_MAC_SHIFTKEY;
  668. }
  669. }
  670. if (modifiers & NSControlKeyMask)
  671. {
  672. m_keyboardCallback(B3G_CONTROL,1);
  673. m_modifierFlags |= MY_MAC_CONTROL_KEY;
  674. } else
  675. {
  676. if (m_modifierFlags&MY_MAC_CONTROL_KEY)
  677. {
  678. m_keyboardCallback(B3G_CONTROL,0);
  679. m_modifierFlags &= ~MY_MAC_CONTROL_KEY;
  680. }
  681. }
  682. if (modifiers & NSAlternateKeyMask)
  683. {
  684. m_keyboardCallback(B3G_ALT,1);
  685. m_modifierFlags |= MY_MAC_ALTKEY;
  686. } else
  687. {
  688. if (m_modifierFlags&MY_MAC_ALTKEY)
  689. {
  690. m_keyboardCallback(B3G_ALT,0);
  691. m_modifierFlags &= ~MY_MAC_ALTKEY;
  692. }
  693. }
  694. //handle NSCommandKeyMask
  695. }
  696. }
  697. if ([event type] == NSKeyUp)
  698. {
  699. handledEvent = true;
  700. uint32 virtualKeycode = [event keyCode];
  701. int keycode = getAsciiCodeFromVirtualKeycode(virtualKeycode);
  702. // printf("keycode = %d\n", keycode);
  703. if (m_keyboardCallback)
  704. {
  705. int state = 0;
  706. m_keyboardCallback(keycode,state);
  707. }
  708. }
  709. if ([event type] == NSKeyDown)
  710. {
  711. handledEvent = true;
  712. if (![event isARepeat])
  713. {
  714. uint32 virtualKeycode = [event keyCode];
  715. int keycode = getAsciiCodeFromVirtualKeycode(virtualKeycode);
  716. //printf("keycode = %d\n", keycode);
  717. if (m_keyboardCallback)
  718. {
  719. int state = 1;
  720. m_keyboardCallback(keycode,state);
  721. }
  722. }
  723. }
  724. if (([event type]== NSRightMouseDown) || ([ event type]==NSLeftMouseDown)||([event type]==NSOtherMouseDown))
  725. {
  726. // printf("right mouse!");
  727. // float mouseX,mouseY;
  728. NSPoint eventLocation = [event locationInWindow];
  729. NSPoint center = [m_internalData->m_myview convertPoint:eventLocation fromView:nil];
  730. m_mouseX = center.x;
  731. m_mouseY = [m_internalData->m_myview GetWindowHeight] - center.y;
  732. int button=0;
  733. switch ([event type])
  734. {
  735. case NSLeftMouseDown:
  736. {
  737. button=0;
  738. break;
  739. }
  740. case NSOtherMouseDown:
  741. {
  742. button=1;
  743. break;
  744. }
  745. case NSRightMouseDown:
  746. {
  747. button=2;
  748. break;
  749. }
  750. default:
  751. {
  752. }
  753. };
  754. // printf("mouse coord = %f, %f\n",mouseX,mouseY);
  755. if (m_mouseButtonCallback)
  756. {
  757. //handledEvent = true;
  758. (*m_mouseButtonCallback)(button,1,m_mouseX,m_mouseY);
  759. }
  760. }
  761. if (([event type]== NSRightMouseUp) || ([ event type]==NSLeftMouseUp)||([event type]==NSOtherMouseUp))
  762. {
  763. // printf("right mouse!");
  764. // float mouseX,mouseY;
  765. NSPoint eventLocation = [event locationInWindow];
  766. NSPoint center = [m_internalData->m_myview convertPoint:eventLocation fromView:nil];
  767. m_mouseX = center.x;
  768. m_mouseY = [m_internalData->m_myview GetWindowHeight] - center.y;
  769. int button=0;
  770. switch ([event type])
  771. {
  772. case NSLeftMouseUp:
  773. {
  774. button=0;
  775. break;
  776. }
  777. case NSOtherMouseUp:
  778. {
  779. button=1;
  780. break;
  781. }
  782. case NSRightMouseUp:
  783. {
  784. button=2;
  785. break;
  786. }
  787. default:
  788. {
  789. }
  790. };
  791. // printf("mouse coord = %f, %f\n",mouseX,mouseY);
  792. if (m_mouseButtonCallback)
  793. (*m_mouseButtonCallback)(button,0,m_mouseX,m_mouseY);
  794. }
  795. if ([event type] == NSMouseMoved)
  796. {
  797. // http://stackoverflow.com/questions/4630509/how-to-find-if-the-mouse-is-over-a-view
  798. NSPoint globalLocation = [ NSEvent mouseLocation ];
  799. NSPoint windowLocation = [ [m_internalData->m_myview window] convertScreenToBase:globalLocation ];
  800. NSPoint viewLocation = [ m_internalData->m_myview convertPoint:windowLocation fromView: nil ];
  801. //NSPoint eventLocation = [NSEvent mouseLocation]; //[event locationInWindow];
  802. //NSPoint center = [m_internalData->m_myview convertPoint:eventLocation fromView:nil];
  803. NSPoint center = viewLocation;
  804. m_mouseX = center.x;
  805. m_mouseY = [m_internalData->m_myview GetWindowHeight] - center.y;
  806. //printf("mouse coord = %f, %f\n",m_mouseX,m_mouseY);
  807. if (m_mouseMoveCallback)
  808. {
  809. //handledEvent = true;
  810. (*m_mouseMoveCallback)(m_mouseX,m_mouseY);
  811. }
  812. }
  813. if (([event type] == NSLeftMouseDragged) || ([event type] == NSRightMouseDragged) || ([event type] == NSOtherMouseDragged))
  814. {
  815. int dx1, dy1;
  816. CGGetLastMouseDelta (&dx1, &dy1);
  817. NSPoint eventLocation = [event locationInWindow];
  818. NSPoint center = [m_internalData->m_myview convertPoint:eventLocation fromView:nil];
  819. m_mouseX = center.x;
  820. m_mouseY = [m_internalData->m_myview GetWindowHeight] - center.y;
  821. if (m_mouseMoveCallback)
  822. {
  823. //handledEvent = true;
  824. (*m_mouseMoveCallback)(m_mouseX,m_mouseY);
  825. }
  826. // printf("mouse coord = %f, %f\n",m_mouseX,m_mouseY);
  827. }
  828. if ([event type] == NSScrollWheel)
  829. {
  830. float dy, dx;
  831. dy = [ event deltaY ];
  832. dx = [ event deltaX ];
  833. if (m_wheelCallback)
  834. {
  835. handledEvent = true;
  836. (*m_wheelCallback)(dx,dy);
  837. }
  838. // m_cameraDistance -= dy*0.1;
  839. // m_azi -= dx*0.1;
  840. }
  841. if (!handledEvent)
  842. [m_internalData->m_myApp sendEvent:event];
  843. [m_internalData->m_myApp updateWindows];
  844. } while (event);
  845. err = glGetError();
  846. assert(err==GL_NO_ERROR);
  847. [m_internalData->m_myview MakeCurrent];
  848. err = glGetError();
  849. assert(err==GL_NO_ERROR);
  850. // glClearColor(1,1,1,1);
  851. glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT | GL_STENCIL_BUFFER_BIT); //clear buffers
  852. err = glGetError();
  853. assert(err==GL_NO_ERROR);
  854. //glCullFace(GL_BACK);
  855. //glFrontFace(GL_CCW);
  856. glEnable(GL_DEPTH_TEST);
  857. err = glGetError();
  858. assert(err==GL_NO_ERROR);
  859. float aspect;
  860. //b3Vector3 extents;
  861. if (m_internalData->m_width > m_internalData->m_height)
  862. {
  863. aspect = (float)m_internalData->m_width / (float)m_internalData->m_height;
  864. //extents.setValue(aspect * 1.0f, 1.0f,0);
  865. } else
  866. {
  867. aspect = (float)m_internalData->m_height / (float)m_internalData->m_width;
  868. //extents.setValue(1.0f, aspect*1.f,0);
  869. }
  870. err = glGetError();
  871. assert(err==GL_NO_ERROR);
  872. [pool release];
  873. }
  874. void MacOpenGLWindow::endRendering()
  875. {
  876. [m_internalData->m_myview MakeCurrent];
  877. //#ifndef NO_OPENGL3
  878. // glSwapAPPLE();
  879. //#else
  880. [[m_internalData->m_myview getContext] flushBuffer];
  881. // #endif
  882. }
  883. bool MacOpenGLWindow::requestedExit() const
  884. {
  885. bool closeme = m_internalData->m_myview.GetRequestClose;
  886. return m_internalData->m_exitRequested || closeme;
  887. }
  888. void MacOpenGLWindow::setRequestExit()
  889. {
  890. m_internalData->m_exitRequested = true;
  891. }
  892. #include <string.h>
  893. int MacOpenGLWindow::fileOpenDialog(char* filename, int maxNameLength)
  894. {
  895. //save/restore the OpenGL context, NSOpenPanel can mess it up
  896. //http://stackoverflow.com/questions/13987148/nsopenpanel-breaks-my-sdl-opengl-app
  897. NSOpenGLContext *foo = [NSOpenGLContext currentContext];
  898. // get the url of a .txt file
  899. NSOpenPanel * zOpenPanel = [NSOpenPanel openPanel];
  900. NSArray * zAryOfExtensions = [NSArray arrayWithObjects:@"urdf",@"bullet",@"obj",@"sdf",nil];
  901. [zOpenPanel setAllowedFileTypes:zAryOfExtensions];
  902. NSInteger zIntResult = [zOpenPanel runModal];
  903. [foo makeCurrentContext];
  904. if (zIntResult == NSFileHandlingPanelCancelButton) {
  905. NSLog(@"readUsingOpenPanel cancelled");
  906. return 0;
  907. }
  908. NSURL *zUrl = [zOpenPanel URL];
  909. if (zUrl)
  910. {
  911. //without the file://
  912. NSString *myString = [zUrl absoluteString];
  913. int slen = [myString length];
  914. if (slen < maxNameLength)
  915. {
  916. const char *cfilename=[myString UTF8String];
  917. //expect file:// at start of URL
  918. const char* p = strstr(cfilename, "file://");
  919. if (p==cfilename)
  920. {
  921. int actualLen = strlen(cfilename)-7;
  922. memcpy(filename, cfilename+7,actualLen);
  923. filename[actualLen]=0;
  924. return actualLen;
  925. }
  926. }
  927. }
  928. return 0;
  929. }
  930. void MacOpenGLWindow::getMouseCoordinates(int& x, int& y)
  931. {
  932. NSPoint pt = [m_internalData->m_window mouseLocationOutsideOfEventStream];
  933. m_mouseX = pt.x;
  934. m_mouseY = pt.y;
  935. x = m_mouseX;
  936. //our convention is x,y is upper left hand side
  937. y = [m_internalData->m_myview GetWindowHeight]-m_mouseY;
  938. }
  939. int MacOpenGLWindow::getWidth() const
  940. {
  941. if (m_internalData && m_internalData->m_myview && m_internalData->m_myview.GetWindowWidth)
  942. return m_internalData->m_myview.GetWindowWidth;
  943. return 0;
  944. }
  945. int MacOpenGLWindow::getHeight() const
  946. {
  947. if (m_internalData && m_internalData->m_myview && m_internalData->m_myview.GetWindowHeight)
  948. return m_internalData->m_myview.GetWindowHeight;
  949. return 0;
  950. }
  951. void MacOpenGLWindow::setResizeCallback(b3ResizeCallback resizeCallback)
  952. {
  953. [m_internalData->m_myview setResizeCallback:resizeCallback];
  954. if (resizeCallback)
  955. {
  956. (resizeCallback)(m_internalData->m_width,m_internalData->m_height);
  957. }
  958. }
  959. b3ResizeCallback MacOpenGLWindow::getResizeCallback()
  960. {
  961. return [m_internalData->m_myview getResizeCallback];
  962. }