106 lines
3.1 KiB
C++
106 lines
3.1 KiB
C++
#include "extension.hpp"
|
|
|
|
glm::vec2 sfw::get_step(glm::vec2 start, glm::vec2 end, float speed)
|
|
{
|
|
float angle = glm::atan(end.x - start.x, end.y - start.y);
|
|
return glm::vec2(speed * glm::sin(angle), speed * glm::cos(angle));
|
|
}
|
|
|
|
void sfw::set_magnitude(glm::vec2& vector, float magnitude)
|
|
{
|
|
vector = glm::normalize(vector) * magnitude;
|
|
}
|
|
|
|
Box sfw::get_texture_box(SDL_Texture* texture)
|
|
{
|
|
int w, h;
|
|
SDL_QueryTexture(texture, NULL, NULL, &w, &h);
|
|
return Box(glm::vec2(0, 0), glm::vec2(w, h));
|
|
}
|
|
|
|
void sfw::fill_texture(SDL_Renderer* renderer, SDL_Texture* texture, int r, int g, int b, int a)
|
|
{
|
|
SDL_SetRenderTarget(renderer, texture);
|
|
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
|
SDL_RenderFillRect(renderer, NULL);
|
|
}
|
|
|
|
void sfw::fill_texture(SDL_Renderer* renderer, SDL_Texture* texture, SDL_Texture* tile)
|
|
{
|
|
SDL_SetRenderTarget(renderer, texture);
|
|
Box texture_box = get_texture_box(texture), tile_box = get_texture_box(tile);
|
|
SDL_FRect draw_rect;
|
|
for (int x = 0; x < texture_box.get_w(); x += tile_box.get_w())
|
|
{
|
|
for (int y = 0; y < texture_box.get_h(); y += tile_box.get_h())
|
|
{
|
|
draw_rect = {(float) x, (float) y, tile_box.get_w(), tile_box.get_h()};
|
|
SDL_RenderCopyF(renderer, tile, NULL, &draw_rect);
|
|
}
|
|
}
|
|
}
|
|
|
|
SDL_Texture* sfw::duplicate_texture(SDL_Renderer* renderer, SDL_Texture* texture)
|
|
{
|
|
Box box = get_texture_box(texture);
|
|
SDL_Texture* duplicate = SDL_CreateTexture(
|
|
renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, box.get_w(), box.get_h());
|
|
SDL_SetRenderTarget(renderer, duplicate);
|
|
SDL_RenderCopyF(renderer, texture, NULL, NULL);
|
|
return duplicate;
|
|
}
|
|
|
|
std::vector<fs::path> sfw::glob(fs::path query)
|
|
{
|
|
fs::path basename = query.parent_path();
|
|
if (basename == "")
|
|
{
|
|
basename = ".";
|
|
}
|
|
std::regex expression(query.string());
|
|
std::vector<fs::path> files;
|
|
for (auto& entry: fs::directory_iterator(basename))
|
|
{
|
|
if (std::regex_match(entry.path().string(), expression))
|
|
{
|
|
files.push_back(entry.path());
|
|
}
|
|
}
|
|
std::sort(files.begin(), files.end());
|
|
return files;
|
|
}
|
|
|
|
fs::path sfw::get_next_file_name(
|
|
fs::path directory, int zfill, std::string prefix, std::string extension)
|
|
{
|
|
std::stringstream file_pattern;
|
|
file_pattern << prefix << "([0-9]+)" << extension;
|
|
fs::path query = directory / file_pattern.str();
|
|
std::vector<fs::path> files = sfw::glob(query);
|
|
int index = 1;
|
|
if (files.size())
|
|
{
|
|
const std::string last = files.back().string();
|
|
std::smatch matches;
|
|
std::regex_match(last, matches, std::regex(query.string()));
|
|
index = std::stoi(matches[1]) + 1;
|
|
}
|
|
std::stringstream filename;
|
|
fs::path path;
|
|
do
|
|
{
|
|
filename << prefix << sfw::pad(index++, zfill) << extension;
|
|
path = directory / filename.str();
|
|
filename.str("");
|
|
filename.clear();
|
|
}
|
|
while (fs::exists(path));
|
|
return path;
|
|
}
|
|
|
|
std::ostream& operator<<(std::ostream& out, const glm::vec2& vector)
|
|
{
|
|
out << "{" << vector.x << ", " << vector.y << "}";
|
|
return out;
|
|
}
|