kick
|
00001 // 00002 // Created by morten on 30/06/14. 00003 // 00004 00005 00006 #include "kick/texture/texture_atlas.h" 00007 #include <rapidjson/document.h> 00008 #include "kick/core/project.h" 00009 #include "kick/material/shader.h" 00010 00011 00012 00013 using namespace std; 00014 using namespace glm; 00015 00016 namespace kick { 00017 00018 namespace { 00019 ivec2 whToVec2(rapidjson::Value &val) { 00020 return {val["w"].GetInt(), val["h"].GetInt()}; 00021 } 00022 00023 ivec4 xywhToVec4(rapidjson::Value &val) { 00024 return {val["x"].GetInt(), val["y"].GetInt(), val["w"].GetInt(), val["h"].GetInt()}; 00025 } 00026 } 00027 00028 TextureAtlas::TextureAtlas() 00029 { 00030 mShader = Project::loadShader("assets/shaders/sprite.shader"); 00031 } 00032 00033 bool TextureAtlas::load(std::string filename, std::string texture) { 00034 this->mTexture = Project::loadTexture2D(texture); 00035 string textureAtlas; 00036 if (!Project::loadTextResource(filename, textureAtlas)){ 00037 return false; 00038 } 00039 00040 mAtlas.clear(); 00041 00042 rapidjson::Document d; 00043 d.Parse<0>(textureAtlas.c_str()); 00044 auto &frames = d["frames"]; 00045 for (auto itr = frames.MemberBegin(); itr != frames.MemberEnd(); ++itr){ 00046 TextureAtlasEntry entry; 00047 auto& val = itr->value; 00048 entry.rotated = val["rotated"].GetBool(); 00049 entry.trimmed = val["trimmed"].GetBool(); 00050 entry.frame = xywhToVec4(val["frame"]); 00051 entry.spriteSourceSize = xywhToVec4(val["spriteSourceSize"]); 00052 entry.sourceSize = whToVec2(val["sourceSize"]); 00053 mAtlas[itr->name.GetString()] = entry; 00054 } 00055 auto &meta = d["meta"]; 00056 mTextureSize = whToVec2(meta["size"]); 00057 return true; 00058 } 00059 00060 shared_ptr<Texture2D> TextureAtlas::texture() const { 00061 return mTexture; 00062 } 00063 00064 TextureAtlasEntry TextureAtlas::get(std::string name) { 00065 return mAtlas[name]; 00066 } 00067 00068 glm::ivec2 TextureAtlas::textureSize() const { 00069 return mTextureSize; 00070 } 00071 00072 std::shared_ptr<Shader> TextureAtlas::shader() const { 00073 return mShader; 00074 } 00075 00076 void TextureAtlas::setShader(std::shared_ptr<Shader> shader) { 00077 TextureAtlas::mShader = shader; 00078 } 00079 }