// // Program starts here // int main( HINSTANCE hInstance, HINSTANCE hPrevInstance, LPTSTR lpCmdLine, int nCmdShow ) { HRESULT hr = D2D1CreateFactory( D2D1_FACTORY_TYPE_SINGLE_THREADED, &g_pD2DFactory ); hr = DWriteCreateFactory( DWRITE_FACTORY_TYPE_SHARED, __uuidof( IDWriteFactory ), reinterpret_cast<IUnknown**>( &g_pDWriteFactory ) ); hr = CoInitialize( NULL ); hr = CoCreateInstance( CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, reinterpret_cast<void**>( &g_pWICFactory ) ); g_pHWND = CreateGameWindow(); createDeviceResources(); // // Create a GWEN Direct2D renderer // g_pRenderer = new gwen::Renderer::Direct2D( g_pRT, g_pDWriteFactory, g_pWICFactory ); runSample(); delete g_pRenderer; g_pRenderer = NULL; if ( g_pRT != NULL ) { g_pRT->Release(); g_pRT = NULL; } }
void GraphicsCanvas::draw() { if (!mRenderTarget) createDeviceResources(); Assert(mRenderTarget); mRenderTarget->BeginDraw(); mRenderTarget->Clear(GraphicsConverter::convertToD2D(mClearColor)); updateRenderTargetTransform(); Assert(mScene); mScene->draw(this); HRESULT hr = mRenderTarget->EndDraw(); if (hr == D2DERR_RECREATE_TARGET) { hr = S_OK; destroyDeviceResources(); } SafeCall(hr); }
void runSample() { RECT FrameBounds; GetClientRect( g_pHWND, &FrameBounds ); // // Create a GWEN skin // gwen::Skin::TexturedBase skin( g_pRenderer ); skin.Init( "DefaultSkin.png" ); // // Create a Canvas (it's root, on which all other GWEN panels are created) // gwen::Controls::Canvas* pCanvas = new gwen::Controls::Canvas( &skin ); pCanvas->SetSize( FrameBounds.right, FrameBounds.bottom ); pCanvas->SetDrawBackground( true ); pCanvas->SetBackgroundColor( gwen::Color( 150, 170, 170, 255 ) ); // // Create our unittest control (which is a Window with controls in it) // UnitTest* pUnit = new UnitTest( pCanvas ); pUnit->SetPos( 10, 10 ); // // Create a Windows Control helper // (Processes Windows MSG's and fires input at GWEN) // gwen::Input::Windows gwenInput; gwenInput.Initialize( pCanvas ); // // Begin the main game loop // MSG msg; while ( true ) { // Skip out if the window is closed if ( !IsWindowVisible( g_pHWND ) ) { break; } // If we have a message from windows.. if ( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) ) { // .. give it to the input handler to process gwenInput.ProcessMessage( msg ); // if it's QUIT then quit.. if ( msg.message == WM_QUIT ) { break; } // Handle the regular window stuff.. TranslateMessage( &msg ); DispatchMessage( &msg ); } { if ( SUCCEEDED( createDeviceResources() ) ) { g_pRT->BeginDraw(); g_pRT->SetTransform( D2D1::Matrix3x2F::Identity() ); g_pRT->Clear( D2D1::ColorF( D2D1::ColorF::White ) ); // This is how easy it is to render GWEN! pCanvas->RenderCanvas(); HRESULT hr = g_pRT->EndDraw(); if ( hr == D2DERR_RECREATE_TARGET ) { discardDeviceResources(); g_pRenderer->DeviceLost(); } } } } delete pCanvas; }
HRESULT BaseApp::initialize() { HRESULT hr; //get the dpi information HDC screen = GetDC(0); int dpiX = GetDeviceCaps(screen, LOGPIXELSX); int dpiY = GetDeviceCaps(screen, LOGPIXELSY); ReleaseDC(0, screen); // Initialize device-indpendent resources, such // as the Direct2D factory. hr = createDeviceIndependentResources(); DPIScale::Initialize(d2dFactory); if (SUCCEEDED(hr)) { // Register the window class. WNDCLASSEX wcex = { sizeof(WNDCLASSEX) }; wcex.style = CS_HREDRAW | CS_VREDRAW; wcex.lpfnWndProc = BaseApp::wndProc; wcex.cbClsExtra = 0; wcex.cbWndExtra = sizeof(LONG_PTR); wcex.hInstance = HINST_THISCOMPONENT; wcex.hbrBackground = NULL; wcex.lpszMenuName = NULL; wcex.hCursor = LoadCursor(NULL, IDI_APPLICATION); wcex.lpszClassName = "Base App Window Class"; RegisterClassEx(&wcex); // Create the window. mainWindowHandle = CreateWindow( "Base App Window Class", APP_WINDOW_CAPTION, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, static_cast<INT>(APP_WINDOW_WIDTH * DPIScale::getScaleX()), // For modern screen static_cast<INT>(APP_WINDOW_HEIGHT * DPIScale::getScaleY()), // For modern screen NULL, NULL, HINST_THISCOMPONENT, this ); hr = mainWindowHandle ? S_OK : E_FAIL; if (SUCCEEDED(hr)) { loadConfig(); hr = createDeviceResources(mainWindowHandle); initializeGameAssets(); ShowWindow(mainWindowHandle, SW_SHOWNORMAL); UpdateWindow(mainWindowHandle); } } return hr; }