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
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.
-rw-r--r--src/client/mainmenu.cc2
-rw-r--r--src/client/video.cc9
-rw-r--r--src/ui/bitmap.cc8
-rw-r--r--src/ui/bitmap.h12
-rw-r--r--src/ui/paint.cc48
-rw-r--r--src/ui/paint.h9
6 files changed, 66 insertions, 22 deletions
diff --git a/src/client/mainmenu.cc b/src/client/mainmenu.cc
index 7374224..a70cad0 100644
--- a/src/client/mainmenu.cc
+++ b/src/client/mainmenu.cc
@@ -352,7 +352,7 @@ void MainMenu::draw_background()
// we override draw_background instead of adding a ui::Bitmap child
// this simplifies child window managment
if (mainmenu_background.size()) {
- ui::Paint::draw_bitmap(global_location(), size(), math::Color(), mainmenu_background);
+ ui::Paint::draw_bitmap(global_location(), size(), mainmenu_background, true);
}
}
diff --git a/src/client/video.cc b/src/client/video.cc
index a75089f..0f0c984 100644
--- a/src/client/video.cc
+++ b/src/client/video.cc
@@ -312,9 +312,12 @@ void draw_loader()
gl::enable(GL_BLEND);
gl::color(1.0f, 1.0f, 1.0f, 1.0f);
- math::Vector2f pos;
- math::Vector2f size(render::State::width(), render::State::height());
- ui::Paint::draw_bitmap(pos, size, "bitmaps/loader");
+
+
+ const std::string loader_texture("bitmaps/loader");
+ const math::Vector2f size(render::State::width(), render::State::height());
+ const math::Vector2f pos;
+ ui::Paint::draw_bitmap(pos, size, loader_texture, true);
if (loader_message.size()) {
using render::Text;
diff --git a/src/ui/bitmap.cc b/src/ui/bitmap.cc
index 03d7166..5caf966 100644
--- a/src/ui/bitmap.cc
+++ b/src/ui/bitmap.cc
@@ -16,6 +16,7 @@ Bitmap::Bitmap(Widget *parent, const char *texture) : Widget(parent)
{
set_border(false);
set_background(true);
+ set_preserve_aspect(false);
set_label("bitmap");
set_texture(texture);
@@ -48,10 +49,15 @@ void Bitmap::set_color(const math::Color & color)
bitmap_color.assign(color);
}
+void Bitmap::set_preserve_aspect(const bool preserve_aspect)
+{
+ bitmap_preserve_aspect = preserve_aspect;
+}
+
void Bitmap::draw_background()
{
if (bitmap_texture.size()) {
- Paint::draw_bitmap(global_location(), size(),bitmap_color, bitmap_texture);
+ Paint::draw_bitmap(global_location(), size(), bitmap_color, bitmap_texture, bitmap_preserve_aspect);
}
}
diff --git a/src/ui/bitmap.h b/src/ui/bitmap.h
index 4838ea2..123f12c 100644
--- a/src/ui/bitmap.h
+++ b/src/ui/bitmap.h
@@ -18,18 +18,25 @@ public:
Bitmap(Widget *parent, const char *texture = 0);
~Bitmap();
- inline std::string const &texture() const {
+ inline const std::string & texture() const {
return bitmap_texture;
}
- inline math::Color const &color() const {
+ inline const math::Color & color() const {
return bitmap_color;
}
+
+ inline const bool preserve_aspect() const {
+ return bitmap_preserve_aspect;
+ }
void set_texture(const std::string & texture);
+
void set_texture(const char *texture);
void set_color(const math::Color &color);
+
+ void set_preserve_aspect(const bool preserve_aspect);
/// print bitmap description
virtual void print(const size_t indent) const;
@@ -41,6 +48,7 @@ protected:
private:
std::string bitmap_texture;
math::Color bitmap_color;
+ bool bitmap_preserve_aspect;
};
}
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();
diff --git a/src/ui/paint.h b/src/ui/paint.h
index b2829ea..76cd8bf 100644
--- a/src/ui/paint.h
+++ b/src/ui/paint.h
@@ -8,6 +8,7 @@
#define __INCLUDED_UI_PAINT_H__
#include "ui/widget.h"
+#include "model/material.h"
namespace ui {
@@ -53,17 +54,17 @@ public:
* @brief draw a color gradient rectangle
* */
static void draw_rectangle_gradient(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color_left, const math::Color & color_right);
-
+
/**
* @brief draw a rectangular bitmap
* @param texture the name of material to be used
- **/
- static void draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture);
+ **/
+ static void draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const std::string &texture, const float preserve_aspect = false);
/**
* @brief draw a rectangular bitmap and overrride material color
* */
- static void draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color, const std::string &texture);
+ static void draw_bitmap(const math::Vector2f &global_location, const math::Vector2f &size, const math::Color & color, const std::string &texture, const float preserve_aspect = false);
/**
* @brief draw a tiled bitmap