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

Implementações em Content e Texture2D

This commit is contained in:
Danilo 2024-05-06 09:58:40 -03:00
parent 9b10adfc38
commit b194a27dad
17 changed files with 123 additions and 43 deletions

View File

@ -39,7 +39,7 @@ add_executable (xna WIN32
"content/manager.cpp"
"content/reader.cpp"
"csharp/binary.cpp"
"content/decstream.cpp" "content/lzx/decoder.cpp" "content/lzx/decoderstream.cpp" "content/typereadermanager.cpp" "csharp/object.cpp" "csharp/type.cpp")
"content/decstream.cpp" "content/lzx/decoder.cpp" "content/lzx/decoderstream.cpp" "content/typereadermanager.cpp" "csharp/object.cpp" "csharp/type.cpp" "platform/init-dx.cpp")
if (CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET xna PROPERTY CXX_STANDARD 20)

View File

@ -42,19 +42,20 @@ namespace xna {
if (assetName.empty()) return nullptr;
if (_loadedAssets.contains(assetName)) {
const auto& ptr = _loadedAssets[assetName];
const auto obj1 = reinterpret_pointer_cast<T>(ptr);
auto& ptr = _loadedAssets[assetName];
auto obj1 = reinterpret_pointer_cast<T>(ptr);
return obj1;
return *obj1;
}
const auto obj2 = ReadAsset<T>(assetName);
auto obj2 = ReadAsset<T>(assetName);
//auto obj3 = reinterpret_pointer_cast<T>(obj2);
return obj2;
}
protected:
template <typename T>
sptr<T> ReadAsset(String const& assetName) {
T ReadAsset(String const& assetName) {
auto input = OpenStream(assetName);
auto contentReader = ContentReader::Create(this, input, assetName);

View File

@ -120,20 +120,21 @@ namespace xna {
return nullptr;
Int num1 = 0;
if (binaryReader.ReadByte() == 'w')
auto _byte = binaryReader.ReadByte(); //desfazer
if (_byte == 'w')
num1 = binaryReader.ReadUInt16();
else
return nullptr;
graphicsProfile = (num1 & 32512) >> 8;
graphicsProfile = (num1 & XnbVersionProfileMask) >> XnbVersionProfileShift;
bool flag = false;
switch (num1 & -32513)
{
case 5:
case XnbVersion:
flag = false;
break;
case 32773:
case XnbCompressedVersion:
flag = true;
break;
default:

View File

@ -12,13 +12,13 @@
#include <any>
namespace xna {
class ContentReader : public BinaryReader, public std::enable_shared_from_this<ContentReader>{
class ContentReader : public BinaryReader, public std::enable_shared_from_this<ContentReader> {
public:
static sptr<ContentReader> Create(ContentManager* contentManager, sptr<Stream>& input, String const& assetName);
template <typename T>
sptr<T> ReadAsset();
T ReadAsset();
template <typename T>
T ReadObject();
@ -38,7 +38,7 @@ namespace xna {
private:
ContentReader(ContentManager* contentManager, sptr<Stream>& input, String const& assetName, Int graphicsProfile)
: BinaryReader(input), _contentManager(contentManager), _assetName(assetName){}
: BinaryReader(input), _contentManager(contentManager), _assetName(assetName) {}
static sptr<Stream> PrepareStream(sptr<Stream>& input, String const& assetName, Int& graphicsProfile);
@ -46,7 +46,7 @@ namespace xna {
template <typename T>
T ReadObjectInternal(std::any& existingInstance, xna_error_nullarg);
template <typename T>
T InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_nullarg);
@ -55,6 +55,11 @@ namespace xna {
String _assetName;
std::vector<sptr<ContentTypeReader>> typeReaders;
Int graphicsProfile{ 0 };
static constexpr Ushort XnbVersionProfileMask = 32512;
static constexpr Ushort XnbCompressedVersion = 32773;
static constexpr Ushort XnbVersion = 5;
static constexpr Int XnbVersionProfileShift = 8;
};
template<typename T>
@ -65,7 +70,7 @@ namespace xna {
if (num == 0) {
return T();
}
const auto index = num - 1;
if (index >= typeReaders.size()) {
@ -73,9 +78,10 @@ namespace xna {
return T();
}
return InvokeReader(typeReaders[index], existingInstance);
auto reader = typeReaders[index];
return InvokeReader<T>(*reader, existingInstance, err);
}
template<typename T>
inline T ContentReader::InvokeReader(ContentTypeReader& reader, std::any& existingInstance, xna_error_ptr_arg)
{
@ -85,13 +91,14 @@ namespace xna {
if (contentTypeReader) {
T existingInstance1 = existingInstance.has_value() ? std::any_cast<T>(existingInstance) : T();
objB = contentTypeReader->Read(*this, existingInstance1);
return objB;
}
return T();
}
template<typename T>
inline sptr<T> ContentReader::ReadAsset()
template<typename T>
inline T ContentReader::ReadAsset()
{
const auto sharedResourceCount = ReadHeader();
T obj = ReadObject<T>();
@ -102,14 +109,15 @@ namespace xna {
template<typename T>
inline T ContentReader::ReadObject()
{
return ReadObjectInternal<T>(nullptr);
auto a = std::any();
return ReadObjectInternal<T>(a);
}
template<typename T>
inline T ContentReader::ReadObject(T existingInstance)
{
return ReadObjectInternal<T>(std::any(existingInstance));
}
}
}
#endif

View File

@ -11,7 +11,11 @@ namespace xna {
for (size_t index = 0; index < typeCount; ++index)
{
auto typeReader = ContentTypeReaderManager::GetTypeReader(contentReader->ReadString(), contentReader, newTypeReaders);
const auto readerTypeName = contentReader->ReadString();
const auto xnaType = readerTypeName.substr(0, readerTypeName.find(","));
auto typeReader = ContentTypeReaderManager::GetTypeReader(xnaType.empty() ? readerTypeName : xnaType, contentReader, newTypeReaders);
if (contentReader->ReadInt32() != typeReader->TypeVersion()) {
xna_error_apply(err, XnaErrorCode::BAD_TYPE);
@ -59,9 +63,12 @@ namespace xna {
{
sptr<ContentTypeReader> reader = nullptr;
if (ContentTypeReaderManager::nameToReader.contains(readerTypeName) || !ContentTypeReaderManager::InstantiateTypeReader(readerTypeName, contentReader, reader, err)) {
if (ContentTypeReaderManager::nameToReader.contains(readerTypeName)) {
return ContentTypeReaderManager::nameToReader[readerTypeName];
}
}
else if (!ContentTypeReaderManager::InstantiateTypeReader(readerTypeName, contentReader, reader, err)) {
return reader;
}
if (xna_error_haserros(err))
return nullptr;
@ -79,12 +86,8 @@ namespace xna {
{
sptr<Type> type = nullptr;
std::map<sptr<Type>, sptr<ContentTypeReader>>::iterator it;
for (it = readerTypeToReader.begin(); it != readerTypeToReader.end(); it++) {
if (it->first->FullName() == readerTypeName)
type = it->first;
}
if (Type::NameOfRegisteredTypes.contains(readerTypeName))
type = Type::NameOfRegisteredTypes[readerTypeName];
if (!type) {
xna_error_apply(err, XnaErrorCode::INVALID_OPERATION);

View File

@ -261,7 +261,7 @@ namespace xna {
} while (num < val1);
return empty;
return sb;
}
Int BinaryReader::InternalReadOneChar(xna_error_ptr_arg)

View File

@ -11,5 +11,5 @@ namespace xna {
XnaHHashCombine(seed, isPrimitive);
return seed;
}
}
}

View File

@ -5,6 +5,7 @@
#include "object.hpp"
#include <type_traits>
#include <typeinfo>
#include <map>
namespace xna {
class Type : public Object {
@ -33,12 +34,15 @@ namespace xna {
template <class T>
friend sptr<Type> typeof();
public:
inline static auto NameOfRegisteredTypes = std::map<std::string, sptr<Type>>();
private:
String fullName;
bool isClass{ false };
bool isEnum{ false };
bool isValueType{ false };
bool isPrimitive{ false };
bool isPrimitive{ false };
};
template <class T>

View File

@ -45,7 +45,7 @@ namespace xna {
switch (format)
{
case SurfaceFormat::Color://21
return DXGI_FORMAT_B8G8R8A8_UNORM;
return DXGI_FORMAT_R8G8B8A8_UNORM;
case SurfaceFormat::Bgr565: //23
return DXGI_FORMAT_B5G6R5_UNORM;
case SurfaceFormat::Bgra5551://25
@ -92,6 +92,7 @@ namespace xna {
static constexpr SurfaceFormat ConvertDXGIFORMATToSurface(DXGI_FORMAT format) {
switch (format)
{
case DXGI_FORMAT_R8G8B8A8_UNORM:
case DXGI_FORMAT_B8G8R8A8_UNORM:
return SurfaceFormat::Color;
case DXGI_FORMAT_B5G6R5_UNORM:

View File

@ -6,19 +6,22 @@
namespace xna {
class Texture2DReader : public ContentTypeReaderT<Texture2D> {
public:
Texture2DReader() : ContentTypeReaderT(typeof<Texture2D>()){}
Texture2D Read(ContentReader& input, Texture2D& existingInstance) override{
const auto format = static_cast<SurfaceFormat>(input.ReadInt32());
const auto width = input.ReadInt32();
const auto height = input.ReadInt32();
const auto mipMaps = input.ReadInt32();
auto texture2D = Texture2D(nullptr, width, height, mipMaps, format);
for (size_t level = 0; level < mipMaps; ++level) {
auto elementCount = input.ReadInt32();
std::vector<Byte> data = input.ReadByteBuffer(elementCount);
texture2D.SetData(level, nullptr, data, 0, elementCount);
}
}
return texture2D;
}

View File

@ -9,6 +9,7 @@
#include "mouse-dx.hpp"
#include "window-dx.hpp"
#include <Windows.h>
#include "../csharp/type.hpp"
namespace xna {
Game::Game() {
@ -156,5 +157,5 @@ namespace xna {
});
Draw(_currentGameTime);
}
}
}

View File

@ -0,0 +1,30 @@
#include "init-dx.hpp"
#include "../csharp/type.hpp"
#include "texture-dx.hpp"
#include "content-readers/texture2Dreader-dx.hpp"
#include "../content/typereadermanager.hpp"
namespace xna {
void InitPlatform::InitRegisteredTypes()
{
Type::NameOfRegisteredTypes.insert({ "Texture2D", typeof<Texture2D>() });
//Texture2DReader
Type::NameOfRegisteredTypes.insert({ "Texture2DReader", typeof<Texture2DReader>() });
Type::NameOfRegisteredTypes.insert({ "xna::Texture2DReader", typeof<Texture2DReader>() });
Type::NameOfRegisteredTypes.insert({ "Microsoft.Xna.Framework.Content.Texture2DReader", typeof<Texture2DReader>() });
}
void InitPlatform::InitActivadors()
{
ContentTypeReaderActivador::SetActivador(typeof<ObjectReader>(), []() -> sptr<ContentTypeReader> {
auto obj = New <ObjectReader>();
return reinterpret_pointer_cast<ContentTypeReader>(obj);
});
ContentTypeReaderActivador::SetActivador(typeof<Texture2DReader>(), []() -> sptr<ContentTypeReader> {
auto obj = New <Texture2DReader>();
return reinterpret_pointer_cast<ContentTypeReader>(obj);
});
}
}

View File

@ -0,0 +1,18 @@
#ifndef XNA_PLATFORM_INIT_HPP
#define XNA_PLATFORM_INIT_HPP
#include "../default.hpp"
namespace xna {
struct InitPlatform {
static void Init() {
InitRegisteredTypes();
InitActivadors();
}
static void InitRegisteredTypes();
static void InitActivadors();
};
}
#endif

View File

@ -10,6 +10,10 @@
namespace xna {
class Texture2D : public ITexture2D, public GraphicsResource {
public:
Texture2D() : GraphicsResource(nullptr){
setDefaultDesc();
}
Texture2D(GraphicsDevice* device);
Texture2D(GraphicsDevice* device, size_t width, size_t height);
Texture2D(GraphicsDevice* device, size_t width, size_t height, size_t mipMap, SurfaceFormat format);

View File

@ -44,6 +44,7 @@ namespace xna {
constexpr double DoubleMinValue = std::numeric_limits<double>::min();
using String = std::string;
using WString = std::wstring;
template <typename T>
using sptr = std::shared_ptr<T>;

View File

@ -32,7 +32,9 @@ namespace xna {
//std::vector<UINT> data(256 * 256, 0xffffffff);
//std::vector<Uint> data(256 * 256, 4278190080U);
//texture->SetData(data, 0, data.size());
//Texture2D tx = contentManager->Load<Texture2D>("Idle");
tx = contentManager->Load<Texture2D>("Idle");
tx.Bind(_graphicsDevice.get());
tx.Initialize(&err);
Game::LoadContent();
}
@ -48,7 +50,7 @@ namespace xna {
_graphicsDevice->Clear(Colors::CornflowerBlue);
spriteBatch->Begin();
//spriteBatch->Draw(*texture, position, Colors::White);
spriteBatch->Draw(tx, position, Colors::White);
spriteBatch->End();
Game::Draw(gameTime);
@ -65,12 +67,14 @@ namespace xna {
float vel = 1;
int var = 0;
sptr<ContentManager> contentManager;
Texture2D tx;
};
}
int APIENTRY WinMain(_In_ HINSTANCE hInstance, _In_opt_ HINSTANCE hPrevInstance, _In_ LPSTR lpCmdLine, _In_ int nCmdShow) {
xna::InitPlatform::Init();
auto game = xna::Game1();
const auto result = game.Run();
return result;

View File

@ -25,5 +25,6 @@
#include <iostream>
#include "content/manager.hpp"
#include "content/decstream.hpp"
#include "platform/init-dx.hpp"
// TODO: Reference additional headers your program requires here.