MyCurry/src/gamelib/drawable.cpp

92 lines
2.5 KiB
C++

/*
Copyright 2016, 2017 Michele "King_DuckZ" Santullo
This file is part of MyCurry.
MyCurry is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
MyCurry is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with MyCurry. If not, see <http://www.gnu.org/licenses/>.
*/
#include "drawable.hpp"
#include "texture.hpp"
#include "safe_stack_object.hpp"
#include <utility>
#include <algorithm>
#include <ciso646>
#include <cassert>
namespace curry {
namespace {
vec2f max_size (const vec2f& parA, const vec2f& parB) {
return vec2f(
std::max(parA.x(), parB.x()),
std::max(parA.y(), parB.y())
);
}
} //unnamed namespace
Drawable::Drawable() :
m_width_height(0.0f)
{
}
Drawable::~Drawable() noexcept = default;
size_t Drawable::add_texture (const char* parTexturePath, cloonel::SDLMain& parSDLMain) {
SafeStackObject<Texture> new_texture;
new_texture->load(parTexturePath, parSDLMain);
const auto retval = store_new_texture(std::move(new_texture));
return retval;
}
size_t Drawable::add_texture (const vec2f& parSize, cloonel::SDLMain& parSDLMain) {
assert(parSize > 0.0f);
SafeStackObject<Texture> new_texture;
new_texture->load_empty(parSDLMain, static_cast<vec2us>(parSize));
const auto retval = store_new_texture(std::move(new_texture));
return retval;
}
void Drawable::unload_textures() noexcept {
for (auto& tex : m_textures) {
tex->unload();
}
m_textures.clear();
}
const vec2f& Drawable::width_height() const {
return m_width_height;
}
Kakoune::SafePtr<Texture>& Drawable::texture() {
assert(not m_textures.empty());
auto& retval = m_textures[0];
return retval;
}
void Drawable::on_texture_size_changed (const vec2f&, const vec2f&) {
}
size_t Drawable::store_new_texture (SafeStackObject<Texture>&& parNewTexture) {
const auto old_wh = m_width_height;
m_width_height = max_size(vector_cast<vec2f>(parNewTexture->width_height()), old_wh);
const auto& new_wh = m_width_height;
if (new_wh != old_wh)
this->on_texture_size_changed(old_wh, new_wh);
m_textures.push_back(std::move(parNewTexture));
assert(m_textures.size() > 0);
return m_textures.size() - 1;
}
} //namespace curry