Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

linux SDL canvas offset bug #2122

Open
wants to merge 5 commits into
base: development
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions Engine/source/gfx/gfxInit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -395,6 +395,11 @@ GFXVideoMode GFXInit::getDesktopResolution()
return resVm;
}

RectI GFXInit::getPrimaryDesktopArea()
{
return WindowManager->getPrimaryDesktopArea();
}

void GFXInit::enumerateAdapters()
{
// Call each device class and have it report any adapters it supports.
Expand Down Expand Up @@ -436,6 +441,13 @@ DefineEngineFunction( getDesktopResolution, Point3F, (),,
return Point3F( res.resolution.x, res.resolution.y, res.bitDepth );
}

DefineEngineFunction( getPrimaryDesktopArea, RectI, (),,
"Return the rect of the available desktop area.\n\n@ingroup GFX" )
{
RectI rect = GFXInit::getPrimaryDesktopArea();
return rect;
}

DefineEngineStaticMethod( GFXInit, getAdapterCount, S32, (),,
"Return the number of graphics adapters available. @ingroup GFX")
{
Expand Down
2 changes: 2 additions & 0 deletions Engine/source/gfx/gfxInit.h
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,8 @@ class GFXInit
/// This should probably move to the abstract window manager
static GFXVideoMode getDesktopResolution();

static RectI getPrimaryDesktopArea();

/// Based on user preferences (or in the absence of a valid user selection,
/// a heuristic), return a "best" adapter.
static GFXAdapter *getBestAdapterChoice();
Expand Down
20 changes: 17 additions & 3 deletions Engine/source/gui/core/guiCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2675,6 +2675,21 @@ DefineConsoleMethod( GuiCanvas, restoreWindow, void, (), , "() - restore this ca
window->restore();
}

DefineConsoleMethod( GuiCanvas, setSize, void, (U32 w, U32 h), , "() - set the size of this canvas' window." )
{
PlatformWindow* window = object->getPlatformWindow();
if( window )
window->setSize(Point2I(w,h));
}

DefineConsoleMethod( GuiCanvas, getWindowBorders, Point2I, (), , "() - get the border sizes of this canvas' window." )
{
PlatformWindow* window = object->getPlatformWindow();
if( window )
return window->getWindowBorders();
return Point2I(0,0);
}

DefineConsoleMethod( GuiCanvas, setFocus, void, (), , "() - Claim OS input focus for this canvas' window.")
{
PlatformWindow* window = object->getPlatformWindow();
Expand All @@ -2683,9 +2698,8 @@ DefineConsoleMethod( GuiCanvas, setFocus, void, (), , "() - Claim OS input focus
}

DefineEngineMethod( GuiCanvas, setMenuBar, void, ( GuiControl* menu ),,
"Translate a coordinate from canvas window-space to screen-space.\n"
"@param coordinate The coordinate in window-space.\n"
"@return The given coordinate translated to screen-space." )
"Set a menu bar to the canvas\n"
"@param menu The menu bar to set to the canvas.\n")
{
return object->setMenuBar( menu );
}
Expand Down
2 changes: 2 additions & 0 deletions Engine/source/windowManager/platformWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -261,6 +261,8 @@ class PlatformWindow
/// Get the Client Area Extent (Resolution) of this window
virtual const Point2I getClientExtent() = 0;

virtual const Point2I getWindowBorders() = 0;

/// @}
/// The bounds of a Window are defined as the entire area occupied by
/// that Window. This includes the area needed for a title-bar, menu,
Expand Down
7 changes: 7 additions & 0 deletions Engine/source/windowManager/sdl/sdlWindow.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,13 @@ const Point2I PlatformWindowSDL::getClientExtent()
return size;
}

const Point2I PlatformWindowSDL::getWindowBorders()
{
int top, right, bottom, left = 0;
SDL_GetWindowBordersSize(mWindowHandle, &top, &right, &bottom, &left);
return Point2I( left+right, top+bottom );
}

void PlatformWindowSDL::setBounds( const RectI &newBounds )
{
// TODO SDL
Expand Down
2 changes: 2 additions & 0 deletions Engine/source/windowManager/sdl/sdlWindow.h
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,8 @@ class PlatformWindowSDL : public PlatformWindow
// Window Client Area Extent
virtual void setClientExtent( const Point2I newExtent );
virtual const Point2I getClientExtent();

virtual const Point2I getWindowBorders();

// Window Bounds
virtual void setBounds(const RectI &newBounds);
Expand Down
7 changes: 3 additions & 4 deletions Engine/source/windowManager/sdl/sdlWindowMgr.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,16 +75,15 @@ PlatformWindowManagerSDL::~PlatformWindowManagerSDL()

RectI PlatformWindowManagerSDL::getPrimaryDesktopArea()
{
// TODO SDL
AssertFatal(0, "");
return RectI(0,0,0,0);
SDL_Rect rect;
SDL_GetDisplayUsableBounds(0, &rect);
return RectI(rect.x, rect.y, rect.w, rect.h);
}

Point2I PlatformWindowManagerSDL::getDesktopResolution()
{
SDL_DisplayMode mode;
SDL_GetDesktopDisplayMode(0, &mode);

// Return Resolution
return Point2I(mode.w, mode.h);
}
Expand Down
42 changes: 34 additions & 8 deletions Templates/BaseGame/game/core/canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,10 @@ function configureCanvas()
echo("--------------");
echo("Attempting to set resolution to \"" @ %resX SPC %resY SPC %fs SPC %bpp SPC %rate SPC %aa @ "\"");

%deskRes = getDesktopResolution();
%deskResX = getWord(%deskRes, $WORD::RES_X);
%deskResY = getWord(%deskRes, $WORD::RES_Y);
%deskResBPP = getWord(%deskRes, 2);
%deskRes = getPrimaryDesktopArea();
%deskResX = getWord(%deskRes, 2) - getWord(%deskRes, 0);
%deskResY = getWord(%deskRes, 3) - getWord(%deskRes, 1);
%deskResBPP = getWord(getDesktopResolution(), 2);

// We shouldn't be getting this any more but just in case...
if (%bpp $= "Default")
Expand All @@ -98,9 +98,11 @@ function configureCanvas()
{
// Windowed mode has to use the same bit depth as the desktop
%bpp = %deskResBPP;

%switched = false;

// Windowed mode also has to run at a smaller resolution than the desktop
if ((%resX >= %deskResX) || (%resY >= %deskResY))
if ((%resX > %deskResX) || (%resY > %deskResY))
{
warn("Warning: The requested windowed resolution is equal to or larger than the current desktop resolution. Attempting to find a better resolution");

Expand All @@ -112,21 +114,36 @@ function configureCanvas()
%testResY = getWord(%testRes, $WORD::RES_Y);
%testBPP = getWord(%testRes, $WORD::BITDEPTH);

if (%testBPP != %bpp)
continue;
//TODO Canvas.getMode does not return bpp reliably
//if (%testBPP != %bpp)
// continue;

if ((%testResX < %deskResX) && (%testResY < %deskResY))
if ((%testResX <= %deskResX) && (%testResY <= %deskResY))
{
// This will work as our new resolution
%resX = %testResX;
%resY = %testResY;
%switched = true;

warn("Warning: Switching to \"" @ %resX SPC %resY SPC %bpp @ "\"");

break;
}
}
}

//If it should change resolution but doesn't, use available desktop area
if(!%switched)
{
%padding = 0;

//need padding for linux (macOS?)
if($platform !$= "windows") %padding = 10;

if(%resX > %deskResX) %resX = %deskResX - 10;
if(%resY > %deskResY) %resY = %deskResY - 10;
warn("Warning: Switching to \"" @ %resX SPC %resY SPC %bpp @ "\"");
}
}

$pref::Video::Resolution = %resX SPC %resY;
Expand All @@ -151,6 +168,15 @@ function configureCanvas()
// Actually set the new video mode
Canvas.setVideoMode(%resX, %resY, %fs, %bpp, %rate, %aa);

//If maximized then restore and set the size to prevent
//linux canvas offseting issue
if(Canvas.isMaximized() && $platform !$= "windows")
{
Canvas.restoreWindow();
Canvas.setSize(%resX, %resY);
}


commandToServer('setClientAspectRatio', %resX, %resY);

// AA piggybacks on the AA setting in $pref::Video::mode.
Expand Down
42 changes: 34 additions & 8 deletions Templates/Full/game/core/scripts/client/canvas.cs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,10 @@ function configureCanvas()
echo("--------------");
echo("Attempting to set resolution to \"" @ %resX SPC %resY SPC %fs SPC %bpp SPC %rate SPC %fsaa @ "\"");

%deskRes = getDesktopResolution();
%deskResX = getWord(%deskRes, $WORD::RES_X);
%deskResY = getWord(%deskRes, $WORD::RES_Y);
%deskResBPP = getWord(%deskRes, 2);
%deskRes = getPrimaryDesktopArea();
%deskResX = getWord(%deskRes, 2) - getWord(%deskRes, 0);
%deskResY = getWord(%deskRes, 3) - getWord(%deskRes, 1);
%deskResBPP = getWord(getDesktopResolution(), 2);

// We shouldn't be getting this any more but just in case...
if (%bpp $= "Default")
Expand All @@ -62,8 +62,10 @@ function configureCanvas()
// Windowed mode has to use the same bit depth as the desktop
%bpp = %deskResBPP;

%switched = false;

// Windowed mode also has to run at a smaller resolution than the desktop
if ((%resX >= %deskResX) || (%resY >= %deskResY))
if ((%resX > %deskResX) || (%resY > %deskResY))
{
warn("Warning: The requested windowed resolution is equal to or larger than the current desktop resolution. Attempting to find a better resolution");

Expand All @@ -75,20 +77,35 @@ function configureCanvas()
%testResY = getWord(%testRes, $WORD::RES_Y);
%testBPP = getWord(%testRes, $WORD::BITDEPTH);

if (%testBPP != %bpp)
continue;
//TODO Canvas.getMode does not return bpp reliably
//if (%testBPP != %bpp)
// continue;

if ((%testResX < %deskResX) && (%testResY < %deskResY))
if ((%testResX <= %deskResX) && (%testResY <= %deskResY))
{
// This will work as our new resolution
%resX = %testResX;
%resY = %testResY;
%switched = true;

warn("Warning: Switching to \"" @ %resX SPC %resY SPC %bpp @ "\"");

break;
}
}

//If it should change resolution but doesn't, use available desktop area
if(!%switched)
{
%padding = 0;

//need padding for linux (macOS?)
if($platform !$= "windows") %padding = 10;

if(%resX > %deskResX) %resX = %deskResX - 10;
if(%resY > %deskResY) %resY = %deskResY - 10;
warn("Warning: Switching to \"" @ %resX SPC %resY SPC %bpp @ "\"");
}
}
}

Expand All @@ -109,6 +126,15 @@ function configureCanvas()

// Actually set the new video mode
Canvas.setVideoMode(%resX, %resY, %fs, %bpp, %rate, %fsaa);

//If maximized then restore and set the size to prevent
//linux canvas offseting issue
if(Canvas.isMaximized() && $platform !$= "windows")
{
Canvas.restoreWindow();
Canvas.setSize(%resX, %resY);
}


// FXAA piggybacks on the FSAA setting in $pref::Video::mode.
if ( isObject( FXAA_PostEffect ) )
Expand Down