spacebox/src/extension.hpp

164 lines
5.1 KiB
C++

#ifndef extension_h_
#define extension_h_
#include <vector>
#include <iostream>
#include <regex>
#include <sstream>
#include <algorithm>
#include <iomanip>
#include <stdexcept>
#include <map>
#include <cmath>
#include "SDL.h"
#include "SDL_image.h"
#include "SDL_pixels.h"
#define GLM_ENABLE_EXPERIMENTAL
#include "glm/trigonometric.hpp"
#include "glm/vec2.hpp"
#include "glm/gtx/vector_angle.hpp"
#include "Box.hpp"
#include "Segment.hpp"
#include "Color.hpp"
#include "filesystem.hpp"
struct Node;
namespace sfw
{
enum scaler {scale2x, xbr};
glm::vec2 get_step(const Segment&, float);
glm::vec2 get_step_relative(const Segment&, float);
std::vector<Segment> get_segments(const Segment&, int);
void set_magnitude(glm::vec2&, float);
Box get_texture_box(SDL_Texture*);
void populate_pixel_2d_array(SDL_Renderer*, SDL_Texture*, std::vector<std::vector<SDL_Color>>&);
void populate_pixel_2d_array(SDL_Renderer*, SDL_Texture*, std::vector<std::vector<SDL_Color>>&, const Box&);
std::vector<SDL_Texture*> get_halo_frames(
Node&, float, int, const std::vector<SDL_Color>& = {{0, 0, 0, 255}, {255, 255, 255, 255}}, float = 4.0f, bool = true);
std::vector<SDL_Texture*> get_portal_frames(SDL_Renderer*, glm::vec2, float = 60, float = 30, int = 4, int = 6);
void fill_texture(SDL_Renderer*, SDL_Texture*, Uint8, Uint8, Uint8, Uint8 = 0xff);
void fill_texture(SDL_Renderer*, SDL_Texture*, SDL_Texture*);
SDL_Texture* get_filled_texture(SDL_Renderer*, glm::vec2, const SDL_Color&, Uint32 = SDL_PIXELFORMAT_RGBA32);
SDL_Texture* get_filled_texture(SDL_Renderer*, glm::vec2, SDL_Texture*, Uint32 = SDL_PIXELFORMAT_RGBA32);
SDL_Texture* get_hue_shifted_texture(SDL_Renderer*, SDL_Texture*, float);
SDL_Texture* duplicate_texture(SDL_Renderer*, SDL_Texture*, Uint32 = SDL_PIXELFORMAT_RGBA32);
SDL_Texture* duplicate_texture(SDL_Renderer*, SDL_Texture*, const glm::vec2&, Uint32 = SDL_PIXELFORMAT_RGBA32);
SDL_Texture* get_remapped_texture(
SDL_Renderer*, SDL_Texture*, const std::map<SDL_Color, SDL_Color>&, Uint32 = SDL_PIXELFORMAT_RGBA32);
SDL_Texture* get_remapped_texture(
SDL_Renderer*, const std::string&, const std::map<SDL_Color, SDL_Color>&,
Uint32 = SDL_PIXELFORMAT_RGBA32);
SDL_Texture* get_pixel_scaled_texture(SDL_Renderer*, SDL_Texture*, int = 1, int = scaler::scale2x);
std::vector<fs::path> glob(fs::path);
fs::path get_next_file_name(
fs::path, int = 0, std::string = "", std::string = "");
void print_error(const std::string&);
void print_sdl_error(const std::string&);
template<typename Key, typename Value, template <typename...> class Map>
std::vector<Key> get_keys(const Map<Key, Value>& map)
{
std::vector<Key> keys;
keys.reserve(map.size());
for (auto& member : map)
{
keys.push_back(member.first);
}
return keys;
}
template<typename T1, typename T2>
bool is_in_container(T1& container, T2& member)
{
return std::find(container.begin(), container.end(), member) != container.end();
}
template<typename N>
float mod(N a, N b)
{
return (b + (a % b)) % b;
}
template<typename T>
std::string pad(T end, int width, char fill = '0')
{
std::stringstream padded;
padded.fill(fill);
padded.width(width);
padded << end;
return padded.str();
}
template <typename N>
std::vector<N> range_step(N start, N end, int count)
{
float step = (end - start) / (count - 1);
std::vector<N> nums;
nums.reserve(count);
for (int ii = 0; ii < count; ii++)
{
nums.push_back(start + ii * step);
}
return nums;
}
// from https://stackoverflow.com/a/30312659/1256386
template <typename IntType>
std::vector<IntType> range(IntType start, IntType stop, IntType step)
{
if (step == IntType(0))
{
throw std::invalid_argument("step for range must be non-zero");
}
std::vector<IntType> result;
IntType i = start;
while ((step > 0) ? (i < stop) : (i > stop))
{
result.push_back(i);
i += step;
}
return result;
}
// from https://stackoverflow.com/a/30312659/1256386
template <typename IntType>
std::vector<IntType> range(IntType start, IntType stop)
{
return range(start, stop, IntType(1));
}
// from https://stackoverflow.com/a/30312659/1256386
template <typename IntType>
std::vector<IntType> range(IntType stop)
{
return range(IntType(0), stop, IntType(1));
}
}
template <typename T>
std::ostream& operator<<(std::ostream& out, const std::vector<T>& members)
{
out << "{ ";
for (const T& member : members)
{
out << member << " ";
}
out << "}";
return out;
}
std::ostream& operator<<(std::ostream&, const glm::vec2&);
bool operator<(const SDL_Color& color_1, const SDL_Color& color_2);
bool operator==(const SDL_Color& color_1, const SDL_Color& color_2);
std::ostream& operator<<(std::ostream&, const SDL_Color&);
#include "Node.hpp"
#endif