Click here to Skip to main content
15,889,992 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: Debug with breakpoint works, but debug without breakpoint or release mode fails ? Pin
Code-o-mat23-Jun-10 7:00
Code-o-mat23-Jun-10 7:00 
GeneralRe: Debug with breakpoint works, but debug without breakpoint or release mode fails ? Pin
oppstp23-Jun-10 7:21
oppstp23-Jun-10 7:21 
GeneralRe: Debug with breakpoint works, but debug without breakpoint or release mode fails ? Pin
Code-o-mat23-Jun-10 8:30
Code-o-mat23-Jun-10 8:30 
AnswerRe: Debug with breakpoint works, but debug without breakpoint or release mode fails ? [modified] Pin
Aescleal23-Jun-10 7:30
Aescleal23-Jun-10 7:30 
GeneralRe: Debug with breakpoint works, but debug without breakpoint or release mode fails ? Pin
oppstp23-Jun-10 15:58
oppstp23-Jun-10 15:58 
QuestionRAW Triangle Format: How to parse it properly Pin
ant-damage23-Jun-10 5:37
ant-damage23-Jun-10 5:37 
AnswerRe: RAW Triangle Format: How to parse it properly Pin
El Corazon23-Jun-10 5:44
El Corazon23-Jun-10 5:44 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
ant-damage23-Jun-10 5:55
ant-damage23-Jun-10 5:55 
Yes. I used that tutorial from "drunkenhyena.com" to understand more about the direct 3d
The code I'm working is huge.
Here is the whole code.

main.cpp
#include "common.h"
#pragma comment(lib, "d3d9")
#pragma comment(lib, "user32")
#pragma comment(lib, "gdi32")

extern IDirect3D9 *g_d3d9;
extern IDirect3DDevice9 *g_device;
extern D3DPRESENT_PARAMETERS g_pparams;
extern const unsigned Width;
extern const unsigned Height;

extern HRESULT Init_d3d9(IDirect3D9 **d3d9);
extern void Init_pparams(HWND hwnd, D3DFORMAT format, D3DFORMAT depth, D3DPRESENT_PARAMETERS *pparams);
extern HRESULT Init_device(IDirect3D9 *d3d9, HWND hwnd, D3DPRESENT_PARAMETERS *pparams, IDirect3DDevice9 **device);
extern void Kill_d3d9(IDirect3D9 **d3d9, IDirect3DDevice9 **device);

HRESULT load_RAW(const char *fname, IDirect3DVertexBuffer9 **buffer, int *count)
{
	HRESULT hr = E_FAIL;
	FILE *f = fopen(fname, "r");
	if(f)
	{
		(*count) = 0;
		float tmp[3] = {0};
		while(fscanf(f, "%g %g %g", &tmp[0], &tmp[1], &tmp[2]) == 3)(*count)++;

		rewind(f);
		HUD_VERTEX vert[(*count)];
		memset(vert, 0, sizeof(vert));
		for(int i = 0; i < (*count); i++)
		{
			fscanf(f, "%g %g %g", &(vert[i].x), &(vert[i].y), &(vert[i].z));
			if(ferror(f))
			{
				fclose(f);
				return hr;
			}
		}
		fclose(f);

		int byteLen = sizeof(vert);
		hr = g_device->CreateVertexBuffer(byteLen, D3DUSAGE_WRITEONLY, HUD_VERTEX_fvf, D3DPOOL_MANAGED, buffer, NULL);
		if(FAILED(hr))return hr;

		void *ptr = NULL;
		hr = (*buffer)->Lock(0, 0, &ptr, 0);
		if(FAILED(hr))return hr;

		memcpy(ptr, vert, byteLen);
		hr = (*buffer)->Unlock();
		if(FAILED(hr))return hr;
	}
	fclose(f);
	return hr;
}

// D3D related functions
void Resources_Init()
{
}

void Resources_Free()
{
}

HRESULT Device_Render(IDirect3DVertexBuffer9 *buffer, int count)
{
	HRESULT hr = D3D_OK;
	hr = g_device->Clear(0, NULL, D3DCLEAR_TARGET, D3DCOLOR_COLORVALUE(1.0, 1.0, 1.0, 1.0), 0.0, 0);
	if(FAILED(hr))return hr;

	hr = g_device->BeginScene();
	if(FAILED(hr))return hr;

	g_device->SetFVF(HUD_VERTEX_fvf);

	g_device->SetStreamSource(0, buffer, 0, sizeof(HUD_VERTEX));
	g_device->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0, count);

	g_device->EndScene();
	g_device->Present(NULL, NULL, NULL, NULL);
}

LRESULT CALLBACK WndProc(HWND hwnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	switch(message)
	{
		case WM_KEYDOWN:
		case WM_CLOSE:
		case WM_LBUTTONDOWN:
			PostQuitMessage(0);
			return 0;
		default: return DefWindowProc(hwnd, message, wParam, lParam);
	}
}

int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int iCmdShow)
{
	WNDCLASS wc;
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wc.hCursor = LoadCursor(NULL, IDC_ARROW);
	wc.hbrBackground = (HBRUSH)GetStockObject(GRAY_BRUSH);
	wc.lpszMenuName = NULL;
	wc.lpszClassName = "Render";
	RegisterClass(&wc);

	HWND hwnd = CreateWindow("Render", "Test window", WS_POPUP | WS_VISIBLE, 0, 0, Width, Height, NULL, NULL, hInstance, NULL);
	HRESULT hr = D3D_OK;
	hr = Init_d3d9(&g_d3d9);
	if(FAILED(hr))
	{
		LOG("Init_d3d9() failed!", hr);
		DestroyWindow(hwnd);
		return 0;
	}
	
	Init_pparams(hwnd, D3DFMT_A8R8G8B8, D3DFMT_D24S8, &g_pparams);
	hr = Init_device(g_d3d9, hwnd, &g_pparams, &g_device);
	if(FAILED(hr))
	{
		LOG("Init_device() failed!", hr);
		Kill_d3d9(&g_d3d9, &g_device);
		DestroyWindow(hwnd);
		return 0;
	}
	
	/*HUD_VERTEX box[] = {
		{200.0, 200.0, 0.0, 0.0, D3DCOLOR_COLORVALUE(0.0, 0.0, 0.0, 1.0)},
		{400.0, 200.0, 0.0, 0.0, D3DCOLOR_COLORVALUE(0.0, 0.0, 0.0, 1.0)},
		{200.0, 400.0, 0.0, 0.0, D3DCOLOR_COLORVALUE(0.0, 0.0, 0.0, 1.0)},
		{400.0, 400.0, 0.0, 0.0, D3DCOLOR_COLORVALUE(0.0, 0.0, 0.0, 1.0)}
	};

	int vert_count = sizeof(box) / sizeof(HUD_VERTEX);
	int byte_count = vert_count * sizeof(HUD_VERTEX);
	IDirect3DVertexBuffer9 *vert_buffer = NULL;
	hr = g_device->CreateVertexBuffer(byte_count, D3DUSAGE_WRITEONLY, HUD_VERTEX_fvf, D3DPOOL_MANAGED, &vert_buffer, NULL);
	if(FAILED(hr))
	{
		LOG("CreateVertexBuffer() failed!", hr);
		Kill_d3d9(&g_d3d9, &g_device);
		DestroyWindow(hwnd);
		return 0;
	}
	void *box_ptr = NULL;
	vert_buffer->Lock(0, 0, &box_ptr, 0);
	memcpy(box_ptr, box, byte_count);
	vert_buffer->Unlock();*/
	
	int circleTriangles = 0;
	IDirect3DVertexBuffer9 *circle = NULL;
	hr = load_RAW("circle.raw", &circle, &circleTriangles);
	if(FAILED(hr))
	{
		LOG("load_RAW() failed!", hr);
		Kill_d3d9(&g_d3d9, &g_device);
		DestroyWindow(hwnd);
		return 0;
	}

	
	/*
	IDirect3DTexture9 *box_texture = NULL;
	hr = D3DXCreateTextureFromFile(g_device, "test.png", &box_texture);
	if(FAILED(hr))
	{
		LOG("D3DXCreateTextureFromFile() failed!", hr);
		Kill_d3d9(&g_d3d9, &g_device);
		DestroyWindow(hwnd);
		return 0;
	}
	
	g_device->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1);
	g_device->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE);
	g_device->SetTextureStageState(0, D3DTSS_COLORARG2, D3DTA_DIFFUSE);
	g_device->SetTexture(0, box_texture);*/
	
	//HUD_VERTEX *cube = load_RAW("cube.raw");
	
	MSG msg;
	while(GetMessage(&msg, NULL, 0, 0))
	{
		hr = Device_Render(circle, circleTriangles + 1);
		if(FAILED(hr))
		{
			LOG("Device_Render() failed!", hr);
			Kill_d3d9(&g_d3d9, &g_device);
			DestroyWindow(hwnd);
			return 0;
		}

		hr = g_device->TestCooperativeLevel();
		if(FAILED(hr))
		{
			LOG("TestCooperativeLevel() failed!", hr);
			Kill_d3d9(&g_d3d9, &g_device);
			DestroyWindow(hwnd);
			return 0;
		}

		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}
	Kill_d3d9(&g_d3d9, &g_device);
	DestroyWindow(hwnd);
	return msg.wParam;
}


d3d9_api.cpp
#include "common.h"

// gameEngine decls
IDirect3D9 *g_d3d9 = Direct3DCreate9(D3D_SDK_VERSION);
IDirect3DDevice9 *g_device = NULL;
D3DPRESENT_PARAMETERS g_pparams = {0};
extern const unsigned Width = GetSystemMetrics(SM_CXSCREEN);
extern const unsigned Height = GetSystemMetrics(SM_CYSCREEN);

HRESULT Init_d3d9(IDirect3D9 **d3d9)
{
	LOG(__FUNCTION__);
	(*d3d9) = Direct3DCreate9(D3D_SDK_VERSION);
	if(!(*d3d9))return E_FAIL;
	else return D3D_OK;
}

void Init_pparams(HWND hwnd, D3DFORMAT format, D3DFORMAT depth, D3DPRESENT_PARAMETERS *pparams)
{
	LOG(__FUNCTION__);
	memset(pparams, 0, sizeof(D3DPRESENT_PARAMETERS));
	pparams->BackBufferCount = 1;
	pparams->MultiSampleType = D3DMULTISAMPLE_NONE;
	pparams->MultiSampleQuality = 0;
	pparams->SwapEffect = D3DSWAPEFFECT_DISCARD;
	pparams->hDeviceWindow = hwnd;
	pparams->Flags = 0;
	pparams->FullScreen_RefreshRateInHz = D3DPRESENT_RATE_DEFAULT;
	pparams->PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
	pparams->BackBufferFormat = format;
	
	if(depth == D3DFMT_UNKNOWN)pparams->EnableAutoDepthStencil = false;
	else pparams->EnableAutoDepthStencil = true;
	pparams->AutoDepthStencilFormat = depth;
	
	pparams->Windowed = false;
	pparams->BackBufferWidth = Width;
	pparams->BackBufferHeight = Height;
}

HRESULT Init_device(IDirect3D9 *d3d9, HWND hwnd, D3DPRESENT_PARAMETERS *pparams, IDirect3DDevice9 **device)
{
	LOG(__FUNCTION__);
	HRESULT hr = d3d9->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd, D3DCREATE_HARDWARE_VERTEXPROCESSING, pparams, device);
	return hr;
}

void Kill_d3d9(IDirect3D9 **d3d9, IDirect3DDevice9 **device)
{
	LOG(__FUNCTION__);
	if((*device))
	{
		(*device)->Release();
		(*device) = NULL;
	}
	if((*d3d9))
	{
		(*d3d9)->Release();
		(*d3d9) = NULL;
	}
}

extern void Resources_Init();
extern void Resources_Free();

HRESULT Reset_device(IDirect3DDevice9 *device, D3DPRESENT_PARAMETERS *pparams, HRESULT hr)
{
	if(hr == D3DERR_DEVICELOST)
	{
		Resources_Free();
		Sleep(500);
	}
	else if(hr == D3DERR_DEVICENOTRESET)
	{
		hr = device->Reset(pparams);
		if(SUCCEEDED(hr))Resources_Init();
	}
	return hr;
}


common.h
#include <d3d9.h>
#include <stdio.h>

class gameLog
{
public:
	gameLog(const char *file = "log.log")
	{
		FILE *f = fopen(file, "w");
		fclose(f);
	}

	void operator()(const char *str, HRESULT hr = D3D_OK, const char *file = "log.log")
	{
		FILE *f = fopen(file, "a");
		if(hr != D3D_OK) fprintf(f, "%s\tHRESULT: %#x\n", str, hr);
		else fprintf(f, "%s\n", str);
		fclose(f);
	}
};

static gameLog LOG;

struct HUD_VERTEX
{
	float x, y, z, rhw;
	unsigned color;
};

const unsigned HUD_VERTEX_fvf = D3DFVF_XYZRHW | D3DFVF_DIFFUSE;

typedef HRESULT (WINAPI *D3DXCreateTextureFromFile_ENTRY)(IDirect3DDevice9 *pDevice, LPCTSTR pSrcFile, IDirect3DTexture9 **ppTexture);

inline HRESULT WINAPI D3DXCreateTextureFromFile(IDirect3DDevice9 *pDevice, LPCTSTR pSrcFile, IDirect3DTexture9 **ppTexture)
{
	HINSTANCE hlib = LoadLibrary("d3dx9.dll");
	if(hlib)
	{
#ifdef UNICODE
		D3DXCreateTextureFromFile_ENTRY func = (D3DXCreateTextureFromFile_ENTRY)GetProcAddress(hlib, "D3DXCreateTextureFromFileW");
#else
		D3DXCreateTextureFromFile_ENTRY func = (D3DXCreateTextureFromFile_ENTRY)GetProcAddress(hlib, "D3DXCreateTextureFromFileA");
#endif
		if(func)return func(pDevice, pSrcFile, ppTexture);
		else return E_FAIL;
	}
	else return E_FAIL;
}

GeneralRe: RAW Triangle Format: How to parse it properly Pin
El Corazon23-Jun-10 6:03
El Corazon23-Jun-10 6:03 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
ant-damage23-Jun-10 6:05
ant-damage23-Jun-10 6:05 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
El Corazon23-Jun-10 6:45
El Corazon23-Jun-10 6:45 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
ant-damage23-Jun-10 7:01
ant-damage23-Jun-10 7:01 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
El Corazon23-Jun-10 7:03
El Corazon23-Jun-10 7:03 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
ant-damage23-Jun-10 8:08
ant-damage23-Jun-10 8:08 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
ant-damage23-Jun-10 8:16
ant-damage23-Jun-10 8:16 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
El Corazon23-Jun-10 8:21
El Corazon23-Jun-10 8:21 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
ant-damage24-Jun-10 9:41
ant-damage24-Jun-10 9:41 
GeneralRe: RAW Triangle Format: How to parse it properly Pin
El Corazon24-Jun-10 10:03
El Corazon24-Jun-10 10:03 
QuestionRead an executable and write the data to a file. Pin
ALLERSLIT23-Jun-10 5:14
ALLERSLIT23-Jun-10 5:14 
AnswerRe: Read an executable and write the data to a file. Pin
Not Active23-Jun-10 5:26
mentorNot Active23-Jun-10 5:26 
GeneralRe: Read an executable and write the data to a file. Pin
harold aptroot23-Jun-10 5:31
harold aptroot23-Jun-10 5:31 
GeneralRe: Read an executable and write the data to a file. Pin
Not Active23-Jun-10 6:00
mentorNot Active23-Jun-10 6:00 
GeneralRe: Read an executable and write the data to a file. Pin
harold aptroot23-Jun-10 6:15
harold aptroot23-Jun-10 6:15 
AnswerRe: Read an executable and write the data to a file. Pin
Code-o-mat23-Jun-10 5:28
Code-o-mat23-Jun-10 5:28 
GeneralRe: Read an executable and write the data to a file. Pin
ALLERSLIT23-Jun-10 5:46
ALLERSLIT23-Jun-10 5:46 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.