//----------------------------------------------------------------------------- void D2DDrawContext::createRenderTarget () { if (window) { RECT rc; GetClientRect (window, &rc); D2D1_SIZE_U size = D2D1::SizeU (rc.right - rc.left, rc.bottom - rc.top); ID2D1HwndRenderTarget* hwndRenderTarget = 0; // D2D1_RENDER_TARGET_TYPE targetType = D2D1_RENDER_TARGET_TYPE_DEFAULT; D2D1_RENDER_TARGET_TYPE targetType = D2D1_RENDER_TARGET_TYPE_SOFTWARE; D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat (DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED); HRESULT hr = getD2DFactory ()->CreateHwndRenderTarget (D2D1::RenderTargetProperties (targetType, pixelFormat), D2D1::HwndRenderTargetProperties (window, size, D2D1_PRESENT_OPTIONS_RETAIN_CONTENTS), &hwndRenderTarget); if (SUCCEEDED (hr)) { hwndRenderTarget->SetDpi (96.0, 96.0); renderTarget = hwndRenderTarget; } } else if (bitmap) { D2DBitmap* d2dBitmap = dynamic_cast<D2DBitmap*> (bitmap->getPlatformBitmap ()); if (d2dBitmap) { D2D1_RENDER_TARGET_TYPE targetType = D2D1_RENDER_TARGET_TYPE_SOFTWARE; D2D1_PIXEL_FORMAT pixelFormat = D2D1::PixelFormat (DXGI_FORMAT_UNKNOWN, D2D1_ALPHA_MODE_PREMULTIPLIED); HRESULT hr = getD2DFactory ()->CreateWicBitmapRenderTarget (d2dBitmap->getBitmap (), D2D1::RenderTargetProperties (targetType, pixelFormat), &renderTarget); } } init (); }
void CSprite::Draw(const sRect& src, const sRect& dest) { ID2D1HwndRenderTarget* prt = g_Graphics.GetRenderTarget(); if (!prt) return; D2D1_RECT_F destx = D2D1::RectF(dest.left, dest.top, dest.right, dest.bottom), srcx = D2D1::RectF(src.left, src.top, src.right, src.bottom); prt->DrawBitmap(m_pBitmap, destx, m_fOpacity, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR, srcx); }
LRESULT CALLBACK wndproc(HWND hwnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { LONG_PTR userdata; ID2D1HwndRenderTarget *rt = NULL; WINDOWPOS *wp = (WINDOWPOS *) lParam; RECT client; D2D1_SIZE_U size; userdata = GetWindowLongPtrW(hwnd, GWLP_USERDATA); if (userdata == 0) { if (uMsg == WM_CREATE) SetWindowLongPtrW(hwnd, GWLP_USERDATA, 1); return DefWindowProcW(hwnd, uMsg, wParam, lParam); } if (userdata == 1) { rt = mkRenderTarget(hwnd); if (rt == NULL) { PostQuitMessage(1); return DefWindowProcW(hwnd, uMsg, wParam, lParam); } SetWindowLongPtrW(hwnd, GWLP_USERDATA, (LONG_PTR) rt); } else rt = (ID2D1HwndRenderTarget *) userdata; switch (uMsg) { case WM_PAINT: switch (doPaint(rt)) { case S_OK: ValidateRect(hwnd, NULL); return 0; case D2DERR_RECREATE_TARGET: rt->Release(); SetWindowLongPtrW(hwnd, GWLP_USERDATA, 1); return 0; } rt->Release(); PostQuitMessage(1); break; case WM_WINDOWPOSCHANGED: if ((wp->flags & SWP_NOSIZE) != 0) break; GetClientRect(hwnd, &client); size.width = client.right - client.left; size.height = client.bottom - client.top; rt->Resize(&size); InvalidateRect(hwnd, NULL, TRUE); return 0; case WM_CLOSE: rt->Release(); PostQuitMessage(0); } return DefWindowProcW(hwnd, uMsg, wParam, lParam); }
HRESULT MainWindow::CreateGraphicsResources() { HRESULT hr = S_OK; if (pRenderTarget == NULL) { RECT rc; GetClientRect(m_hwnd, &rc); D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom); hr = pFactory->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(m_hwnd, size), &pRenderTarget); if (SUCCEEDED(hr)) { const D2D1_COLOR_F color = D2D1::ColorF(1.0f, 1.0f, 0); hr = pRenderTarget->CreateSolidColorBrush(color, &pBrush); if (SUCCEEDED(hr)) { CalculateLayout(); } } } return hr; }
void MWinDeviceImpl::Save() { ID2D1DrawingStateBlock* state; THROW_IF_HRESULT_ERROR(sD2DFactory->CreateDrawingStateBlock(&state)); mRenderTarget->SaveDrawingState(state); mState.push(state); }
void MWinDeviceImpl::ClipRect( MRect inRect) { if (mClipLayer != nil) { mRenderTarget->PopLayer(); mClipLayer->Release(); } THROW_IF_HRESULT_ERROR(mRenderTarget->CreateLayer(&mClipLayer)); // Push the layer with the geometric mask. mRenderTarget->PushLayer( D2D1::LayerParameters(D2D1::RectF(inRect.x, inRect.y, inRect.x + inRect.width, inRect.y + inRect.height)), mClipLayer); }
void MWinDeviceImpl::CreateAndUsePattern( MColor inColor1, MColor inColor2) { uint32 data[8][8]; uint32 c1 = 0, c2 = 0; c1 |= inColor1.red << 16; c1 |= inColor1.green << 8; c1 |= inColor1.blue << 0; c2 |= inColor2.red << 16; c2 |= inColor2.green << 8; c2 |= inColor2.blue << 0; for (uint32 y = 0; y < 8; ++y) { for (uint32 x = 0; x < 4; ++x) data[y][x] = c1; for (uint32 x = 4; x < 8; ++x) data[y][x] = c2; } ID2D1BitmapBrush* brush; ID2D1Bitmap* bitmap; THROW_IF_HRESULT_ERROR(mRenderTarget->CreateBitmap(D2D1::SizeU(8, 8), data, 32, D2D1::BitmapProperties( D2D1::PixelFormat(DXGI_FORMAT_B8G8R8A8_UNORM, D2D1_ALPHA_MODE_IGNORE) ), &bitmap)); THROW_IF_HRESULT_ERROR(mRenderTarget->CreateBitmapBrush( bitmap, D2D1::BitmapBrushProperties(D2D1_EXTEND_MODE_WRAP, D2D1_EXTEND_MODE_WRAP), D2D1::BrushProperties(1.0f, D2D1::Matrix3x2F::Rotation(45.f)), &brush)); bitmap->Release(); if (mForeBrush != nil) mForeBrush->Release(); mForeBrush = brush; }
void MWinDeviceImpl::FillRect( MRect inRect) { assert(mForeBrush); assert(mRenderTarget); mRenderTarget->FillRectangle( D2D1::RectF(inRect.x, inRect.y, inRect.x + inRect.width, inRect.y + inRect.height), mForeBrush); }
void LetterHunter::render(float timeDelta) { // Create device dependent resources d2d_->createDeviceResources(hwnd_); // Get Hwnd render target ID2D1HwndRenderTarget* rendertarget = d2d_->getD2DHwndRenderTarget(); // Update text object before rendering update(timeDelta); rendertarget->BeginDraw(); // Set render target background color to white rendertarget->Clear(D2D1::ColorF(D2D1::ColorF::White)); // render background image wchar_t* imageFile = L"./Media/Picture/Fairy_of_the_Water.jpg"; drawBackgroundImage(imageFile); // render text objects for(unsigned int i = 0; i < textBuffer_.size(); ++i) { if(textBuffer_[i]->isLive()) { textBuffer_[i]->render(); } } // render bullet objects for(unsigned int i = 0; i < bulletBuffer_.size(); ++i) { if(bulletBuffer_[i]->isLive()) { bulletBuffer_[i]->render(); } } // render score score_->draw(); rendertarget->EndDraw(); }
void MWinDeviceImpl::StrokeRect( MRect inRect, uint32 inLineWidth) { assert(mForeBrush); assert(mRenderTarget); mRenderTarget->DrawRectangle( D2D1::RectF(inRect.x, inRect.y, inRect.x + inRect.width, inRect.y + inRect.height), mForeBrush); }
void MainWindow::CalculateLayout() { if (pRenderTarget != NULL) { D2D1_SIZE_F size = pRenderTarget->GetSize(); const float x = size.width / 2; const float y = size.height / 2; const float radius = min(x, y); ellipse = D2D1::Ellipse(D2D1::Point2F(x, y), radius, radius); } }
void MainWindow::OnPaint() { HRESULT hr = CreateGraphicsResources(); if (SUCCEEDED(hr)) { PAINTSTRUCT ps; BeginPaint(m_hwnd, &ps); pRenderTarget->BeginDraw(); pRenderTarget->Clear(D2D1::ColorF(D2D1::ColorF::SkyBlue)); pRenderTarget->FillEllipse(ellipse, pBrush); hr = pRenderTarget->EndDraw(); if (FAILED(hr) || hr == D2DERR_RECREATE_TARGET) { DiscardGraphicsResources(); } EndPaint(m_hwnd, &ps); } }
void MWinDeviceImpl::SetBackColor( MColor inColor) { if (mBackBrush != nil) mBackBrush->Release(); ID2D1SolidColorBrush* brush; THROW_IF_HRESULT_ERROR( mRenderTarget->CreateSolidColorBrush(D2D1::ColorF(inColor.red / 255.f, inColor.green / 255.f, inColor.blue / 255.f), &brush)); mBackBrush = brush; }
// ---------------------------------------------------------------- // LoadResource // ---------------------------------------------------------------- bool CD2DBitmap::LoadResource( ResourceId id ) { ID2D1HwndRenderTarget * renderTarget = CD2DRenderer::GetInstance().GetHwndRenderTarget(); IWICImagingFactory * imagingFactory = CD2DRenderer::GetInstance().GetImagingFactory(); IWICBitmapDecoder * bitmapDecoder = nullptr; IWICBitmapFrameDecode* bitmapFrameDecode = nullptr; imagingFactory->CreateDecoderFromFilename( id.c_str(), nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &bitmapDecoder ); bitmapDecoder->GetFrame( 0, &bitmapFrameDecode ); imagingFactory->CreateFormatConverter( &m_FmtConverter ); m_FmtConverter->Initialize( bitmapFrameDecode, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, nullptr, 0.0f, WICBitmapPaletteTypeCustom ); renderTarget->CreateBitmapFromWicBitmap( m_FmtConverter, nullptr, &m_D2DBitmap ); SafeRelease( bitmapDecoder ); SafeRelease( bitmapFrameDecode ); return true; }
void MWinDeviceImpl::DrawString( const string& inText, float inX, float inY, uint32 inTruncateWidth, MAlignment inAlign) { wstring s(c2w(inText)); mRenderTarget->DrawTextW(s.c_str(), s.length(), GetTextFormat(), D2D1::RectF(inX, inY, 200, 14), mForeBrush); }
void MainWindow::Resize() { if (pRenderTarget != NULL) { RECT rc; GetClientRect(m_hwnd, &rc); D2D1_SIZE_U size = D2D1::SizeU(rc.right, rc.bottom); pRenderTarget->Resize(size); CalculateLayout(); InvalidateRect(m_hwnd, NULL, FALSE); } }
void MWinDeviceImpl::FillEllipse( MRect inRect) { assert(mForeBrush); assert(mRenderTarget); float radius; if (inRect.height < inRect.width) radius = inRect.height / 2.f; else radius = inRect.width / 2.f; D2D1_ROUNDED_RECT r = D2D1::RoundedRect(D2D1::RectF(inRect.x, inRect.y, inRect.x + inRect.width, inRect.y + inRect.height), radius, radius); mRenderTarget->FillRoundedRectangle(r, mForeBrush); }
void MWinDeviceImpl::SetOrigin( int32 inX, int32 inY) { mRenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(inX, inY)); }
HRESULT D2DRenderer::InitializeDeviceDependentResources() { HRESULT hr = S_OK; if (renderTarget_ != NULL) return hr; ID2D1HwndRenderTarget* renderTarget = NULL; ID2D1SolidColorBrush* backBrush = NULL; ID2D1SolidColorBrush* textBrush = NULL; ID2D1SolidColorBrush* borderBrush = NULL; D2D1_RENDER_TARGET_PROPERTIES renderTargetProperties = D2D1::RenderTargetProperties( D2D1_RENDER_TARGET_TYPE_DEFAULT, D2D1::PixelFormat(), static_cast<float>(g_dpiX), static_cast<float>(g_dpiY) ); if (SUCCEEDED(hr)) { hr = g_d2dFactory->CreateHwndRenderTarget( renderTargetProperties, D2D1::HwndRenderTargetProperties(hwnd_, D2D1::SizeU(width_, height_)), &renderTarget ); } if (renderingParams_ != NULL) { renderTarget->SetTextRenderingParams(renderingParams_); } backColor_ = D2D1::ColorF(GetSysColor(COLOR_WINDOW)); if (SUCCEEDED(hr)) { hr = renderTarget->CreateSolidColorBrush( backColor_, &backBrush ); } if (SUCCEEDED(hr)) { hr = renderTarget->CreateSolidColorBrush( D2D1::ColorF(GetSysColor(COLOR_WINDOWTEXT)), &textBrush ); } if (SUCCEEDED(hr)) { hr = renderTarget->CreateSolidColorBrush( D2D1::ColorF(1.0f, 0, 0, 0.5f), &borderBrush ); } // Commit changes. SafeAttach(&renderTarget_, SafeDetach(&renderTarget)); SafeAttach(&backBrush_, SafeDetach(&backBrush)); SafeAttach(&textBrush_, SafeDetach(&textBrush)); SafeAttach(&borderBrush_, SafeDetach(&borderBrush)); return hr; }
void MWinDeviceImpl::Restore() { assert(not mState.empty()); mRenderTarget->RestoreDrawingState(mState.top()); mState.pop(); }
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPSTR lpCmdLine, int nShowCmd) { xxd.SetScreenResolution(ScreenWidth, ScreenHeight); WNDCLASSEX pencere; ZeroMemory(&pencere, sizeof(WNDCLASSEX)); pencere.cbClsExtra = NULL; pencere.cbSize = sizeof(WNDCLASSEX); pencere.cbWndExtra = NULL; pencere.hCursor = LoadCursor(NULL, IDC_ARROW); pencere.hIcon = NULL; pencere.hIconSm = NULL; pencere.hInstance = hInst; pencere.lpfnWndProc = (WNDPROC)WinProc; pencere.lpszClassName = "Pencere"; pencere.lpszMenuName = NULL; pencere.style = CS_HREDRAW | CS_VREDRAW; RegisterClassEx(&pencere); ID2D1Factory* directx = NULL; D2D1CreateFactory(D2D1_FACTORY_TYPE_SINGLE_THREADED,&directx); HWND hWnd = NULL; hWnd = CreateWindowEx(NULL, "Pencere", "Direct2D",WS_POPUP | WS_EX_TOPMOST, 0, 0, ScreenWidth, ScreenHeight, NULL, NULL, hInst, NULL); ShowWindow(hWnd, nShowCmd); /*D2D1Rect*/ RECT rect; GetClientRect(hWnd, &rect); ID2D1HwndRenderTarget* RenderTarget = NULL; directx->CreateHwndRenderTarget( D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(hWnd,D2D1::SizeU(rect.right - rect.left, rect.bottom - rect.top)), &RenderTarget); /*D2D1Rect*/ /* D2D1Brush */ /*ID2D1SolidColorBrush* Brush = NULL; RenderTarget->CreateSolidColorBrush(D2D1::ColorF(D2D1::ColorF::Black), &Brush);*/ /* D2D1Brush */ MSG msg; ZeroMemory(&msg, sizeof(MSG)); /*D2D1_ELLIPSE ellipse = D2D1::Ellipse( D2D1::Point2F(100.f, 100.f), 50.f, 50.f );*/ /*POINT pts; pts.x = (ScreenWidth/2) - 25; float gravity = 1.0f; float y = 20; int alo = 0; pts.y = (ScreenHeight/2) - 80;*/ ID2D1Bitmap* Bitmap = NULL; IWICImagingFactory* ImagingFactory = NULL; CoInitializeEx(NULL, COINIT_MULTITHREADED); CoCreateInstance(CLSID_WICImagingFactory, NULL, CLSCTX_INPROC_SERVER, IID_IWICImagingFactory, (LPVOID*)&ImagingFactory); /*lb.LoadBitmapFromFile(RenderTarget, ImagingFactory, L"Oyuncu/Dokular/Aylak/ön.png", &Bitmap); D2D1_SIZE_F size = Bitmap->GetSize(); D2D1_POINT_2F upperLeftCorner;*/ btn.InitFactory(ImagingFactory); btn.InitTarget(RenderTarget); btn.setPos(100, 100); btn.getImages(); btn.InitText(L"Buton Bura Oglim"); Bush trn; trn.InitGadgets(RenderTarget,ImagingFactory); trn.InitImage(); trn.CalcThat(); trn.InitThat(); Player plyr; plyr.InitFactories(ImagingFactory,RenderTarget); plyr.InitIdleImages(); plyr.InitMoveImages(); DWORD baslangic_noktasi; float deltaTime, oldTime = 0; while (TRUE){ baslangic_noktasi = GetTickCount(); deltaTime = baslangic_noktasi - oldTime; oldTime = baslangic_noktasi; if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) { break; } TranslateMessage(&msg); DispatchMessage(&msg); } //KEY if (TUS(VK_SPACE)) { //alo = 1; } if (TUS(VK_LEFT)) { tp.x += 2; //pts.x -= 2; } if (TUS(VK_RIGHT)) { tp.x -= 2; //pts.x += 2; } if (TUS(VK_UP)) { tp.y += 2; //pts.y -= 2; } if (TUS(VK_DOWN)) { tp.y -= 2; //pts.y += 2; } plyr.HandleKeysForIdle(); plyr.HandleKeysForMove(deltaTime); /*if (alo == 1) { y -= gravity; pts.y -= y; if (y == -19) { alo = 0; y = 20.f; } }*/ /*if (clps.x != 0 && clps.y!= 0) { text.setPos(clps.x, clps.y); }*/ /*upperLeftCorner = D2D1::Point2F(pts.x, pts.y);*/ //text.setFont(L"Comic Sans MS"); //LOOP RenderTarget->BeginDraw(); RenderTarget->Clear(D2D1::ColorF(0.25f,0.80f,0.22f,1.f)); /*Terrain*/ trn.drawBush(); /*Terrain*/ //RenderTarget->DrawRectangle(D2D1::RectF(100,100,200,300), Brush); //RenderTarget->DrawEllipse(ellipse, Brush, 2.f); RenderTarget->SetTransform(D2D1::Matrix3x2F::Translation(0 + tp.x,0 + tp.y)); /*RenderTarget->DrawBitmap( Bitmap, D2D1::RectF( upperLeftCorner.x, upperLeftCorner.y, upperLeftCorner.x + 50, upperLeftCorner.y + 160), 1.0, D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR );*/ //text.drawatext(50,L"Merhaba",RenderTarget,Brush); plyr.Render(); btn.RenderIt(cups, clk, tp); RenderTarget->EndDraw(); /*if (TUS(VK_ESCAPE)) { break; PostMessage(hWnd, WM_DESTROY, 0, 0); }*/ // } directx->Release(); RenderTarget->Release(); /*Brush->Release();*/ Bitmap->Release(); ImagingFactory->Release(); xxd.RestoreScreenResolution(); return 0; }