From 01a40b8f8995b035b293028449f1afc24906b9be Mon Sep 17 00:00:00 2001 From: LaG1924 <12997935+LaG1924@users.noreply.github.com> Date: Thu, 9 Aug 2018 06:37:09 +0500 Subject: Replaced sdl2_image with stb_image --- src/AssetManager.cpp | 37 +++++++++++++------------------------ src/TextureAtlas.cpp | 2 +- 2 files changed, 14 insertions(+), 25 deletions(-) (limited to 'src') diff --git a/src/AssetManager.cpp b/src/AssetManager.cpp index 9671491..97833f5 100644 --- a/src/AssetManager.cpp +++ b/src/AssetManager.cpp @@ -7,7 +7,10 @@ #include #include #include -#include +#define STBI_NO_STDIO +#define STB_IMAGE_IMPLEMENTATION +#define STBI_ONLY_PNG +#include #include "Utility.hpp" @@ -132,35 +135,21 @@ void ParseAsset(AssetTreeNode &node) { } void 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) { + int w, h, n; + unsigned char *data = stbi_load_from_memory(node.data.data(),node.data.size(), &w, &h, &n, 4); + if (data == nullptr) { 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; + size_t dataLen = w * h * 4; asset->textureData.resize(dataLen); - std::memcpy(asset->textureData.data(), surface->pixels, dataLen); - asset->realWidth = surface->w; - asset->realHeight = surface->h; + std::memcpy(asset->textureData.data(), data, dataLen); + asset->realWidth = w; + asset->realHeight = h; - SDL_UnlockSurface(surface); - SDL_FreeSurface(surface); + stbi_image_free(data); node.data.swap(std::vector()); } @@ -525,7 +514,7 @@ const BlockModel *AssetManager::GetBlockModelByBlockId(BlockId block) { AssetBlockModel *assetModel = GetAsset("/minecraft/models/block/" + model.modelName); if (!assetModel) return &GetAsset("/minecraft/models/block/error")->blockModel; - + blockIdToBlockModel.insert(std::make_pair(block, &assetModel->blockModel)); return &assetModel->blockModel; diff --git a/src/TextureAtlas.cpp b/src/TextureAtlas.cpp index ad62073..406418f 100644 --- a/src/TextureAtlas.cpp +++ b/src/TextureAtlas.cpp @@ -106,7 +106,7 @@ TextureAtlas::TextureAtlas(std::vector &textures) { } } glTexSubImage3D(GL_TEXTURE_2D_ARRAY, 0, textureCoords[i].pixelX, textureSize - textureCoords[i].pixelY - textureCoords[i].pixelH, textureCoords[i].layer, - textureCoords[i].pixelW, textureCoords[i].pixelH, 1, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8, textures[i].data.data()); + textureCoords[i].pixelW, textureCoords[i].pixelH, 1, GL_RGBA, GL_UNSIGNED_INT_8_8_8_8_REV, textures[i].data.data()); glCheckError(); } -- cgit v1.2.3