Skip to content

Commit

Permalink
Refactored handling of window/renderer size getters
Browse files Browse the repository at this point in the history
* Removed display::screen_area(), display::w(), and display::h().
* Moved the global screen_area() function into the CVideo class.
* Renamed CVideo::getx() and gety() to get_width() and get_height()
* Made those two functions return the result of screen_area() instead of the other way around.
* Added preliminary support for high-DPI rendering to screen_area()

Note on the last point: When I fixed bug #1772 (aa8f6c7 right now but will probably change with rebasing)
I noted that SDL_GetWindowSize and SDL_GetRendererOutputSize returned the same results for me (even with
Window's automatic scaling for non-high-DPI-enabled apps disabled) but that the SDL documentation stated the
former returned screen coordinates and the latter pixels. In that same commit I changed the dimension functions
to use SDL_GetWindowSize. I've now reversed that decision and gone back to using SDL_GetRendererOutputSize so
I can guarantee output in pixels. If use_pixels is false, the code will return coordinates in 96 DPI, so I need
to have pixel measurements for the calculations.

Again, though, I do not know if SDL_GetWindowSize returns a different value that pixel size (which it's said
to do) on macOS or iOS. I'll need to do some testing. It's possible on those platforms I won't need the 96 DPI
measurements, but it's also possible it will be needed on on platforms, since all of our code relies on pixel
measurements.
  • Loading branch information
Vultraz committed Jul 31, 2017
1 parent 609ed21 commit a91a30b
Show file tree
Hide file tree
Showing 16 changed files with 74 additions and 81 deletions.
8 changes: 4 additions & 4 deletions src/controller_base.cpp
Expand Up @@ -190,15 +190,15 @@ bool controller_base::handle_scroll(int mousex, int mousey, int mouse_flags, dou
dy -= scroll_speed;
}

if(mousey > get_display().h() - scroll_threshold) {
if(mousey > get_display().video().get_height() - scroll_threshold) {
dy += scroll_speed;
}

if(mousex < scroll_threshold) {
dx -= scroll_speed;
}

if(mousex > get_display().w() - scroll_threshold) {
if(mousex > get_display().video().get_width() - scroll_threshold) {
dx += scroll_speed;
}
}
Expand Down Expand Up @@ -258,15 +258,15 @@ void controller_base::play_slice(bool is_delay_enabled)

const theme::menu* const m = get_display().menu_pressed();
if(m != nullptr) {
const SDL_Rect& menu_loc = m->location(get_display().screen_area());
const SDL_Rect& menu_loc = m->location(get_display().video().screen_area());
show_menu(m->items(), menu_loc.x + 1, menu_loc.y + menu_loc.h + 1, false, get_display());

return;
}

const theme::action* const a = get_display().action_pressed();
if(a != nullptr) {
const SDL_Rect& action_loc = a->location(get_display().screen_area());
const SDL_Rect& action_loc = a->location(get_display().video().screen_area());
execute_action(a->items(), action_loc.x + 1, action_loc.y + action_loc.h + 1, false);

return;
Expand Down
16 changes: 8 additions & 8 deletions src/display.cpp
Expand Up @@ -149,7 +149,7 @@ display::display(const display_context* dc,
, xpos_(0)
, ypos_(0)
, view_locked_(false)
, theme_(theme_cfg, screen_area())
, theme_(theme_cfg, video.screen_area())
, zoom_index_(0)
, fake_unit_man_(new fake_unit_manager(*this))
, builder_(new terrain_builder(level, &dc_->map(), theme_.border().tile_image))
Expand Down Expand Up @@ -241,7 +241,7 @@ display::~display()
}

void display::set_theme(config theme_cfg) {
theme_ = theme(theme_cfg, screen_area());
theme_ = theme(theme_cfg, video_.screen_area());
menu_buttons_.clear();
action_buttons_.clear();
create_buttons();
Expand Down Expand Up @@ -824,7 +824,7 @@ void display::layout_buttons()
for(const auto& menu : theme_.menus()) {
std::shared_ptr<gui::button> b = find_menu_button(menu.get_id());
if(b) {
const SDL_Rect& loc = menu.location(screen_area());
const SDL_Rect& loc = menu.location(video_.screen_area());
b->set_location(loc);
b->set_measurements(0,0);
b->set_label(menu.title());
Expand All @@ -836,7 +836,7 @@ void display::layout_buttons()
for(const auto& action : theme_.actions()) {
std::shared_ptr<gui::button> b = find_action_button(action.get_id());
if(b) {
const SDL_Rect& loc = action.location(screen_area());
const SDL_Rect& loc = action.location(video_.screen_area());
b->set_location(loc);
b->set_measurements(0,0);
b->set_label(action.title());
Expand Down Expand Up @@ -1083,7 +1083,7 @@ static void draw_panel(CVideo &video, const theme::panel& panel, std::vector<std

texture tex(image::get_texture(panel.image()));

const SDL_Rect screen = screen_area();
const SDL_Rect screen = video.screen_area();
SDL_Rect& loc = panel.location(screen);

DBG_DP << "panel location: x=" << loc.x << ", y=" << loc.y
Expand Down Expand Up @@ -1113,7 +1113,7 @@ static void draw_label(CVideo& video, surface target, const theme::label& label)
text = color.str();
}
const std::string& icon = label.icon();
SDL_Rect& loc = label.location(screen_area());
SDL_Rect& loc = label.location(video.screen_area());

if(icon.empty() == false) {
surface surf(image::get_image(icon));
Expand Down Expand Up @@ -1914,7 +1914,7 @@ void display::refresh_report(const std::string& report_name, const config * new_
new_cfg = &generated_cfg;

SDL_Rect &rect = reportRects_[report_name];
const SDL_Rect &new_rect = item->location(screen_area());
const SDL_Rect &new_rect = item->location(video_.screen_area());
surface &surf = reportSurfaces_[report_name];
config &report = reports_[report_name];

Expand Down Expand Up @@ -2218,7 +2218,7 @@ void display::redraw_everything()

tooltips::clear_tooltips();

theme_.set_resolution(screen_area());
theme_.set_resolution(video_.screen_area());

if(!menu_buttons_.empty() || !action_buttons_.empty()) {
create_buttons();
Expand Down
26 changes: 4 additions & 22 deletions src/display.hpp
Expand Up @@ -296,41 +296,23 @@ class display : public filter_context, public video2::draw_layering
}

/**
* the dimensions of the display. x and y are width/height.
* mapx is the width of the portion of the display which shows the game area.
* Between mapx and x is the sidebar region.
*/

/** Screen width */
int w() const
{
return video_.getx();
}

/** Screen height */
int h() const
{
return video_.gety();
}

const SDL_Rect& minimap_area() const
{
return theme_.mini_map_location(screen_area());
return theme_.mini_map_location(video_.screen_area());
}

const SDL_Rect& palette_area() const
{
return theme_.palette_location(screen_area());
return theme_.palette_location(video_.screen_area());
}

const SDL_Rect& unit_image_area() const
{
return theme_.unit_image_location(screen_area());
}

SDL_Rect screen_area() const
{
return {0, 0, w(), h()};
return theme_.unit_image_location(video_.screen_area());
}

/** Returns the maximum area used for the map regardless to resolution and view size */
Expand All @@ -345,7 +327,7 @@ class display : public filter_context, public video2::draw_layering
*/
const SDL_Rect& map_outside_area() const
{
return map_screenshot_ ? max_map_area() : theme_.main_map_location(screen_area());
return map_screenshot_ ? max_map_area() : theme_.main_map_location(video_.screen_area());
}

/** Check if the bbox of the hex at x,y has pixels outside the area rectangle. */
Expand Down
4 changes: 2 additions & 2 deletions src/events.cpp
Expand Up @@ -646,8 +646,8 @@ void raise_resize_event()
event.window.type = SDL_WINDOWEVENT;
event.window.event = SDL_WINDOWEVENT_RESIZED;
event.window.windowID = 0; // We don't check this anyway... I think...
event.window.data1 = CVideo::get_singleton().getx();
event.window.data2 = CVideo::get_singleton().gety();
event.window.data1 = CVideo::get_singleton().get_width();
event.window.data2 = CVideo::get_singleton().get_height();

SDL_PushEvent(&event);
}
Expand Down
3 changes: 2 additions & 1 deletion src/floating_label.cpp
Expand Up @@ -20,6 +20,7 @@
#include "sdl/render_utils.hpp"
#include "sdl/surface.hpp"
#include "utils/general.hpp"
#include "video.hpp"

#include <boost/algorithm/string.hpp>

Expand Down Expand Up @@ -60,7 +61,7 @@ floating_label::floating_label(const std::string& text)
, border_(0)
, alpha_change_(0)
, current_alpha_(255)
, clip_rect_(screen_area())
, clip_rect_(CVideo::get_singleton().screen_area())
, align_(CENTER_ALIGN)
, scroll_(ANCHOR_LABEL_SCREEN)
, visible_(true)
Expand Down
2 changes: 1 addition & 1 deletion src/gui/dialogs/lobby/lobby.cpp
Expand Up @@ -1010,7 +1010,7 @@ void mp_lobby::show_preferences_button_callback(window& window)
*
* @todo This might no longer be needed when gui2 is done.
*/
const SDL_Rect rect = screen_area();
const SDL_Rect rect = window.video().screen_area();

gui2::settings::gamemap_width += rect.w - gui2::settings::screen_width;
gui2::settings::gamemap_height += rect.h - gui2::settings::screen_height;
Expand Down
3 changes: 2 additions & 1 deletion src/gui/widgets/window.cpp
Expand Up @@ -427,7 +427,8 @@ void window::update_screen_size()
{
// Only if we're the toplevel window we need to update the size, otherwise
// it's done in the resize event.
const SDL_Rect rect = screen_area();
const SDL_Rect rect = CVideo::get_singleton().screen_area();

settings::screen_width = rect.w;
settings::screen_height = rect.h;

Expand Down
11 changes: 5 additions & 6 deletions src/help/help.cpp
Expand Up @@ -162,11 +162,10 @@ void show_help(CVideo& video, const section &toplevel_sec,
const events::event_context dialog_events_context;
const gui::dialog_manager manager;

const int screen_w = video.getx();
const int screen_h = video.gety();
SDL_Rect screen_area = video.screen_area();

const int width = std::min<int>(font::relative_size(1200), screen_w - font::relative_size(20));
const int height = std::min<int>(font::relative_size(850), screen_h - font::relative_size(150));
const int width = std::min<int>(font::relative_size(1200), screen_area.w - font::relative_size(20));
const int height = std::min<int>(font::relative_size(850), screen_area.h - font::relative_size(150));
const int left_padding = font::relative_size(10);
const int right_padding = font::relative_size(10);
const int top_padding = font::relative_size(10);
Expand All @@ -175,8 +174,8 @@ void show_help(CVideo& video, const section &toplevel_sec,
// If not both locations were supplied, put the dialog in the middle
// of the screen.
if (yloc <= -1 || xloc <= -1) {
xloc = screen_w / 2 - width / 2;
yloc = screen_h / 2 - height / 2;
xloc = screen_area.w / 2 - width / 2;
yloc = screen_area.h / 2 - height / 2;
}
std::vector<gui::button*> buttons_ptr;
gui::button close_button_(video, _("Close"));
Expand Down
2 changes: 1 addition & 1 deletion src/help/help_topic_generators.cpp
Expand Up @@ -239,7 +239,7 @@ std::string unit_topic_generator::operator()() const {
const unit_type& female_type = type_.get_gender_unit_type(unit_race::FEMALE);
const unit_type& male_type = type_.get_gender_unit_type(unit_race::MALE);

const int screen_width = CVideo::get_singleton().getx();
const int screen_width = CVideo::get_singleton().get_width();

ss << "Level " << type_.level();
ss << "\n\n";
Expand Down
4 changes: 2 additions & 2 deletions src/show_dialog.cpp
Expand Up @@ -201,7 +201,7 @@ dialog_frame::dimension_measurements dialog_frame::layout(int x, int y, int w, i
h += dim_.title.h + dim_.button_row.h;
dim_.button_row.x += x + w;

SDL_Rect bounds = screen_area();
SDL_Rect bounds = video_.screen_area();
if(have_border_) {
bounds.x += left_->w;
bounds.y += top_->h;
Expand Down Expand Up @@ -321,7 +321,7 @@ void dialog_frame::draw_background()

SDL_Rect dialog_frame::draw_title(CVideo* video)
{
SDL_Rect rect = screen_area();
SDL_Rect rect = video->screen_area();
return font::draw_text(video, rect, font::SIZE_TITLE, font::TITLE_COLOR,
title_, dim_.title.x, dim_.title.y, false, TTF_STYLE_NORMAL);
}
Expand Down
2 changes: 1 addition & 1 deletion src/tooltips.cpp
Expand Up @@ -71,7 +71,7 @@ static void show_tooltip(const tooltip& tip)
clear_tooltip();

const color_t bgcolor {0,0,0,160};
SDL_Rect area = screen_area();
SDL_Rect area = video_->screen_area();

unsigned int border = 10;

Expand Down
44 changes: 23 additions & 21 deletions src/video.cpp
Expand Up @@ -108,17 +108,6 @@ bool CVideo::non_interactive() const
return fake_interactive ? false : (window == nullptr);
}

SDL_Rect screen_area()
{
sdl::window* w = CVideo::get_singleton().get_window();
if(!w) {
return sdl::empty_rect;
}

SDL_Point size = w->get_size();
return {0, 0, size.x, size.y};
}

void CVideo::video_event_handler::handle_window_event(const SDL_Event &event)
{
if(event.type == SDL_WINDOWEVENT) {
Expand Down Expand Up @@ -230,22 +219,35 @@ void CVideo::setMode(int x, int y, const MODE_EVENT mode)
}
}

int CVideo::getx() const
SDL_Rect CVideo::screen_area(bool as_pixels) const
{
if(!window) {
return frameBuffer->w;
return sdl::empty_rect;
}

return window->get_size().x;
// First, get the renderer size in pixels.
SDL_Point size = window->get_output_size();

// Then convert the dimensions into screen coordinates, if applicable.
if(!as_pixels) {
float scale_x, scale_y;
std::tie(scale_x, scale_y) = get_dpi_scale_factor();

size.x /= scale_x;
size.y /= scale_y;
}

return {0, 0, size.x, size.y};
}

int CVideo::gety() const
int CVideo::get_width(bool as_pixels) const
{
if(!window) {
return frameBuffer->h;
}
return screen_area(as_pixels).w;
}

return window->get_size().y;
int CVideo::get_height(bool as_pixels) const
{
return screen_area(as_pixels).h;
}

SDL_Renderer* CVideo::get_renderer()
Expand Down Expand Up @@ -456,7 +458,7 @@ int CVideo::set_help_string(const std::string& str)
int size = font::SIZE_LARGE;

while(size > 0) {
if(font::line_width(str, size) > getx()) {
if(font::line_width(str, size) > get_width()) {
size--;
} else {
break;
Expand All @@ -467,7 +469,7 @@ int CVideo::set_help_string(const std::string& str)

font::floating_label flabel(str);
flabel.set_font_size(size);
flabel.set_position(getx()/2, gety());
flabel.set_position(get_width()/2, get_height());
flabel.set_bg_color(color);
flabel.set_border_size(border);

Expand Down
18 changes: 13 additions & 5 deletions src/video.hpp
Expand Up @@ -28,8 +28,6 @@
class surface;
class texture;

SDL_Rect screen_area();

class CVideo {
public:
CVideo(const CVideo&) = delete;
Expand Down Expand Up @@ -77,9 +75,19 @@ class CVideo {
std::pair<int,int> current_resolution();
int current_refresh_rate() { return refresh_rate_; }

//functions to get the dimensions of the current video-mode
int getx() const;
int gety() const;
/**
* Returns the current window renderer area, either in pixels or screen coordinates.
*
* @param as_pixels Whether to return the area in pixels (default true) or
* DPI-independent (DIP) screen coordinates.
*/
SDL_Rect screen_area(bool as_pixels = true) const;

/** Returns the window renderer width in pixels or screen coordinates. */
int get_width(bool as_pixels = true) const;

/** Returns the window renderer height in pixels or in screen coordinates. */
int get_height(bool as_pixels = true) const;

std::pair<float, float> get_dpi_scale_factor() const;

Expand Down
2 changes: 1 addition & 1 deletion src/widgets/button.cpp
Expand Up @@ -228,7 +228,7 @@ void button::calculate_size()
}

if (type_ != TYPE_IMAGE){
textRect_ = font::draw_text(nullptr, screen_area(), font_size,
textRect_ = font::draw_text(nullptr, video().screen_area(), font_size,
font::BUTTON_COLOR, label_text_, 0, 0);
}

Expand Down

0 comments on commit a91a30b

Please sign in to comment.