Archived

This topic is now archived and is closed to further replies.

shakazed

Ugh! Rendering call fails bigtime...

Recommended Posts

I have a class with graphics functions...looks liek this.
  
/////////////////////////////////////////////////////////////////////////////////////

//MAIN GAME CLASS/////////////////////////////////////////////////////////////////////

//////////////////////////////////////////////////////////////////////////////////////



#define MY_FVF (D3DFVF_XYZ|D3DFVF_TEX1)

//Graphics class, will handle all of the rendering and other graphics related operations

class cGraphics
{
public:
	
	bool Initialize(HWND hWnd);
	void Render(void);
	void Release(void);
	bool SetupBox(void);
	void RenderBox(void);

private:
	IDirect3D8 *m_iD3D8;
	IDirect3DDevice8 *m_iD3DDevice8;
	IDirect3DVertexBuffer8 *m_iD3DBoxVertBuf8;
	IDirect3DTexture8 *m_iD3DBoxTexture8;

	struct CUSTOMVERTEX 
	{
		float x,y,z;
		float tU,tV; //Texture coordinates

	};

};

//Sets up a device for rendering

bool cGraphics::Initialize(HWND hWnd)
{
	//Set all interface pointers to NULL

	m_iD3D8 = NULL;
	m_iD3DDevice8 = NULL;
	m_iD3DBoxVertBuf8 = NULL;
	m_iD3DBoxTexture8 = NULL;

	if(FAILED(m_iD3D8 = Direct3DCreate8(D3D_SDK_VERSION)))
		return false;

	D3DDISPLAYMODE d3ddm;
	if(FAILED(m_iD3D8->GetAdapterDisplayMode(D3DADAPTER_DEFAULT, &d3ddm)))
		return false;


	D3DPRESENT_PARAMETERS d3dpp;
	ZeroMemory(&d3dpp, sizeof(D3DPRESENT_PARAMETERS));
	d3dpp.Windowed = true;
	d3dpp.BackBufferFormat = d3ddm.Format;
	d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;

	if(FAILED(m_iD3D8->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hWnd, 
		D3DCREATE_SOFTWARE_VERTEXPROCESSING, &d3dpp, &m_iD3DDevice8)))
		return false;

	//Set some rendering states

	m_iD3DDevice8->SetRenderState(D3DRS_ZENABLE, D3DZB_TRUE);
	m_iD3DDevice8->SetRenderState(D3DRS_LIGHTING, FALSE);

	if(FAILED(D3DXCreateTextureFromFile(m_iD3DDevice8, "textures/Box_Side_01.bmp", &m_iD3DBoxTexture8)))
		return false;


	return true;
}


//In this function all the rendering takes place

void cGraphics::Render(void)
{

	//Clear the surface to a chosen color

	m_iD3DDevice8->Clear(0, NULL, D3DCLEAR_TARGET|D3DCLEAR_ZBUFFER, D3DCOLOR_XRGB(0,0,255), 1.0f, 0);

	m_iD3DDevice8->BeginScene();

	RenderBox();

	m_iD3DDevice8->EndScene();

	m_iD3DDevice8->Present(NULL,NULL,NULL,NULL);

}

//Releases all resources

void cGraphics::Release(void)
{
	if(m_iD3DBoxTexture8 != NULL)
	{
		m_iD3DBoxTexture8->Release();
		m_iD3DBoxVertBuf8 = NULL;
	}

	if(m_iD3DBoxVertBuf8 != NULL)
	{
		m_iD3DBoxVertBuf8->Release();
		m_iD3DBoxVertBuf8 = NULL;
	}

	if(m_iD3DDevice8 != NULL)
	{
		m_iD3DDevice8->Release();
		m_iD3DDevice8 = NULL;
	}

	if(m_iD3D8 != NULL)
	{
		m_iD3D8->Release();
		m_iD3D8 = NULL;
	}
}


bool cGraphics::SetupBox(void)
{
	CUSTOMVERTEX sBoxVertices[] = 
	
	{
		//First wall

		{-10.0f, -10.0f, 0.0f, 1.0f, 0.0f,},
		{-10.0f, 10.0f, 0.0f, 0.0f, 0.0f,},
		{10.0f, -10.0f, 0.0f, 1.0f, 1.0f,},
		{10.0f, 10.0f, 0.0f, 1.0f, 0.0f,}
	};

	if(FAILED(m_iD3DDevice8->CreateVertexBuffer(4*sizeof(CUSTOMVERTEX), 0, MY_FVF,
		D3DPOOL_DEFAULT, &m_iD3DBoxVertBuf8)))
		return false;

	VOID *pVertices;

	if(FAILED(m_iD3DBoxVertBuf8->Lock(0, sizeof(sBoxVertices), (BYTE**)&pVertices, 0)))
		return false;

	memcpy(pVertices, sBoxVertices, sizeof(sBoxVertices));

	m_iD3DBoxVertBuf8->Unlock();

	return true;
	

}

void cGraphics::RenderBox(void)
{

	D3DXMATRIX matWorld;
	//D3DXMatrixRotationY(&matWorld, D3DXToRadian());

	m_iD3DDevice8->SetTransform(D3DTS_WORLD, &matWorld);

	D3DXMATRIX matView;
	D3DXMatrixLookAtLH(&matView, &D3DXVECTOR3(0.0f,0.0f,-50.0f), //Camera position

	&D3DXVECTOR3(0.0f,0.0f,0.0f), //Look at position

	&D3DXVECTOR3(0.0f,1.0f,0.0f)); //Up direction


	m_iD3DDevice8->SetTransform(D3DTS_VIEW, &matView);

	D3DXMATRIX matProj;
	D3DXMatrixPerspectiveFovLH(&matProj, D3DX_PI/4, 1.0f, 1.0f, 100.0f);
	m_iD3DDevice8->SetTransform(D3DTS_PROJECTION, &matProj);

	m_iD3DDevice8->SetTexture(0, m_iD3DBoxTexture8); //Set the texture

	m_iD3DDevice8->SetStreamSource(0, m_iD3DBoxVertBuf8, sizeof(CUSTOMVERTEX));
	m_iD3DDevice8->SetVertexShader(MY_FVF);
	m_iD3DDevice8->DrawPrimitive(D3DPT_TRIANGLESTRIP, 0,2);
}

  
...and this is my WinMain file...
  

#include<windows.h>
#include<d3d8.h>
#include<d3dx8.h>
#include"cEngine.h"


///////////////////////////////////////////////////////////////////////////////

//GLOBALS//////////////////////////////////////////////////////////////////////

///////////////////////////////////////////////////////////////////////////////




char *WINDOW_CLASS_NAME = "My Class Name";
char *WINDOW_NAME = "Evoeka";

cGraphics cMyGraphics;

HWND hWnd;

////////////////////////////////////////////////////////////////////////////////

//FUNCTIONS/////////////////////////////////////////////////////////////////////

////////////////////////////////////////////////////////////////////////////////




LRESULT CALLBACK WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
	switch(msg)
	{
	case WM_DESTROY:
		{
			PostQuitMessage(0);
			return 0;
		}break;
	}

	return DefWindowProc(hWnd, msg,wParam, lParam);
}


int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
{

	WNDCLASSEX wClass;

	wClass.cbClsExtra = 0;
	wClass.cbSize = sizeof(WNDCLASSEX);
	wClass.cbWndExtra = 0;
	wClass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
	wClass.hCursor = LoadCursor(NULL, IDC_ARROW);
	wClass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
	wClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
	wClass.hInstance = hInstance;
	wClass.lpfnWndProc = WndProc;
	wClass.lpszClassName = WINDOW_CLASS_NAME;
	wClass.lpszMenuName = NULL;
	wClass.style = CS_HREDRAW|CS_VREDRAW;

	RegisterClassEx(&wClass);

	hWnd = CreateWindowEx(NULL,
		WINDOW_CLASS_NAME,
		WINDOW_NAME, WS_OVERLAPPEDWINDOW|WS_VISIBLE,
		100,100, 640, 480,
		NULL, NULL,hInstance,
		NULL);

	if(hWnd == NULL)
		return 0;

	MSG msg;
	
	cMyGraphics.Initialize(hWnd);
	cMyGraphics.SetupBox();
	cMyGraphics.Render();


	while(1)
	{
		if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
		{
			if(msg.message == WM_QUIT)
				break;

			TranslateMessage(&msg);
			DispatchMessage(&msg);
		}

		cMyGraphics.Render();
	}
	
	//Release all resources

	cMyGraphics.Release();

	return 0;

}//End Application

	
  
Now when I run this I get a nice distorted image of my desktop in my window and no textured quad. Any ideas?
Bad Monkey Productions

Share this post


Link to post
Share on other sites