summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-08-03 19:07:29 +0200
committerLaG1924 <12997935+LaG1924@users.noreply.github.com>2018-08-03 19:07:29 +0200
commit76552663f49578e1d231214916461ac7cd93a44f (patch)
tree6c9bb3dd2bc0b4e5226a3f7c443ce4175d0ecce6 /src
parentBlockModels are using asset tree now (diff)
downloadAltCraft-76552663f49578e1d231214916461ac7cd93a44f.tar
AltCraft-76552663f49578e1d231214916461ac7cd93a44f.tar.gz
AltCraft-76552663f49578e1d231214916461ac7cd93a44f.tar.bz2
AltCraft-76552663f49578e1d231214916461ac7cd93a44f.tar.lz
AltCraft-76552663f49578e1d231214916461ac7cd93a44f.tar.xz
AltCraft-76552663f49578e1d231214916461ac7cd93a44f.tar.zst
AltCraft-76552663f49578e1d231214916461ac7cd93a44f.zip
Diffstat (limited to 'src')
-rw-r--r--src/AssetManager.cpp50
-rw-r--r--src/AssetManager.hpp10
2 files changed, 59 insertions, 1 deletions
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 <nlohmann/json.hpp>
#include <easylogging++.h>
#include <glm/gtc/matrix_transform.hpp>
+#include <SDL.h>
+#include <SDL_image.h>
#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>();
+ AssetTexture *asset = dynamic_cast<AssetTexture*>(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<unsigned char>());
+}
+
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<unsigned char> textureData;
+ double x, y, w, h;
+ unsigned int realWidth, realHeight;
+};
+
class AssetManager {
Texture *textureAtlas;
std::map<std::string, BlockId> assetIds;
@@ -209,4 +215,8 @@ public:
void RecursiveWalkAsset(const std::string &assetPath, std::function<void(AssetTreeNode&)> fnc);
AssetTreeNode *GetAssetByAssetName(const std::string &assetName);
+
+ void LoadTextures();
+
+ void ParseAssetTexture(AssetTreeNode &node);
};