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 Mar 13, 2018
1 parent 7df3a1f commit 602e177
Show file tree
Hide file tree
Showing 4 changed files with 9 additions and 27 deletions.
14 changes: 6 additions & 8 deletions src/display.cpp
Expand Up @@ -145,7 +145,7 @@ display::display(const display_context * dc, std::weak_ptr<wb::manager> wb, repo
, xpos_(0)
, ypos_(0)
, view_locked_(false)
, theme_(theme_cfg, screen_.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_ ? &dc_->map() : nullptr), theme_.border().tile_image, theme_.border().show_border))
Expand Down Expand Up @@ -241,9 +241,7 @@ display::~display()
}

void display::set_theme(config theme_cfg) {
theme_ = theme(theme_cfg, screen_.screen_area());
builder_->set_draw_border(theme_.border().show_border);
menu_buttons_.clear();
theme_ = theme(theme_cfg, video_.screen_area()); menu_buttons_.clear();
action_buttons_.clear();
create_buttons();
}
Expand Down Expand Up @@ -821,7 +819,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_.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 @@ -833,7 +831,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_.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 @@ -1968,7 +1966,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_.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 @@ -2286,7 +2284,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
17 changes: 0 additions & 17 deletions src/display.hpp
Expand Up @@ -275,18 +275,6 @@ class display : public video2::draw_layering
* Between mapx and x is the sidebar region.
*/

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

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

const SDL_Rect& minimap_area() const
{
return theme_.mini_map_location(video_.screen_area());
Expand All @@ -302,11 +290,6 @@ class display : public video2::draw_layering
return theme_.unit_image_location(video_.screen_area());
}

SDL_Rect screen_area() const
{
return {0, 0, w(), h()};
}

/** Returns the maximum area used for the map regardless to resolution and view size */
const SDL_Rect& max_map_area() const;

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/show_dialog.cpp
Expand Up @@ -321,7 +321,7 @@ void dialog_frame::draw_background()

SDL_Rect dialog_frame::draw_title(CVideo* video)
{
SDL_Rect rect = CVideo::get_singleton().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

0 comments on commit 602e177

Please sign in to comment.