Răsfoiți Sursa

Adds a CoreServices::getScreenInfo() which fetches screen width, height and refresh rate in a cross-platform way. If a Core object is given (0,0) as the screen coordinates in its constructor and also full screen mode is chosen, use getScreenInfo() to set the screen to the current resolution exactly.

Uses (with permission) Average Software's screen interrogation routines for Mac, Linux and Windows.

I have tested this code for Mac, but not Windows, Linux or any other platform.
mcc 14 ani în urmă
părinte
comite
72e45c7c9c

+ 8 - 0
Core/Contents/Include/PolyCoreServices.h

@@ -147,6 +147,14 @@ namespace Polycode {
 			*/																													
 			Config *getConfig();
 		
+			/**
+			 * Provides the current width, height, and refresh rate of the screen.
+			 * @param width If non-NULL, current screen width will be written here (or 0 if unknown).
+			 * @param hight If non-NULL, current screen height will be written here (or 0 if unknown).
+			 * @param hz If non-NULL, current screen refresh rate will be written here (or 0 if unknown).
+			 */
+			void getScreenInfo(int *width, int *height, int *hz);
+		
 			~CoreServices();
 		
 		protected:

+ 1 - 1
Core/Contents/Source/PolyAGLCore.cpp

@@ -29,7 +29,7 @@ long getThreadID() {
 	return (long)pthread_self();
 }
 
-AGLCore::AGLCore(WindowRef window, Polycode::Rectangle *clippingArea, int xRes, int yRes, bool fullScreen,int aaLevel, int frameRate) : Core(xRes, yRes, fullScreen,aaLevel, frameRate) {
+AGLCore::AGLCore(WindowRef window, Polycode::Rectangle *clippingArea, int _xRes, int _yRes, bool fullScreen, int aaLevel, int frameRate) : Core(_xRes, _yRes, fullScreen,aaLevel, frameRate) {
 
 	eventMutex = createMutex();
 	

+ 1 - 1
Core/Contents/Source/PolyCocoaCore.mm

@@ -29,7 +29,7 @@ long getThreadID() {
 	return (long)pthread_self();
 }
 
-CocoaCore::CocoaCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate) {	
+CocoaCore::CocoaCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : Core(_xRes, _yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate) {	
 	eventMutex = createMutex();
 	
 //	NSLog(@"BUNDLE: %@", [[NSBundle mainBundle] bundlePath]);

+ 6 - 3
Core/Contents/Source/PolyCore.cpp

@@ -31,7 +31,7 @@
 
 namespace Polycode {
 	
-	Core::Core(int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : EventDispatcher() {
+	Core::Core(int _xRes, int _yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : EventDispatcher() {
 		services = CoreServices::getInstance();
 		input = new CoreInput();
 		services->setCore(this);
@@ -41,8 +41,11 @@ namespace Polycode {
 		lastFrameTicks=0;
 		lastFPSTicks=0;
 		elapsed = 0;
-		this->xRes = xRes;
-		this->yRes = yRes;
+		xRes = _xRes;
+		yRes = _yRes;
+		if (fullScreen && !xRes && !yRes) {
+			CoreServices::getInstance()->getScreenInfo(&xRes, &yRes, NULL);
+		}
 		mouseEnabled = true;
 		lastSleepFrameTicks = 0;
 		

+ 69 - 0
Core/Contents/Source/PolyCoreServices.cpp

@@ -37,6 +37,18 @@
 #include "PolyTweenManager.h"
 #include "PolySoundManager.h"
 
+// For use by getScreenInfo
+#if defined(_WINDOWS)
+#include <windows.h>
+#elif defined(__APPLE__) && defined(__MACH__)
+#include <ApplicationServices/ApplicationServices.h>
+#elif defined(__linux) || defined(__linux__) || defined(linux)
+#include <X11/extensions/Xrandr.h>
+#include <gdk/gdkx.h>
+#elif defined(SDL_VERSION)
+#include <SDL/SDL.h>
+#endif
+
 using namespace Polycode;
 
 std::map<long, CoreServices*> CoreServices::instanceMap;
@@ -213,3 +225,60 @@ TweenManager *CoreServices::getTweenManager() {
 ResourceManager *CoreServices::getResourceManager() {
 	return resourceManager;
 }
+
+void CoreServices::getScreenInfo(int *width, int *height, int *hz) {
+#if defined(_WINDOWS)
+	
+	DEVMODE mode = {.dmSize = sizeof(DEVMODE), .dmDriverExtra = 0};
+	
+    EnumDisplaySettings(0, ENUM_CURRENT_SETTINGS, &mode);
+	
+    // Store the current display settings.
+    if (width) *width = mode.dmPelsWidth;
+    if (height) *height = mode.dmPelsHeight;
+    if (hz) *hz = mode.dmDisplayFrequency;
+	
+#elif defined(__APPLE__) && defined(__MACH__)
+	
+	CGDisplayModeRef mode = CGDisplayCopyDisplayMode(CGMainDisplayID());
+    
+    // Copy the relevant data.
+    if (width) *width = CGDisplayModeGetWidth(mode);
+    if (height) *height = CGDisplayModeGetHeight(mode);
+    if (hz) *hz = CGDisplayModeGetRefreshRate(mode);
+    
+    CGDisplayModeRelease(mode);
+	
+#elif defined(__linux) || defined(__linux__) || defined(linux)
+	
+    // Get the current configuration.
+    XRRScreenConfiguration *config = XRRGetScreenInfo(gdk_x11_get_default_xdisplay(),
+                                                      gdk_x11_get_default_root_xwindow());
+    int size_count;
+    // Obtain the dimensions.
+    XRRScreenSize *sizes = XRRConfigSizes(config, &size_count);
+    Rotation current_rotation;
+    SizeID current_mode = XRRConfigCurrentConfiguration(config, &current_rotation);
+	
+    // Store the results.
+    if (width) *width = sizes[current_mode].width;
+    if (height) *height = sizes[current_mode].height;
+    if (hz) *hz = XRRConfigCurrentRate(config); // Warning: On some drivers (nvidia?) this can lie.
+	
+    XRRFreeScreenConfigInfo(config);
+	
+#elif defined(SDL_VERSION)
+	
+	const SDL_VideoInfo *video = SDL_GetVideoInfo();
+	if (width) *width = video->current_w;
+	if (height) *height = video->current_h;
+	if (hz) *hz = 0;
+	
+#else
+	
+	if (width) *width = 0;
+	if (height) *height = 0;
+	if (hz) *hz = 0;
+	
+#endif
+}

+ 1 - 1
Core/Contents/Source/PolySDLCore.cpp

@@ -41,7 +41,7 @@ long getThreadID() {
 	return (long)pthread_self();
 }
 
-SDLCore::SDLCore(PolycodeView *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate) {
+SDLCore::SDLCore(PolycodeView *view, int _xRes, int _yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) : Core(_xRes, _yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate) {
 
 	String *windowTitle = (String*)view->windowData;
 

+ 2 - 2
Core/Contents/Source/PolyWinCore.cpp

@@ -52,8 +52,8 @@ void ClientResize(HWND hWnd, int nWidth, int nHeight)
   MoveWindow(hWnd,rcWindow.left, rcWindow.top, nWidth + ptDiff.x, nHeight + ptDiff.y, TRUE);
 }
 
-Win32Core::Win32Core(PolycodeViewBase *view, int xRes, int yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) 
-	: Core(xRes, yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate) {
+Win32Core::Win32Core(PolycodeViewBase *view, int _xRes, int _yRes, bool fullScreen, bool vSync, int aaLevel, int anisotropyLevel, int frameRate) 
+	: Core(_xRes, _yRes, fullScreen, vSync, aaLevel, anisotropyLevel, frameRate) {
 
 	hWnd = *((HWND*)view->windowData);
 	core = this;