Hey all,
***SCROLL DOWN FOR NEWER PROBLEM***
I've been attempting to get some 2D graphics displayed using textured quads. Everything seems to be loading fine, but when I display my quad, I only get 1 triangle and it isn't the right size or in the right position.
HRESULT Graphics2D::Init(HWND hwnd, bool windowed){
d3dObject = Direct3DCreate9(D3D_SDK_VERSION);
if (d3dObject == NULL)
return E_FAIL;
D3DPRESENT_PARAMETERS d3dpp;
ZeroMemory( &d3dpp, sizeof(d3dpp) );
d3dpp.BackBufferWidth = 1024;
d3dpp.BackBufferHeight = 768;
d3dpp.Windowed = windowed;
d3dpp.SwapEffect = D3DSWAPEFFECT_FLIP;
d3dpp.hDeviceWindow = hwnd;
d3dpp.BackBufferCount = 1;
d3dpp.BackBufferFormat = D3DFMT_A8R8G8B8;
d3dpp.PresentationInterval = D3DPRESENT_INTERVAL_IMMEDIATE;
if( FAILED( d3dObject->CreateDevice( D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING, &d3dpp, &d3dDevice ) ) )
{
return E_FAIL;
}
d3dDevice->SetVertexShader(NULL);
d3dDevice->SetFVF(D3DFVF_TLVERTEX);
d3dDevice->CreateVertexBuffer(sizeof(TLVERTEX) * 4, NULL,
D3DFVF_TLVERTEX, D3DPOOL_MANAGED, &vertexBuffer, NULL);
d3dDevice->SetStreamSource(0, vertexBuffer, 0, sizeof(TLVERTEX));
d3dDevice->SetRenderState(D3DRS_LIGHTING, FALSE);
d3dDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE);
d3dDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA);
d3dDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA);
d3dDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_MODULATE);
font -> InitDeviceObjects( d3dDevice );
font -> RestoreDeviceObjects();
quad = new TexQuad(this, "rainbowtex.jpg");
position.left = 64;
position.right = 128;
position.top = 64;
position.bottom = 128;
return S_OK;
}
IDirect3DTexture9* Graphics2D::LoadTexture(char *fileName)
{
IDirect3DTexture9 *d3dTexture;
D3DXIMAGE_INFO SrcInfo; //Optional
//Use a magenta colourkey
D3DCOLOR colorkey = 0xFFFF00FF;
// Load image from file
if (FAILED(D3DXCreateTextureFromFileEx (d3dDevice, fileName, 0, 0, 1, 0,
D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_FILTER_NONE, D3DX_DEFAULT,
colorkey, &SrcInfo, NULL, &d3dTexture)))
{
MessageBox(NULL, "", "NO TEXTURE", MB_OK);
return NULL;
}
//Return the newly made texture
return d3dTexture;
}
//Draw a textured quad on the back-buffer
void Graphics2D::BlitD3D (TexQuad* texture, RECT* rDest, D3DCOLOR vertexColour)
{
TLVERTEX* vertices = texture->getVerticies();
//Lock the vertex buffer
vertexBuffer->Lock(0, 0, (void**)&vertices, NULL);
//Setup vertices
//A -0.5f modifier is applied to vertex coordinates to match texture
//and screen coords. Some drivers may compensate for this
//automatically, but on others texture alignment errors are introduced
//More information on this can be found in the Direct3D 9 documentation
vertices[0].colour = vertexColour;
vertices[0].x = (float) rDest->left - 0.5f;
vertices[0].y = (float) rDest->top - 0.5f;
vertices[0].z = 0.0f;
vertices[0].rhw = 1.0f;
vertices[0].u = 0.0f + (TEXDIM * texture->getTexRef().animPoint);
vertices[0].v = 0.0f + (TEXDIM * texture->getTexRef().posNum);
vertices[1].colour = vertexColour;
vertices[1].x = (float) rDest->right - 0.5f;
vertices[1].y = (float) rDest->top - 0.5f;
vertices[1].z = 0.0f;
vertices[1].rhw = 1.0f;
vertices[1].u = TEXDIM + (TEXDIM * texture->getTexRef().animPoint);
vertices[1].v = 0.0f + (TEXDIM * texture->getTexRef().posNum);
vertices[2].colour = vertexColour;
vertices[2].x = (float) rDest->right - 0.5f;
vertices[2].y = (float) rDest->bottom - 0.5f;
vertices[2].z = 0.0f;
vertices[2].rhw = 1.0f;
vertices[2].u = TEXDIM + (TEXDIM * texture->getTexRef().animPoint);
vertices[2].v = TEXDIM + (TEXDIM * texture->getTexRef().posNum);
vertices[3].colour = vertexColour;
vertices[3].x = (float) rDest->left - 0.5f;
vertices[3].y = (float) rDest->bottom - 0.5f;
vertices[3].z = 0.0f;
vertices[3].rhw = 1.0f;
vertices[3].u = 0.0f + (TEXDIM * texture->getTexRef().animPoint);
vertices[3].v = TEXDIM + (TEXDIM * texture->getTexRef().posNum);
//Unlock the vertex buffer
vertexBuffer->Unlock();
//Set texture
d3dDevice->SetTexture (0, texture->getTexRef().d3dTex);
//Draw image
d3dDevice->DrawPrimitive (D3DPT_TRIANGLEFAN, 0, 2);
}
TexQuad::TexQuad(Graphics2D* handle, char* filename)
{
texRef.d3dTex = handle->LoadTexture("C:\\Programming\\NewGame\\yellow.jpg");
texRef.posNum = 0;
texRef.animPoint = 0;
}
Thanks in advance for any help,
Jeff
[Edited by - GodlyGamr on June 5, 2005 6:18:46 PM]