13 Replies - 1932 Views - Last Post: 17 October 2011 - 01:12 PM Rate Topic: -----

#1 XeVile  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-August 11

Starting DirectX 10 Programming

Posted 14 October 2011 - 07:29 AM

Well, now that I got the grasp of SFML I am going onto learning DirectX 10 (Only 2D for now). Now I have seen there are only 3 DirectX 10 Books. Out of them 2 are all about 3D and only one Begining, DirectX 10 Game Programming, has the 2D basics. And I managed to get hands on it(used). But, problem is that the book is very hard to understand, as it is VERY HEAVILY based on code examples rather than in the code.

Okay now I just need some tutorials or even books, could anyone show me to some. I have googled for it but to my luck none.

Thanks

Is This A Good Question/Topic? 0
  • +

Replies To: Starting DirectX 10 Programming

#2 XeVile  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-August 11

Re: Starting DirectX 10 Programming

Posted 14 October 2011 - 08:10 AM

Okay, I tried to put the code together for showing a texture/image but got an error.

CODE:-
#include <windows.h>
#include <d3d10.h>
#include <d3dx10.h>

HWND hWnd;
ID3D10Device* device;
ID3D10RenderTargetView* rtv;
IDXGISwapChain* sc;

void InitD3D(HWND hWnd);
void Render(void);
void CleanD3D(void);
ID3D10Texture2D* GetTexture2DFromFile(LPCWSTR filename);

//Message Loop
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
		//Handles Keyboard Input
	case WM_KEYDOWN:
		switch(wParam)
		{
		case VK_ESCAPE:
			PostQuitMessage(0);
		}

		//Handle Window Destroy/Close message
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	}

	//Forward any messages not handled
	return DefWindowProc(hWnd, msg, wParam, lParam);
}

//The Windows Main Function
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR cmdLine, int showCmd)
{
	WNDCLASSEX wCX;
	wCX.cbSize = sizeof(WNDCLASSEX);
	wCX.style = CS_HREDRAW | CS_VREDRAW;
	wCX.lpfnWndProc = WndProc;
	wCX.cbClsExtra = 0;
	wCX.cbWndExtra = 0;
	wCX.hInstance = hInstance;
	wCX.hIcon = 0;
	wCX.hCursor = LoadCursor(NULL, IDC_ARROW);
	wCX.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wCX.lpszMenuName = NULL;
	wCX.lpszClassName = TEXT("DirectX - Example 1");
	wCX.hIconSm = 0;

	RECT rect = {0, 0, 800, 600};
	AdjustWindowRect(&rect, WS_OVERLAPPEDWINDOW, FALSE);

	RegisterClassEx(&wCX);

	hWnd = CreateWindow(
		TEXT("DirectX - Example 1"),
		TEXT("DirectX - Example 1"),
		WS_OVERLAPPEDWINDOW,
		275,
		60,
		rect.right - rect.left,
		rect.bottom - rect.top,
		0,
		0,
		hInstance,
		0);

	if(!hWnd)
	{
		MessageBoxEx(0, TEXT("CreateWindow Failed!"), 0, 0, 0);
		return false;
	}

	ShowWindow(hWnd, showCmd);
	InitD3D(hWnd);

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

	while(WM_QUIT != msg.message)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE) == TRUE)
		{
			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}
		Render();
	}
	CleanD3D();
	return(int)msg.wParam;
}

void InitD3D(HWND hWnd)
{
	DXGI_SWAP_CHAIN_DESC scd;

	ZeroMemory(&scd, sizeof(DXGI_SWAP_CHAIN_DESC));

	scd.BufferCount = 1;
	scd.BufferDesc.Format = DXGI_FORMAT_R8G8B8A8_UNORM;
	scd.BufferUsage = DXGI_USAGE_RENDER_TARGET_OUTPUT;
	scd.OutputWindow = hWnd;
	scd.SampleDesc.Count = 1;
	scd.Windowed = true;

	D3D10CreateDeviceAndSwapChain(
		NULL,
		D3D10_DRIVER_TYPE_HARDWARE,
		NULL,
		0,
		D3D10_SDK_VERSION,
		&scd,
		&sc,
		&device
		);

	ID3D10Texture2D* pBackBuffer;
	HRESULT hr = sc->GetBuffer(0, __uuidof(ID3D10Texture2D), (LPVOID*)&pBackBuffer);
	if(hr != S_OK)
	{
		return;
	}

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

	device->CreateRenderTargetView(pBackBuffer, NULL, &rtv);
	device->CopySubresourceRegion(pBackBuffer, 0, 0, 0, 0, GetTexture2DFromFile(TEXT("Retro-VS.png")), 0, &sourceRegion);
	pBackBuffer->Release();

	device->OMSetRenderTargets(1, &rtv, NULL);

	D3D10_VIEWPORT viewport;

	ZeroMemory(&viewport, sizeof(D3D10_VIEWPORT));

	viewport.TopLeftX = 0;
	viewport.TopLeftY = 0;
	viewport.Width = 800;
	viewport.Height = 600;

	device->RSSetViewports(1, &viewport);
}

void Render(void)
{
	device->ClearRenderTargetView(rtv, D3DXCOLOR(0.0f, 0.2f, 0.4f, 1.0f));
	sc->Present(0, 0);
}

void CleanD3D(void)
{
	sc->Release();
	rtv->Release();
	device->Release();
}

ID3D10Texture2D* GetTexture2DFromFile(LPCWSTR filename)
{
	ID3D10Texture2D* texture2D = NULL;
	ID3D10Resource* pResource = NULL;

	HRESULT hr = D3DX10CreateTextureFromFile(
		device,
		LPCWSTR(filename),
		NULL,
		NULL,
		&pResource,
		NULL);

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

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

	return texture2D;
}


ERROR:-
Attached Image

Thanks
Was This Post Helpful? 0
  • +
  • -

#3 XeVile  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-August 11

Re: Starting DirectX 10 Programming

Posted 15 October 2011 - 10:04 AM

Ermm.. Anyone?
Was This Post Helpful? 0
  • +
  • -

#4 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Starting DirectX 10 Programming

Posted 15 October 2011 - 04:12 PM

When you click 'break' which line of code is pointed to?
Was This Post Helpful? 0
  • +
  • -

#5 Dev4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-May 10

Re: Starting DirectX 10 Programming

Posted 15 October 2011 - 04:27 PM

Sometimes if you don't specify a new name for lets say a quadratic before the rendering function you can get errors like that I went through the same thing not to long ago.
Was This Post Helpful? 0
  • +
  • -

#6 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Starting DirectX 10 Programming

Posted 15 October 2011 - 04:39 PM

View PostDev4life, on 16 October 2011 - 12:27 AM, said:

Sometimes if you don't specify a new name for lets say a quadratic before the rendering function you can get errors like that I went through the same thing not to long ago.

Not sure that makes sense. Have you got example code?
Was This Post Helpful? 0
  • +
  • -

#7 Dev4life  Icon User is offline

  • New D.I.C Head

Reputation: 0
  • View blog
  • Posts: 13
  • Joined: 12-May 10

Re: Starting DirectX 10 Programming

Posted 15 October 2011 - 05:14 PM

Yes in fact I do have an example. okay I about two months ago I was working with opengl trying to create and render a quadratic as such you would need do this after creating a pointer
cylinder_obj= gluNewQuadric();
, but if you just go ahead with the render function
gluCylinder(cylinder_obj, 60, 60, 1000, 20, 2);
then you're most likely to get an access violation reading at the time of debugging. So in order to fix this you first have to specify the name then put down the render function.
cylinder_obj= gluNewQuadric();  gluCylinder(cylinder_obj, 60, 60, 1000, 20, 2);


Yes in fact I do have an example. okay I about two months ago I was working with opengl trying to create and render a quadratic as such you would need do this after creating a pointer
cylinder_obj= gluNewQuadric();
, but if you just go ahead with the render function
gluCylinder(cylinder_obj, 60, 60, 1000, 20, 2);
then you're most likely to get an access violation reading at the time of debugging. So in order to fix this you first have to specify the name then put down the render function.
cylinder_obj= gluNewQuadric();  gluCylinder(cylinder_obj, 60, 60, 1000, 20, 2);
Although this might not be the source of the problem.

Sorry for the accidental double post.
Was This Post Helpful? 0
  • +
  • -

#8 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Starting DirectX 10 Programming

Posted 15 October 2011 - 07:21 PM

But it's identical code right?
Was This Post Helpful? 0
  • +
  • -

#9 XeVile  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-August 11

Re: Starting DirectX 10 Programming

Posted 16 October 2011 - 03:03 AM

It takes me to line 139:-
device->CopySubresourceRegion(pBackBuffer, 0, 0, 0, 0, GetTexture2DFromFile(TEXT("Retro-VS.png")), 0, &sourceRegion);


@Dev4Life I Dont understand. Please Explain.
Was This Post Helpful? 0
  • +
  • -

#10 Oler1s  Icon User is offline

  • D.I.C Lover
  • member icon

Reputation: 1397
  • View blog
  • Posts: 3,884
  • Joined: 04-June 09

Re: Starting DirectX 10 Programming

Posted 16 October 2011 - 08:42 AM

Quote

It takes me to line 139:-
And if you look at line 139, you can see three variables that are involved in memory access. device, pBackBuffer, and sourceRegion.

Since sourceRegion is an auto variable, that leaves pBackBuffer and device as the questionable variables. Are you sure they are valid? How do you know?
Was This Post Helpful? 0
  • +
  • -

#11 XeVile  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-August 11

Re: Starting DirectX 10 Programming

Posted 17 October 2011 - 12:55 AM

View PostOler1s, on 16 October 2011 - 08:42 AM, said:

Quote

It takes me to line 139:-
And if you look at line 139, you can see three variables that are involved in memory access. device, pBackBuffer, and sourceRegion.

Since sourceRegion is an auto variable, that leaves pBackBuffer and device as the questionable variables. Are you sure they are valid? How do you know?


As I said at the second post I "Tried" to put it together correctly from the examples in the book, so I dont know if it is correct. All I managed to do in DirectX was to set it up correctly and just show the window. Also, I would appreciate it if anyone can show me a Full Example code on displaying an image in DirectX 10 or show me to a good tutorial.

This post has been edited by XeVile: 17 October 2011 - 12:58 AM

Was This Post Helpful? 0
  • +
  • -

#12 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Starting DirectX 10 Programming

Posted 17 October 2011 - 05:15 AM

You need to understand it, not copy from tutorials and think you are making progress - you will never learn that way.

The most likely reason why this is happening is because you are failing to initialize your objects correctly and not checking to see if they have been successfully initialized before progressing. What you should be doing is inspecting the DirectX 10 docs and see how to properly initialize objects and handle any errors or failures that may arise. It will take a little bit of research but if you are really interested and determined you can work it out.
Was This Post Helpful? 0
  • +
  • -

#13 XeVile  Icon User is offline

  • New D.I.C Head

Reputation: 2
  • View blog
  • Posts: 44
  • Joined: 23-August 11

Re: Starting DirectX 10 Programming

Posted 17 October 2011 - 10:58 AM

Okay, only problem is that most of the DirectX documentation is highly based on 3d. But if initializing 2d is same as doing it in 3d then I am fine with it. I will still be looking into the documentation as you suggested.

Thanks
Was This Post Helpful? 0
  • +
  • -

#14 anonymous26  Icon User is offline

  • D.I.C Lover

Reputation: 2
  • View blog
  • Posts: 3,638
  • Joined: 26-November 10

Re: Starting DirectX 10 Programming

Posted 17 October 2011 - 01:12 PM

I didn't have much of a problem finding info! :nottalkingtoyou:
Was This Post Helpful? 0
  • +
  • -

Page 1 of 1