Archived

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

nanobyte

ID3DXSprite Help

Recommended Posts

Could someone please tell me how to use this interface? I have read the sdk but I just don''t get where it is usefull (or how to use it). Right now I''m using the sprite class that comes with "zen of direct3d game programming" and have modified it to do animation. Is there a tutorial on using id3dxsprite? Or what are the basics of it? Thanks

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
here is the very best article I found, it is all I needed to get started:

http://www.gamasutra.com/features/20010629/geczy_01.htm#l1

here is my current implementation of my graphics class, I am new so it might not be that good. I have the DrawImage function commented out because I''m not using it (the whole puppet thing that I''m doing is a lot better for my project, it will sort of act like a Z buffer). The height parameter to the Color[] is so I can give different heights different lighting, it will help give a sense of depth.

Basically the way you use it is every frame you call Start(), draw your tiles, then add your puppets then call DrawPuppets(), then call end. If you are using DrawImage just substitute that for the puppet stuff. Also you should have a map of textures and strings that loads up on initialization. Then when you construct images you get a texture pointer by passing in the name of the image. That way the class is able to hold all the direct X stuff and the rest of your program can be oblivious. I seem to be having trouble getting my map working so I''m going to have to implement my own tonight so that isn''t in yet. Oh and you''ll want to set the lighting, Graphics needs a private array of colors and use the set lighting commands to set ranges or individual levels to specific colors.

It isn''t finished and I plan on changing stuff so keep that in mind, you''ll notice that much of it is commented it out. Oh and the h and w members of image are half the height and width, that way images are centered on the x,y of the puppet. Angle is my own class, basically just a wrapper for double. This line: "sort(puppets.begin(),puppets.end(),Puppet::Compare());" sorts the puppet pointers by their upper height (which is also what they will be lit by in the color multiplication) Oh and Color has just one member, a long unsigned int and operator* is used for multiplying colors (which makes them darker btw, white is the identity). Hmm if anyone has any questions or suggestions for improvement I''d like to hear them.

class Image
{
public:
Rect region;
IDirect3DTexture8* texture;
Color tint;
int h,w;//half the height and width

};
class Puppet
{public:

int x;
int y;
Range range;
Color tint;
Angle angle;
double radius;
Image* image;
Creature* owner;

Puppet();

class Compare
{public:
bool operator()(Puppet* l, Puppet* r)
{
return l->range.upper < r->range.upper;
}
};
};
graphics.h
#if !defined(Graphics_header)
#define Graphics_header

#pragma warning(disable : 4786)

#include "stdafx.h"
#include "Angle.h"
#include "Color.h"
#include "Image.h"
#include "Puppet.h"
//#include
using namespace std;
#include
#include
#include

struct IDirect3D8;
struct IDirect3DDevice8;
struct IDirect3DTexture8;
typedef struct _D3DCAPS8 D3DCAPS8;
struct ID3DXSprite;

class Graphics
{public:

//----Constructor/Destructor------------------------------------

Graphics(HWND hwnd);
~Graphics();



//----Control-functions----------------------------------------------

bool StartFrame();
void DrawPuppets();
void EndFrame();
// void SetLighting(Range r, Color c);
void SetLighting(int i, Color c);
void SetLighting(Color c[]);

//----Uses-------------------------------------------------------------

void AddPuppet(Puppet* p);

void DrawTileImage
(
Image* i,//change to TileImage*
int x, int y, int h
);

IDirect3DTexture8* getTexture(string s);


//----data--------------------------------------------------------------
private:

IDirect3D8* pD3D;
IDirect3DDevice8* pd3dDevice;
D3DCAPS8 *pd3dcaps;
ID3DXSprite* pd3dxSprite;

vector puppets;
//priority_queue< Puppet*, vector, Puppet::Compare > puppets;
//comp;

class dumbStringCompare
{
bool operator() (string l, string r)
{ return string::operator< (l,r); }
};

//map< string,IDirect3DTexture8*, dumbStringCompare > textures;

IDirect3DTexture8* LoadTexture(char *filename);

};

#endif

//OBSOLETE//default parameters are used for drawing overlays
/*
void DrawImage
(
Image* i,
int x, int y, int h=255,
Angle a=0,
Color c=0xFFFFFFFF// = white
);*/

graphics.cpp

#include "stdafx.h"

#include "Graphics.h"
#include
#include
#include
#include
#include
#include

using namespace std;

Graphics::Graphics(HWND hWnd)
{
pd3dDevice = NULL;
pd3dxSprite = NULL;
pd3dcaps = new D3DCAPS8();
pD3D = Direct3DCreate8( D3D_SDK_VERSION );
D3DDISPLAYMODE d3ddm;

vector imageLibrary;

// Get the current desktop display mode
pD3D->GetAdapterDisplayMode( D3DADAPTER_DEFAULT,&d3ddm );

// Parameters for the D3DDevice.
// This sets the video format to match the current desktop display.
// Check docs for other options you can set here
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.BackBufferWidth = d3ddm.Width;
d3dpp.BackBufferHeight = d3ddm.Height;
d3dpp.BackBufferFormat = d3ddm.Format;

// Create the Direct3D device, using the default adapter (most systems
// only have one, unless they have multiple graphics hardware cards
// installed). See SDK for more details on other options here.
//turn REF into HAL to get default back
pD3D->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_REF,hWnd,D3DCREATE_SOFTWARE_VERTEXPROCESSING,&d3dpp, &pd3dDevice );

// Device state would normally be set here

//make the sprite thing
D3DXCreateSprite(pd3dDevice,&pd3dxSprite);
}

Graphics::~Graphics()//this might need more work
{
//put more stuff here
if (pd3dcaps)
delete pd3dcaps;
if( pd3dDevice )
pd3dDevice->Release();
if( pD3D )
pD3D->Release();
}

//default parameters are used for drawing overlays

//change to TileImage*
void Graphics::DrawTileImage(Image* i,int x,int y,int h);

bool Graphics::StartFrame()
{
if( pd3dDevice == NULL)
return false;
// Clear the backbuffer to red
pd3dDevice->Clear( 0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_ARGB(255,255,0,0), 1.0f, 0 );

// Begin the scene
if(SUCCEEDED( pd3dDevice->BeginScene()))
{
if(FAILED(pd3dxSprite->Begin()))
{ EndFrame();
return false;
}
return true;
}
return false;
}

void Graphics::EndFrame()
{
pd3dxSprite->End();
pd3dDevice->EndScene();
// Present the backbuffer contents to the display
pd3dDevice->Present( NULL, NULL, NULL, NULL );
}

// void Graphics::SetLighting(Range r, Color c);
void Graphics::SetLighting(int i, Color c);
void Graphics::SetLighting(Color c[]);

IDirect3DTexture8* Graphics::getTexture(string s)
{
return textures[s];

}//map textures


void Graphics::AddPuppet(Puppet* p)
{
puppets.push_back(p);
}

void Graphics::DrawPuppets()
{
Puppet* p;
sort(puppets.begin(),puppets.end(),Puppet::Compare());

while(!puppets.empty())//drawImage
{
p=puppets.back();
puppets.pop_back();

D3DXVECTOR2
scaling( (float)1, (float)1 ),
rcenter( (float)(p->image->w), (float)(p->image->h) ),
trans( (float)(p->x-p->image->w), (float)(p->y-p->image->h) );

pd3dxSprite->Draw
(
p->image->texture,
&(p->image->region),
&scaling,
&rcenter,
p->angle,
&trans,
p->image->tint * p->tint //p->tint*color[h]
);
}
}

IDirect3DTexture8* Graphics::LoadTexture(char *filename)
{
IDirect3DTexture8* pd3dTexture;
//pd3dTexture->Release();//THIS IS HOW TO AVOID MEMORY LEAKS
// Set black as our source color key. Use 0xFFFF00FF for magenta instead.
// Use 0x00000000 for no ''color key'' substitution
D3DCOLOR colorkey = 0xFF00FF00;
// The D3DX function will fill in the following image info for us
//D3DXIMAGE_INFO SrcInfo; // Optional, third to last param is &ScrInfo

// Load image from file - maintain size of original file.
// It is also possible to specify a new height/width and ask D3DX to filter
// the image to the new size (stretch/shrink). See SDK docs for details.
/*if(FAILED( */D3DXCreateTextureFromFileEx( pd3dDevice, filename, 0, 0, 1, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
colorkey, NULL , NULL, &pd3dTexture)/* )*/;//deal with error here




// Remember, textures sizes may be larger or smaller than your image,
// to allow for legal texture sizes & maximums on user''s hardware
return pd3dTexture;
}




/*void Graphics::DrawImage(Image* i,int x,int y,int h,Angle a,Color c)
{
D3DXVECTOR2
scaling( (float)1, (float)1 ),
rcenter( (float)(i->w), (float)(i->h) ),
trans( (float)(x-i->w), (float)(y-i->h) );

pd3dxSprite->Draw
(
i->texture,
&i->region,
&scaling,
&rcenter,
a,
&trans,
i->tint*c//i->tint*c*color[h]
);
}*/

//void Graphics::DrawTile(TileImage *ti_p, int xc, int yc)
//{

//}

Share this post


Link to post
Share on other sites