summaryrefslogtreecommitdiffstats
path: root/src/core
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--src/core/AssetManager.cpp70
-rw-r--r--src/core/AssetManager.hpp33
-rw-r--r--src/core/Core.cpp9
-rw-r--r--src/core/Core.hpp3
4 files changed, 107 insertions, 8 deletions
diff --git a/src/core/AssetManager.cpp b/src/core/AssetManager.cpp
new file mode 100644
index 0000000..9913c18
--- /dev/null
+++ b/src/core/AssetManager.cpp
@@ -0,0 +1,70 @@
+#include <easylogging++.h>
+#include <nlohmann/json.hpp>
+#include "AssetManager.hpp"
+
+namespace fs = std::experimental::filesystem;
+
+const fs::path pathToAssets = "./assets/";
+const fs::path pathToAssetsList = "./items.json";
+const fs::path pathToTextureIndex = "./textures.json";
+
+AssetManager::AssetManager() {
+ for (auto &it:fs::recursive_directory_iterator(pathToAssets)) {
+
+ }
+ LoadIds();
+ LoadTextureResources();
+}
+
+void AssetManager::LoadIds() {
+ std::ifstream in(pathToAssetsList);
+ nlohmann::json index;
+ in >> index;
+ for (auto &it:index) {
+ int id = it["type"].get<int>();
+ int state = it["meta"].get<int>();
+ std::string blockName = it["text_type"].get<std::string>();
+ assetIds[blockName] = Block(id, state, 0);
+ }
+ LOG(INFO) << "Loaded " << assetIds.size() << " ids";
+}
+
+AssetManager::~AssetManager() {
+ delete textureAtlas;
+}
+
+//TODO: This function must be replaced with runtime texture atlas generating
+void AssetManager::LoadTextureResources() {
+ std::ifstream in(pathToTextureIndex);
+ nlohmann::json index;
+ in >> index;
+ std::string filename = index["meta"]["image"].get<std::string>();
+ for (auto &it:index["frames"]) {
+ auto frame = it["frame"];
+ TextureCoord coord;
+ coord.x = frame["x"].get<int>();
+ coord.y = frame["y"].get<int>();
+ coord.w = frame["w"].get<int>();
+ coord.h = frame["h"].get<int>();
+ std::string assetName = it["filename"].get<std::string>();
+ assetName.insert(0,"minecraft/textures/");
+ assetName.erase(assetName.length()-4);
+ LOG(ERROR)<<assetName;
+ assetTextures[assetName]=coord;
+ }
+
+ textureAtlas = new Texture(filename);
+ LOG(INFO) << "Texture atlas id is " << textureAtlas->texture;
+}
+
+TextureCoord AssetManager::GetTextureByAssetName(std::string AssetName) {
+ return TextureCoord{0, 0, 0, 0};
+}
+
+std::string AssetManager::GetTextureAssetNameByBlockId(unsigned short BlockId, unsigned char BlockSide) {
+
+}
+
+const GLuint AssetManager::GetTextureAtlas() {
+ return textureAtlas->texture;
+}
diff --git a/src/core/AssetManager.hpp b/src/core/AssetManager.hpp
new file mode 100644
index 0000000..23b2ba6
--- /dev/null
+++ b/src/core/AssetManager.hpp
@@ -0,0 +1,33 @@
+#pragma once
+
+#include <experimental/filesystem>
+#include <map>
+#include <GL/glew.h>
+#include <glm/vec4.hpp>
+#include <nlohmann/json.hpp>
+#include "../world/Block.hpp"
+#include "../graphics/Texture.hpp"
+
+struct TextureCoord{
+ unsigned int x,y,w,h;
+};
+
+class AssetManager {
+ Texture *textureAtlas;
+ std::map<std::string,Block> assetIds;
+ std::map<std::string,TextureCoord> assetTextures;
+public:
+ AssetManager();
+
+ ~AssetManager();
+
+ void LoadTextureResources();
+
+ TextureCoord GetTextureByAssetName(std::string AssetName);
+
+ std::string GetTextureAssetNameByBlockId(unsigned short BlockId, unsigned char BlockSide = 0);
+
+ const GLuint GetTextureAtlas();
+
+ void LoadIds();
+};
diff --git a/src/core/Core.cpp b/src/core/Core.cpp
index 54a16a4..7814c32 100644
--- a/src/core/Core.cpp
+++ b/src/core/Core.cpp
@@ -108,6 +108,7 @@ Core::Core() {
gameState = new GameState(client);
std::thread loop = std::thread(&Core::UpdateGameState,this);
std::swap(loop,gameStateLoopThread);
+ assetManager = new AssetManager;
LOG(INFO) << "Core is initialized";
}
@@ -291,13 +292,8 @@ void Core::RenderWorld(World &Target) {
glUniformMatrix4fv(modelLoc, 1, GL_FALSE, glm::value_ptr(model));
glUniform1i(blockLoc, block.id);
- std::string textureName = AssetManager::GetAssetNameByBlockId(block.id);
- if (textureName.find("air") != std::string::npos)
- continue;
- Texture &texture1 = *(AssetManager::GetAsset(textureName).data.texture);
-
glActiveTexture(GL_TEXTURE0);
- glBindTexture(GL_TEXTURE_2D, texture1.texture);
+ //glBindTexture(GL_TEXTURE_2D, texture1.texture);
glUniform1i(glGetUniformLocation(shader->Program, "blockTexture"), 0);
glDrawArrays(GL_TRIANGLES, 0, 36);
@@ -316,7 +312,6 @@ void Core::SetMouseCapture(bool IsCaptured) {
}
void Core::PrepareToWorldRendering() {
-
glGenBuffers(1, &VBO);
glGenBuffers(1, &VBO2);
glGenVertexArrays(1, &VAO);
diff --git a/src/core/Core.hpp b/src/core/Core.hpp
index a877613..1c2bbc5 100644
--- a/src/core/Core.hpp
+++ b/src/core/Core.hpp
@@ -10,12 +10,13 @@
#include "../gui/Gui.hpp"
#include "../graphics/Camera3D.hpp"
#include "../graphics/Shader.hpp"
-#include "../graphics/AssetManager.hpp"
+#include "AssetManager.hpp"
class Core {
GameState *gameState;
NetworkClient *client;
sf::Window *window;
+ AssetManager *assetManager;
bool isMouseCaptured = false, isRunning = true;
enum {
MainMenu,