size_t VID_ListModes(vid_mode_t *modes, size_t maxcount) { CGDirectDisplayID mainDisplay = CGMainDisplayID(); CFArrayRef vidmodes = CGDisplayAvailableModes(mainDisplay); CFDictionaryRef thismode; unsigned int n = CFArrayGetCount(vidmodes); unsigned int i; size_t k; k = 0; for(i = 0; i < n; ++i) { thismode = (CFDictionaryRef) CFArrayGetValueAtIndex(vidmodes, i); if(!GetDictionaryBoolean(thismode, kCGDisplayModeIsSafeForHardware)) continue; if(k >= maxcount) break; modes[k].width = GetDictionaryLong(thismode, kCGDisplayWidth); modes[k].height = GetDictionaryLong(thismode, kCGDisplayHeight); modes[k].bpp = GetDictionaryLong(thismode, kCGDisplayBitsPerPixel); modes[k].refreshrate = GetDictionaryLong(thismode, kCGDisplayRefreshRate); modes[k].pixelheight_num = 1; modes[k].pixelheight_denom = 1; // OS X doesn't expose this either ++k; } return k; }
bool OpenGLApp::initAPI(){ initialMode = CGDisplayCurrentMode(kCGDirectMainDisplay); dmodes = CGDisplayAvailableModes(kCGDirectMainDisplay); int count = CFArrayGetCount(dmodes); Array <DispRes> modes; int foundMode = -1; for (int i = 0; i < count; i++){ CFDictionaryRef mode = (CFDictionaryRef) CFArrayGetValueAtIndex(dmodes, i); long bitsPerPixel = GetDictionaryLong(mode, kCGDisplayBitsPerPixel); Boolean safeForHardware = GetDictionaryBoolean(mode, kCGDisplayModeIsSafeForHardware); Boolean stretched = GetDictionaryBoolean(mode, kCGDisplayModeIsStretched); if (bitsPerPixel < colorBits || !safeForHardware || stretched) continue; long width = GetDictionaryLong(mode, kCGDisplayWidth); long height = GetDictionaryLong(mode, kCGDisplayHeight); long refreshRate = GetDictionaryLong(mode, kCGDisplayRefreshRate); // printf("Mode: %dx%dx%d @ %d\n", width, height, bitsPerPixel, refreshRate); if (width >= 640 && height >= 480){ modes.add(newRes(width, height, i)); if (width == fullscreenWidth && height == fullscreenHeight){ foundMode = i; } } } resolution->clear(); modes.sort(dComp); char str[64]; for (uint i = 0; i < modes.getCount(); i++){ sprintf(str, "%dx%d", modes[i].w, modes[i].h); int index = resolution->addItemUnique(str); if (modes[i].index == foundMode) resolution->selectItem(index); } if (fullscreen){ if (foundMode < 0 || CGDisplaySwitchToMode(kCGDirectMainDisplay, (CFDictionaryRef) CFArrayGetValueAtIndex(dmodes, foundMode)) != kCGErrorSuccess){ sprintf(str, "Couldn't set fullscreen to %dx%d.", fullscreenWidth, fullscreenHeight); ErrorMsg(str); fullscreen = false; } } Rect rect; if (fullscreen){ rect.left = 0; rect.top = 0; } else { long w = GetDictionaryLong(initialMode, kCGDisplayWidth); long h = GetDictionaryLong(initialMode, kCGDisplayHeight); rect.left = (w - width) / 2; rect.top = (h - height) / 2; } rect.right = rect.left + width; rect.bottom = rect.top + height; WindowAttributes attributes = fullscreen? (kWindowNoTitleBarAttribute | kWindowNoShadowAttribute) : (kWindowStandardDocumentAttributes | kWindowStandardHandlerAttribute); OSStatus error = CreateNewWindow(kDocumentWindowClass, attributes, &rect, &window); if (error != noErr || window == NULL){ ErrorMsg("Couldn't create window"); return false; } GDHandle screen = GetGWorldDevice(GetWindowPort(window)); if (screen == NULL){ ErrorMsg("Couldn't get device"); ReleaseWindow(window); return false; } AGLPixelFormat pixelFormat; while (true){ GLint attributes[] = { fullscreen? AGL_FULLSCREEN : AGL_WINDOW, AGL_RGBA, AGL_DOUBLEBUFFER, AGL_RED_SIZE, 8, AGL_GREEN_SIZE, 8, AGL_BLUE_SIZE, 8, AGL_ALPHA_SIZE, (colorBits > 24)? 8 : 0, AGL_DEPTH_SIZE, depthBits, AGL_STENCIL_SIZE, stencilBits, AGL_SAMPLE_BUFFERS_ARB, (antiAliasSamples > 0), AGL_SAMPLES_ARB, antiAliasSamples, AGL_NONE }; pixelFormat = aglChoosePixelFormat(&screen, 1, attributes); if (pixelFormat != NULL) break; antiAliasSamples -= 2; if (antiAliasSamples < 0){ ErrorMsg("No suitable pixel format"); ReleaseWindow(window); return false; } } glContext = aglCreateContext(pixelFormat, NULL); aglDestroyPixelFormat(pixelFormat); if (glContext == NULL){ ErrorMsg("Couldn't create context"); ReleaseWindow(window); return false; } if (fullscreen){ CGCaptureAllDisplays(); aglSetFullScreen(glContext, 0, 0, 0, 0); } else { if (!aglSetDrawable(glContext, GetWindowPort(window))){ ErrorMsg("Couldn't set drawable"); aglDestroyContext(glContext); ReleaseWindow(window); return false; } } if (!aglSetCurrentContext(glContext)){ ErrorMsg("Couldn't make context current"); aglDestroyContext(glContext); ReleaseWindow(window); return false; } setWindowTitle(getTitle()); ShowWindow(window); initExtensions(); if (antiAliasSamples > 0){ glEnable(GL_MULTISAMPLE_ARB); } if (fullscreen) captureMouse(!configDialog->isVisible()); renderer = new OpenGLRenderer(glContext); renderer->setViewport(width, height); antiAlias->selectItem(antiAliasSamples / 2); linearClamp = renderer->addSamplerState(LINEAR, CLAMP, CLAMP, CLAMP); defaultFont = renderer->addFont("../Textures/Fonts/Future.dds", "../Textures/Fonts/Future.font", linearClamp); blendSrcAlpha = renderer->addBlendState(SRC_ALPHA, ONE_MINUS_SRC_ALPHA); noDepthTest = renderer->addDepthState(false, false); noDepthWrite = renderer->addDepthState(true, false); cullNone = renderer->addRasterizerState(CULL_NONE); cullBack = renderer->addRasterizerState(CULL_BACK); cullFront = renderer->addRasterizerState(CULL_FRONT); return true; }