Exemple #1
0
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 GraphicsCanvas::createDeviceResources()
{
	RECT rc;
	GetClientRect(mWindowHandle, &rc);

	D2D1_SIZE_U size = D2D1::SizeU(rc.right - rc.left, rc.bottom - rc.top);
	mViewportDimensions = Vector2(float(size.width), float(size.height));

	// Create render target
	ID2D1Factory* factory = GraphicsManager::getInstance()->getD2DFactory();
	SafeCall(factory->CreateHwndRenderTarget(D2D1::RenderTargetProperties(), D2D1::HwndRenderTargetProperties(mWindowHandle, size), &mRenderTarget));

	//mRenderTarget->SetAntialiasMode(D2D1_ANTIALIAS_MODE_FORCE_DWORD);
	mRenderTarget->SetTextAntialiasMode(D2D1_TEXT_ANTIALIAS_MODE_CLEARTYPE);
}
HRESULT CreateDrawingResources(HWND hwnd)
{
    HRESULT hr = S_OK;
    RECT rcClient = { 0 };

    ID2D1Factory *pFactory = NULL;
    ID2D1HwndRenderTarget *pRenderTarget = NULL;

    GetClientRect(hwnd, &rcClient);

    hr = D2D1CreateFactory(
        D2D1_FACTORY_TYPE_SINGLE_THREADED,
        &pFactory
        );


    if (SUCCEEDED(hr))
    {
        hr = pFactory->CreateHwndRenderTarget(
            D2D1::RenderTargetProperties(),
            D2D1::HwndRenderTargetProperties(
                hwnd,
                D2D1::SizeU(rcClient.right, rcClient.bottom)
                ),
            &pRenderTarget
            );
    }

    if (SUCCEEDED(hr))
    {
        g_pRT = pRenderTarget;
        g_pRT->AddRef();
    }

    SafeRelease(&pFactory);
    SafeRelease(&pRenderTarget);
    return hr;
}
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;
}