From 76552663f49578e1d231214916461ac7cd93a44f Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Fri, 3 Aug 2018 22:07:29 +0500 Subject: Loading texture data in AssetTree --- src/AssetManager.cpp | 50 +++++++++++++++++++++++++++++++++++++++++++++++++- src/AssetManager.hpp | 10 ++++++++++ 2 files changed, 59 insertions(+), 1 deletion(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index f5f92cc..25e9f24 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -6,8 +6,11 @@ #include #include #include +#include +#include #include "Texture.hpp" +#include "Utility.hpp" namespace fs = std::experimental::filesystem::v1; @@ -28,6 +31,8 @@ AssetManager::AssetManager() { RecursiveWalkAsset("/", parseAssetRecur); + LoadTextures(); + LoadIds(); LoadTextureResources(); ParseBlockModels(); @@ -597,12 +602,55 @@ AssetTreeNode * AssetManager::GetAssetByAssetName(const std::string & assetName) return node; } +void AssetManager::LoadTextures() { + +} + +void AssetManager::ParseAssetTexture(AssetTreeNode &node) { + SDL_RWops *rw = SDL_RWFromMem(node.data.data(), node.data.size()); + SDL_Surface *surface = IMG_LoadPNG_RW(rw); + + SDL_RWclose(rw); + if (!surface) { + return; + } + + if (surface->format->format != SDL_PIXELFORMAT_RGBA8888) { + SDL_Surface *temp = SDL_ConvertSurfaceFormat(surface, SDL_PIXELFORMAT_RGBA8888, 0); + std::swap(temp, surface); + if (!temp) { + std::swap(temp, surface); + } + SDL_FreeSurface(temp); + } + + SDL_LockSurface(surface); + + node.asset = std::make_unique(); + AssetTexture *asset = dynamic_cast(node.asset.get()); + size_t dataLen = surface->h * surface->pitch; + asset->textureData.resize(dataLen); + std::memcpy(asset->textureData.data(), surface->pixels, dataLen); + asset->realWidth = surface->w; + asset->realHeight = surface->h; + + SDL_UnlockSurface(surface); + SDL_FreeSurface(surface); + + node.data.swap(std::vector()); +} + void AssetManager::ParseAsset(AssetTreeNode &node) { if (node.data.empty() || node.asset) return; if (node.parent->name == "block" && node.parent->parent->name == "models") { ParseAssetBlockModel(node); - if (true) {}; + return; + } + + if (node.data[0] == 0x89 && node.data[1] == 'P' && node.data[2] == 'N' && node.data[3] == 'G') { + ParseAssetTexture(node); + return; } } \ No newline at end of file diff --git a/src/AssetManager.hpp b/src/AssetManager.hpp index cfeb91a..b8748ae 100644 --- a/src/AssetManager.hpp +++ b/src/AssetManager.hpp @@ -158,6 +158,12 @@ struct AssetBlockModel : Asset { BlockModel blockModel; }; +struct AssetTexture : Asset { + std::vector textureData; + double x, y, w, h; + unsigned int realWidth, realHeight; +}; + class AssetManager { Texture *textureAtlas; std::map assetIds; @@ -209,4 +215,8 @@ public: void RecursiveWalkAsset(const std::string &assetPath, std::function fnc); AssetTreeNode *GetAssetByAssetName(const std::string &assetName); + + void LoadTextures(); + + void ParseAssetTexture(AssetTreeNode &node); }; -- cgit v1.2.3