Advertisement
elvman

GLFW problem

Mar 1st, 2015
348
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.46 KB | None | 0 0
  1. #include <Cocoa/Cocoa.h>
  2.  
  3. #include <GLFW/glfw3.h>
  4.  
  5. #define GLFW_EXPOSE_NATIVE_NSGL
  6. #define GLFW_EXPOSE_NATIVE_COCOA
  7. #include <GLFW/glfw3native.h>
  8.  
  9. void testOpenPanel()
  10. {
  11.     NSOpenPanel* openPanel = [NSOpenPanel openPanel];
  12.    
  13.     openPanel.title = @"Choose a .JSON file";
  14.     openPanel.showsResizeIndicator = YES;
  15.     openPanel.showsHiddenFiles = NO;
  16.     openPanel.canChooseDirectories = NO;
  17.     openPanel.canCreateDirectories = YES;
  18.     openPanel.allowsMultipleSelection = NO;
  19.     openPanel.allowedFileTypes = @[@"json"];
  20.    
  21.     [openPanel beginWithCompletionHandler:^(NSInteger result) {
  22.         if (result == NSModalResponseOK) {
  23.             NSURL *selection = openPanel.URLs[0];
  24.             NSString* path = [selection.path stringByResolvingSymlinksInPath];
  25.             NSLog(@"Path: %@", path);
  26.         }
  27.     }];
  28. }
  29.  
  30. void testSavePanel()
  31. {
  32.     NSSavePanel* savePanel = [NSSavePanel savePanel];
  33.    
  34.     savePanel.title = @"Choose a .JSON file";
  35.     savePanel.showsResizeIndicator = YES;
  36.     savePanel.showsHiddenFiles = NO;
  37.     savePanel.canCreateDirectories = YES;
  38.     savePanel.allowedFileTypes = @[@"json"];
  39.    
  40.     [savePanel beginWithCompletionHandler:^(NSInteger result) {
  41.         if (result == NSModalResponseOK) {
  42.             NSURL *selection = savePanel.URL;
  43.             NSString* path = [selection.path stringByResolvingSymlinksInPath];
  44.             NSLog(@"Path: %@", path);
  45.         }
  46.     }];
  47. }
  48.  
  49. static void error_callback(int error, const char* description)
  50. {
  51.     fputs(description, stderr);
  52. }
  53. static void key_callback(GLFWwindow* window, int key, int scancode, int action, int mods)
  54. {
  55.     if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
  56.         glfwSetWindowShouldClose(window, GL_TRUE);
  57. }
  58.  
  59. int main(void)
  60. {
  61.     GLFWwindow* window;
  62.     glfwSetErrorCallback(error_callback);
  63.     if (!glfwInit())
  64.         exit(EXIT_FAILURE);
  65.     window = glfwCreateWindow(640, 480, "Simple example", NULL, NULL);
  66.     if (!window)
  67.     {
  68.         glfwTerminate();
  69.         exit(EXIT_FAILURE);
  70.     }
  71.     glfwMakeContextCurrent(window);
  72.     glfwSwapInterval(1);
  73.     glfwSetKeyCallback(window, key_callback);
  74.    
  75.     NSWindow* cocoaWindow = glfwGetCocoaWindow(window);
  76.     NSOpenGLContext* context = glfwGetNSGLContext(window);
  77.    
  78.     testOpenPanel();
  79.    
  80.     while (!glfwWindowShouldClose(window))
  81.     {
  82.         //[context makeCurrentContext];
  83.        
  84.         glfwMakeContextCurrent(nullptr);
  85.         glfwMakeContextCurrent(window);
  86.        
  87.         float ratio;
  88.         int width, height;
  89.         glfwGetFramebufferSize(window, &width, &height);
  90.         ratio = width / (float) height;
  91.         glViewport(0, 0, width, height);
  92.        
  93.         glClear(GL_COLOR_BUFFER_BIT);
  94.        
  95.         glMatrixMode(GL_PROJECTION);
  96.         glLoadIdentity();
  97.        
  98.         glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f);
  99.         glMatrixMode(GL_MODELVIEW);
  100.         glLoadIdentity();
  101.         glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f);
  102.        
  103.         glBegin(GL_TRIANGLES);
  104.        
  105.         glColor3f(1.f, 0.f, 0.f);
  106.         glVertex3f(-0.6f, -0.4f, 0.f);
  107.         glColor3f(0.f, 1.f, 0.f);
  108.         glVertex3f(0.6f, -0.4f, 0.f);
  109.         glColor3f(0.f, 0.f, 1.f);
  110.         glVertex3f(0.f, 0.6f, 0.f);
  111.         CHECK_GL_ERROR_DEBUG();
  112.        
  113.         glEnd();
  114.        
  115.         glfwSwapBuffers(window);
  116.        
  117.         glfwPollEvents();
  118.     }
  119.     glfwDestroyWindow(window);
  120.     glfwTerminate();
  121.     exit(EXIT_SUCCESS);
  122. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement