Kaynağa Gözat

Refactored display modes.

Try to set best mode for full screen.
Brucey 3 yıl önce
ebeveyn
işleme
e4be28c82a

+ 21 - 0
sdlgraphics.mod/sdlgraphics.bmx

@@ -171,6 +171,27 @@ Type TSDLGraphicsDriver Extends TGraphicsDriver
 		End If
 
 		context.window = TSDLWindow.Create(AppTitle, x, y, width, height, ULong(flags))
+
+		If depth > 0 Then
+			Local display:TSDLDisplay = context.window.GetDisplay()
+			Local mode:SDLDisplayMode
+			Select depth
+				Case 32
+					mode.format = SDL_PIXELFORMAT_RGBA8888
+				Case 24
+					mode.format = SDL_PIXELFORMAT_RGB888
+			End Select
+			mode.width = width
+			mode.height = height
+			mode.refreshRate = hertz
+
+			Local closest:SDLDisplayMode
+
+			If SDL_GetClosestDisplayMode(display.index, mode, closest) <> Null Then
+				context.window.SetDisplayMode(closest)
+			End If
+		End If
+
 		If flags & SDL_GRAPHICS_GL Then
 			context.context = context.window.GLCreateContext()
 			SDL_GL_SetSwapInterval(-1)

+ 32 - 4
sdlvideo.mod/common.bmx

@@ -64,9 +64,6 @@ Extern
 	Function bmx_sdl_video_VideoInit:Int(driver:String)
 	Function bmx_sdl_video_GetDisplayBounds:Int(index:Int, w:Int Ptr, h:Int Ptr)
 	Function bmx_sdl_video_GetDisplayUsableBounds:Int(index:Int, w:Int Ptr, h:Int Ptr)
-	Function bmx_sdl_video_GetDisplayMode:Byte Ptr(index:Int, modeIndex:Int)
-	Function bmx_sdl_video_GetDesktopDisplayMode:Byte Ptr(index:Int)
-	Function bmx_sdl_video_GetCurrentDisplayMode:Byte Ptr(index:Int)
 	Function bmx_sdl_video_SDL_GetCurrentVideoDriver:String()
 
 	Function SDL_GetNumVideoDisplays:Int()
@@ -93,7 +90,8 @@ Extern
 	Function SDL_GetWindowDisplayIndex:Int(handle:Byte Ptr)
 	Function SDL_GetWindowPixelFormat:UInt(handle:Byte Ptr)
 	Function SDL_GetWindowID:UInt(handle:Byte Ptr)
-	Function SDL_SetWindowDisplayMode:Int(handle:Byte Ptr, Mode:Byte Ptr)
+	Function SDL_SetWindowDisplayMode:Int(handle:Byte Ptr, mode:SDLDisplayMode Var)
+	Function SDL_GetWindowDisplayMode:Int(handle:Byte ptr, mode:SDLDisplayMode Var)
 	Function SDL_ShowWindow(handle:Byte Ptr)
 	Function SDL_HideWindow(handle:Byte Ptr)
 	Function SDL_RaiseWindow(handle:Byte Ptr)
@@ -128,6 +126,10 @@ Extern
 	Function SDL_RestoreWindow(handle:Byte Ptr)
 	Function SDL_SetWindowIcon(handle:Byte Ptr, surface:Byte Ptr)
 	Function SDL_WarpMouseInWindow(handle:Byte Ptr, x:Int, y:Int)
+	Function SDL_GetDisplayMode:Int(displayIndex:Int, modeIndex:Int, mode:SDLDisplayMode Var)
+	Function SDL_GetDesktopDisplayMode:Int(displayIndex:Int, mode:SDLDisplayMode Var)
+	Function SDL_GetCurrentDisplayMode:Int(displayIndex:Int, mode:SDLDisplayMode Var)
+	Function SDL_GetClosestDisplayMode:SDLDisplayMode Ptr(displayIndex:Int, mode:SDLDisplayMode Var, closest:SDLDisplayMode Var)
 
 	Function SDL_GetGrabbedWindow:Byte Ptr()
 	Function SDL_IsScreenSaverEnabled:Int()
@@ -399,3 +401,29 @@ Rem
 bbdoc: The display is in portrait mode, upside down
 End Rem
 Const SDL_ORIENTATION_PORTRAIT_FLIPPED:Int = 4
+
+Rem
+bbdoc: A structure that contains the description of a display mode.
+End Rem
+Struct SDLDisplayMode
+	Rem
+	bbdoc: pixel format
+	End Rem
+	Field format:UInt
+	Rem
+	bbdoc: width, in screen coordinates
+	End Rem
+	Field width:Int
+	Rem
+	bbdoc: height, in screen coordinates
+	End Rem
+	Field height:Int
+	Rem
+	bbdoc: refresh rate (or zero for unspecified)
+	End Rem
+	Field refreshRate:Int
+	Rem
+	bbdoc: driver-specific data, initialize to 0
+	End Rem
+	Field driverdata:Byte Ptr
+End Struct

+ 5 - 6
sdlvideo.mod/examples/displays.bmx

@@ -28,12 +28,11 @@ For Local i:Int = 0 Until displayCount
 	Local modes:Int = display.GetNumDisplayModes()
 	
 	For Local m:Int = 0 Until modes
-	
-		Local mode:TSDLDisplayMode = display.GetDisplayMode(m)
-		If mode Then
-			Print "      : " + (mode.Width() + " x " + mode.Height() + "             ")[..20] + ..
-				(SDLGetPixelFormatName(mode.Format()) + "               ")[..30] + ..
-				mode.RefreshRate() + " hz"
+		Local mode:SDLDisplayMode
+		If Not display.GetDisplayMode(m, mode) Then
+			Print "      : " + (mode.width + " x " + mode.height + "             ")[..20] + ..
+				(SDLGetPixelFormatName(mode.format) + "               ")[..30] + ..
+				mode.refreshRate + " hz"
 		End If
 	Next
 	

+ 0 - 81
sdlvideo.mod/glue.c

@@ -74,36 +74,6 @@ int bmx_sdl_video_GetDisplayUsableBounds(int index, int * w, int * h) {
 	return res;
 }
 
-SDL_DisplayMode * bmx_sdl_video_GetDisplayMode(int index, int modeIndex) {
-	SDL_DisplayMode * mode = (SDL_DisplayMode*)calloc(1, sizeof(SDL_DisplayMode));
-	if (!SDL_GetDisplayMode(index, modeIndex, mode)) {
-		return mode;
-	} else {
-		free(mode);
-		return NULL;
-	}
-}
-
-SDL_DisplayMode * bmx_sdl_video_GetDesktopDisplayMode(int index) {
-	SDL_DisplayMode * mode = (SDL_DisplayMode*)calloc(1, sizeof(SDL_DisplayMode));
-	if (!SDL_GetDesktopDisplayMode(index, mode)) {
-		return mode;
-	} else {
-		free(mode);
-		return NULL;
-	}
-}
-
-SDL_DisplayMode * bmx_sdl_video_GetCurrentDisplayMode(int index) {
-	SDL_DisplayMode * mode = (SDL_DisplayMode*)calloc(1, sizeof(SDL_DisplayMode));
-	if (!SDL_GetCurrentDisplayMode(index, mode)) {
-		return mode;
-	} else {
-		free(mode);
-		return NULL;
-	}
-}
-
 BBString * bmx_sdl_video_SDL_GetCurrentVideoDriver() {
 	if (!SDL_GetCurrentVideoDriver()) {
 		return &bbEmptyString;
@@ -114,51 +84,6 @@ BBString * bmx_sdl_video_SDL_GetCurrentVideoDriver() {
 
 // --------------------------------------------------------
 
-SDL_DisplayMode * bmx_sdl_video_DisplayMode_new(Uint32 format, int width, int height, int refreshRate) {
-	SDL_DisplayMode * mode = (SDL_DisplayMode*)calloc(1, sizeof(SDL_DisplayMode));
-	mode->format = format;
-	mode->w = width;
-	mode->h = height;
-	mode->refresh_rate = refreshRate;
-	return mode;
-}
-
-void bmx_sdl_video_DisplayMode_free(SDL_DisplayMode * mode) {
-	free(mode);
-}
-
-Uint32 bmx_sdl_video_DisplayMode_format(SDL_DisplayMode * mode) {
-	return mode->format;
-}
-
-int bmx_sdl_video_DisplayMode_width(SDL_DisplayMode * mode) {
-	return mode->w;
-}
-
-int bmx_sdl_video_DisplayMode_height(SDL_DisplayMode * mode) {
-	return mode->h;
-}
-
-int bmx_sdl_video_DisplayMode_refreshRate(SDL_DisplayMode * mode) {
-	return mode->refresh_rate;
-}
-
-void * bmx_sdl_video_DisplayMode_driverData(SDL_DisplayMode * mode) {
-	return mode->driverdata;
-}
-
-SDL_DisplayMode * bmx_sdl_video_GetClosestDisplayMode(SDL_DisplayMode * mode, int index) {
-	SDL_DisplayMode * closest = (SDL_DisplayMode*)calloc(1, sizeof(SDL_DisplayMode));
-	if (SDL_GetClosestDisplayMode(index, mode, closest) != NULL) {
-		return closest;
-	} else {
-		free(closest);
-		return NULL;
-	}
-}
-
-// --------------------------------------------------------
-
 #ifdef _WIN32
 void bmx_sdl_video_MakeAppIcon() {
 
@@ -251,12 +176,6 @@ SDL_Window * bmx_sdl_video_CreateWindow(BBString * title, int x, int y, int w, i
 	return window;
 }
 
-SDL_DisplayMode * bmx_sdl_video_GetWindowDisplayMode(SDL_Window * window) {
-	SDL_DisplayMode * mode = (SDL_DisplayMode*)calloc(1, sizeof(SDL_DisplayMode));
-	int res = SDL_GetWindowDisplayMode(window, mode);
-	return mode;
-}
-
 void bmx_sdl_video_SetWindowTitle(SDL_Window * window, BBString * title) {
 	char * t = bbStringToUTF8String(title);
 	SDL_SetWindowTitle(window, t);

+ 13 - 84
sdlvideo.mod/sdlvideo.bmx

@@ -67,16 +67,16 @@ Type TSDLWindow
 	about: This only affects the display mode used when the window is fullscreen. To change the window size when the window is not fullscreen,
 	use #SetSize().
 	End Rem
-	Method SetDisplayMode:Int(Mode:TSDLDisplayMode)
-		Return SDL_SetWindowDisplayMode(windowPtr, Mode.modePtr)
+	Method SetDisplayMode:Int(mode:SDLDisplayMode Var)
+		Return SDL_SetWindowDisplayMode(windowPtr, mode)
 	End Method
 	
 	Rem
 	bbdoc: Gets information about the display mode to use when a window is visible at fullscreen.
 	returns: The display mode on success or Null on failure.
 	End Rem
-	Method GetDisplayMode:TSDLDisplayMode()
-		Return TSDLDisplayMode._create(bmx_sdl_video_GetWindowDisplayMode(windowPtr))
+	Method GetDisplayMode:Int(mode:SDLDisplayMode Var)
+		Return SDL_GetWindowDisplayMode(windowPtr, mode)
 	End Method
 	
 	Rem
@@ -671,87 +671,24 @@ Type TSDLDisplay
 	bbdoc: Gets information about a specific display mode.
 	returns: A display mode or Null on failure.
 	End Rem
-	Method GetDisplayMode:TSDLDisplayMode(modeIndex:Int)
-		If modeIndex >= 0 And modeIndex < SDL_GetNumDisplayModes(index) Then
-			Return TSDLDisplayMode._create(bmx_sdl_video_GetDisplayMode(index, modeIndex), index)
-		End If
+	Method GetDisplayMode:Int(modeIndex:Int, mode:SDLDisplayMode Var)
+		Return SDL_GetDisplayMode(index, modeIndex, mode)
 	End Method
 	
 	Rem
 	bbdoc: Gets information about the desktop display mode.
 	returns: A display mode or Null on failure.
 	End Rem
-	Method GetDesktopDisplayMode:TSDLDisplayMode()
-		Return TSDLDisplayMode._create(bmx_sdl_video_GetDesktopDisplayMode(index), index)
+	Method GetDesktopDisplayMode:Int(mode:SDLDisplayMode Var)
+		Return SDL_GetDesktopDisplayMode(index, mode)
 	End Method
 	
 	Rem
 	bbdoc: Gets information about the current display mode.
 	returns: A display mode or Null on failure.
 	End Rem
-	Method GetCurrentDisplayMode:TSDLDisplayMode()
-		Return TSDLDisplayMode._create(bmx_sdl_video_GetCurrentDisplayMode(index), index)
-	End Method
-
-End Type
-
-Rem
-bbdoc: The description of a display mode.
-End Rem
-Type TSDLDisplayMode
-
-	Field displayIndex:Int
-	Field modePtr:Byte Ptr
-	
-	Function _create:TSDLDisplayMode(modePtr:Byte Ptr, displayIndex:Int = -1)
-		If modePtr Then
-			Local this:TSDLDisplayMode = New TSDLDisplayMode
-			this.displayIndex = displayIndex
-			this.modePtr = modePtr
-			Return this
-		End If
-	End Function
-	
-	Rem
-	bbdoc: Creates a new custom display mode, useful for calling #GetClosestDisplayMode.
-	End Rem
-	Function Create:TSDLDisplayMode(format:UInt, width:Int, height:Int, refreshRate:Int)
-		Return _create(bmx_sdl_video_DisplayMode_new(format, width, height, refreshRate))
-	End Function
-	
-	Rem
-	bbdoc: One of the SDL pixel formats.
-	End Rem
-	Method Format:UInt()
-		Return bmx_sdl_video_DisplayMode_format(modePtr)
-	End Method
-	
-	Rem
-	bbdoc: The width, in screen coordinates.
-	End Rem
-	Method Width:Int()
-		Return bmx_sdl_video_DisplayMode_width(modePtr)
-	End Method
-	
-	Rem
-	bbdoc: The height, in screen coordinates.
-	End Rem
-	Method Height:Int()
-		Return bmx_sdl_video_DisplayMode_height(modePtr)
-	End Method
-	
-	Rem
-	bbdoc: The refresh rate (in Hz), or 0 for unspecified.
-	End Rem
-	Method RefreshRate:Int()
-		Return bmx_sdl_video_DisplayMode_refreshRate(modePtr)
-	End Method
-	
-	Rem
-	bbdoc: Driver-specific data.
-	End Rem
-	Method DriverData:Byte Ptr()
-		Return bmx_sdl_video_DisplayMode_driverData(modePtr)
+	Method GetCurrentDisplayMode:Int(mode:SDLDisplayMode Var)
+		Return SDL_GetCurrentDisplayMode(index, mode)
 	End Method
 
 	Rem
@@ -762,19 +699,11 @@ Type TSDLDisplayMode
 	The modes are scanned with size being first priority, format being second priority, and finally checking the refresh rate.
 	If all the available modes are too small, then Null is returned.
 	End Rem
-	Method GetClosestDisplayMode:TSDLDisplayMode(display:TSDLDisplay)
-		Return _create(bmx_sdl_video_GetClosestDisplayMode(modePtr, display.index), display.index)
-	End Method
-	
-	Method Delete()
-		If modePtr Then
-			bmx_sdl_video_DisplayMode_free(modePtr)
-			modePtr = Null
-		End If
+	Method GetClosestDisplayMode:SDLDisplayMode Ptr(mode:SDLDisplayMode, closest:SDLDisplayMode Var)
+		Return SDL_GetClosestDisplayMode(index, mode, closest)
 	End Method
-	
-End Type
 
+End Type
 
 Rem
 bbdoc: Returns the list of built-in video drivers.