Skip to content

Commit

Permalink
DrawingContext now accounts for floating-point transform scales
Browse files Browse the repository at this point in the history
Previously, the `DrawingContext` size getters would always return an integer value, which didn't account for possible floating-point scale values of the current transform.

Those functions now return floating-point values, which fixes some graphical bugs.
  • Loading branch information
Vankata453 committed Aug 26, 2023
1 parent 5f426b5 commit 88c1b72
Show file tree
Hide file tree
Showing 34 changed files with 175 additions and 176 deletions.
8 changes: 5 additions & 3 deletions src/control/mobile_controller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -84,10 +84,12 @@ MobileController::draw(DrawingContext& context)
if (!g_config->mobile_controls)
return;

if (m_screen_width != context.get_width() || m_screen_height != context.get_height() || m_mobile_controls_scale != g_config->m_mobile_controls_scale)
if (m_screen_width != static_cast<int>(context.get_width()) ||
m_screen_height != static_cast<int>(context.get_height()) ||
m_mobile_controls_scale != g_config->m_mobile_controls_scale)
{
m_screen_width = context.get_width();
m_screen_height = context.get_height();
m_screen_width = static_cast<int>(context.get_width());
m_screen_height = static_cast<int>(context.get_height());
float width = static_cast<float>(m_screen_width);
float height = static_cast<float>(m_screen_height);
m_mobile_controls_scale = g_config->m_mobile_controls_scale;
Expand Down
4 changes: 2 additions & 2 deletions src/editor/overlay_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1353,8 +1353,8 @@ EditorOverlayWidget::draw_tile_grid(DrawingContext& context, int tile_size,
int tm_height = current_tm->get_height() * (32 / tile_size);
auto cam_translation = m_editor.get_sector()->get_camera().get_translation();
Rectf draw_rect = Rectf(cam_translation, cam_translation +
Vector(static_cast<float>(context.get_width() - 128),
static_cast<float>(context.get_height() - 32)));
Vector(context.get_width() - 128.f,
context.get_height() - 32.f));
Vector start = sp_to_tp( Vector(draw_rect.get_left(), draw_rect.get_top()), tile_size );
Vector end = sp_to_tp( Vector(draw_rect.get_right(), draw_rect.get_bottom()), tile_size );
start.x = std::max(0.0f, start.x);
Expand Down
8 changes: 4 additions & 4 deletions src/editor/toolbox_widget.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -76,8 +76,8 @@ EditorToolboxWidget::draw(DrawingContext& context)
{
//SCREEN_WIDTH SCREEN_HEIGHT
context.color().draw_filled_rect(Rectf(Vector(static_cast<float>(m_Xpos), 0),
Vector(static_cast<float>(context.get_width()),
static_cast<float>(context.get_height()))),
Vector(context.get_width(),
context.get_height())),
g_config->editorcolor,
0.0f, LAYER_GUI-10);
if (m_dragging) {
Expand All @@ -97,10 +97,10 @@ EditorToolboxWidget::draw(DrawingContext& context)
}

context.color().draw_text(Resources::normal_font, _("Tiles"),
Vector(static_cast<float>(context.get_width()), 5),
Vector(context.get_width(), 5),
ALIGN_RIGHT, LAYER_GUI, ColorScheme::Menu::default_color);
context.color().draw_text(Resources::normal_font, _("Objects"),
Vector(static_cast<float>(context.get_width()), 37),
Vector(context.get_width(), 37),
ALIGN_RIGHT, LAYER_GUI, ColorScheme::Menu::default_color);

m_rubber->draw(context);
Expand Down
8 changes: 4 additions & 4 deletions src/gui/dialog.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -189,11 +189,11 @@ void
Dialog::draw(DrawingContext& context)
{
Rectf bg_rect(Vector(static_cast<float>(m_passive ?
(static_cast<float>(context.get_width()) - m_text_size.width - 20.0f) :
static_cast<float>(context.get_width()) / 2.0f - m_text_size.width / 2.0f),
(context.get_width() - m_text_size.width - 20.0f) :
context.get_width() / 2.0f - m_text_size.width / 2.0f),
static_cast<float>(m_passive ?
(static_cast<float>(context.get_height()) - m_text_size.height - 65.0f) :
(static_cast<float>(context.get_height()) / 2.0f - m_text_size.height / 2.0f))),
(context.get_height() - m_text_size.height - 65.0f) :
(context.get_height() / 2.0f - m_text_size.height / 2.0f))),
m_size);

// Draw background rect.
Expand Down
2 changes: 1 addition & 1 deletion src/gui/notification.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ Notification::draw(DrawingContext& context)
return;
}

m_pos = Vector(static_cast<float>(context.get_width()) - std::max(m_text_size.width, m_mini_text_size.width) - 90.0f,
m_pos = Vector(context.get_width() - std::max(m_text_size.width, m_mini_text_size.width) - 90.0f,
static_cast<float>(context.get_height() / 12) - m_text_size.height - m_mini_text_size.height + 10.0f);
Rectf bg_rect(m_pos, m_size);

Expand Down
35 changes: 17 additions & 18 deletions src/interface/control_scrollbar.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ ControlScrollbar::ControlScrollbar() :
m_covered_region(),
m_progress(),
m_rect(),
m_scaled_rect(),
//is_horizontal(),
last_mouse_pos()
//zoom_factor()
Expand All @@ -43,7 +42,7 @@ ControlScrollbar::ControlScrollbar() :
void
ControlScrollbar::draw(DrawingContext& context)
{
m_rect = Rect(0, 0, 10, context.get_height());
m_rect = Rectf(0, 0, 10, context.get_height());

context.color().draw_filled_rect(m_rect, Color(0.5f, 0.5f, 0.5f, 1.f), 8, LAYER_GUI);
context.color().draw_filled_rect(get_bar_rect(),
Expand Down Expand Up @@ -86,7 +85,7 @@ ControlScrollbar::on_mouse_button_down(const SDL_MouseButtonEvent& button)
{
if (button.button == SDL_BUTTON_LEFT) {
Vector mouse_pos = VideoSystem::current()->get_viewport().to_logical(button.x, button.y);
if (get_bar_rect().contains(int(mouse_pos.x), int(mouse_pos.y))) {
if (get_bar_rect().contains(mouse_pos)) {
m_scrolling = true;
return true;
} else {
Expand All @@ -112,7 +111,7 @@ ControlScrollbar::on_mouse_motion(const SDL_MouseMotionEvent& motion)
}
}*/

m_hovering = get_bar_rect().contains(int(mouse_pos.x), int(mouse_pos.y));
m_hovering = get_bar_rect().contains(mouse_pos);

int new_progress = m_progress + int((mouse_pos.y - last_mouse_pos) * VideoSystem::current()->get_viewport().get_scale().y * float(m_total_region) / float(m_covered_region));
last_mouse_pos = mouse_pos.y;
Expand All @@ -129,22 +128,22 @@ ControlScrollbar::on_mouse_motion(const SDL_MouseMotionEvent& motion)
}
}

Rect
Rectf
ControlScrollbar::get_bar_rect()
{
return Rect(m_rect.left,
m_rect.top + int(float(m_progress)
* float(m_covered_region)
/ float(m_total_region)
),
m_rect.right,
m_rect.top + int(float(m_progress)
* float(m_covered_region)
/ float(m_total_region))
+ int(float(m_rect.get_height())
* float(m_covered_region)
/ float(m_total_region)
)
return Rectf(m_rect.get_left(),
m_rect.get_top() + int(float(m_progress)
* float(m_covered_region)
/ float(m_total_region)
),
m_rect.get_right(),
m_rect.get_top() + int(float(m_progress)
* float(m_covered_region)
/ float(m_total_region))
+ int(m_rect.get_height()
* float(m_covered_region)
/ float(m_total_region)
)
);
}

Expand Down
10 changes: 4 additions & 6 deletions src/interface/control_scrollbar.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
#define HEADER_SUPERTUX_INTERFACE_CONTROL_SCROLLBAR_HPP

#include "editor/widget.hpp"
#include "math/rect.hpp"

#include "math/rectf.hpp"
#include "math/vector.hpp"

class DrawingContext;
Expand Down Expand Up @@ -54,16 +55,13 @@ class ControlScrollbar final : public Widget
int m_progress;

/** The logical position and size of the widget */
Rect m_rect;

/** The position and size of the widget, to scale */
Rect m_scaled_rect;
Rectf m_rect;

/** `true` of the scroller is horizontal; `false` if it is vertical */
//bool is_horizontal;

private:
Rect get_bar_rect();
Rectf get_bar_rect();

float last_mouse_pos;
//float zoom_factor;
Expand Down
16 changes: 8 additions & 8 deletions src/object/background.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -279,8 +279,8 @@ void
Background::draw_image(DrawingContext& context, const Vector& pos_)
{
const Sizef level(d_gameobject_manager->get_width(), d_gameobject_manager->get_height());
const Sizef screen(static_cast<float>(context.get_width()),
static_cast<float>(context.get_height()));
const Sizef screen(context.get_width(),
context.get_height());
const Sizef parallax_image_size((1.0f - m_parallax_speed.x) * screen.width + level.width * m_parallax_speed.x,
(1.0f - m_parallax_speed.y) * screen.height + level.height * m_parallax_speed.y);

Expand All @@ -301,10 +301,10 @@ Background::draw_image(DrawingContext& context, const Vector& pos_)

if (m_fill)
{
Rectf dstrect(Vector(pos_.x - static_cast<float>(context.get_width()) / 2.0f,
pos_.y - static_cast<float>(context.get_height()) / 2.0f),
Sizef(static_cast<float>(context.get_width()),
static_cast<float>(context.get_height())));
Rectf dstrect(Vector(pos_.x - context.get_width() / 2.0f,
pos_.y - context.get_height() / 2.0f),
Sizef(context.get_width(),
context.get_height()));
canvas.draw_surface_scaled(m_image, dstrect, m_layer);
}
else
Expand Down Expand Up @@ -384,8 +384,8 @@ Background::draw(DrawingContext& context)

Sizef level_size(d_gameobject_manager->get_width(),
d_gameobject_manager->get_height());
Sizef screen(static_cast<float>(context.get_width()),
static_cast<float>(context.get_height()));
Sizef screen(context.get_width(),
context.get_height());
Sizef translation_range = level_size - screen;
Vector center_offset(context.get_translation().x - translation_range.width / 2.0f,
context.get_translation().y - translation_range.height / 2.0f);
Expand Down
Loading

0 comments on commit 88c1b72

Please sign in to comment.