Skip to content

Instantly share code, notes, and snippets.

@jeremyfromearth
Last active April 2, 2016 00:00
Show Gist options
  • Save jeremyfromearth/67961348ab40fd77af3c07711ea7ad80 to your computer and use it in GitHub Desktop.
Save jeremyfromearth/67961348ab40fd77af3c07711ea7ad80 to your computer and use it in GitHub Desktop.
Make assets from your Cinder project accessible to html rendered in Awesomium
#include "cinder/app/App.h"
#include "AssetDataSource.h"
using namespace ci;
using namespace ci::app;
void AssetDataSource::OnRequest(int request_id, const WebString & path) {
// load the asset the Cinder way and convert it to a buffer
auto asset = loadAsset(toString(path));
auto buffer = asset->getBuffer();
// send the asset as a char string
SendResponse(request_id,
buffer->getSize(),
static_cast<unsigned char *>(buffer->getData()),
WSLit("text/data"));
}
#pragma once
#include "CinderAwesomium.h"
class AssetDataSourceImlp;
class AssetDataSource : public Awesomium::DataSource {
public:
AssetDataSource(){};
virtual ~AssetDataSource(){};
virtual void OnRequest(int request_id, const Awesomium::WebString & path);
};
using namespace Awesomium;
WebCore * core = WebCore::Initialize(WebConfig());
WebSession * session = core->CreateWebSession(WSLit(""), WebPreferences());
session->AddDataSource(WSLit("cinder-app"), new AssetDataSource());
WebView webView = core->CreateWebView(960, 540, session);
std::string html = loadString(loadFile(getAssetPath("main.html")));
std::string path("data:text/html," + html);
webView->LoadURL(path);
<!DOCTYPE html>
<html lang="en" xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta charset="utf-8" />
<title></title>
<style>
@font-face {
font-family: SomeFont;
src: url("asset://cinder-app/SomeFont.otf");
}
</style>
</head>
<body>
<img src="asset://cinder-app/some-image.jpg"
</body>
</html>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment