Move texture-related code out from Character class.

This commit is contained in:
King_DuckZ 2017-03-11 12:22:40 +00:00
parent c320abbed2
commit 15e874f0fe
5 changed files with 104 additions and 31 deletions

View file

@ -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)

View file

@ -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<vec2f>(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<float> Character::destination_rect (const vec2f& parWorldPos) const {
return Rect<float>(
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

View file

@ -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<float> destination_rect (const vec2f& parWorldPos) const;
Rect<float> 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<vec2f> m_position;
Texture m_texture;
vec2f m_width_height;
cloonel::InputBag* m_input_bag;
WorldGrid* m_world;
float m_speed;

51
src/gamelib/drawable.cpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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<vec2f>(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

42
src/gamelib/drawable.hpp Normal file
View file

@ -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 <http://www.gnu.org/licenses/>.
*/
#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