Here it is:
- Code: Select all
#include <windows.h>
#include "Graphics.h"
#include "include/WebCore.h"
#include <iostream>
#include <fstream>
#define WIDTH 512
#define HEIGHT 512
#define URL "http://www.google.com"
static bool isRunning = true;
static Awesomium::WebView* webView = 0;
#define TEST_DIRECT3D
//Globals
//This global window handler will be used to initialize Direct3D
HWND gHwnd;
void saveImageTGA(const std::string& filename, unsigned char* buffer, int width, int height);
class MyWebViewListener : public Awesomium::WebViewListener
{
public:
MyWebViewListener()
{
}
void onBeginNavigation(const std::string& url, const std::wstring& frameName)
{
std::cout << "Navigating to URL: " << url << std::endl;
}
void onBeginLoading(const std::string& url, const std::wstring& frameName, int statusCode, const std::wstring& mimeType)
{
std::cout << "Begining to load URL: " << url;
std::cout << "\n\twith status code: " << statusCode;
std::wcout << L"\n\twith mime-type: " << mimeType << std::endl;
}
void onFinishLoading()
{
std::cout << "Finished loading the page!" << std::endl;
unsigned char* buffer = new unsigned char[WIDTH * HEIGHT * 4];
webView->render(buffer, WIDTH * 4, 4);
saveImageTGA("result.tga", buffer, WIDTH, HEIGHT);
delete buffer;
std::cout << "Saved a render of the page to 'result.tga'." << std::endl;
isRunning = false;
}
void onCallback(const std::string& name, const Awesomium::JSArguments& args)
{
}
void onReceiveTitle(const std::wstring& title, const std::wstring& frameName)
{
}
void onChangeTooltip(const std::wstring& tooltip)
{
}
#if defined(_WIN32)
void onChangeCursor(const HCURSOR& cursor)
{
}
#endif
void onChangeKeyboardFocus(bool isFocused)
{
}
void onChangeTargetURL(const std::string& url)
{
}
};
//Function declarations
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wp, LPARAM lp);
//Entry point of the application
int WINAPI WinMain(HINSTANCE instance, HINSTANCE hPreviousInstance, LPSTR command_line, int show)
{
WNDCLASS wc;
MSG msg;
wc.style = CS_HREDRAW | CS_VREDRAW; /*Class style*/
wc.lpfnWndProc = WinProc; /*A function pointer which should point to the procedure function. Remember that the procedure function handles the window messages*/
wc.cbClsExtra = 0; /*The number of extra bytes you want to allocate for this window class structure. The default value is 0*/
wc.cbWndExtra = 0; /*The number of extra bytes you want to allocate for the window instance*/
wc.hInstance = instance; /*Instance of the module associated with the window. This is the 1st paramter passed to the WinMain function*/
wc.hIcon = LoadIcon(NULL, IDI_EXCLAMATION); /*Handle to the icon class which will be displayed on the top left part of the window*/
wc.hCursor = LoadCursor(NULL, IDC_ARROW); /*Handle to the cursor class which will be used in this window class*/
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH); /*Handle to the class background brush. It can be a color value*/
wc.lpszMenuName = NULL; /*Pointer to a null terminated string which specifies the name of the menu resources used by this window class (if any)*/
wc.lpszClassName = "Window Class Name"; /*String that specifies thw window class name. This is needed to create any window based on this window class*/
RegisterClass(&wc);
//Saving a copy of the window handle. We will need it to initialize Direct3D
gHwnd = CreateWindow(wc.lpszClassName, /*The class name we chose for the window class*/
"D3D Sample", /*The window caption*/
0, /*The style of the window, which determines if the window will have a minimize/maximize buttons, if its frame is thick or not.. */
200, /*The X position of the top left corner of the window. Remember that (0,0) is the top left of the monitor*/
100, /*The Y position of the top left corner of the window. Remember that (0,0) is the top left of the monitor*/
800, /*The width of the window*/
600, /*The heiht of the window*/
NULL, /*Handle to the parent window (in case this was a child window)*/
NULL, /*Handle to a menu (In case there is a menu for this window)*/
instance, /*Instance of the module associated with the window. This is the 1st paramter passed to the WinMain function*/
NULL); /*Pointer to a value sent to the window in the WM_CREATE message*/
ShowWindow(gHwnd, show);
UpdateWindow(gHwnd);
#ifdef TEST_DIRECT3D
int GameLoop = 1;
bool draw = true;
Awesomium::WebCore* webCore = new Awesomium::WebCore();
MyWebViewListener* myListener = new MyWebViewListener();
webView = webCore->createWebView(WIDTH, HEIGHT);
GraphicsInitialize();
while(GameLoop)
{
pD3DDevice->Clear(0, 0, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0, 0, 0), 1.0f, 0);
//Inform DirectX that we are about to render some shapes
pD3DDevice->BeginScene();
webView->loadURL(URL);
webView->setListener(myListener);
while(isRunning)
{
Sleep(250);
webCore->update();
}
pD3DDevice->EndScene();
pD3DDevice->Present (0, 0, 0, 0);
if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
if(GetAsyncKeyState('Q') || GetAsyncKeyState(VK_ESCAPE) || WM_QUIT == msg.message)
GameLoop = 0;
}
#else
while(GetMessage(&msg, NULL, 0, 0))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
#endif
GraphicsFree();
UnregisterClass(wc.lpszClassName, instance);
webView->destroy();
return (int)msg.wParam;
}
LRESULT CALLBACK WinProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC dc; /* device context */
PAINTSTRUCT ps; /* the paint struct */
RECT rect;
switch (msg)
{
/* when the window is created */
case WM_CREATE:
break;
/* when the rectangle is drawn */
case WM_LBUTTONDOWN:
break;
case WM_MOUSEMOVE:
break;
case WM_PAINT:
dc = BeginPaint(hWnd, &ps);
EndPaint(hWnd, &ps);
break;
/* When it's time for the window to be closed and removed */
case WM_DESTROY:
PostQuitMessage(0);
break;
case WM_KEYDOWN:
if(wParam == VK_ESCAPE)
PostQuitMessage(0);
break;
/* called any time the window is moved */
case WM_MOVE:
/* Invalidate the rect to force a redraw */
InvalidateRect(hWnd, NULL, FALSE);
break;
default:
return DefWindowProc(hWnd, msg, wParam, lParam);
}
return 0;
}
// Templated helper utility to write binary to stream
template<class T>
void writeVal(std::ofstream& destination, T value)
{
destination.write(reinterpret_cast<char*>(&value), sizeof(T));
}
// Utility function that saves a TGA Image from a BGRA buffer
void saveImageTGA(const std::string& filename, unsigned char* buffer, int width, int height)
{
std::ofstream out(filename.c_str(), std::ios_base::binary);
if(out.bad())
{
std::cerr << "Could not save image." << std::endl;
out.close();
return;
}
// Write TGA Header
writeVal<char>(out, 0);
writeVal<char>(out, 0);
writeVal<char>(out, 2); // compressed RGBA
writeVal<short>(out, 0);
writeVal<short>(out, 0);
writeVal<char>(out, 0);
writeVal<short>(out, 0); // x origin
writeVal<short>(out, 0); // y origin
writeVal<short>(out, width); // width
writeVal<short>(out, height); // height
writeVal<char>(out, 32); // 32 BPP
writeVal<char>(out, 0);
int rowSpan = width * 4;
// Write Image Data
for(int row = height - 1; row >= 0; row--)
out.write(reinterpret_cast<char*>(buffer + row * rowSpan), rowSpan);
out.close();
}
Currently with Debug mode nothing happens I see about 1 second of white screen then nothing appears. In release mode it crashes at LoadURL. I know DirectX is initialized correct because if I just call the Draw2D function I get a sprite on screen that I can move around.
Thank you for any help.
