Newbie: How would I do this??

Started by
17 comments, last by Coleco 19 years, 11 months ago
Here is my code:

QUADS
//---------------------------------------------------------------------------------//	Constructor//---------------------------------------------------------------------------------CSprite_QUAD::CSprite_QUAD(IDirect3DDevice9 *Device,			//D3D Device						   D3DPRESENT_PARAMETERS pPresent,		//D3D Present						   CTexture_D3DX *pTexture,				//Texture						   int fwidth,							//Width of FRAME						   int fheight)							//Height of FRAME{	VB_BUFFER_SIZE = 4;							// size of the vertext buffer	d3dDevice = Device;							// grab the D3D Device	texture = pTexture->GetTexture();			// grab the texture	//color key	D3DCOLOR colorkey = 0xFFFF00FF;	    //Create vertex buffer and set as stream source    d3dDevice->CreateVertexBuffer( sizeof(TLVERTEX) * VB_BUFFER_SIZE,								   D3DUSAGE_WRITEONLY,		//write only..faster								   D3DFVF_TLVERTEX,			//FVF format								   D3DPOOL_MANAGED,			//DirectX manages memory								   &VB,						//Vertex buffer								   NULL );	//Lock the vertex buffer and set default color and other stuff	VB->Lock(0, 0, (void **)&vertices, NULL);	    //Setup vertices    vertices[0].color = 0xFFFFFFFF;    vertices[1].color = 0xFFFFFFFF;    vertices[2].color = 0xFFFFFFFF;    vertices[3].color = 0xFFFFFFFF;    vertices[0].x = 0.0f;    vertices[0].y = 0.0f;    vertices[1].x = 1.0f;    vertices[1].y = 0.0f;	    vertices[2].x = 1.0f;    vertices[2].y = -1.0f;    vertices[3].x = 0.0f;    vertices[3].y = -1.0f;		//Unlock the vertex buffer	VB->Unlock();    //Set stream source to vertex buffer    d3dDevice->SetStreamSource(0, VB, 0, sizeof(TLVERTEX));	//get texture surface description	texture->GetLevelDesc(0, &surfaceDesc);	tex_width	= surfaceDesc.Width;	tex_height	= surfaceDesc.Height;	//setup dimensions	FrameWidth = fwidth;	FrameHeight = fheight;	NumFramesWide = tex_width / FrameWidth;	NumFramesTall = tex_height / FrameHeight;	U = 1.0f / (float)NumFramesWide;	V = 1.0f / (float)NumFramesTall;    //Setup destination rectangle    rDest.left		= x;    rDest.right		= x + tex_width;    rDest.top		= y;    rDest.bottom	= y + tex_height;	//other variables	scr_width = pPresent.BackBufferWidth;	scr_height = pPresent.BackBufferHeight;	// set the texture    d3dDevice->SetTexture(0, texture);	//Defaults	x = 0;	y = 0;	CurrentFrame = 0;	xspeed = 0;	yspeed = 0;	rotation = 0.0f;	BorderState		= BOUNCE;	}//---------------------------------------------------------------------------------//	Destructor//---------------------------------------------------------------------------------CSprite_QUAD::~CSprite_QUAD(){	//free resources	if(!VB == NULL) VB->Release();	if(!texture == NULL) texture->Release();}//---------------------------------------------------------------------------------//	Move//---------------------------------------------------------------------------------int CSprite_QUAD::Move(){	x += xspeed;	y += yspeed;	switch(BorderState)	{	case BOUNCE:		{			if(x <= MinXPos) xspeed = -xspeed;			if(x >= MaxXPos) xspeed = -xspeed;			if(y <= MinYPos) yspeed = -yspeed;			if(y >= MaxYPos) yspeed = -yspeed;		}		break;	case WRAP:		{			if(x <= MinXPos) x = MaxXPos;			if(x >= MaxXPos) x = MinXPos;			if(y <= MinYPos) y = MaxYPos;			if(y >= MaxYPos) y = MinYPos;		}		break;	}	return true;}//---------------------------------------------------------------------------------//	Draw//---------------------------------------------------------------------------------int CSprite_QUAD::Draw(){    float X;    float Y;	    //Get coordinates	rDest.left		= x;	rDest.top		= y;	rDest.right		= x + FrameWidth;	rDest.bottom	= y + FrameHeight;	    X = rDest.left - (float)(scr_width) / 2;    Y = -rDest.top + (float)(scr_height) / 2; 		rfSource.top	= ( CurrentFrame / NumFramesWide ) * V;	rfSource.left	= ( CurrentFrame % NumFramesTall ) * V;	rfSource.bottom	= rfSource.top + V;	rfSource.right	= rfSource.left + U;	//Lock the vertex buffer	VB->Lock(0, 0, (void **)&vertices, NULL);    //Setup vertices in buffer	//top left    vertices[0].z = 1.0f;    vertices[0].u = rfSource.left;    vertices[0].v = rfSource.top;		//top right    vertices[1].z = 1.0f;    vertices[1].u = rfSource.right;    vertices[1].v = rfSource.top;		//bottom right    vertices[2].z = 1.0f;    vertices[2].u = rfSource.right;    vertices[2].v = rfSource.bottom;		//bottom left    vertices[3].z = 1.0f;    vertices[3].u = rfSource.left;    vertices[3].v = rfSource.bottom;    //Setup translation and scaling matrices    D3DXMatrixScaling( &matScaling,					   (float)(rDest.right - rDest.left),					   (float)(rDest.bottom - rDest.top),					   1.0f );									// scales the sprite	    D3DXMatrixTranslation (&matTranslation, X, Y, 0.0f);		// moves the sprite    matTransform = matScaling * matTranslation;	    //Check if quad is rotated    if(rotation)    {        D3DXMATRIX matRotate;		        //Create rotation matrix about the z-axis        D3DXMatrixRotationZ (&matRotate, rotation);		        //Multiply matrices together        matTransform *= matRotate;    }	//Set world matrix to an identity matrix	d3dDevice->SetTransform(D3DTS_WORLD, &matTransform);    //Unlock vertex buffer    VB->Unlock();    //Set texture    d3dDevice->SetTexture(0, texture);    //Set stream source to vertex buffer    d3dDevice->SetStreamSource(0, VB, 0, sizeof(TLVERTEX));	//Draw image	d3dDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);	return true;}//---------------------------------------------------------------------------------//	SetColor//---------------------------------------------------------------------------------int CSprite_QUAD::SetColor(D3DCOLOR aColor){	//Lock the vertex buffer	VB->Lock(0, 0, (void **)&vertices, NULL);	    //Setup vertices    vertices[0].color = aColor;    vertices[1].color = aColor;    vertices[2].color = aColor;    vertices[3].color = aColor;		//Unlock the vertex buffer	VB->Unlock();	return true;}//---------------------------------------------------------------------------------//	SetCorner0//---------------------------------------------------------------------------------int CSprite_QUAD::AdjustVertex( float x0, float y0,							    float x1, float y1,							    float x2, float y2,							    float x3, float y3 ){	//Adding to Y coords moves the vertex UP	//Lock the vertex buffer	VB->Lock(0, 0, (void **)&vertices, NULL);    vertices[0].x = x0;    vertices[0].y = y0;    vertices[1].x = x1;    vertices[1].y = y1;    vertices[2].x = x2;    vertices[2].y = y2;    vertices[3].x = x3;    vertices[3].y = y3;	//Unlock the vertex buffer	VB->Unlock();	return true;}//---------------------------------------------------------------------------------//	Properties//---------------------------------------------------------------------------------CSprite_QUAD::SetX(int xx){	x = xx;}CSprite_QUAD::SetY(int yy){	y = yy;}CSprite_QUAD::SetXSpeed(int xs){	xspeed = xs;}CSprite_QUAD::SetYSpeed(int ys){	yspeed = ys;}CSprite_QUAD::SetFrame(int cf){	CurrentFrame = cf;}CSprite_QUAD::SetRotation(float ang)			// sets the rotation angle{	rotation = ang;}CSprite_QUAD::SetMinXPos(int mx){	MinXPos = mx;}CSprite_QUAD::SetMinYPos(int my){	MinYPos = my;}CSprite_QUAD::SetMaxXPos(int mx){	MaxXPos = mx;}CSprite_QUAD::SetMaxYPos(int my){	MaxYPos = my;}CSprite_QUAD::SetBorderState(int state){	BorderState = state;}LIGHTS   //---------------------------------------------------------------------------------//	Constructor//---------------------------------------------------------------------------------CLights::CLights( IDirect3DDevice9 *Device,			// d3d device				  D3DCAPS9 Caps,					// capabilities of graphics adapter				  CLog log,							// Log File				  float r,							// red				  float g,							// green				  float b,							// blue				  float a)							// ambience{	d3dDevice = Device;							// grab the D3D Device	d3dCaps = Caps;								// grab the capabilities	Log = log;									// grab the log file pointer	HRESULT hr;	// Set up a material	ZeroMemory( &mtrl, sizeof(D3DMATERIAL9) );    mtrl.Diffuse.r = mtrl.Ambient.r = r;    mtrl.Diffuse.g = mtrl.Ambient.g = g;    mtrl.Diffuse.b = mtrl.Ambient.b = b;    mtrl.Diffuse.a = mtrl.Ambient.a = a;	d3dDevice->SetMaterial( &mtrl );    D3DLIGHT9 light;	D3DXVECTOR3 vecDir;    ZeroMemory( &light, sizeof(D3DLIGHT9) );    light.Type = D3DLIGHT_SPOT;    light.Diffuse.r  = 1.0f;    light.Diffuse.g  = 0.0f;    light.Diffuse.b  = 0.0f;	light.Ambient.r = 0.0f;	light.Ambient.g = 0.0f;	light.Ambient.b = 0.0f;	light.Direction.x = 0.0f;   //tried diff values here	light.Direction.y = -10.0f;  //tried diff values here	light.Direction.z = 10.0f;  //tried diff values here	light.Attenuation0	= 1.0f;	light.Attenuation1	= 0.0f;	light.Attenuation2	= 0.0f;	light.Falloff = 0.0f;	light.Phi = 0.5f;	light.Theta = 1.0f;    light.Range	= 10.0f;	vecDir = D3DXVECTOR3(0,5,0);    	D3DXVec3Normalize( (D3DXVECTOR3*)&light.Direction, &vecDir );    d3dDevice->SetLight( 0, &light );	d3dDevice->LightEnable(0, TRUE );}


-cbmeeks

~ c o l ec o ~



Rock the cradle of love!
You stupid WANKER!
S i g n a l D E V .com
Metroid Classic



[edited by - coleco on May 25, 2004 3:34:34 PM]
Rock the cradle of love! You stupid WANKER!
Advertisement
Could you not do another quad for the light, that kinda goes on top of littable(is this a word?) objects, but goes behind unlittable(again?) objects.

If I''m correct, this is called Additive Blending.

--
You''re Welcome,
Rick Wong
- Google | Google for GameDev.net | GameDev.net''s DirectX FAQ. (not as cool as the Graphics and Theory FAQ)
I''ve experimented with this but I can''t get it to look natural.



~ c o l ec o ~



Rock the cradle of love!
You stupid WANKER!
S i g n a l D E V .com
Metroid Classic

Rock the cradle of love! You stupid WANKER!
try ricks idea, use a single quad, for the texture you want a circular gradient that fades to nothing near the edges. obviously you''ll move that around with your player.

add a lighteable property to your other tiles and test for collision(2d box-box would prol be fine) if you find a collsion do the additive blend previously suggested.

I''ve used this in 3d, with a billboard to mimic a "flash grenade" effect and it turned out ok.

Dredd
________________________________________

"To die with your sword still in its sheath is most regrettable" -- Miyomoto Musashi


"Let Us Now Try Liberty"-- Frederick Bastiat
I don''t see where you set the render state to enable lighting. Did you do that?

g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );

Chris
Chris ByersMicrosoft DirectX MVP - 2005
This gamedev article on soft 2d shadows might be of interest.

Edit: Fixing the clicky.

[edited by - lab-rat on May 26, 2004 9:01:21 AM]
quote:
I don''t see where you set the render state to enable lighting. Did you do that?

g_pd3dDevice->SetRenderState( D3DRS_LIGHTING, TRUE );


Yes, I have it in my screen setup code.


Thanks guys, I will try and check that article out.

-cbmeeks

~ c o l ec o ~



Rock the cradle of love!
You stupid WANKER!
S i g n a l D E V .com
Metroid Classic

Rock the cradle of love! You stupid WANKER!
That article is a bit too complex for this I think.. And quads of 2 triangles, don''t lit well in Direct3D. You need more if you want it to look good.

If you''d go for additive blending, you''d have this loop (pseudo-code):

Clear();EnableTesting(); // Alpha Testing always on// Layer 1Draw();// Layer 2EnableBlending( ADDITIVE ); // Alpha Blending for the lightsDraw();// Layer 3DisableBlending(); // Disable Alpha Blending and draw the remaining thingsDraw();


--
You''re Welcome,
Rick Wong
- Google | Google for GameDev.net | GameDev.net''s DirectX FAQ. (not as cool as the Graphics and Theory FAQ)
Try the rt_lighting sample here. It allows you to modify light parameters on the fly and observe their effect.

This topic is closed to new replies.

Advertisement