Jump to content
  • Advertisement
Sign in to follow this  
MadMax1992

[SOLVED][DX10] Can't display texture + runtime error

This topic is 3913 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

Hi guys, I've been fooling around with some code I've made for a long time and I can't find what I'm doing wrong. So I thought that maybe some of you guys could help me out. Here is the code I made based on the book "Beginning DirectX 10 game programming": d3dApp.h:
#ifndef D3DAPP_H
#define D3DAPP_H

#include <windows.h>
#include <d3d10.h>
#include <d3dx10.h>

class d3dApp {

public:

	//constructor and destructor
	d3dApp(HINSTANCE hInstanceVar, HINSTANCE hPrevInstanceVar, LPSTR lpCmdLineVar, int nShowCmdVar);
	~d3dApp();
	
	//initializing functions
	bool InitWindow ();
	bool InitDirect3D ();
	
	//other functions
	int MsgLoop();

	void CleanUpDirect3D ();

	//rendering functions
	void Render();
	ID3D10Texture2D* CreateTexture(LPCTSTR sourceFile);

protected:

	HINSTANCE				hInstance;
	HINSTANCE				hPrevInstance;
	LPSTR					lpCmdLine;
	int						nShowCmd;
	HWND					hWnd;

	ID3D10Device*			d3dDevice;
	IDXGISwapChain*			d3dSwapChain;
	ID3D10RenderTargetView* d3dRenderTargetView;

	ID3D10Texture2D* bodyTexture;

	int windowedWidth;
	int windowedHeight;
	int windowedXPos;
	int windowedYPos;

};

LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);

#endif

d3dApp.cpp
#include "d3dApp.h"

//--------------------------------------------------------------------------------------------------------
// WndProc ( = global )
//--------------------------------------------------------------------------------------------------------
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam) 
{
	switch (message) {
		case WM_KEYDOWN:		
			switch (wParam) {
				case VK_ESCAPE:
				PostQuitMessage(0);
				break;
			}
		break;

		case WM_DESTROY:
			PostQuitMessage(0);
			break;
	}

	//further processing of message
	return DefWindowProc(hWnd, message, wParam, lParam);
}

//--------------------------------------------------------------------------------------------------------
// Constructor
//--------------------------------------------------------------------------------------------------------
d3dApp::d3dApp(HINSTANCE hInstanceVar, HINSTANCE hPrevInstanceVar, LPSTR lpCmdLineVar, int nShowCmdVar)
{
	hInstance = hInstanceVar;
	hPrevInstance = hPrevInstance;
	lpCmdLine = lpCmdLineVar;
	nShowCmd = nShowCmdVar;

	windowedWidth = 640;
	windowedHeight = 480;
	windowedXPos = 10;
	windowedYPos = 10;

	if (!InitWindow() ) {
		MessageBox(0, "InitWindow returned false", "Error", MB_OK);
		PostQuitMessage(0);
	}

	if (!InitDirect3D() ) {
		MessageBox(0, "InitDirect3D returned false", "Error", MB_OK);
		PostQuitMessage(0);
	}

	MsgLoop();
}

//--------------------------------------------------------------------------------------------------------
// Destructor
//--------------------------------------------------------------------------------------------------------
d3dApp::~d3dApp()
{
	CleanUpDirect3D();
}

//--------------------------------------------------------------------------------------------------------
// InitWindow
//--------------------------------------------------------------------------------------------------------
bool d3dApp::InitWindow()
{
	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(NULL, IDC_ARROW);
	wcex.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
	wcex.lpszMenuName = 0;
	wcex.lpszClassName = "wcName";
	wcex.hIconSm = 0;

	RegisterClassEx(&wcex);

	RECT rect = { windowedXPos, windowedYPos, windowedWidth, windowedHeight };
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);

	hWnd = CreateWindow("wcName", "Shattered", WS_OVERLAPPEDWINDOW,
							 rect.left, rect.top, rect.right - rect.left, rect.bottom - rect.top,
							 NULL, NULL, hInstance, NULL);

	if (!hWnd) {
		MessageBox(0, "Creating window failed", "Error", MB_OK);
		return false;
	}

	ShowWindow(hWnd, SW_SHOW);
	UpdateWindow(hWnd);

	return true;
}

//--------------------------------------------------------------------------------------------------------
// MsgLoop
//--------------------------------------------------------------------------------------------------------
int d3dApp::MsgLoop()
{
	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;
}

//--------------------------------------------------------------------------------------------------------
// InitDirect3d
//--------------------------------------------------------------------------------------------------------
bool d3dApp::InitDirect3D ()
{	
	DXGI_SWAP_CHAIN_DESC	swapChainDesc;
	ZeroMemory(&swapChainDesc, sizeof(swapChainDesc));

	swapChainDesc.BufferCount = 1;
	swapChainDesc.BufferDesc.Width = windowedWidth;
	swapChainDesc.BufferDesc.Height = windowedHeight;
	swapChainDesc.BufferDesc.RefreshRate.Numerator = 60;
	swapChainDesc.BufferDesc.RefreshRate.Denominator = 1;
	swapChainDesc.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	swapChainDesc.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;

	swapChainDesc.SampleDesc.Count = 1;
	swapChainDesc.SampleDesc.Quality = 0;

	swapChainDesc.OutputWindow = hWnd;
	swapChainDesc.Windowed = true;

	HRESULT hr;

	hr = D3D10CreateDeviceAndSwapChain(NULL, D3D10_DRIVER_TYPE_REFERENCE, NULL, 0, D3D10_SDK_VERSION,
									   &swapChainDesc, &d3dSwapChain, &d3dDevice);

	if (FAILED(hr)) {
		MessageBox(0, "D3D10CreateDeviceAndSwapChain failed", "Error", MB_OK);
		return false;
	}

	ID3D10Texture2D *backBuffer;

	hr = d3dSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&backBuffer);

	if (FAILED(hr)) {
		MessageBox(0, "Getting buffer from d3dSwapChain failed", "Error", MB_OK);
		return false;
	}

	hr = d3dDevice->CreateRenderTargetView(backBuffer, NULL, &d3dRenderTargetView);
	backBuffer->Release();

	if (FAILED(hr)) {
		MessageBox(0, "Creating render targetview failed", "Error", MB_OK);
		return false;
	}

	d3dDevice->OMSetRenderTargets(1, &d3dRenderTargetView, NULL);

	D3D10_VIEWPORT viewport;
	viewport.Width = windowedWidth;
	viewport.Height = windowedHeight;
	viewport.MinDepth = -2.0f;
	viewport.MaxDepth = 2.0f;
	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	d3dDevice->RSSetViewports(1, &viewport);


	D3D10_TEXTURE2D_DESC desc;
	bodyTexture = CreateTexture((LPCSTR)"./Textures/body.dds");
	bodyTexture->GetDesc(&desc);

	return true;
}

//--------------------------------------------------------------------------------------------------------
// CleanUpDirect3D
//--------------------------------------------------------------------------------------------------------
void d3dApp::CleanUpDirect3D () 
{
	if (d3dRenderTargetView) {
		d3dRenderTargetView->Release();
	} if (d3dSwapChain) {
		d3dSwapChain->Release();
	} if (d3dDevice) {
		d3dDevice->Release();
	}
}

//--------------------------------------------------------------------------------------------------------
// Render
//--------------------------------------------------------------------------------------------------------
void d3dApp::Render()
{
	if (d3dDevice != NULL) {
		d3dDevice->ClearRenderTargetView(d3dRenderTargetView, D3DXCOLOR(0.0f, 0.0f, 0.0f, 1.0f));


		ID3D10Texture2D *backBuffer;
		HRESULT hr = d3dSwapChain->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&backBuffer);
		if(FAILED(hr)) {
			return;
		}

		/*D3D10_BOX sourceRegion;
		sourceRegion.left = 0;
		sourceRegion.right = 640;
		sourceRegion.top = 0;
		sourceRegion.bottom = 480 ;
		sourceRegion.front = 0;
		sourceRegion.back = 1;*/

		d3dDevice->CopyResource(backBuffer, bodyTexture);

		//d3dDevice->CopySubresourceRegion(backBuffer, 0, 0, 0, 0, bodyTexture, 0, &sourceRegion);

		//draw draw draaaaaw

		d3dSwapChain->Present(0, 0);
	}
}
//--------------------------------------------------------------------------------------------------------
// CreateTexture
//--------------------------------------------------------------------------------------------------------
ID3D10Texture2D* d3dApp::CreateTexture(LPCSTR sourceFile) 
{
	ID3D10Texture2D* texture2D;
	ID3D10Resource* resource;

	HRESULT hr;
	hr = D3DX10CreateTextureFromFile(d3dDevice, (LPCSTR)sourceFile, NULL, NULL, &resource, NULL);

	if (FAILED(hr)) {
		MessageBox(NULL, "Creating Texture from file failed", "Error", MB_OK);
		return false;
	}

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

	return texture2D;
}

winMain.cpp:
#include "d3dApp.h"

//-----------------------------------------------------------------
// Windows Entry Point
//-----------------------------------------------------------------

int APIENTRY WinMain (HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd) 
{
	d3dApp Application(hInstance, hPrevInstance, lpCmdLine, nShowCmd);
}

When I run the program, the program nicely steps through every piece of code (I double checked with the debugger). However, it doesn't display the texture I've made (its a .dds, 512x512) and on exiting the program, it gives the following error message:
 Debug Error!
Program: ...
Module: ...tX
programming\tutorials\chapter 3\debug\Chapter 3.exe
File:

Run-Time Check Failure #2 - Stack around the variable 'Application' was corrupted.

(Press Retry to debug the application)
I hope someone can see the errors I've made in my code! Thanks in advance! -Max Henger [Edited by - MadMax1992 on November 3, 2007 6:52:52 AM]

Share this post


Link to post
Share on other sites
Advertisement
If you haven't already, try stepping through the code with Direct3D's debug runtime enabled and have an eye on the debugger's output window. Make sure to set the debug output level as high as possible.

Share this post


Link to post
Share on other sites
It might sound stupid, but I've never enabled this directx runtime debugger you're talking about. I'll google on it, but if you have a link to a tutorial handy, could you give it to me?

Share this post


Link to post
Share on other sites
You need to add D3D10_CREATE_DEVICE_DEBUG as flag during device creation.

Your code doesn’t work because you can’t use CopyResource to transfer an image from a texture to the backbuffer. You have to use one of the draw methods.

Share this post


Link to post
Share on other sites
Ah, I found the problem thanks to the DirectX debugger!

Demirug, I found out that you can copy it to the backbuffer. The problem was that the backbuffer is stored as a bitmap or is castable to a bitmap, because when I used the body.dds texture, the debugger said:

Cannot invoke CopySubresourceRegion when the Formats of each Resource are not the same, or castable to each other. [ RESOURCE_MANIPULATION ERROR #281: COPYSUBRESOURCEREGION_INVALIDSOURCE ]

when I changed it to a bitmap with photoshop, it worked like a charm! Once again, thank you for the tip to the directx debugger :p

-MadMax

Share this post


Link to post
Share on other sites
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!