202 lines
3.4 KiB
C++
202 lines
3.4 KiB
C++
#include "Sprite.hpp"
|
|
#include "Game.hpp"
|
|
|
|
Sprite::Sprite(Node* parent) : Node(parent, true)
|
|
{
|
|
frame_animation.play();
|
|
}
|
|
|
|
Sprite::Sprite(Node* parent, std::string path) : Sprite(parent)
|
|
{
|
|
associate(path);
|
|
}
|
|
|
|
void Sprite::set_frame_length(float length)
|
|
{
|
|
frame_animation.set_frame_length(length);
|
|
}
|
|
|
|
void Sprite::associate(std::string path)
|
|
{
|
|
if (fs::is_regular_file(path))
|
|
{
|
|
frame_paths.push_back(fs::path(path));
|
|
}
|
|
else if (fs::is_directory(path))
|
|
{
|
|
fs::directory_iterator directory(path);
|
|
std::vector<fs::path> paths;
|
|
std::copy(directory, fs::directory_iterator(), std::back_inserter(paths));
|
|
std::sort(paths.begin(), paths.end());
|
|
for (const fs::path &name : paths)
|
|
{
|
|
std::cout << "associating " << name << std::endl;
|
|
frame_paths.push_back(name);
|
|
}
|
|
}
|
|
else
|
|
{
|
|
std::ostringstream message;
|
|
message << "invalid path " << path;
|
|
get_root()->print_error(message.str());
|
|
}
|
|
}
|
|
|
|
void Sprite::load()
|
|
{
|
|
for (const fs::path &path : frame_paths)
|
|
{
|
|
load_file(path);
|
|
}
|
|
}
|
|
|
|
void Sprite::load_file(fs::path path)
|
|
{
|
|
Game *game = get_root();
|
|
SDL_Texture *texture = IMG_LoadTexture(game->renderer, path.string().c_str());
|
|
if (not texture)
|
|
{
|
|
game->print_sdl_error("Could not load image");
|
|
}
|
|
else
|
|
{
|
|
add_frame(texture);
|
|
}
|
|
}
|
|
|
|
void Sprite::add_frame(SDL_Texture* texture)
|
|
{
|
|
int w, h;
|
|
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
|
|
if (box.get_w() < w)
|
|
{
|
|
box.set_w(w);
|
|
}
|
|
if (box.get_h() < h)
|
|
{
|
|
box.set_h(h);
|
|
}
|
|
frames.push_back(texture);
|
|
}
|
|
|
|
void Sprite::unload()
|
|
{
|
|
while (not frames.empty())
|
|
{
|
|
SDL_DestroyTexture(frames.back());
|
|
frames.pop_back();
|
|
}
|
|
box.zero();
|
|
}
|
|
|
|
void Sprite::advance_frame()
|
|
{
|
|
if (++frame_ii >= frames.size())
|
|
{
|
|
frame_ii = 0;
|
|
}
|
|
}
|
|
|
|
void Sprite::hide()
|
|
{
|
|
is_hidden = true;
|
|
}
|
|
|
|
void Sprite::unhide()
|
|
{
|
|
is_hidden = false;
|
|
}
|
|
|
|
void Sprite::toggle_hidden()
|
|
{
|
|
is_hidden = !is_hidden;
|
|
}
|
|
|
|
void Sprite::set_step(glm::vec2 s)
|
|
{
|
|
step.x = s.x;
|
|
step.y = s.y;
|
|
}
|
|
|
|
float Sprite::get_w()
|
|
{
|
|
return box.get_w();
|
|
}
|
|
|
|
float Sprite::get_h()
|
|
{
|
|
return box.get_h();
|
|
}
|
|
|
|
glm::vec2 Sprite::get_size()
|
|
{
|
|
return box.get_size();
|
|
}
|
|
|
|
float Sprite::get_top()
|
|
{
|
|
return box.get_top();
|
|
}
|
|
|
|
float Sprite::get_right()
|
|
{
|
|
return box.get_right();
|
|
}
|
|
|
|
float Sprite::get_bottom()
|
|
{
|
|
return box.get_bottom();
|
|
}
|
|
|
|
float Sprite::get_left()
|
|
{
|
|
return box.get_left();
|
|
}
|
|
|
|
glm::vec2 Sprite::get_nw()
|
|
{
|
|
return box.get_nw();
|
|
}
|
|
|
|
glm::vec2 Sprite::get_north()
|
|
{
|
|
return box.get_north();
|
|
}
|
|
|
|
glm::vec2 Sprite::get_west()
|
|
{
|
|
return box.get_west();
|
|
}
|
|
|
|
void Sprite::set_nw(glm::vec2 nw)
|
|
{
|
|
box.set_nw(nw);
|
|
}
|
|
|
|
void Sprite::move(glm::vec2 delta, bool weighted)
|
|
{
|
|
Game *game = get_root();
|
|
if (weighted)
|
|
{
|
|
delta = game->weight(delta);
|
|
}
|
|
box.move(delta);
|
|
}
|
|
|
|
void Sprite::update()
|
|
{
|
|
if (active)
|
|
{
|
|
move(step);
|
|
frame_animation.update();
|
|
blink_animation.update();
|
|
if (frames.size() && !is_hidden)
|
|
{
|
|
SDL_Texture* texture = frames[frame_ii];
|
|
SDL_Renderer* renderer = get_root()->renderer;
|
|
SDL_SetRenderTarget(renderer, NULL);
|
|
SDL_RenderCopyF(renderer, texture, NULL, box.get_rect());
|
|
}
|
|
}
|
|
}
|