Skip to main content
gamedev.net
🔒 Locked

Unexplainable bug

Started by Programmer16 Jan 27, 2004 at 11:26 PM 2 replies 400+ views
Original Post
Programmer16
Programmer16
I''m in the middle of creating a DirectX (version 8.1) wrapper, and I was adding a texture class. Its the same layout I always use, a static IDirect3DDevice8 pointer. The only difference is the addition of a static D3DFORMAT variable. The problem is when I run the program, I get no errors, no output (from my error checking), but the program closes. If I remove the file, the program works right. It starts, clears the screen, and writes to a file that: Could not create texture, please make sure that "Data\TreeTownExt.bmp" exists. And it continues to run until I press escape. Here is my code: // Texture.h
#ifndef _THE_CORE__TEXTURE_H_
#define _THE_CORE__TEXTURE_H_

#include <d3d8.h>
#include <d3dx8.h>
#include <dxerr8.h>
#include <fstream.h>
#include <stdio.h>

class CTexture  
{
public:
	static IDirect3DDevice8* m_pDevice;
	static D3DFORMAT m_fmtFormat;
	IDirect3DTexture8* m_pTexture;
	char* m_pFilename;

	CTexture();
	~CTexture();
	HRESULT Create(char* pFilename);
	void Destroy();
	HRESULT LoadFromFile(char* pFilename);
	void SetDevice(IDirect3DDevice8* pDevice);
	void SetFormat(D3DFORMAT fmtFormat);
	void LogText(char* pString, ...);
};

#endif // #ifndef _THE_CORE__TEXTURE_H_

// Texture.cpp
//##############################################################################

// Texture.cpp

//##############################################################################


#include "Texture.h"

//##############################################################################

// Static member variables

//##############################################################################

IDirect3DDevice8* CTexture::m_pDevice = NULL;
D3DFORMAT CTexture::m_fmtFormat = D3DFMT_UNKNOWN;

//##############################################################################

// Constructor / Destructor

//##############################################################################

CTexture::CTexture()
{
	m_pTexture = NULL;
}

CTexture::~CTexture()
{
	Destroy();
}

//##############################################################################

// Create() / Destroy()

//##############################################################################

HRESULT CTexture::Create(char* pFilename)
{
	HRESULT hResult = LoadFromFile(pFilename);
	if(FAILED(hResult))
		LogText("Failed to create texture.");

	return hResult;
}

void CTexture::Destroy()
{
	SafeRelease(m_pTexture);
}

//##############################################################################

// SetDevice() / SetFormat()

//##############################################################################

void CTexture::SetDevice(IDirect3DDevice8* pDevice)
{
	m_pDevice = pDevice;
}

void CTexture::SetFormat(D3DFORMAT fmtFormat)
{
	m_fmtFormat = fmtFormat;
}

//##############################################################################

// LoadFromFile()

//##############################################################################

HRESULT CTexture::LoadFromFile(char* pFilename)
{
	m_pFilename = pFilename;

	HRESULT hResult = D3DXCreateTextureFromFileEx(m_pDevice, pFilename, D3DX_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, 0, m_fmtFormat, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_FILTER_NONE, 0x00ff00ff, NULL, NULL, &m_pTexture);
	if(FAILED(hResult))
	{
		LogText("Error creating texture. Please make sure %s exists.", pFilename);
		return hResult;
	}

	return hResult;
}

//##############################################################################

// LogText()

//##############################################################################

void CTexture::LogText(char* pString, ...)
{
	char Text[1024];
	va_list valist;
	
	
	va_start(valist,pString);
	vsprintf(Text,pString,valist);
	
	va_end(valist);
	
	ofstream fout("TheCore_ErrorLog.txt",ios::app);
	fout<<Text<<endl;
	fout.close();
}
Then I use this my project''s source code:
// After setting Direct3D up

// g_Texture defined globally

// g_Gfx: Global instance of my Direct3D wrapper

g_Texture.SetDevice(g_Gfx.m_pDevice);
g_Texture.SetFormat(D3DFMT_A8R8G8B8);
It can''t be my loading function, since I don''t get any output (or errors from Microsoft), so what could it be?
Programmer16
Programmer16
I''m really sorry about this, but I figured out WHERE it''s doing it, but not why. In my CGraphics class, I have an pointer of IDirect3D8, IDirect3DDevice8, and ID3DXSprite. To make things easy, I have added 4 draw functions, 2 that take IDirect3DTexture, and 2 that take my own CTexture. There are two so that the user can send a POINT or a D3DXVECTOR2. Here are the functions. (Note: in the declaration, all parameters of all 4 draw functions are set to NULL as default parameters)

HRESULT CGraphics::Draw(IDirect3DTexture8* pTexture, RECT* pRect, POINT* pPoint)
{
D3DXVECTOR2 Vector = D3DXVECTOR2((float)pPoint->x, (float)pPoint->y);
return Draw(pTexture, pRect, &Vector);
}

HRESULT CGraphics::Draw(IDirect3DTexture8* pTexture, RECT* pRect, D3DXVECTOR2* pVector)
{
return m_pSprite->Draw(pTexture, pRect, NULL, NULL, 0, pVector, 0xffffffff);
}

HRESULT CGraphics::Draw(CTexture Texture, RECT* pRect, POINT* pPoint)
{
D3DXVECTOR2 Vector = D3DXVECTOR2((float)pPoint->x, (float)pPoint->y);
return Draw(Texture.m_pTexture, pRect, &Vector);
}

HRESULT CGraphics::Draw(CTexture Texture, RECT* pRect, D3DXVECTOR2* pVector)
{
return m_pSprite->Draw(Texture.m_pTexture, pRect, NULL, NULL, 0, pVector, 0xffffffff);
}


Thanks in advance
HaywireGuy
HaywireGuy
If you have used the first Draw() function with the default
parameter values, the "pPoint->x" will cause an access
violation, won''t it? It''ll be better if you can check the
values before touchin'' them, especially with pointers.





Programmer16
Programmer16
Okay, I fixed it. Evidentally I should be passing CTexture as a pointer? I didn''t think this would effect it. Thanks HaywireGuy, I didn''t think about that. How come I wasn''t getting anything from MVC++? It didn''t output anything to the debug window.

/*
I use DirectX 8.1 and C++ (Microsoft Visual C++ 6.0 Professional edition)
*/

Topic Locked

This topic has been locked by a moderator. New replies are not allowed.

Sign in to reply to this topic.