From 136e54a332da44772835ab57c19dd36630a36688 Mon Sep 17 00:00:00 2001 From: ExMix Date: Tue, 18 Mar 2014 16:55:28 +0300 Subject: [PATCH] [drape] load symbols skin --- drape/drape_common.pri | 6 +- drape/glfunctions.cpp | 4 +- drape/glfunctions.hpp | 2 +- drape/symbols_texture.cpp | 25 ++++++++ drape/symbols_texture.hpp | 17 ++++++ drape/texture.cpp | 2 +- drape/texture_structure_desc.cpp | 97 ++++++++++++++++++++++++++++++++ drape/texture_structure_desc.hpp | 17 ++++++ 8 files changed, 164 insertions(+), 6 deletions(-) create mode 100644 drape/symbols_texture.cpp create mode 100644 drape/symbols_texture.hpp create mode 100644 drape/texture_structure_desc.cpp create mode 100644 drape/texture_structure_desc.hpp diff --git a/drape/drape_common.pri b/drape/drape_common.pri index ead565ca88..790180b930 100644 --- a/drape/drape_common.pri +++ b/drape/drape_common.pri @@ -26,7 +26,8 @@ SOURCES += \ $$DRAPE_DIR/buffer_base.cpp \ $$DRAPE_DIR/cpu_buffer.cpp \ $$DRAPE_DIR/utils/lodepng.cpp \ - $$DRAPE_DIR/texture_structure_desc.cpp + $$DRAPE_DIR/texture_structure_desc.cpp \ + $$DRAPE_DIR/symbols_texture.cpp HEADERS += \ $$DRAPE_DIR/data_buffer.hpp \ @@ -56,4 +57,5 @@ HEADERS += \ $$DRAPE_DIR/buffer_base.hpp \ $$DRAPE_DIR/cpu_buffer.hpp \ $$DRAPE_DIR/utils/lodepng.h \ - $$DRAPE_DIR/texture_structure_desc.hpp + $$DRAPE_DIR/texture_structure_desc.hpp \ + $$DRAPE_DIR/symbols_texture.hpp diff --git a/drape/glfunctions.cpp b/drape/glfunctions.cpp index 0d164600d6..f35a90e9d5 100644 --- a/drape/glfunctions.cpp +++ b/drape/glfunctions.cpp @@ -532,9 +532,9 @@ void GLFunctions::glTexSubImage2D(int x, int y, int width, int height, glConst l GLCHECK(::glTexSubImage2D(GL_TEXTURE_2D, 0, x, y, width, height, layout, pixelType, data)); } -void GLFunctions::glTexParameter(glConst target, glConst param, glConst value) +void GLFunctions::glTexParameter(glConst param, glConst value) { - GLCHECK(::glTexParameteri(target, param, value)); + GLCHECK(::glTexParameteri(GL_TEXTURE_2D, param, value)); } void GLFunctions::glDrawElements(uint16_t indexCount) diff --git a/drape/glfunctions.hpp b/drape/glfunctions.hpp index 6ade886592..74920a797e 100644 --- a/drape/glfunctions.hpp +++ b/drape/glfunctions.hpp @@ -95,7 +95,7 @@ public: static void glBindTexture(uint32_t textureID); static void glTexImage2D(int width, int height, glConst layout, glConst pixelType, const void * data); static void glTexSubImage2D(int x, int y, int width, int height, glConst layout, glConst pixelType, const void * data); - static void glTexParameter(glConst target, glConst param, glConst value); + static void glTexParameter(glConst param, glConst value); // Draw support static void glDrawElements(uint16_t indexCount); diff --git a/drape/symbols_texture.cpp b/drape/symbols_texture.cpp new file mode 100644 index 0000000000..2c6a58b8b8 --- /dev/null +++ b/drape/symbols_texture.cpp @@ -0,0 +1,25 @@ +#include "symbols_texture.hpp" + +#include "utils/lodepng.h" + +SymbolsTexture::SymbolsTexture(const string & skinPathName) +{ + uint32_t width, height; + m_desc.Load(skinPathName + ".sdf", width, height); + + vector pngData; + unsigned w, h; + lodepng::decode(pngData, w, h, skinPathName + ".png"); + + ASSERT(width == w && height == h, ()); + Create(width, height, Texture::RGBA8, MakeStackRefPointer(&pngData[0])); +} + +m2::RectD SymbolsTexture::FindSymbol(const string & symbolName) +{ + m2::RectU r; + if (!m_desc.GetResource(symbolName, r)) + return m2::RectD(); + + return m2::RectD(GetS(r.minX()), GetT(r.minY()), GetS(r.maxX()), GetT(r.maxY())); +} diff --git a/drape/symbols_texture.hpp b/drape/symbols_texture.hpp new file mode 100644 index 0000000000..058ec25c5a --- /dev/null +++ b/drape/symbols_texture.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "texture.hpp" +#include "texture_structure_desc.hpp" + +#include "../std/string.hpp" + +class SymbolsTexture : public Texture +{ +public: + SymbolsTexture(string const & skinPathName); + + m2::RectD FindSymbol(string const & symbolName); + +private: + TextureStructureDesc m_desc; +}; diff --git a/drape/texture.cpp b/drape/texture.cpp index 553b76b198..efc0a44ad8 100644 --- a/drape/texture.cpp +++ b/drape/texture.cpp @@ -43,7 +43,7 @@ void Texture::SetFilterParams(glConst minFilter, glConst magFilter) { VERIFY_ID; GLFunctions::glTexParameter(GLConst::GLMinFilter, minFilter); - GLFunctions::glTexParameter(GLConst::GLMaxFilter, magFilter); + GLFunctions::glTexParameter(GLConst::GLMagFilter, magFilter); } void Texture::SetWrapMode(glConst sMode, glConst tMode) diff --git a/drape/texture_structure_desc.cpp b/drape/texture_structure_desc.cpp new file mode 100644 index 0000000000..2d2555338a --- /dev/null +++ b/drape/texture_structure_desc.cpp @@ -0,0 +1,97 @@ +#include "texture_structure_desc.hpp" + +#include "../platform/platform.hpp" + +#include "../coding/file_reader.hpp" +#include "../coding/reader.hpp" +#include "../coding/parse_xml.hpp" + +#include "../base/string_utils.hpp" + +namespace +{ + class SknLoad + { + public: + SknLoad(map & skn) + : m_skn(skn) + { + } + + bool Push(string const & /*element*/) { return true;} + + void Pop(string const & element) + { + if (element == "symbol") + { + ASSERT(m_isSymbol == true, ()); + ASSERT(!m_name.empty(), ()); + ASSERT(m_rect.IsValid(), ()); + m_skn.insert(make_pair(m_name, m_rect)); + + m_name = ""; + m_rect.MakeEmpty(); + } + } + + void AddAttr(string const & attribute, string const & value) + { + if (attribute == "name") + m_name = value; + else + { + int v; + if (!strings::to_int(value, v)) + return; + + if (attribute == "minX") + m_rect.setMinX(v); + else if (attribute == "minY") + m_rect.setMinY(v); + else if (attribute == "maxX") + m_rect.setMaxX(v); + else if (attribute == "maxY") + m_rect.setMaxY(v); + else if (attribute == "height") + m_height = v; + else if (attribute == "width") + m_width = v; + } + } + + void CharData(string const &) {} + + uint32_t m_width; + uint32_t m_height; + + private: + bool m_isSymbol; + + string m_name; + m2::RectU m_rect; + + map & m_skn; + }; +} + +void TextureStructureDesc::Load(const string & descFilePath, uint32_t & width, uint32_t & height) +{ + SknLoad loader(m_structure); + + ReaderSource > source(ReaderPtr(GetPlatform().GetReader(descFilePath))); + if (!ParseXML(source, loader)) + LOG(LERROR, ("Error parsing skin")); + + width = loader.m_width; + height = loader.m_height; +} + +bool TextureStructureDesc::GetResource(const string & name, m2::RectU & rect) const +{ + structure_t::const_iterator it = m_structure.find(name); + if (it == m_structure.end()) + return false; + + rect = it->second; + return true; +} diff --git a/drape/texture_structure_desc.hpp b/drape/texture_structure_desc.hpp new file mode 100644 index 0000000000..f62c4b3500 --- /dev/null +++ b/drape/texture_structure_desc.hpp @@ -0,0 +1,17 @@ +#pragma once + +#include "../geometry/rect2d.hpp" + +#include "../std/map.hpp" +#include "../std/stdint.hpp" + +class TextureStructureDesc +{ +public: + void Load(const string & descFilePath, uint32_t & width, uint32_t & height); + bool GetResource(string const & name, m2::RectU & rect) const; + +private: + typedef map structure_t; + structure_t m_structure; +};