mirror of
https://github.com/borgesdan/xn65
synced 2024-12-29 21:54:47 +01:00
Implementa XnaHelper
This commit is contained in:
parent
b21ff469ac
commit
9327fc0ba5
@ -5,7 +5,7 @@ namespace xna {
|
||||
size_t Object::GetHashCode() const
|
||||
{
|
||||
size_t seed = 0;
|
||||
XnaHHashCombine(seed, this);
|
||||
XnaHelper::HashCombine(seed, this);
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
@ -4,11 +4,11 @@ namespace xna {
|
||||
size_t Type::GetHashCode() const
|
||||
{
|
||||
size_t seed = 0;
|
||||
XnaHHashCombine(seed, fullName);
|
||||
XnaHHashCombine(seed, isClass);
|
||||
XnaHHashCombine(seed, isEnum);
|
||||
XnaHHashCombine(seed, isValueType);
|
||||
XnaHHashCombine(seed, isPrimitive);
|
||||
XnaHelper::HashCombine(seed, fullName);
|
||||
XnaHelper::HashCombine(seed, isClass);
|
||||
XnaHelper::HashCombine(seed, isEnum);
|
||||
XnaHelper::HashCombine(seed, isValueType);
|
||||
XnaHelper::HashCombine(seed, isPrimitive);
|
||||
|
||||
return seed;
|
||||
}
|
||||
|
@ -89,7 +89,7 @@ namespace xna {
|
||||
|
||||
DXGI_ADAPTER_DESC1 desc;
|
||||
impl->dxadapter->GetDesc1(&desc);
|
||||
String description = XnaHToString(desc.Description);
|
||||
String description = XnaHelper::ToString(desc.Description);
|
||||
return description;
|
||||
}
|
||||
|
||||
@ -110,7 +110,7 @@ namespace xna {
|
||||
|
||||
if (impl->dxadapter->EnumOutputs(0, &pOutput) != DXGI_ERROR_NOT_FOUND) {
|
||||
pOutput->GetDesc(&outputDesc);
|
||||
String deviceName = XnaHToString(outputDesc.DeviceName);
|
||||
String deviceName = XnaHelper::ToString(outputDesc.DeviceName);
|
||||
|
||||
pOutput->Release();
|
||||
pOutput = nullptr;
|
||||
|
@ -20,7 +20,7 @@ namespace xna {
|
||||
if (!AudioEngine::impl || !AudioEngine::impl->_dxAudioEngine)
|
||||
return;
|
||||
|
||||
const auto file = XnaHToWString(fileName);
|
||||
const auto file = XnaHelper::ToWString(fileName);
|
||||
impl->_dxSoundEffect = unew<DxSoundEffect>(AudioEngine::impl->_dxAudioEngine.get(), file.c_str());
|
||||
}
|
||||
|
||||
|
@ -11,7 +11,7 @@ namespace xna {
|
||||
auto _this = device.shared_from_this();
|
||||
auto texture2d = New<Texture2D>(_this);
|
||||
ID3D11Resource* resource = nullptr;
|
||||
auto wstr = XnaHToWString(fileName);
|
||||
auto wstr = XnaHelper::ToWString(fileName);
|
||||
|
||||
HRESULT result = DirectX::CreateWICTextureFromFile(
|
||||
device.impl->_device,
|
||||
|
@ -34,7 +34,7 @@ namespace xna {
|
||||
template <typename T>
|
||||
auto Load(String const& assetName) {
|
||||
if (assetName.empty()) {
|
||||
return ReturnDefaultOrNull<T>();
|
||||
return XnaHelper::ReturnDefaultOrNull<T>();
|
||||
}
|
||||
|
||||
auto obj2 = ReadAsset<T>(assetName);
|
||||
@ -48,7 +48,7 @@ namespace xna {
|
||||
auto input = OpenStream(assetName);
|
||||
|
||||
if (input->IsClosed())
|
||||
return ReturnDefaultOrNull<T>();
|
||||
return XnaHelper::ReturnDefaultOrNull<T>();
|
||||
|
||||
auto contentReader = ContentReader::Create(this, input, assetName);
|
||||
|
||||
|
@ -75,7 +75,7 @@ namespace xna {
|
||||
const auto num = Read7BitEncodedInt();
|
||||
|
||||
if (num == 0) {
|
||||
ReturnDefaultOrNull<T>();
|
||||
XnaHelper::ReturnDefaultOrNull<T>();
|
||||
}
|
||||
|
||||
const auto index = num - 1;
|
||||
@ -83,7 +83,7 @@ namespace xna {
|
||||
if (index >= typeReaders.size()) {
|
||||
xna_error_apply(err, XnaErrorCode::ARGUMENT_OUT_OF_RANGE);
|
||||
|
||||
ReturnDefaultOrNull<T>();
|
||||
XnaHelper::ReturnDefaultOrNull<T>();
|
||||
}
|
||||
|
||||
auto reader = typeReaders[index];
|
||||
@ -103,7 +103,7 @@ namespace xna {
|
||||
return objB;
|
||||
}
|
||||
|
||||
return ReturnDefaultOrNull<T>();
|
||||
return XnaHelper::ReturnDefaultOrNull<T>();
|
||||
}
|
||||
|
||||
template<typename T>
|
||||
|
@ -3,47 +3,47 @@
|
||||
|
||||
#include <string>
|
||||
#include <utility>
|
||||
#include <stdexcept>
|
||||
|
||||
namespace xna {
|
||||
inline std::wstring XnaHToWString(const std::string& str)
|
||||
{
|
||||
std::wstring wstr;
|
||||
size_t size;
|
||||
wstr.resize(str.length());
|
||||
mbstowcs_s(&size, &wstr[0], wstr.size() + 1, str.c_str(), str.size());
|
||||
return wstr;
|
||||
}
|
||||
struct XnaHelper {
|
||||
template<typename T> struct is_shared_ptr : std::false_type {};
|
||||
template<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
|
||||
|
||||
inline std::string XnaHToString(const std::wstring& wstr)
|
||||
{
|
||||
std::string str;
|
||||
size_t size;
|
||||
str.resize(wstr.length());
|
||||
wcstombs_s(&size, &str[0], str.size() + 1, wstr.c_str(), wstr.size());
|
||||
return str;
|
||||
}
|
||||
static inline std::wstring ToWString(const std::string& str)
|
||||
{
|
||||
std::wstring wstr;
|
||||
size_t size;
|
||||
wstr.resize(str.length());
|
||||
mbstowcs_s(&size, &wstr[0], wstr.size() + 1, str.c_str(), str.size());
|
||||
return wstr;
|
||||
}
|
||||
|
||||
template <class T>
|
||||
constexpr void XnaHHashCombine(std::size_t& seed, const T& v) {
|
||||
std::hash<T> hasher;
|
||||
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
||||
}
|
||||
static inline std::string ToString(const std::wstring& wstr)
|
||||
{
|
||||
std::string str;
|
||||
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<typename T> struct is_shared_ptr<std::shared_ptr<T>> : std::true_type {};
|
||||
template <class T>
|
||||
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>
|
||||
inline auto ReturnDefaultOrNull() {
|
||||
if constexpr (is_shared_ptr<T>::value)
|
||||
return (T)nullptr;
|
||||
else
|
||||
return T();
|
||||
}
|
||||
|
||||
/*template<typename T>
|
||||
inline auto ReturnAuto(T& value) {
|
||||
return value;
|
||||
}*/
|
||||
template<typename T>
|
||||
static inline auto ReturnDefaultOrNull() {
|
||||
if constexpr (is_shared_ptr<T>::value)
|
||||
return (T)nullptr;
|
||||
else if (std::is_default_constructible<T>::value)
|
||||
return T();
|
||||
else
|
||||
throw std::runtime_error("Unable to build object");
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
#endif
|
Loading…
x
Reference in New Issue
Block a user