// Create a pattern brush
HRESULT DemoApp::CreateGridPatternBrush(
	ID2D1RenderTarget *pRenderTarget,
	ID2D1BitmapBrush **ppBitmapBrush)
{
	HRESULT hr = S_OK;

	// Create a compaitble render target
	ID2D1BitmapRenderTarget *pCompatibleRenderTarget = NULL;
	hr = pRenderTarget->CreateCompatibleRenderTarget(D2D1::SizeF(10.f, 10.f), &pCompatibleRenderTarget);
	if (SUCCEEDED(hr))
	{
		// Draw a pattern
		ID2D1SolidColorBrush *pGridBrush = NULL;
		hr = pCompatibleRenderTarget->CreateSolidColorBrush(
			D2D1::ColorF(D2D1::ColorF(0.93f, 0.94f, 0.96f, 1.0f)),
			&pGridBrush);
		if (SUCCEEDED(hr))
		{
			pCompatibleRenderTarget->BeginDraw();
			pCompatibleRenderTarget->FillRectangle(D2D1::RectF(0.0f, 0.0f, 10.0f, 1.0f), pGridBrush);
			pCompatibleRenderTarget->FillRectangle(D2D1::RectF(0.0f, 0.1f, 1.0f, 10.0f), pGridBrush);
			pCompatibleRenderTarget->EndDraw();

			// Retrieve the bitmap from the render target
			ID2D1Bitmap *pGridBitmap = NULL;
			hr = pCompatibleRenderTarget->GetBitmap(&pGridBitmap);
			if (SUCCEEDED(hr))
			{
				// Choose the filling mode for the bitmap brush
				D2D1_BITMAP_BRUSH_PROPERTIES brushProperties =
					D2D1::BitmapBrushProperties(D2D1_EXTEND_MODE_WRAP, D2D1_EXTEND_MODE_WRAP);

				// Create the bitmap brush
				hr = m_pRenderTarget->CreateBitmapBrush(pGridBitmap, brushProperties, ppBitmapBrush);

				pGridBitmap->Release();
			}

			pGridBrush->Release();
		}

		pCompatibleRenderTarget->Release();
	}

	return hr;
}
Esempio n. 2
0
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;
}
Esempio n. 3
0
HRESULT CMainWindow::FindImages()
{
    HRESULT hr = S_OK;
    
    if (m_thumbs == NULL)
    {
        // Walk the Pictures library

        IShellItem *pShellItemPicturesLibrary;
        hr = SHGetKnownFolderItem(
            FOLDERID_PicturesLibrary,
            KF_FLAG_CREATE,
            NULL,
            IID_PPV_ARGS(&pShellItemPicturesLibrary)
            );
        if (SUCCEEDED(hr))
        {
            INamespaceWalk *pNamespaceWalk;
            hr = CoCreateInstance(
                CLSID_NamespaceWalker,
                NULL,
                CLSCTX_INPROC,
                IID_PPV_ARGS(&pNamespaceWalk)
                );
            if (SUCCEEDED(hr))
            {
                hr = pNamespaceWalk->Walk(
                    pShellItemPicturesLibrary,
                    NSWF_NONE_IMPLIES_ALL,
                    1,
                    NULL
                    );
                if (SUCCEEDED(hr))
                {
                    // Retrieve the array of PIDLs gathered in the walk
                
                    UINT itemCount;
                    PIDLIST_ABSOLUTE *ppidls;
                    hr = pNamespaceWalk->GetIDArrayResult(
                        &itemCount,
                        &ppidls
                        );
                    if (SUCCEEDED(hr))
                    {
                        // Create the uninitialized thumbnails
                        m_thumbs = new CThumbnail[itemCount];                 
                    
                        // Get the bitmap for each item and initialize the corresponding thumbnail object
                    
                        for (UINT i = 0; i < itemCount; i++)
                        {
                            IShellItem *pShellItem;
                            hr = SHCreateItemFromIDList(
                                ppidls[i],
                                IID_PPV_ARGS(&pShellItem)
                                );
                            if (SUCCEEDED(hr))
                            {
                                ID2D1Bitmap *pBitmap;
                                hr = DecodeImageFromThumbCache(
                                    pShellItem,
                                    &pBitmap
                                    );
                                if (SUCCEEDED(hr))
                                {
                                    hr = m_thumbs[m_uThumbCount].Initialize(
                                        pBitmap,
                                        m_pAnimationManager,
                                        m_pRenderTarget->GetSize().width * 0.5,
                                        m_pRenderTarget->GetSize().height * 0.5
                                        );
                                    if (SUCCEEDED(hr))
                                    {
                                        m_uThumbCount++;
                                    }
                                        
                                    pBitmap->Release();
                                }
                                
                                pShellItem->Release();
                            }
                        }
                        
                        // The calling function is responsible for freeing the PIDL array
                        FreeIDListArray(ppidls, itemCount);
                        
                        if (SUCCEEDED(hr))
                        {
                            // Arrange the images when they are first loaded

                            m_pLayoutManager = new CLayoutManager();
                            hr = m_pLayoutManager->Initialize(
                                m_pAnimationManager,
                                m_pAnimationTimer,
                                m_pTransitionLibrary,
                                m_uThumbCount,
                                m_thumbs
                                );
                            if (SUCCEEDED(hr))
                            {
                                hr = m_pLayoutManager->Resize(
                                    m_pRenderTarget->GetSize()
                                    );
                            }
                        }
                    }
                }
            
                pNamespaceWalk->Release();
            }
        
            pShellItemPicturesLibrary->Release();
        }
    }
    
    return hr;
}
Esempio n. 4
0
// this imitates http://blogs.msdn.com/b/wpfsdk/archive/2006/10/26/uncommon-dialogs--font-chooser-and-color-picker-dialogs.aspx
static void drawGrid(ID2D1RenderTarget *rt, D2D1_RECT_F *fillRect)
{
	D2D1_SIZE_F size;
	D2D1_PIXEL_FORMAT pformat;
	ID2D1BitmapRenderTarget *brt;
	D2D1_COLOR_F color;
	D2D1_BRUSH_PROPERTIES bprop;
	ID2D1SolidColorBrush *brush;
	D2D1_RECT_F rect;
	ID2D1Bitmap *bitmap;
	D2D1_BITMAP_BRUSH_PROPERTIES bbp;
	ID2D1BitmapBrush *bb;
	HRESULT hr;

	// mind the divisions; they represent the fact the original uses a viewport
	size.width = 100 / 10;
	size.height = 100 / 10;
	// yay more ABI bugs

	pformat = rt->GetPixelFormat();

	hr = rt->CreateCompatibleRenderTarget(&size, NULL,
		&pformat, D2D1_COMPATIBLE_RENDER_TARGET_OPTIONS_NONE,
		&brt);
	if (hr != S_OK)
		logHRESULT(L"error creating render target for grid", hr);

	brt->BeginDraw();

	color.r = 1.0;
	color.g = 1.0;
	color.b = 1.0;
	color.a = 1.0;
	brt->Clear(&color);

	color = D2D1::ColorF(D2D1::ColorF::LightGray, 1.0);
	ZeroMemory(&bprop, sizeof (D2D1_BRUSH_PROPERTIES));
	bprop.opacity = 1.0;
	bprop.transform._11 = 1;
	bprop.transform._22 = 1;
	hr = brt->CreateSolidColorBrush(&color, &bprop, &brush);
	if (hr != S_OK)
		logHRESULT(L"error creating brush for grid", hr);
	rect.left = 0;
	rect.top = 0;
	rect.right = 50 / 10;
	rect.bottom = 50 / 10;
	brt->FillRectangle(&rect, brush);
	rect.left = 50 / 10;
	rect.top = 50 / 10;
	rect.right = 100 / 10;
	rect.bottom = 100 / 10;
	brt->FillRectangle(&rect, brush);
	brush->Release();

	hr = brt->EndDraw(NULL, NULL);
	if (hr != S_OK)
		logHRESULT(L"error finalizing render target for grid", hr);
	hr = brt->GetBitmap(&bitmap);
	if (hr != S_OK)
		logHRESULT(L"error getting bitmap for grid", hr);
	brt->Release();

	ZeroMemory(&bbp, sizeof (D2D1_BITMAP_BRUSH_PROPERTIES));
	bbp.extendModeX = D2D1_EXTEND_MODE_WRAP;
	bbp.extendModeY = D2D1_EXTEND_MODE_WRAP;
	bbp.interpolationMode = D2D1_BITMAP_INTERPOLATION_MODE_NEAREST_NEIGHBOR;
	hr = rt->CreateBitmapBrush(bitmap, &bbp, &bprop, &bb);
	if (hr != S_OK)
		logHRESULT(L"error creating bitmap brush for grid", hr);
	rt->FillRectangle(fillRect, bb);
	bb->Release();
	bitmap->Release();
}
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;
}
bool ResourceManager::LoadFile(ID2D1HwndRenderTarget* renderTarget, wchar_t * filename)
{
	HRESULT result;
	IWICImagingFactory2* wicFactory;
	IWICBitmapDecoder* wicDecoder;
	IWICBitmapFrameDecode* wicFrame;
	IWICBitmapFlipRotator* wicFlip;
	IWICFormatConverter *wicConverter;
	Sprite*	newSprite;

	// WIC의 각종 인터페이스를 사용하기 위한 factory 생성
	result = CoCreateInstance(CLSID_WICImagingFactory, nullptr, CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&wicFactory));
	if (FAILED(result))
		return false;

	// 파일을 읽고 디코딩 하기 위한 decoder 생성
	result = wicFactory->CreateDecoderFromFilename(filename, nullptr, GENERIC_READ, WICDecodeMetadataCacheOnDemand, &wicDecoder);
	if (FAILED(result))
		return false;

	// decoder에서 프레임을 얻어옴, // 일반적인 이미지 파일은 single frame만을 지원하므로 0으로 고정
	result = wicDecoder->GetFrame(0, &wicFrame);
	if (FAILED(result))
		return false;

	// 수평으로 뒤집힌 이미지를 얻기 위해 BitmapFlipRotator 생성
	result = wicFactory->CreateBitmapFlipRotator(&wicFlip);
	if (FAILED(result))
		return false;

	// wicFrame를 수평으로 뒤집음
	wicFlip->Initialize(wicFrame, WICBitmapTransformFlipHorizontal);

	// WICBitmap을 D2DBitmap으로 변환시키기 위해 format converter 생성
	result = wicFactory->CreateFormatConverter(&wicConverter);
	if (FAILED(result))
		return false;

	// Converter[0]의 Format을 일반 이미지(wicFrame)에 맞춤
	result = wicConverter->Initialize(wicFrame, GUID_WICPixelFormat32bppPBGRA, WICBitmapDitherTypeNone, NULL, 0.0f, WICBitmapPaletteTypeCustom);
	if (FAILED(result))
		return false;

	// 리소스 정보를 저장 할 Sprite 생성
	newSprite = new Sprite(renderTarget);
	if (!newSprite)
		return false;

	// WICBitmap을 D2DBitmap으로 변환
	//result = renderTarget->CreateBitmapFromWicBitmap(wicConverter, nullptr, &m_Bitmap);
	result = renderTarget->CreateBitmapFromWicBitmap(wicConverter, nullptr, newSprite->GetBitmap());
	if (FAILED(result))
		return false;

	ID2D1Bitmap* bitmap = *(newSprite->GetBitmap());
	int numberOfFrame = bitmap->GetSize().width / IMAGE_SIZE;
	int numberOfAction = bitmap->GetSize().height / IMAGE_SIZE;
	newSprite->Initialize(numberOfFrame, numberOfAction);

	wchar_t* buffer = new wchar_t[128];
	wcscpy_s(buffer, wcslen(filename) + 1, filename);

	// 스프라이트 등록
	m_Sprites.push_back(newSprite);
	m_Filenames.push_back(buffer);

	wicConverter->Release();
	wicConverter = nullptr;

	wicFrame->Release();
	wicFrame = nullptr;

	wicFlip->Release();
	wicFlip = nullptr;

	wicDecoder->Release();
	wicDecoder = nullptr;

	wicFactory->Release();
	wicFactory = nullptr;

	return true;
}