use filesource object for loading all http stuff

This commit is contained in:
Konstantin Käfer
2014-08-08 15:08:41 +02:00
parent 70a31baf5c
commit a045d9c9d4
9 changed files with 117 additions and 30 deletions

View File

@@ -5,6 +5,7 @@
#include <string>
#include <mbgl/platform/platform.hpp>
#include <mbgl/util/filesource.hpp>
#include <mbgl/util/uv.hpp>
#include <mbgl/util/std.hpp>
@@ -20,9 +21,9 @@ SpritePosition::SpritePosition(uint16_t x, uint16_t y, uint16_t width, uint16_t
pixelRatio(pixelRatio) {
}
std::shared_ptr<Sprite> Sprite::Create(const std::string& base_url, float pixelRatio) {
std::shared_ptr<Sprite> Sprite::Create(const std::string& base_url, float pixelRatio, const std::shared_ptr<FileSource> &fileSource) {
std::shared_ptr<Sprite> sprite(std::make_shared<Sprite>(Key(), base_url, pixelRatio));
sprite->load();
sprite->load(fileSource);
return sprite;
}
@@ -49,7 +50,7 @@ Sprite::operator bool() const {
// Note: This is a separate function that must be called exactly once after creation
// The reason this isn't part of the constructor is that calling shared_from_this() in
// the constructor fails.
void Sprite::load() {
void Sprite::load(const std::shared_ptr<FileSource> &fileSource) {
if (!valid) {
// Treat a non-existent sprite as a successfully loaded empty sprite.
loadedImage = true;
@@ -60,7 +61,7 @@ void Sprite::load() {
std::shared_ptr<Sprite> sprite = shared_from_this();
platform::request_http(jsonURL, [sprite](platform::Response *res) {
fileSource->load(ResourceType::JSON, jsonURL, [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->body.swap(res->body);
sprite->parseJSON();
@@ -73,7 +74,7 @@ void Sprite::load() {
}
});
platform::request_http(spriteURL, [sprite](platform::Response *res) {
fileSource->load(ResourceType::Image, spriteURL, [sprite](platform::Response *res) {
if (res->code == 200) {
sprite->image.swap(res->body);
sprite->parseImage();