Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add emscripten support #509

Open
wants to merge 3 commits into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions all/native/graphics/BitmapCanvas.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,9 @@
#elif defined(__ANDROID__)
#define CARTO_BITMAP_CANVAS_IMPL AndroidImpl
#include "graphics/BitmapCanvasAndroidImpl.h"
#elif defined(__EMSCRIPTEN__)
#define CARTO_BITMAP_CANVAS_IMPL EmscriptenImpl
#include "graphics/BitmapCanvasEmscriptenImpl.h"
#else
#error "Unsupported platform"
#endif
Expand Down
1 change: 1 addition & 0 deletions all/native/graphics/BitmapCanvas.h
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ namespace carto {
class AndroidImpl;
class IOSImpl;
class UWPImpl;
class EmscriptenImpl;

std::unique_ptr<Impl> _impl;
};
Expand Down
3 changes: 3 additions & 0 deletions all/native/network/HTTPClient.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
#elif defined(__ANDROID__)
#define CARTO_HTTP_SOCKET_IMPL AndroidImpl
#include "network/HTTPClientAndroidImpl.h"
#elif defined(__EMSCRIPTEN__)
#define CARTO_HTTP_SOCKET_IMPL EmscriptenImpl
#include "network/HTTPClientEmscriptenImpl.h"
#else
#define CARTO_HTTP_SOCKET_IMPL PionImpl
#include "HTTPClientPionImpl.h"
Expand Down
1 change: 1 addition & 0 deletions all/native/network/HTTPClient.h
Original file line number Diff line number Diff line change
Expand Up @@ -78,6 +78,7 @@ namespace carto {
class AndroidImpl;
class IOSImpl;
class WinSockImpl;
class EmscriptenImpl;

int makeRequest(Request request, Response& response, HandlerFunc handlerFn, std::uint64_t offset) const;

Expand Down
9 changes: 9 additions & 0 deletions all/native/utils/Log.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,15 @@ namespace carto {
OutputDebugStringA("\n");
}
#endif
#ifdef __EMSCRIPTEN__
enum LogType { LOG_TYPE_FATAL = 0, LOG_TYPE_ERROR = 1, LOG_TYPE_WARNING = 2, LOG_TYPE_INFO = 3, LOG_TYPE_DEBUG = 4 };

static void OutputLog(LogType logType, const std::string& tag, const char* text) {
if ((int)logType <= 1) {
printf("LogType: %d, tag: %s, message: %s\n", logType, tag.c_str(), text);
bugra9 marked this conversation as resolved.
Show resolved Hide resolved
}
}
#endif

bool Log::IsShowError() {
std::lock_guard<std::mutex> lock(_Mutex);
Expand Down
3 changes: 2 additions & 1 deletion all/native/utils/PlatformUtils.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,8 @@ namespace carto {
PLATFORM_TYPE_WINDOWS,
PLATFORM_TYPE_WINDOWS_PHONE,
PLATFORM_TYPE_XAMARIN_IOS,
PLATFORM_TYPE_XAMARIN_ANDROID
PLATFORM_TYPE_XAMARIN_ANDROID,
PLATFORM_TYPE_WEB
};
}

Expand Down
29 changes: 29 additions & 0 deletions emscripten/modules/ui/MapView.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#ifndef _MAPVIEW_I
#define _MAPVIEW_I

%module MAPVIEW

!proxy_imports(carto::MapView, ui.MapView)

%{
#include "ui/MapView.h"
#include "ui/BaseMapView.h"
#include "ui/MapEventListener.h"
#include "ui/MapRedrawRequestListener.h"
#include "ui/EmscriptenInput.h"
#include "components/Options.h"
#include "components/Layers.h"
#include "core/MapBounds.h"
#include "core/MapPos.h"
#include "core/MapVec.h"
#include "core/ScreenPos.h"
#include "core/ScreenBounds.h"
#include "utils/Const.h"
#include "renderers/MapRenderer.h"
%}

%include <std_string.i>

%include "ui/MapView.h"

#endif
18 changes: 18 additions & 0 deletions emscripten/modules/utils/AssetUtils.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
#ifndef _ASSETUTILS_I
#define _ASSETUTILS_I

%module AssetUtils

!proxy_imports(carto::AssetUtils, core.BinaryData)

%{
#include "utils/AssetUtils.h"
%}

%include <std_string.i>

%import "core/BinaryData.i"

%include "utils/AssetUtils.h"

#endif
24 changes: 24 additions & 0 deletions emscripten/modules/utils/BitmapUtils.i
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
#ifndef _BITMAPUTILS_I
#define _BITMAPUTILS_I

%module BitmapUtils

!proxy_imports(carto::BitmapUtils, graphics.Bitmap)

%{
#include "utils/BitmapUtils.h"
#include "components/Exceptions.h"
%}

%include <std_shared_ptr.i>
%include <std_string.i>
%include <cartoswig.i>

%import "graphics/Bitmap.i"

%std_exceptions(carto::BitmapUtils::CreateBitmapFromUIImage)
%std_exceptions(carto::BitmapUtils::CreateUIImageFromBitmap)

%include "utils/BitmapUtils.h"

#endif
9 changes: 9 additions & 0 deletions emscripten/native/components/Task.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
#include "components/Task.h"

namespace carto {

void Task::operator()() {
run();
}

}
63 changes: 63 additions & 0 deletions emscripten/native/graphics/BitmapCanvasEmscriptenImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
#include "graphics/BitmapCanvasEmscriptenImpl.h"
#include "components/Exceptions.h"
#include "utils/BitmapUtils.h"

#include <cmath>

namespace carto {
BitmapCanvas::EmscriptenImpl::EmscriptenImpl(int width, int height)
{

bugra9 marked this conversation as resolved.
Show resolved Hide resolved
}

BitmapCanvas::EmscriptenImpl::~EmscriptenImpl() {
}

void BitmapCanvas::EmscriptenImpl::setDrawMode(DrawMode mode) {

}

void BitmapCanvas::EmscriptenImpl::setColor(const Color& color) {

}

void BitmapCanvas::EmscriptenImpl::setStrokeWidth(float width) {

}

void BitmapCanvas::EmscriptenImpl::setFont(const std::string& name, float size) {

}

void BitmapCanvas::EmscriptenImpl::pushClipRect(const ScreenBounds& clipRect) {

}

void BitmapCanvas::EmscriptenImpl::popClipRect() {

}

void BitmapCanvas::EmscriptenImpl::drawText(std::string text, const ScreenPos& pos, int maxWidth, bool breakLines) {

}

void BitmapCanvas::EmscriptenImpl::drawRoundRect(const ScreenBounds& rect, float radius) {

}

void BitmapCanvas::EmscriptenImpl::drawPolygon(const std::vector<ScreenPos>& poses) {

}

void BitmapCanvas::EmscriptenImpl::drawBitmap(const ScreenBounds& rect, const std::shared_ptr<Bitmap>& bitmap) {

}

ScreenBounds BitmapCanvas::EmscriptenImpl::measureTextSize(std::string text, int maxWidth, bool breakLines) const {
return ScreenBounds(ScreenPos(0, 0), ScreenPos(0, 0));
}

std::shared_ptr<Bitmap> BitmapCanvas::EmscriptenImpl::buildBitmap() const {
return std::shared_ptr<Bitmap>();
}
}
33 changes: 33 additions & 0 deletions emscripten/native/graphics/BitmapCanvasEmscriptenImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
#ifndef _CARTO_BITMAPCANVASEMSCRIPTENIMPL_H_
#define _CARTO_BITMAPCANVASEMSCRIPTENIMPL_H_

#include "graphics/BitmapCanvas.h"

namespace carto {

class BitmapCanvas::EmscriptenImpl : public BitmapCanvas::Impl {
public:
EmscriptenImpl(int width, int height);
virtual ~EmscriptenImpl();

virtual void setDrawMode(DrawMode mode);
virtual void setColor(const Color& color);
virtual void setStrokeWidth(float width);
virtual void setFont(const std::string& name, float size);

virtual void pushClipRect(const ScreenBounds& clipRect);
virtual void popClipRect();

virtual void drawText(std::string text, const ScreenPos& pos, int maxWidth, bool breakLines);
virtual void drawPolygon(const std::vector<ScreenPos>& poses);
virtual void drawRoundRect(const ScreenBounds& rect, float radius);
virtual void drawBitmap(const ScreenBounds& rect, const std::shared_ptr<Bitmap>& bitmap);

virtual ScreenBounds measureTextSize(std::string text, int maxWidth, bool breakLines) const;

virtual std::shared_ptr<Bitmap> buildBitmap() const;
};

}

#endif
45 changes: 45 additions & 0 deletions emscripten/native/network/HTTPClientEmscriptenImpl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
#include "HTTPClientEmscriptenImpl.h"

#include <stdio.h>
#include <string.h>
#include <math.h>
#include <iostream>
#include <emscripten/fetch.h>

namespace carto {
HTTPClient::EmscriptenImpl::EmscriptenImpl(bool log) :
_log(log),
_timeout(-1)
{
}

void HTTPClient::EmscriptenImpl::setTimeout(int milliseconds) {
_timeout = milliseconds;
}

bool HTTPClient::EmscriptenImpl::makeRequest(const HTTPClient::Request& request, HeadersFunc headersFn, DataFunc dataFn) const {
emscripten_fetch_attr_t attr;
emscripten_fetch_attr_init(&attr);
strcpy(attr.requestMethod, "GET");
bugra9 marked this conversation as resolved.
Show resolved Hide resolved
attr.attributes = EMSCRIPTEN_FETCH_LOAD_TO_MEMORY | EMSCRIPTEN_FETCH_SYNCHRONOUS;
attr.timeoutMSecs = _timeout;
emscripten_fetch_t *fetch = emscripten_fetch(&attr, request.url.c_str());

bool cancel = false;
std::map<std::string, std::string> headers;

if (!headersFn(fetch->status, headers)) {
cancel = true;
}

if (!cancel) {
if(!dataFn((const unsigned char *)fetch->data, fetch->numBytes)) {
cancel = true;
}
}

emscripten_fetch_close(fetch);

return !cancel;
}
}
21 changes: 21 additions & 0 deletions emscripten/native/network/HTTPClientEmscriptenImpl.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
#ifndef _CARTO_HTTPCLIENTEMSCRIPTENImpl_H_
#define _CARTO_HTTPCLIENTEMSCRIPTENImpl_H_

#include "network/HTTPClient.h"

namespace carto {
class HTTPClient::EmscriptenImpl : public HTTPClient::Impl {
public:
explicit EmscriptenImpl(bool log);

virtual void setTimeout(int milliseconds);
virtual bool makeRequest(const HTTPClient::Request& request, HeadersFunc headersFn, DataFunc dataFn) const;

private:
bool _log;
int _timeout;
};

}

#endif
Loading