Files
kraken/kraken/KRHelpers.h

195 lines
5.6 KiB
C
Raw Normal View History

2021-08-16 16:35:36 -07:00
//
// KRHelpers.h
// Kraken Engine
//
2025-08-31 14:11:20 -07:00
// Copyright 2025 Kearwood Gilbert. All rights reserved.
2021-08-16 16:35:36 -07:00
//
// Redistribution and use in source and binary forms, with or without modification, are
// permitted provided that the following conditions are met:
//
// 1. Redistributions of source code must retain the above copyright notice, this list of
// conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
// of conditions and the following disclaimer in the documentation and/or other materials
// provided with the distribution.
//
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// The views and conclusions contained in the software and documentation are those of the
// authors and should not be interpreted as representing official policies, either expressed
// or implied, of Kearwood Gilbert.
//
#pragma once
#include "hydra.h"
2019-11-14 23:23:24 -08:00
#include <string>
#include "../3rdparty/tinyxml2/tinyxml2.h"
#include "simdjson.h"
#define KRMIN(x,y) ((x) < (y) ? (x) : (y))
#define KRMAX(x,y) ((x) > (y) ? (x) : (y))
#define KRCLAMP(x, min, max) (KRMAX(KRMIN(x, max), min))
#define KRALIGN(x) ((x + 3) & ~0x03)
float const PI = 3.141592653589793f;
float const D2R = PI * 2 / 360;
namespace kraken {
// XML Helpers
2023-08-05 21:14:53 -07:00
void setXMLAttribute(const std::string& base_name, ::tinyxml2::XMLElement* e, const hydra::Vector3& value, const hydra::Vector3& default_value);
void setXMLAttribute(const std::string& base_name, ::tinyxml2::XMLElement* e, const hydra::AABB& value, const hydra::AABB& default_value);
2023-08-05 21:14:53 -07:00
const hydra::Vector3 getXMLAttribute(const std::string& base_name, ::tinyxml2::XMLElement* e, const hydra::Vector3& default_value);
const hydra::AABB getXMLAttribute(const std::string& base_name, ::tinyxml2::XMLElement* e, const hydra::AABB& default_value);
// JSON Helpers
bool tryJsonRequired(simdjson::error_code error);
bool tryJson(simdjson::error_code error);
} // namespace kraken
namespace simdjson {
// 32-bit float simdjson deserialization helper
template <typename simdjson_value>
auto tag_invoke(deserialize_tag, simdjson_value &val, float& ret)
{
double doubleRet = ret;
auto error = val.get(doubleRet);
ret = static_cast<float>(doubleRet);
return error;
}
template <typename builder_type>
void tag_invoke(serialize_tag, builder_type& builder, const hydra::Vector2& vec)
{
builder.start_array();
builder.append(vec.x);
builder.append_comma();
builder.append(vec.y);
builder.end_array();
}
template <typename simdjson_value>
auto tag_invoke(deserialize_tag, simdjson_value &val, hydra::Vector2& vec)
{
ondemand::array components;
auto error = val.get_array().get(components);
if (error) {
return error;
}
ondemand::array_iterator itr;
if ((error = components.begin().get(itr)))
{
return error;
}
double v[2] = {0.f};
for(int i=0; i < 2; i++) {
if ((error = (*itr).get(v[i]))) {
return error;
}
++itr;
}
vec[0] = v[0];
vec[1] = v[1];
return simdjson::SUCCESS;
}
template <typename builder_type>
void tag_invoke(serialize_tag, builder_type& builder, const hydra::Vector3& vec)
{
builder.start_array();
builder.append(vec.x);
builder.append_comma();
builder.append(vec.y);
builder.append_comma();
builder.append(vec.z);
builder.end_array();
}
template <typename simdjson_value>
auto tag_invoke(deserialize_tag, simdjson_value &val, hydra::Vector3& vec) {
ondemand::array components;
auto error = val.get_array().get(components);
if (error) {
return error;
}
ondemand::array_iterator itr;
if ((error = components.begin().get(itr)))
{
return error;
}
double v[3] = {0.f};
for(int i=0; i < 3; i++) {
if ((error = (*itr).get(v[i]))) {
return error;
}
++itr;
}
vec[0] = v[0];
vec[1] = v[1];
vec[2] = v[2];
return simdjson::SUCCESS;
}
template <typename builder_type>
void tag_invoke(serialize_tag, builder_type& builder, const hydra::Vector4& vec)
{
builder.start_array();
builder.append(vec.x);
builder.append_comma();
builder.append(vec.y);
builder.append_comma();
builder.append(vec.z);
builder.append_comma();
builder.append(vec.w);
builder.end_array();
}
template <typename simdjson_value>
auto tag_invoke(deserialize_tag, simdjson_value &val, hydra::Vector4& vec)
{
ondemand::array components;
auto error = val.get_array().get(components);
if (error) {
return error;
}
ondemand::array_iterator itr;
if ((error = components.begin().get(itr)))
{
return error;
}
double v[4] = {0.f};
for(int i=0; i < 4; i++) {
if ((error = (*itr).get(v[i]))) {
return error;
}
++itr;
}
vec[0] = v[0];
vec[1] = v[1];
vec[2] = v[2];
vec[3] = v[3];
return simdjson::SUCCESS;
}
} // namespace simdjson