Jump to content
  • Advertisement
Sign in to follow this  
colter13

[DirectX]Not rendering when trying to display image

This topic is 3164 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I created a simple DX program which creates a window and clears it to a green color. I'm following the book Beginning DirectX 10 Game Programming and I tried to get it to render an image after clearing the screen. When I implement this, not only does the picture not render, but the window is no longer cleared. The most important parts of my program are seeing in this code:
void Render()
{
	if(pD3DDevice!=NULL)
	{
		pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(0.0f, 1.0f, 0.0f, 0.0f));
		pD3DDevice->CopySubresourceRegion(pBackBuffer, 0, 0, 0, 0, Get2DTexture("lena.bmp"), 0, GetSourceRegion(HEIGHT, WIDTH));

		pSwapChain->Present(0, 0);
	}
}

ID3D10Texture2D* Get2DTexture(LPCSTR fileName)
{
	ID3D10Texture2D* texture2D=NULL;
	ID3D10Resource* rTexture2D=NULL;

	HRESULT hr=D3DX10CreateTextureFromFile(pD3DDevice,
		LPCSTR(fileName),
		NULL,
		NULL,
		&rTexture2D,
		NULL);

	if(FAILED(hr))
	{
		return NULL;
	}

	rTexture2D->QueryInterface(__uuidof(ID3D10Texture2D), (LPVOID*)&texture2D);
	rTexture2D->Release();

	return texture2D;
}

D3D10_BOX* GetSourceRegion(USHORT width, USHORT height)
{
	D3D10_BOX sourceRegion;
	sourceRegion.left=0;
	sourceRegion.right=width;
	sourceRegion.top=0;
	sourceRegion.bottom=height;
	sourceRegion.front=0;
	sourceRegion.back=1;

	return &sourceRegion;
}


In the render function you see I clear the screen with ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(0.0f, 1.0f, 0.0f, 0.0f)). With only this, all works fine and the screen appears green. When I add CopySubresourceRegion(pBackBuffer, 0, 0, 0, 0, Get2DTexture("lena.bmp"), 0, GetSourceRegion(HEIGHT, WIDTH)), it stops working in all and only displays a regular window. Here is the full program if you need to see it:
#include <d3d10.h>
#include <d3dx10.h>

#include <windows.h>

#include "resource.h"
#define WIDTH 640
#define HEIGHT 480

ID3D10Device * pD3DDevice=NULL;
IDXGISwapChain * pSwapChain=NULL;
ID3D10RenderTargetView * pRenderTargetView;
ID3D10Texture2D* pBackBuffer;

bool InitDirect3D(HWND hWnd, int width, int height);
void Render();
ID3D10Texture2D* Get2DTexture(LPCSTR fileName);
D3D10_BOX* GetSourceRegion(USHORT width, USHORT height);

HINSTANCE hInstance;
HWND hWindow;

bool InitWindow(HINSTANCE hInstance);
LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, 
				   LPSTR lpCmdLine, int nShowCmd)
{
	if(!InitWindow(hInstance))
		return false;

	if(!InitDirect3D(hWindow, WIDTH, HEIGHT))
	{
		MessageBox(NULL, "DirectX Initialization Failed", "Error", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	HRESULT hr=pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
	if(hr!=S_OK)
	{
		MessageBox(NULL, "Error creating texture back buffer", "Error!", MB_ICONEXCLAMATION | MB_OK);
		return false;
	}

	MSG msg;
	ZeroMemory(&msg, sizeof(MSG));

	while(msg.message!=WM_QUIT)
	{
		while(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE)==TRUE)
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		Render();
	}

	return (int) msg.wParam;
}

bool InitWindow(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize=sizeof(WNDCLASSEX);
	wcex.style=CS_HREDRAW | CS_VREDRAW;
	wcex.lpfnWndProc=(WNDPROC)WndProc;
	wcex.cbClsExtra=0;
	wcex.cbWndExtra=0;
	wcex.hInstance=hInstance;
	wcex.hIcon=0;
	wcex.hCursor=LoadCursor(hInstance, IDC_ARROW);
	wcex.hbrBackground=(HBRUSH)(COLOR_WINDOW-2);
	wcex.lpszMenuName=NULL;
	wcex.lpszClassName="MainWindow";
	wcex.hIconSm=0;
	
	RegisterClassEx(&wcex);

	hWindow=CreateWindow(
		"MainWindow",
		"DirectX Game Window",
		WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT,
		CW_USEDEFAULT,
		WIDTH,
		HEIGHT,
		NULL,
		NULL,
		hInstance,
		NULL);

	if(!hWindow)
	{
			MessageBox(NULL, "Window Creation Failed", "Error!", MB_OK | MB_ICONEXCLAMATION);
			return false;
	}

	ShowWindow(hWindow, SW_SHOW);
	UpdateWindow(hWindow);
	return true;
}

LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
	case WM_CLOSE:
		DestroyWindow(hWindow);
	break;
	case WM_DESTROY:
		PostQuitMessage(0);
	break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	
	return 0;
}

bool InitDirect3D(HWND hWnd, int width, int height)
{
	DXGI_SWAP_CHAIN_DESC swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(DXGI_SWAP_CHAIN_DESC));

	swapChainDesc.BufferCount=1;
	swapChainDesc.BufferDesc.Width=WIDTH;
	swapChainDesc.BufferDesc.Height=HEIGHT;
	swapChainDesc.BufferDesc.Format=DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferDesc.RefreshRate.Numerator=60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator=1;
	swapChainDesc.BufferUsage=DXGI_USAGE_RENDER_TARGET_OUTPUT;
	swapChainDesc.OutputWindow=hWnd;
	swapChainDesc.SampleDesc.Count=1;
	swapChainDesc.SampleDesc.Quality=0;
	swapChainDesc.Windowed=TRUE;

	HRESULT hr = D3D10CreateDeviceAndSwapChain(NULL,
        D3D10_DRIVER_TYPE_REFERENCE,
        NULL,
        0,
        D3D10_SDK_VERSION,
        &swapChainDesc,
        &pSwapChain,
        &pD3DDevice);

	if(hr!=S_OK)
	{
		MessageBox(NULL, "Device creation failed", "Error!", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	ID3D10Texture2D * pBackBuffer;
	hr=pSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*) &pBackBuffer);

	pBackBuffer->Release();

	if(hr!=S_OK)
	{
		MessageBox(NULL, "Swap chain creation failed", "Error!", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}
	
	hr=pD3DDevice->CreateRenderTargetView(pBackBuffer, NULL, &pRenderTargetView);

	pBackBuffer->Release();

	if(hr!=S_OK)
	{
		MessageBox(NULL, "Viewport creation failed", "Error!", MB_OK | MB_ICONEXCLAMATION);
		return false;
	}

	pD3DDevice->OMSetRenderTargets(1, &pRenderTargetView, NULL);

	D3D10_VIEWPORT viewPort;
	viewPort.Width=WIDTH;
	viewPort.Height=HEIGHT;
	viewPort.MinDepth=0.0f;
	viewPort.MaxDepth=1.0f;
	viewPort.TopLeftX=0;
	viewPort.TopLeftY=0;
	pD3DDevice->RSSetViewports(1, &viewPort);

	return true;
}

void Render()
{
	if(pD3DDevice!=NULL)
	{
		pD3DDevice->ClearRenderTargetView(pRenderTargetView, D3DXCOLOR(0.0f, 1.0f, 0.0f, 0.0f));
		pD3DDevice->CopySubresourceRegion(pBackBuffer, 0, 0, 0, 0, Get2DTexture("lena.bmp"), 0, GetSourceRegion(HEIGHT, WIDTH));

		pSwapChain->Present(0, 0);
	}
}

void ShutDownDirect3D()
{
	if(pRenderTargetView)
	{
		pRenderTargetView->Release();
	}

	if(pSwapChain)
	{
		pSwapChain->Release();
	}

	if(pD3DDevice)
	{
		pD3DDevice->Release();
	}
}

ID3D10Texture2D* Get2DTexture(LPCSTR fileName)
{
	ID3D10Texture2D* texture2D=NULL;
	ID3D10Resource* rTexture2D=NULL;

	HRESULT hr=D3DX10CreateTextureFromFile(pD3DDevice,
		LPCSTR(fileName),
		NULL,
		NULL,
		&rTexture2D,
		NULL);

	if(FAILED(hr))
	{
		return NULL;
	}

	rTexture2D->QueryInterface(__uuidof(ID3D10Texture2D), (LPVOID*)&texture2D);
	rTexture2D->Release();

	return texture2D;
}

D3D10_BOX* GetSourceRegion(USHORT width, USHORT height)
{
	D3D10_BOX sourceRegion;
	sourceRegion.left=0;
	sourceRegion.right=width;
	sourceRegion.top=0;
	sourceRegion.bottom=height;
	sourceRegion.front=0;
	sourceRegion.back=1;

	return &sourceRegion;
}


Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!