2011-11-21 38 views
1

我需要我的程序在Mac OS 10.5-10.7上運行,但有些系統函數自10.5以來已被棄用。我的問題是​​從10.6開始不存在,而新函數CGDisplayModeCopyPixelEncoding在10.5下不能運行。如何避免CGDisplayModeCopyPixelEncoding獲得BPP?

如何讓我的程序可用於所有這些版本的mac os?

我可以使用一些#ifdef _xxx_解決這個問題,但這意味着我需要兩個不同的應用程序版本,我需要一個。

這是我如何設置:視頻模式:

  1. 獲取displayID
  2. 獲取所有可用的模式,推動「時間VideoModeList
  3. 獲取屏幕寬度,高度和BPP

代碼示例:

VideoModeList->setDesktop(rect(screen_w, screen_h), screen_bpp); 
VideoModeList->setEffectiveDesktop(rect(screen_w, screen_h), screen_bpp); 

UPD(由於新用戶無法在8小時內發佈自己的問題解答): 我想我已經找到了解決問題的方法。我已經避免了​​。這是一個代碼smaple:

// That's how you know what OS you are dealing with in runtime instead of compiletime 
bool macOSX_10_6_orHigher = (CGDisplayCopyAllDisplayModes != NULL); 

if (macOSX_10_6_orHigher) 
{ 
    // This function uses CGDisplayModeCopyPixelEncoding and CGDisplayCopyDisplayMode 
    // to determine BPP. 
    screenBPP = getDisplayBitsPerPixel(displayID); 
} 
else 
{ 
    // This function in deprecated, bit it is still there, so you will get a warning 
    // instead of error 
    CFDictionaryRef = currentDisplayMode = CGDisplayCurrentMode(mode); 
    CFNumberGetValue((CFNumberRef)CFDictionaryGetValue(currentDisplayMode, kCGDisplayBitsPerPixel), kCFNumberIntType, &screenBPP); 
} 

P.S.如果您想提出另一種解決方案,我仍然關注這個話題,並且可以進行討論。 P.P.S.感謝您對此問題的關注,並感謝管理員更正了我的第一篇文章。

回答

0

我試圖在MacOS 10.7用C++類似的東西用gcc 4.5

if (CGDisplayCopyAllDisplayModes != NULL) 
    screenBPP = DisplayBitsPerPixel (displayID); // MacOS > 10.5 
else 
    screenBPP = CGDisplayBitsPerPixel (displayID); 

和遇到下面的錯誤。

error: 'CGDisplayBitsPerPixel' was not declared in this scope 

除非Objective-C處理這個不同,否則我認爲你的解決方案不會起作用。要在10.5+上運行,您可以直接鏈接到10.5的SDK(僅限32位?)。以下選項建議在wxwidgets wiki

-isysroot /Developer/SDKs/MacOSX10.5.sdk -mmacosx-version-min=10.5 

的10.5 SDK不是的Xcode 4的一部分。說明在Lion上安裝SDK適用於MacOS 10.5可用here

1

新功能CGDisplayCopyDisplayMode和CGDisplayCopyAllDisplayModes已知返回CGDisplayModeRef。 我很驚訝API沒有提供直接獲取BitsPerPixel的函數。 再見,CGDisplayModeCopyPixelEncoding在OS X v10.11中已棄用。

然後我拆開CGDisplayModeGetWidth並看到了如何從CGDisplayModeRef(短)獲得的參數:

movq 0x10(%rdi), %rdi   ; get CFDictionaryRef from CGDisplayModeRef 
leaq -0x103fd9f2(%rip), %rsi ; @"Width" 
callq 0x7fff8812570e   ; symbol stub for: CFDictionaryGetValue 

我知道它看起來可怕,而蘋果可以隨時更改API,但現在我可以得到任何的從模式詞典中的參數:

CGDisplayModeRef mode = CGDisplayCopyDisplayMode(display); 
CFDictionaryRef dict = (CFDictionaryRef)*((int64_t *)mode + 2); 
CFNumberRef num; 
int bpp; 
if (CFGetTypeID(dict) == CFDictionaryGetTypeID() 
    && CFDictionaryGetValueIfPresent(dict, kCGDisplayBitsPerPixel, (const void**)&num)) 
{ 
    CFNumberGetValue(num, kCFNumberSInt32Type, (void*)&bpp); 
} 
CFRelease(mode); 
+0

如果你找到一個更清潔的版本,我會很感興趣。 :-) – Hiura