Project::OSiRiON - Git repositories
Project::OSiRiON
News . About . Screenshots . Downloads . Forum . Wiki . Tracker . Git
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorStijn Buys <ingar@osirion.org>2013-12-17 21:28:15 +0000
committerStijn Buys <ingar@osirion.org>2013-12-17 21:28:15 +0000
commitc7f28c2c0c7d23712552f0cd6ea0cf462068e081 (patch)
treec5e1b21d7362ae923283bc766115725fb97b6759 /src/ui/paint.cc
parent765d03abb9f031a9d608edaeb0f0f1bd6eded591 (diff)
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.
Diffstat (limited to 'src/ui/paint.cc')
-rw-r--r--src/ui/paint.cc48
1 files changed, 37 insertions, 11 deletions
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 <cassert>
+
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();