Sign in to follow this  
rms

ID3DXSprite and IDirect3DTexture9

Recommended Posts

rms    122
I'm trying to simplify sprite-creation in my 2D-game by making a new class for it, but I've run into some problems. Here is my code:
//CSprite.h
#ifndef CSPRITE_H
#define CSPRITE_H

class CSprite
{
public:
	void Init(LPCSTR file, unsigned int m_width, unsigned int m_height, float m_x, float m_y);
	void Free();
	void Render();
private:
	ID3DXSprite* sprite;
	IDirect3DTexture9* texture;
	unsigned int c_width;
	unsigned int c_height;
	float c_x;
	float c_y;
};

#endif

//CSprite.cpp
#include "CShared.h"
#include "CSprite.h"

void CSprite::Init(LPCSTR file, unsigned int m_width, unsigned int m_height, float m_x, float m_y)
{
	c_width = m_width;
	c_height = m_height;
	c_x = m_x;
	c_y = m_y;
	D3DXCreateSprite(shared.D3DDevice, &sprite);
	D3DXCreateTextureFromFileEx(shared.D3DDevice, file, c_width, c_height, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_DEFAULT, D3DX_FILTER_NONE, D3DX_DEFAULT, 0xFF000000, NULL, NULL, &texture);
}

void CSprite::Free()
{
	SAFE_RELEASE(sprite);
	SAFE_RELEASE(texture);
}

void CSprite::Render()
{
	RECT rect = {0, 0, c_width, c_height};
	sprite->Begin(D3DXSPRITE_ALPHABLEND);
	sprite->Draw(texture, &rect, &D3DXVECTOR3(0.0f, 0.0f, 0.0f), &D3DXVECTOR3(c_x, c_y, 1.0f), 0xFFFFFFFF);
	sprite->End();
}

//In main InitResources()
CSprite s; //Actually this is declared as private in an other class where I do the rendering, but I don't think that is the problem.
s.Init("test.png", 194, 147, 1.0f, 1.0f);

//In main RenderScene()
s.Render();

When running this the game crashes and throws a memory exception in the s.Render-method. When error-checking, I got D3DERR_INVALIDCALL from both D3DXCreateSprite() as well as D3DXCreateTextureFromFileEx(). So I guess that the problem is there and not in the Render()-method? I tried declaring sprite and texture as public, but it didn't help. So what am I doing wrong, or should I do it different? This is working when not putting it in it's own class (by using it directly in main InitResources() and RenderScene()).

Share this post


Link to post
Share on other sites
rms    122
Thanks for your reply. I don't think it has to do with that though. As I said I have tried the code outside the class, and it worked.
I tried changing to a 64x64 texture now, but still no luck. I believe it has something to do with the declaration of "sprite" and "texture" but I can't find out what.

Share this post


Link to post
Share on other sites
IFooBar    906
Have you checked the debug output? DX usually tells you what's wrong. Hit F5 and see what it says? Are the sprite and texture functions being completed successfully? Maybe the texture interface is invalid, so when you pass it to the draw function your app crashes...

Share this post


Link to post
Share on other sites
Hamster    247
This is a bit of a guess as I'm primarily from an OpenGL background and haven't really done much DirectX yet. Anyway, in OpenGL you can get hard to track errors if you start trying to create textures and suchlike before you've completely initliased the window and requested whatever pixel format you'll be using, etc. Not sure if things work the same in direct X, but I assume they're essentially the same once it gets down to hardware level, so you might want to make sure that you've initialised DX as much as possible prior to trying to load the texture.

Best of Luck

John

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
The problem is somewhere in the s.Render() function. I think it might be when the program tries to access the "texture" pointer. Make sure the file is loading properly, the D3DXCreateTextureFromFileEx() might be failing. This will cause the texture pointer to become invalid, "memory exception". If you run this on a debugger it should tell you where this exception is occuring.

Share this post


Link to post
Share on other sites
rms    122
Quote:
Original post by Hamster
This is a bit of a guess as I'm primarily from an OpenGL background and haven't really done much DirectX yet. Anyway, in OpenGL you can get hard to track errors if you start trying to create textures and suchlike before you've completely initliased the window and requested whatever pixel format you'll be using, etc. Not sure if things work the same in direct X, but I assume they're essentially the same once it gets down to hardware level, so you might want to make sure that you've initialised DX as much as possible prior to trying to load the texture.

Best of Luck

John


OK, thanks everyone for your replies. This was my problem though, so thanks*2 to you John/Hamster. :)
I moved my default-state loading code from my state-manager constructor to a public function instead, which I call after the device is created.

Now I'm going to try out DirectSound and DirectInput. Wish me luck :)

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this