diff --git a/src/gamelib/CMakeLists.txt b/src/gamelib/CMakeLists.txt index 8a6ace3..2433b8e 100644 --- a/src/gamelib/CMakeLists.txt +++ b/src/gamelib/CMakeLists.txt @@ -19,6 +19,7 @@ add_library(${PROJECT_NAME} worlditems.cpp moveable.cpp grid_raytrace.cpp + drawable.cpp ) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 14) set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) diff --git a/src/gamelib/character.cpp b/src/gamelib/character.cpp index c72b8fc..de29eff 100644 --- a/src/gamelib/character.cpp +++ b/src/gamelib/character.cpp @@ -30,7 +30,6 @@ namespace curry { Character::Character (cloonel::InputBag* parInputBag, const WorldSizeNotifiable::DeferredRegister& parDeferredRegister) : WorldSizeNotifiable(parDeferredRegister), m_position(vec2f(0.0f), vec2f(0.0f), vec2f(0.0f)), - m_width_height(0.0f), m_input_bag(parInputBag), m_world(parDeferredRegister.world()), m_speed(1.0f) @@ -39,19 +38,6 @@ namespace curry { assert(m_world); } - void Character::load (const char* parTexturePath, cloonel::SDLMain& parSDLMain) { - const auto old_wh = m_width_height; - m_texture.load(parTexturePath, parSDLMain); - m_width_height = vector_cast(m_texture.width_height()); - const auto& new_wh = m_width_height; - if (m_position.get_min() != m_position.get_max()) - m_position.change_minmax(m_position.get_min(), m_position.get_max() + old_wh - new_wh); - } - - void Character::unload() { - m_texture.unload(); - } - void Character::set_position (const vec2f& parPos) { m_position = parPos; } @@ -60,14 +46,6 @@ namespace curry { return m_position.get(); } - const vec2f& Character::width_height() const { - return m_width_height; - } - - Texture& Character::texture() { - return m_texture; - } - Rect Character::destination_rect (const vec2f& parWorldPos) const { return Rect( m_position.get() - parWorldPos, @@ -111,7 +89,7 @@ namespace curry { return true; //wtp.property->walkable; }; - for_each_voxel_under_segment(old_pos, this->position(), m_texture.width_height(), *m_world, is_walkable); + for_each_voxel_under_segment(old_pos, this->position(), width_height(), *m_world, is_walkable); //for (auto tile_vec : crossed_tiles(old_pos, this->position(), m_world->tile_size())) { // const TileIndex* const tile = m_world->tile(tile_vec); // for (uint16_t z = 0; z < m_world->layer_count(); ++z) { @@ -131,4 +109,9 @@ namespace curry { assert(parSpeed > 0.0f); m_speed = parSpeed; } + + void Character::on_texture_size_changed (const vec2f& parOldSz, const vec2f& parNewSz) { + if (m_position.get_min() != m_position.get_max()) + m_position.change_minmax(m_position.get_min(), m_position.get_max() + parOldSz - parNewSz); + } } //namespace curry diff --git a/src/gamelib/character.hpp b/src/gamelib/character.hpp index 008410a..cb91aa8 100644 --- a/src/gamelib/character.hpp +++ b/src/gamelib/character.hpp @@ -19,12 +19,12 @@ #pragma once -#include "texture.hpp" #include "vector.hpp" #include "rect.hpp" #include "constrained_position.hpp" #include "worldsizenotifiable.hpp" #include "moveable.hpp" +#include "drawable.hpp" namespace cloonel { class SDLMain; @@ -34,17 +34,13 @@ namespace cloonel { namespace curry { class WorldGrid; - class Character : public WorldSizeNotifiable, public Moveable { + class Character : public Drawable, public WorldSizeNotifiable, public Moveable { public: Character (cloonel::InputBag* parInputBag, const WorldSizeNotifiable::DeferredRegister& parDeferredRegister); virtual ~Character() noexcept = default; - void load (const char* parTexturePath, cloonel::SDLMain& parSDLMain); - void unload(); const vec2f& position() const; void set_position (const vec2f& parPos); - const vec2f& width_height() const; - Texture& texture(); Rect destination_rect (const vec2f& parWorldPos) const; Rect source_rect() const; virtual void world_size_changed (const vec2us& parSize, const vec2i& parPixelSize) override; @@ -52,9 +48,9 @@ namespace curry { void set_speed (float parSpeed); private: + virtual void on_texture_size_changed (const vec2f& parOldSz, const vec2f& parNewSz); + ConstrainedPosition m_position; - Texture m_texture; - vec2f m_width_height; cloonel::InputBag* m_input_bag; WorldGrid* m_world; float m_speed; diff --git a/src/gamelib/drawable.cpp b/src/gamelib/drawable.cpp new file mode 100644 index 0000000..b3a60af --- /dev/null +++ b/src/gamelib/drawable.cpp @@ -0,0 +1,51 @@ +/* + 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 . +*/ + +#include "drawable.hpp" + +namespace curry { + Drawable::Drawable() : + m_width_height(0.0f) + { + } + + void Drawable::load (const char* parTexturePath, cloonel::SDLMain& parSDLMain) { + const auto old_wh = m_width_height; + m_texture.load(parTexturePath, parSDLMain); + m_width_height = vector_cast(m_texture.width_height()); + const auto& new_wh = m_width_height; + if (new_wh != old_wh) + this->on_texture_size_changed(old_wh, new_wh); + } + + void Drawable::unload() { + m_texture.unload(); + } + + const vec2f& Drawable::width_height() const { + return m_width_height; + } + + Texture& Drawable::texture() { + return m_texture; + } + + void Drawable::on_texture_size_changed (const vec2f&, const vec2f&) { + } +} //namespace curry diff --git a/src/gamelib/drawable.hpp b/src/gamelib/drawable.hpp new file mode 100644 index 0000000..c0d7735 --- /dev/null +++ b/src/gamelib/drawable.hpp @@ -0,0 +1,42 @@ +/* + 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 . +*/ + +#pragma once + +#include "texture.hpp" +#include "vector.hpp" + +namespace curry { + class Drawable { + public: + Drawable(); + virtual ~Drawable() noexcept = default; + + void load (const char* parTexturePath, cloonel::SDLMain& parSDLMain); + void unload(); + const vec2f& width_height() const; + Texture& texture(); + + private: + virtual void on_texture_size_changed (const vec2f& parOldSz, const vec2f& parNewSz); + + Texture m_texture; + vec2f m_width_height; + }; +} //namespace curry