Made glutGet, for window width and height, return width and height determined by...
[freeglut] / src / blackberry / fg_window_blackberry.c
index 5201eb5..0cc14c9 100644 (file)
@@ -48,68 +48,135 @@ void fgPlatformOpenWindow( SFG_Window* window, const char* title,
         fgWarning("You can't have more than one window on BlackBerry");
         return;
     }
-    window->Window.pContext.event = NULL; //XXX Should probably be done elsewhere. Done here so there is no event at the moment
 
     /* Create window */
-    if (screen_create_context(&window->Window.pContext.screenContext, 0)) {
-        fgError("Could not create screen context");
-        return;
-    }
     screen_window_t sWindow;
-    if (screen_create_window(&sWindow, window->Window.pContext.screenContext)) {
-        screen_destroy_context(window->Window.pContext.screenContext);
+    if (screen_create_window(&sWindow, fgDisplay.pDisplay.screenContext)) {
         fgError("Could not create window");
         return;
     }
     fgDisplay.pDisplay.single_native_window = sWindow;
 
     /* Set window properties */
+    int orientation = atoi(getenv("ORIENTATION"));
     int screenFormat = SCREEN_FORMAT_RGBA8888; //XXX Should this be determined by config?
 #ifdef GL_ES_VERSION_2_0
-    int screenUsage = SCREEN_USAGE_OPENGL_ES2;
+    int screenUsage = SCREEN_USAGE_OPENGL_ES2 | SCREEN_USAGE_ROTATION;
 #elif GL_VERSION_ES_CM_1_0 || GL_VERSION_ES_CL_1_0 || GL_VERSION_ES_CM_1_1 || GL_VERSION_ES_CL_1_1
-    int screenUsage = SCREEN_USAGE_OPENGL_ES1;
+    int screenUsage = SCREEN_USAGE_OPENGL_ES1 | SCREEN_USAGE_ROTATION;
 #endif
 #ifndef __X86__
     screenUsage |= SCREEN_USAGE_DISPLAY; // Physical device copy directly into physical display
 #endif
     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_FORMAT, &screenFormat)) {
         screen_destroy_window(sWindow);
-        screen_destroy_context(window->Window.pContext.screenContext);
         fgError("Could not set window format");
         return;
     }
     if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_USAGE, &screenUsage)) {
         screen_destroy_window(sWindow);
-        screen_destroy_context(window->Window.pContext.screenContext);
         fgError("Could not set window usage");
         return;
     }
 
-    /* Could set size based on what is specified for window. Work on another time
-    int size[2];
-    size[0] = w;
-    size[1] = h;
-    if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, size)) {
+    int value[2];
+    /* Uncomment when multiple windows are supported
+    if(positionUse) {
+        value[0] = x;
+        value[1] = y;
+        if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_POSITION, value)) {
+            screen_destroy_window(sWindow);
+            fgError("Could not set window position");
+            return;
+        }
+    }*/
+
+    if(sizeUse) {
+        /* Uncomment when multiple windows are supported
+        value[0] = w;
+        value[1] = h;
+        */
+        //TEMP until ^^ is uncommented
+        if (screen_get_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
+            screen_destroy_window(sWindow);
+            fgError("Could not get window mode");
+            return;
+        }
+    } else {
+        /* From PlatformBlackBerry in GamePlay3d */
+        screen_display_t display;
+        if (screen_get_window_property_pv(sWindow, SCREEN_PROPERTY_DISPLAY, (void**)&display)) {
+            screen_destroy_window(sWindow);
+            fgError("Could not get window display");
+            return;
+        }
+
+        screen_display_mode_t displayMode;
+        if (screen_get_display_property_pv(display, SCREEN_PROPERTY_MODE, (void**)&displayMode)) {
+            screen_destroy_window(sWindow);
+            fgError("Could not get display mode");
+            return;
+        }
+
+        if (screen_get_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
+            screen_destroy_window(sWindow);
+            fgError("Could not get window mode");
+            return;
+        }
+
+        /* Adjust buffer sizes based on rotation */
+        if ((orientation == 0) || (orientation == 180))
+        {
+            if (((displayMode.width > displayMode.height) && (value[0] < value[1])) ||
+                ((displayMode.width < displayMode.height) && (value[0] > value[1])))
+            {
+                int tmp = value[1];
+                value[1] = value[0];
+                value[0] = tmp;
+            }
+        }
+        else if ((orientation == 90) || (orientation == 270))
+        {
+            if (((displayMode.width > displayMode.height) && (value[0] > value[1])) ||
+                ((displayMode.width < displayMode.height) && (value[0] < value[1])))
+            {
+                int tmp = value[1];
+                value[1] = value[0];
+                value[0] = tmp;
+            }
+        }
+        else
+        {
+            screen_destroy_window(sWindow);
+            fgError("Unexpected rotation angle");
+            return;
+        }
+    }
+
+    /* Set rotation if usage allows it */
+    if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_ROTATION, &orientation)) {
+        screen_destroy_window(sWindow);
+        fgError("Could not set window rotation");
+        return;
+    }
+    window->State.pWState.originalRotation = orientation;
+
+    /* Set buffer sizes */
+    if (screen_set_window_property_iv(sWindow, SCREEN_PROPERTY_BUFFER_SIZE, value)) {
         screen_destroy_window(sWindow);
-        screen_destroy_context(window->Window.pContext.screenContext);
         fgError("Could not set window buffer size");
         return;
-    }*/
+    }
 
     /* Create window buffers */
     if (screen_create_window_buffers(sWindow, (fgState.DisplayMode & GLUT_DOUBLE) ? 2 : 1)) {
         screen_destroy_window(sWindow);
-        screen_destroy_context(window->Window.pContext.screenContext);
         fgError("Could not create window buffers");
         return;
     }
 
-    /* Request window events */
-    screen_request_events(window->Window.pContext.screenContext); //XXX When multiple screens are supported, this needs to be moved to wherever the screen context is actually created
-
     /* Save window and set state */
-    window->Window.Handle = fgDisplay.pDisplay.single_native_window;
+    window->Window.Handle = sWindow;
     window->State.WorkMask |= GLUT_INIT_WORK;
     window->State.IsFullscreen = GL_TRUE; //XXX Always fullscreen for now
 
@@ -123,6 +190,20 @@ void fgPlatformOpenWindow( SFG_Window* window, const char* title,
     window->State.Visible = GL_TRUE;
 }
 
+void fgPlatformFlushCommands()
+{
+    if(screen_flush_context(fgDisplay.pDisplay.screenContext, 0)) {
+        fgWarning("Could not flush screen context");
+    }
+}
+
+void fgPlatformRotateWindow(SFG_Window* window, int rotation)
+{
+    if(screen_set_window_property_iv(window->Window.Handle, SCREEN_PROPERTY_ROTATION, &rotation)) {
+        fgWarning("Could not set window rotation");
+    }
+}
+
 /*
  * Request a window resize
  */
@@ -138,11 +219,7 @@ void fgPlatformCloseWindow( SFG_Window* window )
 {
     fghPlatformCloseWindowEGL(window);
 
-    screen_stop_events(window->Window.pContext.screenContext);
-
     screen_destroy_window((screen_window_t)window->Window.Handle);
-
-    screen_destroy_context(window->Window.pContext.screenContext);
 }
 
 /*
@@ -167,6 +244,7 @@ void fgPlatformHideWindow( SFG_Window *window )
 void fgPlatformIconifyWindow( SFG_Window *window )
 {
     //XXX This is possible via Cascades, but can't seem to find a C-level API
+    //XXX bb::Application::instance()->minimize();
     fprintf(stderr, "fgPlatformGlutIconifyWindow: STUB\n");
 }