1
0
mirror of https://github.com/borgesdan/xn65 synced 2024-12-29 21:54:47 +01:00

Implementa XnaHelper

This commit is contained in:
Danilo 2024-06-01 20:45:00 -03:00
parent b21ff469ac
commit 9327fc0ba5
8 changed files with 50 additions and 50 deletions

View File

@ -5,7 +5,7 @@ namespace xna {
size_t Object::GetHashCode() const size_t Object::GetHashCode() const
{ {
size_t seed = 0; size_t seed = 0;
XnaHHashCombine(seed, this); XnaHelper::HashCombine(seed, this);
return seed; return seed;
} }

View File

@ -4,11 +4,11 @@ namespace xna {
size_t Type::GetHashCode() const size_t Type::GetHashCode() const
{ {
size_t seed = 0; size_t seed = 0;
XnaHHashCombine(seed, fullName); XnaHelper::HashCombine(seed, fullName);
XnaHHashCombine(seed, isClass); XnaHelper::HashCombine(seed, isClass);
XnaHHashCombine(seed, isEnum); XnaHelper::HashCombine(seed, isEnum);
XnaHHashCombine(seed, isValueType); XnaHelper::HashCombine(seed, isValueType);
XnaHHashCombine(seed, isPrimitive); XnaHelper::HashCombine(seed, isPrimitive);
return seed; return seed;
} }

View File

@ -89,7 +89,7 @@ namespace xna {
DXGI_ADAPTER_DESC1 desc; DXGI_ADAPTER_DESC1 desc;
impl->dxadapter->GetDesc1(&desc); impl->dxadapter->GetDesc1(&desc);
String description = XnaHToString(desc.Description); String description = XnaHelper::ToString(desc.Description);
return description; return description;
} }
@ -110,7 +110,7 @@ namespace xna {
if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) { if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
pOutput->GetDesc(&outputDesc); pOutput->GetDesc(&outputDesc);
String deviceName = XnaHToString(outputDesc.DeviceName); String deviceName = XnaHelper::ToString(outputDesc.DeviceName);
pOutput->Release(); pOutput->Release();
pOutput = nullptr; pOutput = nullptr;

View File

@ -20,7 +20,7 @@ namespace xna {
if (!AudioEngine::impl || !AudioEngine::impl->_dxAudioEngine) if (!AudioEngine::impl || !AudioEngine::impl->_dxAudioEngine)
return; return;
const auto file = XnaHToWString(fileName); const auto file = XnaHelper::ToWString(fileName);
impl->_dxSoundEffect = unew<DxSoundEffect>(AudioEngine::impl->_dxAudioEngine.get(), file.c_str()); impl->_dxSoundEffect = unew<DxSoundEffect>(AudioEngine::impl->_dxAudioEngine.get(), file.c_str());
} }

View File

@ -11,7 +11,7 @@ namespace xna {
auto _this = device.shared_from_this(); auto _this = device.shared_from_this();
auto texture2d = New<Texture2D>(_this); auto texture2d = New<Texture2D>(_this);
ID3D11Resource* resource = nullptr; ID3D11Resource* resource = nullptr;
auto wstr = XnaHToWString(fileName); auto wstr = XnaHelper::ToWString(fileName);
HRESULT result = DirectX::CreateWICTextureFromFile( HRESULT result = DirectX::CreateWICTextureFromFile(
device.impl->_device, device.impl->_device,

View File

@ -34,7 +34,7 @@ namespace xna {
template <typename T> template <typename T>
auto Load(String const& assetName) { auto Load(String const& assetName) {
if (assetName.empty()) { if (assetName.empty()) {
return ReturnDefaultOrNull<T>(); return XnaHelper::ReturnDefaultOrNull<T>();
} }
auto obj2 = ReadAsset<T>(assetName); auto obj2 = ReadAsset<T>(assetName);
@ -48,7 +48,7 @@ namespace xna {
auto input = OpenStream(assetName); auto input = OpenStream(assetName);
if (input->IsClosed()) if (input->IsClosed())
return ReturnDefaultOrNull<T>(); return XnaHelper::ReturnDefaultOrNull<T>();
auto contentReader = ContentReader::Create(this, input, assetName); auto contentReader = ContentReader::Create(this, input, assetName);

View File

@ -75,7 +75,7 @@ namespace xna {
const auto num = Read7BitEncodedInt(); const auto num = Read7BitEncodedInt();
if (num == 0) { if (num == 0) {
ReturnDefaultOrNull<T>(); XnaHelper::ReturnDefaultOrNull<T>();
} }
const auto index = num - 1; const auto index = num - 1;
@ -83,7 +83,7 @@ namespace xna {
if (index >= typeReaders.size()) { if (index >= typeReaders.size()) {
xna_error_apply(err, XnaErrorCode::ARGUMENT_OUT_OF_RANGE); xna_error_apply(err, XnaErrorCode::ARGUMENT_OUT_OF_RANGE);
ReturnDefaultOrNull<T>(); XnaHelper::ReturnDefaultOrNull<T>();
} }
auto reader = typeReaders[index]; auto reader = typeReaders[index];
@ -103,7 +103,7 @@ namespace xna {
return objB; return objB;
} }
return ReturnDefaultOrNull<T>(); return XnaHelper::ReturnDefaultOrNull<T>();
} }
template<typename T> template<typename T>

View File

@ -3,47 +3,47 @@
#include <string> #include <string>
#include <utility> #include <utility>
#include <stdexcept>
namespace xna { namespace xna {
inline std::wstring XnaHToWString(const std::string& str) struct XnaHelper {
{ template<typename T> struct is_shared_ptr : std::false_type {};
std::wstring wstr; template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
size_t size;
wstr.resize(str.length());
mbstowcs_s(&size, &wstr[0], wstr.size() + 1, str.c_str(), str.size());
return wstr;
}
inline std::string XnaHToString(const std::wstring& wstr) static inline std::wstring ToWString(const std::string& str)
{ {
std::string str; std::wstring wstr;
size_t size; size_t size;
str.resize(wstr.length()); wstr.resize(str.length());
wcstombs_s(&size, &str[0], str.size() + 1, wstr.c_str(), wstr.size()); mbstowcs_s(&size, &wstr[0], wstr.size() + 1, str.c_str(), str.size());
return str; return wstr;
} }
template <class T> static inline std::string ToString(const std::wstring& wstr)
constexpr void XnaHHashCombine(std::size_t& seed, const T& v) { {
std::hash<T> hasher; std::string str;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); size_t size;
} str.resize(wstr.length());
wcstombs_s(&size, &str[0], str.size() + 1, wstr.c_str(), wstr.size());
return str;
}
template<typename T> struct is_shared_ptr : std::false_type {}; template <class T>
template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {}; static constexpr void HashCombine(std::size_t& seed, const T& v) {
std::hash<T> hasher;
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
}
template<typename T> template<typename T>
inline auto ReturnDefaultOrNull() { static inline auto ReturnDefaultOrNull() {
if constexpr (is_shared_ptr<T>::value) if constexpr (is_shared_ptr<T>::value)
return (T)nullptr; return (T)nullptr;
else else if (std::is_default_constructible<T>::value)
return T(); return T();
} else
throw std::runtime_error("Unable to build object");
/*template<typename T> }
inline auto ReturnAuto(T& value) { };
return value;
}*/
} }
#endif #endif