Last active
March 16, 2016 21:00
-
-
Save pabloko/8d23d465b7ecba55a2ec to your computer and use it in GitHub Desktop.
Awesomium on Direct3D9 C++ class
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "main.h" | |
#include "WebBrowser.h" | |
#include <direct.h> | |
CWebBrowser::CWebBrowser() | |
{ | |
script = NULL; | |
webCore = NULL; | |
webView = NULL; | |
tWebPNG = NULL; | |
sWebPNG = NULL; | |
surface = NULL; | |
w_width = 0; | |
w_height = 0; | |
char cCurrentPath[FILENAME_MAX]; | |
_getcwd(cCurrentPath, sizeof(cCurrentPath)); | |
cCurrentPath[sizeof(cCurrentPath) - 1] = '\0'; | |
sprintf(szUrl, "file:///%s\\ui\\index.html", cCurrentPath); | |
//sprintf(szUrl, "http://google.es", cCurrentPath); | |
} | |
CWebBrowser::~CWebBrowser() | |
{ | |
SAFE_RELEASE(tWebPNG); | |
SAFE_RELEASE(sWebPNG); | |
if (webCore != NULL) webCore->Shutdown(); | |
} | |
bool CWebBrowser::CreateInstance(int width, int height) | |
{ | |
if (width == 0 || height == 0) return false; | |
w_width = width; | |
w_height = height; | |
if (webCore == NULL) | |
webCore = Awesomium::WebCore::Initialize(Awesomium::WebConfig()); | |
if (webCore == NULL) | |
if (webView == NULL) | |
webView = webCore->CreateWebView(width, height); | |
else return false; | |
if (script == NULL) { | |
script = new CWebScript(webView); | |
script->Bind(); | |
webView->set_js_method_handler(&method_dispatcher_); | |
webView->SetTransparent(true); | |
webView->LoadURL((const Awesomium::WebURL)Awesomium::WSLit(szUrl)); | |
} | |
webCore->Update(); | |
return true; | |
} | |
JSValue CWebBrowser::ExecuteScript(char* scr) | |
{ | |
if (webView) | |
return webView->ExecuteJavascriptWithResult(Awesomium::WSLit(scr), Awesomium::WSLit("")); | |
else return JSValue(NULL); | |
} | |
void CWebBrowser::TextureInit(IDirect3DDevice9* g_D3DDev) | |
{ | |
if (webView) { | |
if (tWebPNG != NULL) | |
SAFE_RELEASE(tWebPNG); | |
if (sWebPNG != NULL) | |
SAFE_RELEASE(sWebPNG); | |
tWebPNG = 0; | |
sWebPNG = 0; | |
D3DXCreateTexture(g_D3DDev, w_width, w_height, 1, D3DUSAGE_DYNAMIC, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, &tWebPNG); | |
D3DXCreateSprite(g_D3DDev, &sWebPNG); | |
surface = (Awesomium::BitmapSurface*)webView->surface(); | |
} | |
} | |
void CWebBrowser::TextureInvalidate() | |
{ | |
if (tWebPNG != NULL) | |
SAFE_RELEASE(tWebPNG); | |
if (sWebPNG != NULL) | |
SAFE_RELEASE(sWebPNG); | |
tWebPNG = 0; | |
sWebPNG = 0; | |
} | |
void CWebBrowser::Render() | |
{ | |
if (webCore && webView) { | |
webCore->Update(); | |
if (surface != 0) { | |
if (surface->is_dirty()) { | |
D3DLOCKED_RECT lockedRect; | |
HRESULT lock = tWebPNG->LockRect(0, &lockedRect, NULL, D3DLOCK_DISCARD | D3DLOCK_DONOTWAIT); | |
if (SUCCEEDED(lock)) | |
{ | |
Awesomium::Rect rect; | |
unsigned char* destBuffer = static_cast<unsigned char*>(lockedRect.pBits); | |
surface->CopyTo(destBuffer, lockedRect.Pitch, (int)4, false, false); | |
tWebPNG->UnlockRect(0); | |
} | |
} | |
D3DXMATRIX mat2; | |
D3DXVECTOR2 axisWeb = D3DXVECTOR2(0.0f, 0.0f); | |
if ((sWebPNG) && (tWebPNG)) | |
{ | |
D3DXMatrixTransformation2D(&mat2, NULL, 0.0f, NULL, NULL, 0.0f, &axisWeb); | |
sWebPNG->Begin(D3DXSPRITE_ALPHABLEND); | |
sWebPNG->SetTransform(&mat2); | |
sWebPNG->Draw(tWebPNG, NULL, NULL, NULL, 0xFFFFFFFF); | |
sWebPNG->End(); | |
} | |
} | |
} | |
} | |
bool CWebBrowser::ProcessControl(HWND wnd, UINT umsg, WPARAM wparam, LPARAM lparam) | |
{ | |
if (webView) { | |
Awesomium::WebKeyboardEvent wke(umsg, wparam, lparam); | |
webView->InjectKeyboardEvent(wke); | |
switch (umsg) | |
{ | |
case WM_LBUTTONDOWN: | |
case WM_LBUTTONUP: | |
{ | |
if (umsg == WM_LBUTTONDOWN) | |
webView->InjectMouseDown(Awesomium::MouseButton::kMouseButton_Left); | |
else | |
webView->InjectMouseUp(Awesomium::MouseButton::kMouseButton_Left); | |
} | |
break; | |
case WM_RBUTTONDOWN: | |
case WM_RBUTTONUP: | |
{ | |
if (umsg == WM_RBUTTONDOWN) | |
webView->InjectMouseDown(Awesomium::MouseButton::kMouseButton_Right); | |
else | |
webView->InjectMouseUp(Awesomium::MouseButton::kMouseButton_Right); | |
} | |
break; | |
case WM_MBUTTONDOWN: | |
case WM_MBUTTONUP: | |
{ | |
if (umsg == WM_MBUTTONDOWN) | |
webView->InjectMouseDown(Awesomium::MouseButton::kMouseButton_Middle); | |
else | |
webView->InjectMouseUp(Awesomium::MouseButton::kMouseButton_Middle); | |
} | |
break; | |
case WM_MOUSEWHEEL: | |
{ | |
int zDelta = GET_WHEEL_DELTA_WPARAM(wparam); | |
webView->InjectMouseWheel(zDelta, 0); | |
} | |
break; | |
case WM_MOUSEMOVE: | |
{ | |
POINT cursor_pos; | |
GetCursorPos(&cursor_pos); | |
ScreenToClient(wnd, &cursor_pos); | |
webView->InjectMouseMove(cursor_pos.x, cursor_pos.y); | |
} | |
break; | |
} | |
} | |
return false; | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Awesomium/WebCore.h> | |
#include <Awesomium/BitmapSurface.h> | |
#include <Awesomium/STLHelpers.h> | |
#include <Awesomium/WebKeyboardEvent.h> | |
#include "WebScript.h" | |
#include "method_dispatcher.h" | |
class CWebBrowser | |
{ | |
private: | |
Awesomium::WebCore* webCore; | |
Awesomium::WebView* webView; | |
IDirect3DTexture9* tWebPNG; | |
ID3DXSprite* sWebPNG; | |
int w_width , w_height; | |
char szUrl[MAX_PATH]; | |
CWebScript* script; | |
Awesomium::BitmapSurface* surface; | |
public: | |
CWebBrowser(); | |
~CWebBrowser(); | |
bool CreateInstance(int width, int height); | |
void TextureInit(IDirect3DDevice9* g_D3DDev); | |
void TextureInvalidate(); | |
void Render(); | |
bool ProcessControl(HWND wnd, UINT umsg, WPARAM wparam, LPARAM lparam); | |
Awesomium::JSValue ExecuteScript(char* scr); | |
}; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include "main.h" | |
CWebScript::CWebScript(WebView* wb) | |
{ | |
webView = wb; | |
} | |
CWebScript::~CWebScript() | |
{ | |
} | |
void CWebScript::Bind() { | |
if (webView) { | |
Awesomium::JSValue result = webView->CreateGlobalJavascriptObject(Awesomium::WSLit("app")); | |
if (result.IsObject()) { | |
Awesomium::JSObject& js_object = result.ToObject(); | |
// Add references as you need | |
method_dispatcher_.BindWithRetval(js_object, Awesomium::WSLit("MyCallback"), JSDelegateWithRetval(this, &CWebScript::MyCallback)); | |
// | |
} | |
} | |
} | |
JSValue CWebScript::MyCallback(WebView* caller, const JSArray& args) { | |
char sztxt[100]; | |
args[0].ToString().ToUTF8(sztxt, args[0].ToString().length()); | |
sztxt[args[0].ToString().length()] = '\0'; | |
say(sztxt); | |
return JSArray(0); | |
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#pragma once | |
#include <Awesomium/WebCore.h> | |
#include <Awesomium/BitmapSurface.h> | |
#include <Awesomium/STLHelpers.h> | |
#include <Awesomium/WebKeyboardEvent.h> | |
#include "method_dispatcher.h" | |
class CWebScript | |
{ | |
private: | |
Awesomium::WebView* webView; | |
public: | |
CWebScript(Awesomium::WebView* wb); | |
~CWebScript(); | |
void Bind(); | |
//Impl | |
Awesomium::JSValue MyCallback(Awesomium::WebView* caller, const Awesomium::JSArray& args); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment