spacebox/src/Sprite.hpp

214 lines
6.6 KiB
C++

#pragma once
#include <map>
#include <string>
#include <vector>
#include <sstream>
#include <algorithm>
#include <utility>
#include <list>
#include <stdexcept>
#include "SDL.h"
#include "SDL_image.h"
#include "Node.hpp"
#include "Box.hpp"
#include "Animation.hpp"
#include "Color.hpp"
#include "Log.hpp"
class Game;
class Sprite : public Node
{
public:
class Frameset;
class Child;
std::vector<SDL_Texture*> frames;
std::vector<fs::path> frame_paths;
std::vector<Box> boxes = {{{0, 0}, {0, 0}}};
Animation frame_animation = Animation(&Sprite::advance_frame, this),
blink_animation = Animation(&Sprite::toggle_hidden, this, 500),
wipe_animation = Animation(&Sprite::advance_wipe_frame, this, 40),
toggle_hidden_animation = Animation(&Sprite::toggle_hidden, this);
glm::vec2 step = {0, 0}, child_relative_position;
float scale = 1.0f, bottom_save;
std::string scale_quality = "nearest";
std::uint8_t alpha_mod = 255;
SDL_Color color_mod = {255, 255, 255, 255};
std::map<std::string, Frameset> framesets;
std::string current_frameset_name;
glm::bvec2 wrap = {false, false};
int texture_access = SDL_TEXTUREACCESS_TARGET, wipe_index = 0, wipe_increment = -1;
Box wrap_frame, unscaled_subsection;
bool leave_memory_allocated = false, hidden = false, draw_children_on_frame = true, draw_floating = true;
std::vector<std::vector<Box>> wipe_blinds;
SDL_Rect subsection_int_rect, subsection_destination;
std::list<Child> children = {};
Sprite();
Sprite(Node*);
Sprite(Node*, std::string);
virtual void reset();
virtual void associate(std::string);
virtual void load();
void load_file(fs::path);
void add_frames(SDL_Texture*);
void add_frames(const std::vector<SDL_Texture*>&);
const std::vector<SDL_Texture*>& get_frames() const;
Frameset& get_all_frames_frameset();
Frameset& add_frameset(std::string);
virtual Frameset& set_frameset(std::string);
Frameset& get_current_frameset();
void set_frame_length(float);
const Frameset& get_current_frameset() const;
SDL_Texture* get_current_frame() const;
const Box& get_box(int = 0) const;
const std::vector<Box>& get_boxes() const;
void add_box(glm::vec2, bool = false);
void update_size(bool = false);
void set_scale(float);
void set_scale_quality(const std::string&);
float get_scale() const;
bool is_loaded() const;
virtual void unload();
void advance_frame();
void hide();
void unhide();
void toggle_hidden();
bool is_hidden() const;
void set_step(glm::vec2);
void set_alpha_mod(Uint8);
Uint8 get_alpha_mod() const;
void set_color_mod(const SDL_Color&);
const SDL_Color& get_color_mod() const;
float get_w() const;
float get_h() const;
glm::vec2 get_size() const;
float get_top(int = 0) const;
float get_right(int = 0) const;
float get_bottom(int = 0) const;
float get_left(int = 0) const;
float get_center_x(int = 0) const;
float get_center_y(int = 0) const;
glm::vec2 get_nw(int = 0) const;
glm::vec2 get_north(int = 0) const;
glm::vec2 get_ne(int = 0) const;
glm::vec2 get_east(int = 0) const;
glm::vec2 get_se(int = 0) const;
glm::vec2 get_south(int = 0) const;
glm::vec2 get_sw(int = 0) const;
glm::vec2 get_west(int = 0) const;
glm::vec2 get_center(int = 0) const;
void set_top(float);
void set_right(float);
void set_bottom(float);
void set_left(float);
void set_center_x(float);
void set_center_y(float);
void set_nw(const glm::vec2&);
void set_north(const glm::vec2&);
void set_ne(const glm::vec2&);
void set_se(const glm::vec2&);
void set_south(const glm::vec2&);
void set_sw(const glm::vec2&);
void set_west(const glm::vec2&);
void set_center(const glm::vec2&);
void add_wrap(bool, bool);
void add_wrap(bool, bool, Box);
void add_hue_shift_frames(int);
virtual glm::vec2 move(const glm::vec2&);
virtual glm::vec2 move_weighted(const glm::vec2&);
bool collide(const glm::vec2&, bool = false) const;
bool collide(const Segment&, glm::vec2* = NULL, bool = false) const;
bool collide(const Segment&, glm::vec2&, bool = false) const;
bool collide(const Box&, bool = false, Box* = NULL, bool = false, SDL_Texture* = NULL) const;
bool collide(const Box&, Box&, bool = false, bool = false) const;
bool collide(const Sprite&, bool = false, Box* = NULL, bool = false, bool = false) const;
bool collide(const Sprite&, Box&, bool = false, bool = false, bool = false) const;
void wipe(float = 0.0f);
void advance_wipe_frame();
const std::vector<Box>& get_current_wipe_blinds() const;
void reverse_wipe_direction();
Sprite& insert_child(std::string, std::list<Child>::iterator);
Sprite& insert_child(std::string, std::string);
Sprite& insert_child(std::string, int);
Sprite& insert_child(std::string);
Sprite& get_child(std::string);
bool has_child(std::string) const;
void remove_child(std::string);
void set_draw_children_on_frame(bool);
virtual void update(float timestamp, const std::vector<Box>&);
virtual void update(float timestamp);
void render_subsection(SDL_Renderer*, SDL_Texture*, const Box&, const Box&);
void set_to_leave_memory_allocated();
void set_to_deallocate_memory();
virtual std::string class_name() const { return "Sprite"; }
};
class Sprite::Child
{
public:
std::string name;
Sprite sprite;
Child(std::string name, Node* parent) : name(name), sprite(Sprite(parent)) {}
inline bool operator==(const std::string& name) const
{
return this->name == name;
}
};
class Sprite::Frameset
{
public:
Sprite* sprite;
std::vector<int> order;
int order_index = 0;
float frame_length = 0;
bool reversed = false;
glm::vec2 size = {0, 0};
Frameset();
Frameset(Sprite*);
void add_frame_indicies(int);
void set_frame_length(float);
float get_frame_length() const;
void reset();
void clear();
int get_order_index() const;
void set_order_index(int);
int get_current_frame_index() const;
glm::vec2 measure() const;
void set_size();
void set_size(const glm::vec2&);
const glm::vec2& get_size() const;
void step();
void increment_index();
void increment_index(int);
int get_frame_count() const;
void reverse();
template <typename N>
void add_frame_indicies(const std::vector<N>& indicies)
{
for (const N& index : indicies)
{
add_frame_indicies(index);
}
}
};
#include "Pixels.hpp"
#include "extension.hpp"