]> de.git.xonotic.org Git - xonotic/darkplaces.git/blobdiff - vid_agl.c
fix more warnings
[xonotic/darkplaces.git] / vid_agl.c
index 69329456c17a52ae4237b7991761df9ff7137bc4..977d8b2fe2ed00507ff76702f8cfac805003c9d7 100644 (file)
--- a/vid_agl.c
+++ b/vid_agl.c
@@ -101,6 +101,7 @@ static void IN_Activate( qboolean grab )
        }
 }
 
+#define GAMMA_TABLE_SIZE 256
 void VID_Finish (qboolean allowmousegrab)
 {
        qboolean vid_usemouse;
@@ -137,25 +138,25 @@ void VID_Finish (qboolean allowmousegrab)
                        qglFinish();
                qaglSwapBuffers(context);
        }
+       VID_UpdateGamma(false, GAMMA_TABLE_SIZE);
 }
 
-#define GAMMA_TABLE_SIZE 256
-int VID_SetGamma(unsigned short *ramps)
+int VID_SetGamma(unsigned short *ramps, int rampsize)
 {
        CGGammaValue table_red [GAMMA_TABLE_SIZE];
        CGGammaValue table_green [GAMMA_TABLE_SIZE];
        CGGammaValue table_blue [GAMMA_TABLE_SIZE];
-       unsigned int i;
+       int i;
 
        // Convert the unsigned short table into 3 float tables
-       for (i = 0; i < GAMMA_TABLE_SIZE; i++)
+       for (i = 0; i < rampsize; i++)
                table_red[i] = (float)ramps[i] / 65535.0f;
-       for (i = 0; i < GAMMA_TABLE_SIZE; i++)
-               table_green[i] = (float)ramps[i + GAMMA_TABLE_SIZE] / 65535.0f;
-       for (i = 0; i < GAMMA_TABLE_SIZE; i++)
-               table_blue[i] = (float)ramps[i + 2 * GAMMA_TABLE_SIZE] / 65535.0f;
+       for (i = 0; i < rampsize; i++)
+               table_green[i] = (float)ramps[i + rampsize] / 65535.0f;
+       for (i = 0; i < rampsize; i++)
+               table_blue[i] = (float)ramps[i + 2 * rampsize] / 65535.0f;
 
-       if (CGSetDisplayTransferByTable(CGMainDisplayID(), GAMMA_TABLE_SIZE, table_red, table_green, table_blue) != CGDisplayNoErr)
+       if (CGSetDisplayTransferByTable(CGMainDisplayID(), rampsize, table_red, table_green, table_blue) != CGDisplayNoErr)
        {
                Con_Print("VID_SetGamma: ERROR: CGSetDisplayTransferByTable failed!\n");
                return false;
@@ -164,33 +165,33 @@ int VID_SetGamma(unsigned short *ramps)
        return true;
 }
 
-int VID_GetGamma(unsigned short *ramps)
+int VID_GetGamma(unsigned short *ramps, int rampsize)
 {
        CGGammaValue table_red [GAMMA_TABLE_SIZE];
        CGGammaValue table_green [GAMMA_TABLE_SIZE];
        CGGammaValue table_blue [GAMMA_TABLE_SIZE];
        CGTableCount actualsize = 0;
-       unsigned int i;
+       int i;
 
        // Get the gamma ramps from the system
-       if (CGGetDisplayTransferByTable(CGMainDisplayID(), GAMMA_TABLE_SIZE, table_red, table_green, table_blue, &actualsize) != CGDisplayNoErr)
+       if (CGGetDisplayTransferByTable(CGMainDisplayID(), rampsize, table_red, table_green, table_blue, &actualsize) != CGDisplayNoErr)
        {
                Con_Print("VID_GetGamma: ERROR: CGGetDisplayTransferByTable failed!\n");
                return false;
        }
-       if (actualsize != GAMMA_TABLE_SIZE)
+       if (actualsize != (unsigned int)rampsize)
        {
-               Con_Printf("VID_GetGamma: ERROR: invalid gamma table size (%u != %u)\n", actualsize, GAMMA_TABLE_SIZE);
+               Con_Printf("VID_GetGamma: ERROR: invalid gamma table size (%u != %u)\n", actualsize, rampsize);
                return false;
        }
 
        // Convert the 3 float tables into 1 unsigned short table
-       for (i = 0; i < GAMMA_TABLE_SIZE; i++)
+       for (i = 0; i < rampsize; i++)
                ramps[i] = table_red[i] * 65535.0f;
-       for (i = 0; i < GAMMA_TABLE_SIZE; i++)
-               ramps[i + GAMMA_TABLE_SIZE] = table_green[i] * 65535.0f;
-       for (i = 0; i < GAMMA_TABLE_SIZE; i++)
-               ramps[i + 2 * GAMMA_TABLE_SIZE] = table_blue[i] * 65535.0f;
+       for (i = 0; i < rampsize; i++)
+               ramps[i + rampsize] = table_green[i] * 65535.0f;
+       for (i = 0; i < rampsize; i++)
+               ramps[i + 2 * rampsize] = table_blue[i] * 65535.0f;
 
        return true;
 }
@@ -333,7 +334,7 @@ static void VID_ProcessPendingAsyncEvents (void)
        }
 }
 
-static void VID_BuildAGLAttrib(GLint *attrib, int stencil, qboolean fullscreen)
+static void VID_BuildAGLAttrib(GLint *attrib, GLint stencil, qboolean fullscreen)
 {
        *attrib++ = AGL_RGBA;
        *attrib++ = AGL_RED_SIZE;*attrib++ = 1;
@@ -365,6 +366,11 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp, int refreshrate
        Rect windowBounds;
        AGLPixelFormat pixelFormat;
        GLint attributes [32];
+       GLenum error;
+
+       // FullScreen Display stuff
+       GDHandle gdhDisplay; // = GetMainDevice(); /*deprecated*/
+       CGDirectDisplayID mainDisplay = CGMainDisplayID();
 
        if (!GL_OpenLibrary())
        {
@@ -414,35 +420,106 @@ int VID_InitMode(int fullscreen, int width, int height, int bpp, int refreshrate
        InstallWindowEventHandler (window, NewEventHandlerUPP (MainWindowEventHandler),
                                                           GetEventTypeCount(winEvents), winEvents, window, NULL);
 
-       // Create and set pixel format
+       // Create the desired attribute list
        VID_BuildAGLAttrib(attributes, bpp == 32, fullscreen);
-       pixelFormat = qaglChoosePixelFormat(NULL, 1, attributes);
-       if (pixelFormat == NULL)
+
+       if (!fullscreen)
        {
-               Con_Printf("Unable to create pixel format\n");
-               ReleaseWindow(window);
-               return false;
+               //  Output to Window
+               //  Set pixel format with built attribs
+               //  Note: use NULL then must use 0
+               pixelFormat = qaglChoosePixelFormat(NULL, 0, attributes);
+               error = aglGetError();
+               if (error != AGL_NO_ERROR)
+               {
+                       Con_Printf("qaglChoosePixelFormat FAILED: %s\n",
+                                       (char *)aglErrorString(error));
+                       ReleaseWindow(window);
+                       return false;
+               }
        }
 
-       // Set context and show the window
-       context = qaglCreateContext(pixelFormat, NULL);
-       if (context == NULL)
-               Sys_Error ("aglCreateContext failed");
        if (fullscreen)
        {
-               if (!qaglSetFullScreen (context, width, height, refreshrate, 0))
-                       Sys_Error("aglSetFullScreen failed");
-               vid_isfullscreen = true;
+               //  Output is FullScreen
+               //  Get the mainDisplay and set resolution to current
+               CGDisplayCapture (mainDisplay);
+               CFDictionaryRef refDisplayMode = CGDisplayBestModeForParameters (mainDisplay, bpp, width, height, NULL);
+               CGDisplaySwitchToMode (mainDisplay, refDisplayMode);
+               DMGetGDeviceByDisplayID ((DisplayIDType)mainDisplay, &gdhDisplay, false);
+
+               //  Set pixel format with built attribs
+               //  Note: specifying a device is *required* for AGL_FullScreen
+               pixelFormat = qaglChoosePixelFormat(&gdhDisplay, 1, attributes);
+               error = aglGetError();
+               if (error != AGL_NO_ERROR)
+               {
+                       Con_Printf("qaglChoosePixelFormat FAILED: %s\n",
+                                               (char *)aglErrorString(error));
+                       ReleaseWindow(window);
+                       return false;
+               }
        }
-       else
+
+       // Create a context using the pform
+       context = qaglCreateContext(pixelFormat, NULL);
+       error = aglGetError();
+       if (error != AGL_NO_ERROR)
        {
-               if (!qaglSetDrawable(context, GetWindowPort(window)))
-                       Sys_Error ("aglSetDrawable failed");
+               Con_Printf("qaglCreateContext FAILED: %s\n",
+                                       (char *)aglErrorString(error));
        }
-       if (!qaglSetCurrentContext(context))
-               Sys_Error ("aglSetCurrentContext failed");
+
+       // Make the context the current one ('enable' it)
+       qaglSetCurrentContext(context);   
+       error = aglGetError();
+               if (error != AGL_NO_ERROR)
+               {
+                       Con_Printf("qaglSetCurrentContext FAILED: %s\n",
+                                               (char *)aglErrorString(error));
+                       ReleaseWindow(window);
+                       return false;
+               }
+
+       // Discard pform
        qaglDestroyPixelFormat(pixelFormat);
 
+       // Attempt fullscreen if requested
+       if (fullscreen)
+       {
+               qaglSetFullScreen (context, width, height, refreshrate, 0);
+               error = aglGetError();
+               if (error != AGL_NO_ERROR)
+               {
+                       Con_Printf("qaglSetFullScreen FAILED: %s\n",
+                                               (char *)aglErrorString(error));
+                       return false;
+                       // FullScreen Failed
+                       vid_isfullscreen = false;
+               }
+               else
+               {
+                       // FullScreen Success
+                       vid_isfullscreen = true;
+               }
+       }
+       
+       if (!fullscreen)
+       {
+       // Note: this _is_ a Window != FullScreen
+       // Set Window as Drawable
+       qaglSetDrawable(context, GetWindowPort(window));
+       error = aglGetError();
+               if (error != AGL_NO_ERROR)
+               {
+                       Con_Printf("qaglSetDrawable FAILED: %s\n",
+                                               (char *)aglErrorString(error));
+                       ReleaseWindow(window);
+                       return false;
+               }
+
+       }
+
        scr_width = width;
        scr_height = height;
 
@@ -618,7 +695,10 @@ void Sys_SendKeyEvents(void)
                                                Key_Event(key, '\0', eventKind == kEventMouseDown);
                                                break;
 
+                                       // Note: These two events are mutual exclusives
+                                       // Treat MouseDragged in the same statement, so we don't block MouseMoved while a mousebutton is held
                                        case kEventMouseMoved:
+                                       case kEventMouseDragged:
                                        {
                                                HIPoint deltaPos;
 
@@ -642,9 +722,6 @@ void Sys_SendKeyEvents(void)
                                                break;
                                        }
 
-                                       case kEventMouseDragged:
-                                               break;
-
                                        default:
                                                Con_Printf (">> kEventClassMouse (UNKNOWN eventKind: %d) <<\n", eventKind);
                                                break;