add rapidjson for parsing sprite json files

This commit is contained in:
Konstantin Käfer
2014-02-03 11:36:34 +01:00
parent c7b012ba42
commit 202e0270f7
22 changed files with 2868 additions and 17 deletions

View File

@@ -17,6 +17,7 @@ SET(llmr_SOURCES
renderer/line_bucket.cpp
shader/shaders.cpp
style/style.cpp
style/sprite.cpp
style/resources.cpp
style/value.cpp
style/bucket_description.cpp

View File

@@ -3,6 +3,7 @@
//#include <llmr/util/vec2.hpp>
#include <llmr/style/resources.hpp>
#include <llmr/style/sprite.hpp>
#include <iostream>
#include <thread>
@@ -19,6 +20,7 @@ Map::Map(Settings& settings)
painter(transform, settings, style),
min_zoom(0),
max_zoom(14) {
loadSprite("http://localhost:3333/gl/debug/img/sprite.json");
}
Map::~Map() {
@@ -28,6 +30,17 @@ void Map::setup() {
painter.setup();
style.load(resources::style, resources::style_size);
// style.loadJSON((const char *)resources::style, resources::style_size);
}
void Map::loadSprite(const std::string& url) {
platform::request_http(url, [](const platform::Response& res) {
if (res.code == 200) {
createTexture(res.body);
} else {
fprintf(stderr, "failed to load sprite\n");
}
});
}
void Map::loadStyle(const uint8_t *const data, uint32_t bytes) {

View File

@@ -74,9 +74,9 @@ void Tile::request() {
// Note: Somehow this feels slower than the change to request_http()
std::shared_ptr<Tile> tile = shared_from_this();
platform::request_http(url, [=](const platform::Response& res) {
platform::request_http(url, [=](platform::Response& res) {
if (res.code == 200) {
tile->data.assign(res.body, res.body + res.length);
tile->data.swap(res.body);
platform::async([tile]() {
tile->parse();

93
src/style/sprite.cpp Normal file
View File

@@ -0,0 +1,93 @@
#include <llmr/style/sprite.hpp>
#include <string>
#include <rapidjson/document.h>
// #include <rapidjson/rapidjson.h>
using namespace llmr;
Sprite::Sprite(uint16_t x, uint16_t y, uint16_t width, uint16_t height, uint8_t pixelRatio)
: x(x),
y(y),
width(width),
height(height),
pixelRatio(pixelRatio) {}
Texture llmr::createTexture(const std::string& data) {
Texture texture;
rapidjson::Document d;
d.Parse<0>(data.c_str());
if (d.IsObject()) {
for (rapidjson::Value::ConstMemberIterator itr = d.MemberBegin(); itr != d.MemberEnd(); ++itr) {
const std::string& name = itr->name.GetString();
const rapidjson::Value& value = itr->value;
if (value.IsObject()) {
uint16_t x = 0;
uint16_t y = 0;
uint16_t width = 0;
uint16_t height = 0;
uint8_t pixelRatio = 1;
if (value.HasMember("x")) x = value["x"].GetInt();
if (value.HasMember("y")) y = value["y"].GetInt();
if (value.HasMember("width")) width = value["width"].GetInt();
if (value.HasMember("height")) height = value["height"].GetInt();
if (value.HasMember("pixelRatio")) pixelRatio = value["height"].GetInt();
texture.insert({ name, { x, y, width, height, pixelRatio } });
}
}
}
return texture;
}
// Texture llmr::createTexture(const char *data, size_t length) {
// Texture texture;
// json_t *root;
// json_error_t error;
// root = json_loadb(data, length, 0, &error);
// // TODO: handle error
// if (json_is_object(root)) {
// const char *key;
// json_t *value;
// json_object_foreach(root, key, value) {
// if (json_is_object(value)) {
// uint16_t x = 0;
// uint16_t y = 0;
// uint16_t width = 0;
// uint16_t height = 0;
// uint8_t pixelRatio = 1;
// json_t *property;
// if ((property = json_object_get(value, "x"))) x = json_integer_value(property);
// if ((property = json_object_get(value, "y"))) y = json_integer_value(property);
// if ((property = json_object_get(value, "width"))) width = json_integer_value(property);
// if ((property = json_object_get(value, "height"))) height = json_integer_value(property);
// if ((property = json_object_get(value, "pixelRatio"))) pixelRatio = json_integer_value(property);
// fprintf(stderr, "%s: %d %d | %d %d | %d\n", key, x, y, width, height, pixelRatio);
// texture.insert({ key, { x, y, width, height, pixelRatio } });
// } else {
// fprintf(stderr, "unknown value for key %s\n", key);
// }
// }
// } else {
// fprintf(stderr, "root element should be an object\n");
// }
// json_decref(root);
// return texture;
// }

View File

@@ -1,5 +1,6 @@
#include <llmr/style/style.hpp>
using namespace llmr;
Style::Style() {