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

Add scaling factor option #28253

Closed
wants to merge 6 commits into from
Closed
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
1 change: 1 addition & 0 deletions src/cursesport.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,5 +77,6 @@ void clear_window_area( const catacurses::window &win );
int projected_window_width();
int projected_window_height();
bool handle_resize( int w, int h );
int get_scaling_factor();

#endif
4 changes: 2 additions & 2 deletions src/game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -426,8 +426,8 @@ void game::init_ui( const bool resized )
TERMY = get_terminal_height();

if( resized ) {
get_options().get_option( "TERMINAL_X" ).setValue( TERMX );
get_options().get_option( "TERMINAL_Y" ).setValue( TERMY );
get_options().get_option( "TERMINAL_X" ).setValue( TERMX * get_scaling_factor() );
get_options().get_option( "TERMINAL_Y" ).setValue( TERMY * get_scaling_factor() );
get_options().save();
}
#else
Expand Down
20 changes: 20 additions & 0 deletions src/options.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1594,6 +1594,17 @@ void options_manager::add_options_graphics()
{ "linear", translate_marker( "Linear filtering" ) }
},
"none", COPT_CURSES_HIDE );

#ifndef __ANDROID__
add( "SCALING_FACTOR", "graphics", translate_marker( "Scaling factor" ),
translate_marker( "Factor by which to scale the display. Requires restart." ), {
{ "1", translate_marker( "1x" ) },
{ "2", translate_marker( "2x" )},
{ "4", translate_marker( "4x" )}
},
"1", COPT_CURSES_HIDE );
#endif

}

void options_manager::add_options_debug()
Expand Down Expand Up @@ -2516,6 +2527,15 @@ std::string options_manager::show( bool ingame, const bool world_options_only )

#if !defined(__ANDROID__) && (defined TILES || defined _WIN32 || defined WINDOWS)
if( terminal_size_changed ) {
int scaling_factor = get_scaling_factor();
int TERMX = ::get_option<int>( "TERMINAL_X" );
int TERMY = ::get_option<int>( "TERMINAL_Y" );
TERMX -= TERMX % scaling_factor;
TERMY -= TERMY % scaling_factor;
get_option( "TERMINAL_X" ).setValue( std::max( 80 * scaling_factor, TERMX ) );
get_option( "TERMINAL_Y" ).setValue( std::max( 24 * scaling_factor, TERMY ) );
Copy link
Contributor

@Night-Pryanik Night-Pryanik Feb 24, 2019

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can replace 80 and 24 magic numbers with FULL_SCREEN_WIDTH and FULL_SCREEN_HEIGHT respectively, here and below.

save();

handle_resize( projected_window_width(), projected_window_height() );
}
#else
Expand Down
138 changes: 124 additions & 14 deletions src/sdltiles.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ class Font {
#ifdef __ANDROID__
opacity(1.0f),
#endif
fontwidth(w), fontheight(h) { }
fontwidth( w ), fontheight( h ) { }
virtual ~Font() = default;
/**
* Draw character t at (x,y) on the screen,
Expand Down Expand Up @@ -198,6 +198,7 @@ int fontheight; //the height of the font, background is always this size
static int TERMINAL_WIDTH;
static int TERMINAL_HEIGHT;
bool fullscreen;
int scaling_factor;

static SDL_Joystick *joystick; // Only one joystick for now.

Expand Down Expand Up @@ -311,7 +312,12 @@ void InitSDL()
bool SetupRenderTarget()
{
SetRenderDrawBlendMode( renderer, SDL_BLENDMODE_NONE );
display_buffer.reset( SDL_CreateTexture( renderer.get(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, WindowWidth, WindowHeight ) );
if( scaling_factor > 1 ) {
SDL_RenderSetLogicalSize( renderer.get(), WindowWidth / scaling_factor, WindowHeight / scaling_factor );
display_buffer.reset( SDL_CreateTexture( renderer.get(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, WindowWidth / scaling_factor, WindowHeight / scaling_factor ) );
} else {
display_buffer.reset( SDL_CreateTexture( renderer.get(), SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_TARGET, WindowWidth, WindowHeight ) );
}
if( printErrorIf( !display_buffer, "Failed to create window buffer" ) ) {
return false;
}
Expand All @@ -330,8 +336,8 @@ void WinCreate()

// Common flags used for fulscreen and for windowed
int window_flags = 0;
WindowWidth = TERMINAL_WIDTH * fontwidth;
WindowHeight = TERMINAL_HEIGHT * fontheight;
WindowWidth = TERMINAL_WIDTH * fontwidth * scaling_factor;
WindowHeight = TERMINAL_HEIGHT * fontheight * scaling_factor;
window_flags |= SDL_WINDOW_RESIZABLE | SDL_WINDOW_ALLOW_HIGHDPI;

if( get_option<std::string>( "SCALING_MODE" ) != "none" ) {
Expand Down Expand Up @@ -380,8 +386,8 @@ void WinCreate()
if (window_flags & SDL_WINDOW_FULLSCREEN || window_flags & SDL_WINDOW_FULLSCREEN_DESKTOP) {
SDL_GetWindowSize( ::window.get(), &WindowWidth, &WindowHeight );
// Ignore previous values, use the whole window, but nothing more.
TERMINAL_WIDTH = WindowWidth / fontwidth;
TERMINAL_HEIGHT = WindowHeight / fontheight;
TERMINAL_WIDTH = WindowWidth / fontwidth / scaling_factor;
TERMINAL_HEIGHT = WindowHeight / fontheight / scaling_factor;
}
#endif
// Initialize framebuffer caches
Expand Down Expand Up @@ -448,7 +454,7 @@ void WinCreate()
throwErrorIf( !SetupRenderTarget(), "Failed to initialize display buffer under software rendering, unable to continue." );
}

SDL_SetWindowMinimumSize( ::window.get(), fontwidth * 80, fontheight * 24 );
SDL_SetWindowMinimumSize( ::window.get(), fontwidth * 80 * scaling_factor, fontheight * 24 * scaling_factor );

#ifdef __ANDROID__
// TODO: Not too sure why this works to make fullscreen on Android behave. :/
Expand Down Expand Up @@ -754,6 +760,9 @@ void refresh_display()
// Select default target (the window), copy rendered buffer
// there, present it, select the buffer as target again.
SetRenderTarget( renderer, NULL );
if( scaling_factor > 1 ) {
SDL_RenderSetLogicalSize( renderer.get(), WindowWidth / scaling_factor, WindowHeight / scaling_factor );
}
#ifdef __ANDROID__
SDL_Rect dstrect = get_android_render_rect( TERMINAL_WIDTH * fontwidth, TERMINAL_HEIGHT * fontheight );
SetRenderDrawColor( renderer, 0, 0, 0, 255 );
Expand Down Expand Up @@ -954,6 +963,9 @@ void clear_window_area( const catacurses::window &win_ )

void cata_cursesport::curses_drawwindow( const catacurses::window &w )
{
if( scaling_factor > 1 ) {
SDL_RenderSetLogicalSize( renderer.get(), WindowWidth / scaling_factor, WindowHeight / scaling_factor );
}
WINDOW *const win = w.get<WINDOW>();
bool update = false;
if (g && w == g->w_terrain && use_tiles) {
Expand Down Expand Up @@ -1091,6 +1103,9 @@ bool Font::draw_window( const catacurses::window &w )

bool Font::draw_window( const catacurses::window &w, const int offsetx, const int offsety )
{
// SDL_RenderSetScale( renderer.get(), 1.2, 1.2);
SDL_RenderSetLogicalSize( renderer.get(), WindowWidth / scaling_factor, WindowHeight / scaling_factor );

cata_cursesport::WINDOW *const win = w.get<cata_cursesport::WINDOW>();
//Keeping track of the last drawn window
const cata_cursesport::WINDOW *winBuffer = static_cast<cata_cursesport::WINDOW*>( ::winBuffer.lock().get() );
Expand Down Expand Up @@ -1432,8 +1447,8 @@ bool handle_resize(int w, int h)
if( ( w != WindowWidth ) || ( h != WindowHeight ) ) {
WindowWidth = w;
WindowHeight = h;
TERMINAL_WIDTH = WindowWidth / fontwidth;
TERMINAL_HEIGHT = WindowHeight / fontheight;
TERMINAL_WIDTH = WindowWidth / fontwidth / scaling_factor;
TERMINAL_HEIGHT = WindowHeight / fontheight / scaling_factor;
SetupRenderTarget();
game_ui::init_ui();
tilecontext->reinit_minimap();
Expand All @@ -1445,16 +1460,16 @@ bool handle_resize(int w, int h)

void toggle_fullscreen_window()
{
static int restore_win_w = get_option<int>( "TERMINAL_X" ) * fontwidth;
static int restore_win_h = get_option<int>( "TERMINAL_Y" ) * fontheight;
static int restore_win_w = get_option<int>( "TERMINAL_X" ) * fontwidth * scaling_factor;
static int restore_win_h = get_option<int>( "TERMINAL_Y" ) * fontheight * scaling_factor;

if ( fullscreen ) {
if( printErrorIf( SDL_SetWindowFullscreen( window.get(), 0 ) != 0, "SDL_SetWindowFullscreen failed" ) ) {
return;
}
SDL_RestoreWindow( window.get() );
SDL_SetWindowSize( window.get(), restore_win_w, restore_win_h );
SDL_SetWindowMinimumSize( window.get(), fontwidth * 80, fontheight * 24 );
SDL_SetWindowMinimumSize( window.get(), fontwidth * 80 * scaling_factor, fontheight * 24 * scaling_factor );
} else {
restore_win_w = WindowWidth;
restore_win_h = WindowHeight;
Expand Down Expand Up @@ -2948,6 +2963,98 @@ int projected_window_height()
return get_option<int>( "TERMINAL_Y" ) * fontheight;
}

void init_term_size_and_scaling_factor()
{
scaling_factor = 1;
int TERMX = get_option<int>( "TERMINAL_X" );
int TERMY = get_option<int>( "TERMINAL_Y" );

#ifndef __ANDROID__

SDL_DisplayMode current;
int display_width, display_height;
bool windowed = get_option<std::string>( "FULLSCREEN" ) == "no";

if( SDL_GetDesktopDisplayMode( get_option<int>( "DISPLAY" ), &current ) == 0 ) {
display_width = current.w;
display_height = current.h;

// Require extra space if not in fullscreen or borderless window mode to account for taskbar/whatever
// It would be better if there was a cross platform way to actually check for the maximum window size
if( windowed ) {
display_width -= display_width / 10;
display_height -= display_height / 10;
}

} else {
dbg( D_WARNING ) << "Failed to get current Display Mode, assuming infinite display size.";
display_width = INT_MAX;
display_height = INT_MAX;
}

std::cout << display_width << ", " << display_height << std::endl;

if( get_option<std::string>( "SCALING_FACTOR" ) == "2" ) {
scaling_factor = 2;
} else if( get_option<std::string>( "SCALING_FACTOR" ) == "4" ) {
scaling_factor = 4;
}

if( TERMX * fontwidth > display_width || 80 * fontwidth * scaling_factor > display_width ) {
if( 80 * fontwidth * scaling_factor > display_width ) {
dbg( D_WARNING ) << "SCALING_FACTOR set too high for display size, resetting to 1";
scaling_factor = 1;
TERMX = current.w / fontwidth;
TERMY = current.h / fontheight;
if( windowed ) {
TERMX -= TERMX / 10;
TERMY -= TERMY / 10;
}
get_options().get_option( "SCALING_FACTOR" ).setValue( "1" );
} else {
TERMX = current.w / fontwidth;
if( windowed ) {
TERMX -= TERMX / 10;
}
}
}

if( TERMY * fontheight > display_height || 24 * fontheight * scaling_factor > display_height ) {
if( 24 * fontheight * scaling_factor > display_height ) {
dbg( D_WARNING ) << "SCALING_FACTOR set too high for display size, resetting to 1";
scaling_factor = 1;
TERMX = current.w / fontwidth;
TERMY = current.h / fontheight;
if( windowed ) {
TERMX -= TERMX / 10;
TERMY -= TERMY / 10;
}
get_options().get_option( "SCALING_FACTOR" ).setValue( "1" );
} else {
TERMY = current.h / fontheight;
if( windowed ) {
TERMY -= TERMY / 10;
}
}
}

TERMX -= TERMX % scaling_factor;
TERMY -= TERMY % scaling_factor;

TERMX = std::max( 80 * scaling_factor, TERMX );
TERMY = std::max( 24 * scaling_factor, TERMY );

get_options().get_option( "TERMINAL_X" ).setValue( std::max( 80 * scaling_factor, TERMX ) );
get_options().get_option( "TERMINAL_Y" ).setValue( std::max( 24 * scaling_factor, TERMY ) );

get_options().save();

#endif //__ANDROID__

TERMINAL_WIDTH = TERMX / scaling_factor;
TERMINAL_HEIGHT = TERMY / scaling_factor;
}

//Basic Init, create the font, backbuffer, etc
void catacurses::init_interface()
{
Expand All @@ -2963,8 +3070,7 @@ void catacurses::init_interface()

find_videodisplays();

TERMINAL_WIDTH = get_option<int>( "TERMINAL_X" );
TERMINAL_HEIGHT = get_option<int>( "TERMINAL_Y" );
init_term_size_and_scaling_factor();

WinCreate();

Expand Down Expand Up @@ -3195,6 +3301,10 @@ int get_terminal_height() {
return TERMINAL_HEIGHT;
}

int get_scaling_factor() {
return scaling_factor;
}

BitmapFont::BitmapFont( const int w, const int h, const std::string &typeface_path )
: Font( w, h )
{
Expand Down