From c7f28c2c0c7d23712552f0cd6ea0cf462068e081 Mon Sep 17 00:00:00 2001 From: Stijn Buys Date: Tue, 17 Dec 2013 21:28:15 +0000 Subject: Added a preserve_attribute option to the ui::Paint::print_bitmap() functions and the ui::Bitmap class, preserve aspect ratio on the loader screen image and the main menu background. --- src/ui/paint.cc | 48 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 11 deletions(-) (limited to 'src/ui/paint.cc') diff --git a/src/ui/paint.cc b/src/ui/paint.cc index 6dd85b8..cea363c 100644 --- a/src/ui/paint.cc +++ b/src/ui/paint.cc @@ -12,6 +12,8 @@ #include "render/textures.h" #include "ui/paint.h" +#include + namespace ui { void Paint::set_color(float r, float g, float b, float a) @@ -72,8 +74,7 @@ void Paint::draw_rectangle_gradient(const math::Vector2f &global_location, const gl::end(); } -// draw a bitmap -void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture) +void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture, const float preserve_aspect) { // find the material model::Material *material = model::Material::find(texture); @@ -85,6 +86,19 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto material->set_flags(model::Material::FlagBright); } + math::Vector2f bitmap_location; + math::Vector2f bitmap_size(size); + + if (preserve_aspect) { + const float s = math::min(size.width() / material->size().width(), size.height() / material->size().height()); + bitmap_size.assign(material->size().width() * s, material->size().height() * s); + bitmap_location.assign((size.width() - bitmap_size.width()) * 0.5f, (size.height() - bitmap_size.height()) * 0.5f); + bitmap_location += global_location; + } else { + bitmap_location.assign(global_location); + } + + render::State::set_color(math::Color()); render::State::set_color_second(math::Color()); render::State::use_material(material); @@ -92,16 +106,16 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto gl::begin(gl::Quads); glTexCoord2f(0.0f, 0.0f); - gl::vertex(global_location.x(), global_location.y()); + gl::vertex(bitmap_location.x(), bitmap_location.y()); glTexCoord2f(1.0f, 0.0f); - gl::vertex(global_location.x() + size.width(), global_location.y()); + gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y()); glTexCoord2f(1.0f, 1.0f); - gl::vertex(global_location.x() + size.width(), global_location.y() + size.height()); + gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y() + bitmap_size.height()); glTexCoord2f(0.0f, 1.0f); - gl::vertex(global_location.x(), global_location.y() + size.height()); + gl::vertex(bitmap_location.x(), bitmap_location.y() + bitmap_size.height()); gl::end(); @@ -109,7 +123,7 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto } // draw a bitmap and override material color -void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color, const std::string &texture) +void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color, const std::string &texture, const float preserve_aspect) { // find the material model::Material *material = model::Material::find(texture); @@ -121,6 +135,18 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto material->set_flags(model::Material::FlagBright); } + math::Vector2f bitmap_location; + math::Vector2f bitmap_size(size); + + if (preserve_aspect) { + const float s = math::min(size.width() / material->size().width(), size.height() / material->size().height()); + bitmap_size.assign(material->size().width() * s, material->size().height() * s); + bitmap_location.assign((size.width() - bitmap_size.width()) * 0.5f, (size.height() - bitmap_size.height()) * 0.5f); + bitmap_location += global_location; + } else { + bitmap_location.assign(global_location); + } + render::State::set_power(true); render::State::use_material(material); @@ -130,16 +156,16 @@ void Paint::draw_bitmap(const math::Vector2f &global_location, const math::Vecto gl::begin(gl::Quads); glTexCoord2f(0.0f, 0.0f); - gl::vertex(global_location.x(), global_location.y()); + gl::vertex(bitmap_location.x(), bitmap_location.y()); glTexCoord2f(1.0f, 0.0f); - gl::vertex(global_location.x() + size.width(), global_location.y()); + gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y()); glTexCoord2f(1.0f, 1.0f); - gl::vertex(global_location.x() + size.width(), global_location.y() + size.height()); + gl::vertex(bitmap_location.x() + bitmap_size.width(), bitmap_location.y() + bitmap_size.height()); glTexCoord2f(0.0f, 1.0f); - gl::vertex(global_location.x(), global_location.y() + size.height()); + gl::vertex(bitmap_location.x(), bitmap_location.y() + bitmap_size.height()); gl::end(); -- cgit v1.2.3