VideoMode VideoModeImpl::getDesktopMode() { VideoMode mode; // RVO // Rely exclusively on mode and convertCGModeToSFMode // instead of display id and CGDisplayPixelsHigh/Wide. CGDirectDisplayID display = CGMainDisplayID(); CGDisplayModeRef cgmode = CGDisplayCopyDisplayMode(display); mode = convertCGModeToSFMode(cgmode); CGDisplayModeRelease(cgmode); return mode; }
CGDisplayModeRef convertSFModeToCGMode(VideoMode sfmode) { // Starting with 10.6 we should query the display all the modes and // search for the best one. // Will return NULL if sfmode is not in VideoMode::GetFullscreenModes. CGDisplayModeRef cgbestMode = NULL; // Retrieve all modes available for main screen only. CFArrayRef cgmodes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), NULL); if (cgmodes == NULL) { // Should not happen but anyway... sf::err() << "Couldn't get VideoMode for main display."; return NULL; } // Loop on each mode and convert it into a sf::VideoMode object. CFIndex const modesCount = CFArrayGetCount(cgmodes); for (CFIndex i = 0; i < modesCount; i++) { CGDisplayModeRef cgmode = (CGDisplayModeRef)CFArrayGetValueAtIndex(cgmodes, i); VideoMode mode = convertCGModeToSFMode(cgmode); if (mode == sfmode) { cgbestMode = cgmode; } } // Clean up memory. CFRelease(cgmodes); if (cgbestMode == NULL) { sf::err() << "Couldn't convert the given sf:VideoMode into a CGDisplayMode." << std::endl; } return cgbestMode; }
std::vector<VideoMode> VideoModeImpl::getFullscreenModes() { std::vector<VideoMode> modes; // Retrieve all modes available for main screen only. CFArrayRef cgmodes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), NULL); if (cgmodes == NULL) { sf::err() << "Couldn't get VideoMode for main display." << std::endl; return modes; } VideoMode desktop = getDesktopMode(); // Loop on each mode and convert it into a sf::VideoMode object. const CFIndex modesCount = CFArrayGetCount(cgmodes); for (CFIndex i = 0; i < modesCount; i++) { CGDisplayModeRef cgmode = (CGDisplayModeRef)CFArrayGetValueAtIndex(cgmodes, i); VideoMode mode = convertCGModeToSFMode(cgmode); // Skip if bigger than desktop as we currently don't perform hard resolution switch if ((mode.width > desktop.width) || (mode.height > desktop.height)) continue; // If not yet listed we add it to our modes array. if (std::find(modes.begin(), modes.end(), mode) == modes.end()) modes.push_back(mode); } // Clean up memory. CFRelease(cgmodes); return modes; }
//////////////////////////////////////////////////////////// /// Note : /// Starting with 10.6, CGDisplayModeRef and CGDisplayCopyAllDisplayModes /// should be used instead of CFDictionaryRef and CGDisplayAvailableModes. /// //////////////////////////////////////////////////////////// std::vector<VideoMode> VideoModeImpl::getFullscreenModes() { #if MAC_OS_X_VERSION_MAX_ALLOWED < 1060 std::vector<VideoMode> modes; // Retrieve array of dictionaries representing display modes. CFArrayRef displayModes = CGDisplayAvailableModes(CGMainDisplayID()); if (displayModes == NULL) { sf::err() << "Couldn't get VideoMode for main display."; return modes; } // Loop on each mode and convert it into a sf::VideoMode object. CFIndex const modesCount = CFArrayGetCount(displayModes); for (CFIndex i = 0; i < modesCount; i++) { CFDictionaryRef dictionary = (CFDictionaryRef)CFArrayGetValueAtIndex(displayModes, i); VideoMode mode = convertCGModeToSFMode(dictionary); // If not yet listed we add it to our modes array. if (std::find(modes.begin(), modes.end(), mode) == modes.end()) { modes.push_back(mode); } } return modes; #else // MAC_OS_X_VERSION_MAX_ALLOWED >= 1060 std::vector<VideoMode> modes; // Retrieve all modes available for main screen only. CFArrayRef cgmodes = CGDisplayCopyAllDisplayModes(CGMainDisplayID(), NULL); if (cgmodes == NULL) { sf::err() << "Couldn't get VideoMode for main display."; return modes; } // Loop on each mode and convert it into a sf::VideoMode object. CFIndex const modesCount = CFArrayGetCount(cgmodes); for (CFIndex i = 0; i < modesCount; i++) { CGDisplayModeRef cgmode = (CGDisplayModeRef)CFArrayGetValueAtIndex(cgmodes, i); VideoMode mode = convertCGModeToSFMode(cgmode); // If not yet listed we add it to our modes array. if (std::find(modes.begin(), modes.end(), mode) == modes.end()) { modes.push_back(mode); } } // Clean up memory. CFRelease(cgmodes); return modes; #endif }