The Snivy Video Game Is Complete
This commit is contained in:
11
src/util/imgui_.cpp
Normal file
11
src/util/imgui_.cpp
Normal file
@@ -0,0 +1,11 @@
|
||||
#include "imgui_.h"
|
||||
|
||||
namespace game::util::imgui
|
||||
{
|
||||
float row_widget_width_get(int count, float width)
|
||||
{
|
||||
return (width - (ImGui::GetStyle().ItemSpacing.x * (float)(count - 1))) / (float)count;
|
||||
}
|
||||
|
||||
ImVec2 widget_size_with_row_get(int count, float width) { return ImVec2(row_widget_width_get(count, width), 0); }
|
||||
}
|
||||
19
src/util/imgui_.h
Normal file
19
src/util/imgui_.h
Normal file
@@ -0,0 +1,19 @@
|
||||
#pragma once
|
||||
|
||||
#include <imgui.h>
|
||||
|
||||
#include <glm/glm.hpp>
|
||||
|
||||
using namespace glm;
|
||||
|
||||
namespace game::util::imgui
|
||||
{
|
||||
float widget_width_with_row_get(int = 1, float = ImGui::GetContentRegionAvail().x);
|
||||
ImVec2 widget_size_with_row_get(int = 1, float = ImGui::GetContentRegionAvail().x);
|
||||
|
||||
inline ImVec2 to_imvec2(vec2 value) { return ImVec2(value.x, value.y); }
|
||||
inline vec2 to_vec2(ImVec2 value) { return vec2(value.x, value.y); }
|
||||
|
||||
inline ImVec4 to_imvec4(vec4 value) { return ImVec4(value.r, value.g, value.b, value.a); }
|
||||
inline vec4 to_vec4(ImVec4 value) { return vec4(value.x, value.y, value.z, value.w); }
|
||||
}
|
||||
@@ -1,5 +1,7 @@
|
||||
#include "math_.h"
|
||||
#include "SDL3/SDL_rect.h"
|
||||
#include "glm/ext/matrix_transform.hpp"
|
||||
#include <ctime>
|
||||
|
||||
using namespace glm;
|
||||
|
||||
@@ -11,12 +13,13 @@ namespace game::util::math
|
||||
vec2 scaleSign = glm::sign(scale);
|
||||
vec2 pivotScaled = pivot * scaleAbsolute;
|
||||
vec2 sizeScaled = size * scaleAbsolute;
|
||||
float handedness = (scaleSign.x * scaleSign.y) < 0.0f ? -1.0f : 1.0f;
|
||||
|
||||
mat4 model(1.0f);
|
||||
model = glm::translate(model, vec3(position - pivotScaled, 0.0f));
|
||||
model = glm::translate(model, vec3(pivotScaled, 0.0f));
|
||||
model = glm::scale(model, vec3(scaleSign, 1.0f));
|
||||
model = glm::rotate(model, glm::radians(rotation), vec3(0, 0, 1));
|
||||
model = glm::rotate(model, glm::radians(rotation) * handedness, vec3(0, 0, 1));
|
||||
model = glm::translate(model, vec3(-pivotScaled, 0.0f));
|
||||
model = glm::scale(model, vec3(sizeScaled, 1.0f));
|
||||
return model;
|
||||
@@ -38,4 +41,13 @@ namespace game::util::math
|
||||
return glm::translate(mat4(1.0f), vec3(position, 0.0f)) * local;
|
||||
}
|
||||
|
||||
bool is_point_in_rect(ivec4 rect, ivec2 point) { return SDL_PointInRect((SDL_Point*)&point, (SDL_Rect*)&rect); }
|
||||
bool is_point_in_rectf(vec4 rect, vec2 point) { return SDL_PointInRectFloat((SDL_FPoint*)&point, (SDL_FRect*)&rect); }
|
||||
|
||||
float random() { return (float)rand() / RAND_MAX; }
|
||||
bool random_percent_roll(float percent) { return to_percent(random()) < percent; }
|
||||
float random_in_range(float min, float max) { return min + random() * (max - min); }
|
||||
float random_roll(float value) { return random() * value; }
|
||||
bool random_bool() { return random() < 0.5f; };
|
||||
void random_seed_set() { srand(std::time(nullptr)); }
|
||||
}
|
||||
@@ -5,15 +5,27 @@
|
||||
|
||||
namespace game::util::math
|
||||
{
|
||||
glm::mat4 quad_model_get(glm::vec2, glm::vec2, glm::vec2, glm::vec2, float);
|
||||
glm::mat4 quad_model_parent_get(glm::vec2 position, glm::vec2 pivot, glm::vec2, float);
|
||||
glm::mat4 quad_model_get(glm::vec2 size, glm::vec2 position = {}, glm::vec2 pivot = {},
|
||||
glm::vec2 scale = glm::vec2(1.0f), float rotation = {});
|
||||
glm::mat4 quad_model_parent_get(glm::vec2 position = {}, glm::vec2 pivot = {}, glm::vec2 scale = glm::vec2(1.0f),
|
||||
float rotation = {});
|
||||
|
||||
template <typename T> constexpr T percent_to_unit(T value) { return value / 100.0f; }
|
||||
template <typename T> constexpr T unit_to_percent(T value) { return value * 100.0f; }
|
||||
template <typename T> constexpr T to_percent(T value) { return value * 100.0f; }
|
||||
template <typename T> constexpr T to_unit(T value) { return value / 100.0f; }
|
||||
|
||||
constexpr std::array<float, 16> uv_vertices_get(glm::vec2 uvMin, glm::vec2 uvMax)
|
||||
{
|
||||
return {0.0f, 0.0f, uvMin.x, uvMin.y, 1.0f, 0.0f, uvMax.x, uvMin.y,
|
||||
1.0f, 1.0f, uvMax.x, uvMax.y, 0.0f, 1.0f, uvMin.x, uvMax.y};
|
||||
}
|
||||
|
||||
bool is_point_in_rect(glm::ivec4 rect, glm::ivec2 point);
|
||||
bool is_point_in_rectf(glm::vec4 rect, glm::vec2 point);
|
||||
|
||||
float random();
|
||||
bool random_percent_roll(float percent);
|
||||
float random_roll(float value);
|
||||
float random_in_range(float min, float max);
|
||||
bool random_bool();
|
||||
void random_seed_set();
|
||||
}
|
||||
62
src/util/string_.h
Normal file
62
src/util/string_.h
Normal file
@@ -0,0 +1,62 @@
|
||||
#pragma once
|
||||
|
||||
#include <algorithm>
|
||||
#include <iomanip>
|
||||
#include <sstream>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
|
||||
namespace game::util::string
|
||||
{
|
||||
template <typename Number>
|
||||
std::string format_commas(Number value, int decimalDigits = -1)
|
||||
{
|
||||
static_assert(std::is_arithmetic_v<Number>, "format_commas requires numeric types");
|
||||
|
||||
std::ostringstream stream;
|
||||
if (decimalDigits >= 0)
|
||||
{
|
||||
stream.setf(std::ios::fixed);
|
||||
stream << std::setprecision(decimalDigits);
|
||||
}
|
||||
stream << value;
|
||||
std::string text = stream.str();
|
||||
|
||||
std::string exponent;
|
||||
if (auto exponentPos = text.find_first_of("eE"); exponentPos != std::string::npos)
|
||||
{
|
||||
exponent = text.substr(exponentPos);
|
||||
text = text.substr(0, exponentPos);
|
||||
}
|
||||
|
||||
std::string fraction;
|
||||
if (auto decimalPos = text.find('.'); decimalPos != std::string::npos)
|
||||
{
|
||||
fraction = text.substr(decimalPos);
|
||||
text = text.substr(0, decimalPos);
|
||||
}
|
||||
|
||||
bool isNegative = false;
|
||||
if (!text.empty() && text.front() == '-')
|
||||
{
|
||||
isNegative = true;
|
||||
text.erase(text.begin());
|
||||
}
|
||||
|
||||
std::string formattedInteger;
|
||||
formattedInteger.reserve(text.size() + (text.size() / 3));
|
||||
|
||||
int digitCount = 0;
|
||||
for (auto it = text.rbegin(); it != text.rend(); ++it)
|
||||
{
|
||||
if (digitCount != 0 && digitCount % 3 == 0) formattedInteger.push_back(',');
|
||||
formattedInteger.push_back(*it);
|
||||
++digitCount;
|
||||
}
|
||||
std::reverse(formattedInteger.begin(), formattedInteger.end());
|
||||
|
||||
if (isNegative) formattedInteger.insert(formattedInteger.begin(), '-');
|
||||
|
||||
return formattedInteger + fraction + exponent;
|
||||
}
|
||||
}
|
||||
30
src/util/xml_.cpp
Normal file
30
src/util/xml_.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include "xml_.h"
|
||||
|
||||
using namespace tinyxml2;
|
||||
|
||||
namespace game::util::xml
|
||||
{
|
||||
XMLError query_string_attribute(XMLElement* element, const char* attribute, std::string* value)
|
||||
{
|
||||
const char* temp = nullptr;
|
||||
auto result = element->QueryStringAttribute(attribute, &temp);
|
||||
if (result == XML_SUCCESS && temp && value) *value = temp;
|
||||
return result;
|
||||
}
|
||||
|
||||
XMLError query_path_attribute(XMLElement* element, const char* attribute, std::filesystem::path* value)
|
||||
{
|
||||
std::string temp{};
|
||||
auto result = query_string_attribute(element, attribute, &temp);
|
||||
if (value) *value = std::filesystem::path(temp);
|
||||
return result;
|
||||
}
|
||||
|
||||
XMLError query_color_attribute(XMLElement* element, const char* attribute, float* value)
|
||||
{
|
||||
int temp{};
|
||||
auto result = element->QueryIntAttribute(attribute, &temp);
|
||||
if (result == XML_SUCCESS && value) *value = (temp / 255.0f);
|
||||
return result;
|
||||
}
|
||||
}
|
||||
13
src/util/xml_.h
Normal file
13
src/util/xml_.h
Normal file
@@ -0,0 +1,13 @@
|
||||
#pragma once
|
||||
|
||||
#include <tinyxml2.h>
|
||||
|
||||
#include <filesystem>
|
||||
#include <string>
|
||||
|
||||
namespace game::util::xml
|
||||
{
|
||||
tinyxml2::XMLError query_string_attribute(tinyxml2::XMLElement*, const char*, std::string*);
|
||||
tinyxml2::XMLError query_path_attribute(tinyxml2::XMLElement*, const char*, std::filesystem::path*);
|
||||
tinyxml2::XMLError query_color_attribute(tinyxml2::XMLElement*, const char*, float*);
|
||||
}
|
||||
Reference in New Issue
Block a user