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

Limit crosshair scaling to integer multiples to avoid distortion #14501

Merged
merged 1 commit into from
Mar 30, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
31 changes: 22 additions & 9 deletions src/client/hud.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ with this program; if not, write to the Free Software Foundation, Inc.,
#define OBJECT_CROSSHAIR_LINE_SIZE 8
#define CROSSHAIR_LINE_SIZE 10

static void setting_changed_callback(const std::string &name, void *data)
{
static_cast<Hud*>(data)->readScalingSetting();
}

Hud::Hud(Client *client, LocalPlayer *player,
Inventory *inventory)
{
Expand All @@ -52,12 +57,8 @@ Hud::Hud(Client *client, LocalPlayer *player,
this->player = player;
this->inventory = inventory;

m_hud_scaling = g_settings->getFloat("hud_scaling", 0.5f, 20.0f);
m_scale_factor = m_hud_scaling * RenderingEngine::getDisplayDensity();
m_hotbar_imagesize = std::floor(HOTBAR_IMAGE_SIZE *
RenderingEngine::getDisplayDensity() + 0.5f);
m_hotbar_imagesize *= m_hud_scaling;
m_padding = m_hotbar_imagesize / 12;
readScalingSetting();
g_settings->registerChangedCallback("hud_scaling", setting_changed_callback, this);

for (auto &hbar_color : hbar_colors)
hbar_color = video::SColor(255, 255, 255, 255);
Expand Down Expand Up @@ -138,8 +139,20 @@ Hud::Hud(Client *client, LocalPlayer *player,
m_rotation_mesh_buffer.setHardwareMappingHint(scene::EHM_STATIC);
}

void Hud::readScalingSetting()
{
m_hud_scaling = g_settings->getFloat("hud_scaling", 0.5f, 20.0f);
m_scale_factor = m_hud_scaling * RenderingEngine::getDisplayDensity();
m_hotbar_imagesize = std::floor(HOTBAR_IMAGE_SIZE *
RenderingEngine::getDisplayDensity() + 0.5f);
m_hotbar_imagesize *= m_hud_scaling;
m_padding = m_hotbar_imagesize / 12;
}

Hud::~Hud()
{
g_settings->deregisterChangedCallback("hud_scaling", setting_changed_callback, this);

if (m_selection_mesh)
m_selection_mesh->drop();
}
Expand Down Expand Up @@ -786,9 +799,9 @@ void Hud::drawCrosshair()
{
auto draw_image_crosshair = [this] (video::ITexture *tex) {
core::dimension2di orig_size(tex->getOriginalSize());
core::dimension2di scaled_size(
core::round32(orig_size.Width * m_scale_factor),
core::round32(orig_size.Height * m_scale_factor));
// Integer scaling to avoid artifacts, floor instead of round since too
// small looks better than too large in this case.
core::dimension2di scaled_size = orig_size * std::floor(m_scale_factor);

core::rect<s32> src_rect(orig_size);
core::position2d pos(m_displaycenter.X - scaled_size.Width / 2,
Expand Down
1 change: 1 addition & 0 deletions src/client/hud.h
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,7 @@ class Hud

Hud(Client *client, LocalPlayer *player,
Inventory *inventory);
void readScalingSetting();
~Hud();

enum BlockBoundsMode toggleBlockBounds();
Expand Down