stjernan

Untitled

Feb 2nd, 2016
201
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.10 KB | None | 0 0
  1. // Simple Apple OpenGL framework
  2.  
  3. #ifdef __APPLE__
  4.  
  5. #include <Carbon/Carbon.h>
  6. #include <AGL/agl.h>
  7. #include <OpenGL/gl.h>
  8. #include <OpenGL/glu.h>
  9. #include <OpenGL/glext.h>
  10.  
  11. static AGLContext setupAGL(WindowRef window)
  12. {  
  13.     if ((Ptr) kUnresolvedCFragSymbolAddress == (Ptr) aglChoosePixelFormat)
  14.         return 0;
  15.    
  16.     GLint attributes[] = { AGL_RGBA, AGL_DOUBLEBUFFER, AGL_DEPTH_SIZE, 16, AGL_NONE }; 
  17.    
  18.     AGLPixelFormat format = aglChoosePixelFormat(NULL, 0, attributes);
  19.     if (!format)
  20.         return 0;
  21.    
  22.     AGLContext context = aglCreateContext(format, 0);
  23.     if (!context)
  24.         return 0;
  25.  
  26.     aglDestroyPixelFormat(format);
  27.  
  28.     aglSetDrawable(context, GetWindowPort(window));
  29.    
  30.     aglSetCurrentContext(context);
  31.    
  32.     return context;
  33. }
  34.  
  35. static void cleanupAGL(AGLContext context)
  36. {
  37.     glFlush();
  38.     aglSetDrawable(context, 0);
  39.     aglSetCurrentContext(0);
  40.     aglDestroyContext(context);
  41. }
  42.  
  43. WindowRef window;
  44. AGLContext context;
  45.  
  46. static pascal OSErr quitEventHandler(const AppleEvent *appleEvt, AppleEvent *reply, SInt32 refcon)
  47. {
  48.     onQuit();
  49.     return false;
  50. }
  51.  
  52. bool openDisplay(const char title[], int width, int height)
  53. {
  54.     // create window
  55.    
  56.     Rect rect;
  57.     rect.top = 100;
  58.     rect.left = 100;
  59.     rect.bottom = rect.top + height;
  60.     rect.right = rect.left + width;
  61.    
  62.     // todo: center window and adjust client area to match width/height
  63.    
  64.     OSErr result = CreateNewWindow( kDocumentWindowClass,
  65.                                     (kWindowStandardDocumentAttributes |
  66.                                      kWindowStandardHandlerAttribute) &
  67.                                     ~kWindowResizableAttribute,
  68.                                     &rect, &window);
  69.    
  70.     if (result!=noErr)
  71.         return false;
  72.  
  73.     // set window title
  74.  
  75.     SetWindowTitleWithCFString(window, CFStringCreateWithCString(0, title, CFStringGetSystemEncoding()));
  76.  
  77.     // initialize apple opengl
  78.    
  79.     context = setupAGL(window);
  80.  
  81.     if (!context)
  82.         return false;
  83.    
  84.     // show the window
  85.    
  86.     ShowWindow(window);
  87.        
  88.     // install quit event handler
  89.        
  90.     AEInstallEventHandler(kCoreEventClass, kAEQuitApplication, NewAEEventHandlerUPP(quitEventHandler), 0, false);
  91.    
  92.     return true;
  93. }
  94.  
  95. void updateDisplay(void)
  96. {
  97.     // process pending events
  98.    
  99.     // todo: process all available events instead of one per update!
  100.    
  101.     EventRecord event;
  102.    
  103.     if (WaitNextEvent(everyEvent, &event, 1, NULL))
  104.     {
  105.         switch (event.what)
  106.         {
  107.             case mouseDown:
  108.             {
  109.                 WindowRef window;
  110.  
  111.                 SInt16 part = FindWindow(event.where, &window);
  112.                
  113.                 if (window)
  114.                     SelectWindow(window);
  115.                
  116.                 switch (part)
  117.                 {
  118.                     case inMenuBar:
  119.                         MenuSelect(event.where);
  120.                         break;
  121.                 }
  122.             }
  123.             break;
  124.                
  125.             case keyDown:
  126.             case autoKey:
  127.             {
  128.                 SInt8 key = event.message & charCodeMask;
  129.                
  130.                 if (key==27)
  131.                     onQuit();
  132.             }
  133.             break;
  134.         }
  135.     }
  136.    
  137.     aglSwapBuffers(context);
  138. }
  139.  
  140. void closeDisplay(void)
  141. {  
  142.     cleanupAGL(context);
  143.     context = 0;
  144.    
  145.     DisposeWindow((WindowPtr) window);
  146.     window = 0;
  147. }
  148.  
  149. float time()
  150. {
  151.     static UInt64 start = 0;
  152.    
  153.     if (start==0)
  154.     {
  155.         Microseconds((UnsignedWide*)&start);
  156.         return 0.0f;
  157.     }
  158.    
  159.     UInt64 counter = 0;
  160.     Microseconds((UnsignedWide*)&counter);
  161.     return (counter - start) / 1000000.0f;
  162. }
  163.  
  164. #endif
Advertisement
Add Comment
Please, Sign In to add comment