Archived

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

2D with Direct3D

This topic is 5045 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Hi all, As a means of getting into DirectX I thought it might be interesting to implement a 2D version of pac-man. I''m only focusing on 2D images right now. My question is this... Is there a way to draw an image to the screen without making a 4-point vertex list to drape it over? (I don''t want to use DirectDraw.) Also, can someone point me to a simple tutorial on using 2D graphics in Direct3D? There seem to be a lot of little things such as camera angle (?) SetRenderState (?) and other details like that, which I fear might be causing me problems.

Share this post


Link to post
Share on other sites
use the sprite class, and theres a line class aswell.

heres 2 classes i use, one for loading textures, other for drawing sprites(the textures)

texture/

class DTexture {
private:
LPDIRECT3DTEXTURE9 lp_Texture;
int width;
int height;

friend class Vertex;
friend class Sprite;

public:

DTexture()
{
}

LPDIRECT3DTEXTURE9 getTexture()
{
return lp_Texture;
}

int getWidth()
{
return width;
}

int getHeight()
{
return height;
}

void load(LPDIRECT3DDEVICE9 lp_Device, string filename, DWORD colourKey=0)
{
lp_Texture=NULL;
D3DXIMAGE_INFO fileInfo={NULL};
D3DXGetImageInfoFromFile(filename.c_str() ,&fileInfo);
width =fileInfo.Width;
height=fileInfo.Height;
D3DXIMAGE_INFO d3dxImageInfo;
D3DXCreateTextureFromFileEx( lp_Device, filename.c_str() , fileInfo.Width, fileInfo.Height, 1, D3DPOOL_DEFAULT, D3DFMT_UNKNOWN, D3DPOOL_DEFAULT, D3DX_DEFAULT, D3DX_DEFAULT, colourKey, &d3dxImageInfo, NULL, &lp_Texture );
}

~DTexture()
{
if(lp_Texture != NULL )
{
lp_Texture->Release();
lp_Texture=NULL;
}
}
};


sprite/

class DSprite2 {
private:
LPD3DXSPRITE lp_Sprite;
D3DXVECTOR3 centre;
D3DXVECTOR3 position;
float rotation;
D3DXVECTOR2 scale;
DWORD colour;
RECT rect;
DWORD renderFlag;
D3DXVECTOR2 rCentre;

public:
void dsetRotationCentre(float x, float y)
{
rCentre = D3DXVECTOR2(x,y);
}

void dsetScale(float x, float y)
{
scale = D3DXVECTOR2(x,y);
}

void dsetRotation(float angle)
{
rotation = angle;
}

void dsetColour(int r, int g, int b)
{
colour = D3DCOLOR_XRGB(r,g,b);
}

void dsetCentre(float x, float y)
{
centre= D3DXVECTOR3(x,y,0);
}

void dsetPosition(float x, float y)
{
position= D3DXVECTOR3(x,y,0);
}

void dsetRenderFlag(DWORD flag)
{
renderFlag = flag;
}

void dsetFrame(float x, float y, float x2, float y2)
{
rect.left = x;
rect.top = y;
rect.right = x2;
rect.bottom = y2;
}
DSprite2()
{
dsetRotationCentre(113,113);
dsetScale(1,1);
dsetRotation(0);
dsetCentre(0,0);
dsetPosition(0,0);
dsetFrame(0,0,230,230);
dsetColour(255,255,255);
dsetRenderFlag(D3DXSPRITE_DONOTMODIFY_RENDERSTATE);
}

~DSprite2()
{
if( lp_Sprite != NULL )
{
lp_Sprite->Release();
lp_Sprite=NULL;
}
}

void create(LPDIRECT3DDEVICE9 lp_Device)
{
lp_Sprite=NULL;
D3DXCreateSprite(lp_Device, &lp_Sprite );
}

void draw(LPDIRECT3DTEXTURE9 texture)
{
lp_Sprite->Begin(renderFlag);
D3DXMATRIX mat;
D3DXMatrixTransformation2D(&mat,NULL,NULL,&scale,&rCentre,rotation,NULL);
lp_Sprite->SetTransform(&mat);
lp_Sprite->Draw( texture, &rect, ¢re , &position, colour );
lp_Sprite->End();
}
};



Share this post


Link to post
Share on other sites