Sign in to follow this  
opprobriumark

'Simplifying' TEXTURED QUADS with CLASSES

Recommended Posts

Hello everyone, I'm posting another question because I've had good results from you brainiacs before! :) I am pretty new to the whole DirectX9 and Visual C++ 6 thing and am trying to simplify making a textured quad by creating a class called CTexturedQuad. I want to be able to create a textured quad of a tree with width 64, height 64, located on screen coordinate 200, 200 by typing: CTexturedQuad tree; tree.Create("tree.bmp", 1, 64, 64, 200, 200); I thought I had it right but I get the following error upon building winmain (which doesn't even instantiate a CTexturedQuad object): texquad.obj : error LNK2005: "struct IDirect3D9 * D3DObject" (?D3DObject@@3PAUIDirect3D9@@A) already defined in winmain.obj texquad.obj : error LNK2005: "struct IDirect3DDevice9 * D3DDevice" (?D3DDevice@@3PAUIDirect3DDevice9@@A) already defined in winmain.obj Debug/GraphEng00.exe : fatal error LNK1169: one or more multiply defined symbols found I have 2 files added to the project to implement the class: ////texquad.h///////////////////////////////////// #include "stdafx.h" #include <d3dx9.h> #include "direct3dstuff.h" class CTexturedQuad { public: bool IsAlive; CTexturedQuad(); void Move(float x, float y); bool Create(char *sourcefile, float num_frames, float width, float height, float xpos, float ypos); bool Draw(); ~CTexturedQuad(); private: Free(); char *sFilename; IDirect3DTexture9* pTexture; IDirect3DVertexBuffer9* pVB; CUSTOMVERTEX vertices[3]; VOID *pVertices; float width, height; float x, y; float xpos, ypos; float num_frames, cur_frame; }; and //texquad.cpp///////////////////////////////////////////////// #include "texquad.h" //DEFINITIONS FOR CTEXTUREDQUAD CTexturedQuad::CTexturedQuad() { pTexture = NULL; sFilename = NULL; pVB = NULL; pVertices = NULL; x = 0.0f; y = 0.0f; xpos = 300.0f; ypos = 300.0f; width = 64.0f; height = 64.0f; num_frames = 1; cur_frame = 0; IsAlive = true; } bool CTexturedQuad::Create(char *sourcefile, float num_frames, float width, float height, float posx, float posy) { if(FAILED(D3DXCreateTextureFromFileEx(D3DDevice, sFilename, D3DX_DEFAULT, D3DX_DEFAULT, 1, 0, D3DFMT_A8R8G8B8, D3DPOOL_MANAGED, D3DX_DEFAULT, D3DX_DEFAULT, 0xFF000000, NULL, NULL, &pTexture))) return false; if(FAILED(D3DDevice->CreateVertexBuffer(4 * sizeof(CUSTOMVERTEX), 0, D3DFVF_CUSTOMVERTEX, D3DPOOL_DEFAULT, &pVB, NULL))) return false; return true; } bool CTexturedQuad::Draw() { if(cur_frame == num_frames) cur_frame = 0; vertices[0].Create(xpos, ypos, .5f, 1.0f, cur_frame/num_frames, 0.0f); vertices[1].Create(xpos + width, ypos, .5f, 1.0f, cur_frame/num_frames+1/num_frames, 0.0f); vertices[2].Create(xpos + width, ypos + height, .5f, 1.0f, cur_frame/num_frames +1/num_frames, 1.0f); vertices[3].Create(xpos, ypos + height, .5f, 1.0f, cur_frame/num_frames, 1.0f); if (FAILED(pVB->Lock(0,0,(void**)pVertices, D3DLOCK_DISCARD))) return false; memcpy(pVertices, vertices, sizeof(vertices)); if (FAILED(pVB->Unlock())) return false; D3DDevice->SetTextureStageState(0, D3DTSS_COLOROP, D3DTOP_SELECTARG1); D3DDevice->SetTextureStageState(0, D3DTSS_COLORARG1, D3DTA_TEXTURE); D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAOP, D3DTOP_SELECTARG1); D3DDevice->SetTextureStageState(0, D3DTSS_ALPHAARG1, D3DTA_TEXTURE); D3DDevice->SetRenderState(D3DRS_SRCBLEND, D3DBLEND_SRCALPHA); D3DDevice->SetRenderState(D3DRS_DESTBLEND, D3DBLEND_INVSRCALPHA); D3DDevice->SetRenderState(D3DRS_ALPHABLENDENABLE, TRUE); if (FAILED(D3DDevice->SetTexture(0, pTexture))) return false; if (FAILED(D3DDevice->SetStreamSource(0, pVB, 0, sizeof(CUSTOMVERTEX)))) return false; if (FAILED(D3DDevice->SetFVF(D3DFVF_CUSTOMVERTEX))) return false; if (FAILED(D3DDevice->DrawPrimitive(D3DPT_TRIANGLEFAN, 0, 2))) return false; x = 0.0f; y = 0.0f; return true; } void CTexturedQuad::Move(float x, float y) { xpos += x; ypos += y; } CTexturedQuad::~CTexturedQuad() { if(pTexture) pTexture->Release(); if(pVB) pVB->Release(); } D3DObject and D3DDevice are global variables declared in another .h file along with my vertex class... //direct3dstuff.h///////////////////////////////////////////// #ifndef direct3dstuff_h #define direct3dstuff_h //DEFINES////////////////////////////////////////////////////////////////// #define D3DFVF_CUSTOMVERTEX (D3DFVF_XYZRHW | D3DFVF_TEX1) //PROTOTYPES/////////////////////////////////////////////////////////////// BOOL InitDirect3D(); BOOL Render(); void CleanUp(); //GLOBALS////////////////////////////////////////////////////////////////// LPDIRECT3D9 D3DObject; LPDIRECT3DDEVICE9 D3DDevice; class CUSTOMVERTEX { public: // Its coordinates float fX, fY, fZ, rhw; // Its texture coordinates float fU, fV; // A function to simplify initialization void Create(float fp_fX,float fp_fY,float fp_fZ, float fp_frhw, float fp_fU, float fp_fV) { fX=fp_fX; fY=fp_fY; fZ=fp_fZ; rhw = fp_frhw; fU=fp_fU; fV=fp_fV; } }; #endif I thought maybe it has something to do with D3DDevice being a global variable and the compiler thinks I'm referring to a 'D3DDevice' within the class's scope... I read something about the extern keyword but that didn't work out. I tried passing the device and object by reference to the constructor and ended up cross-eyed... What am I doing wrong? Please help!!! Markito

Share this post


Link to post
Share on other sites
you need to do:

#ifndef HEADERNAME
#define HEADERNAME

...

#endif

for every header file you have. I see it that you dont have it in your texquad.h, but you do have it in direct3dstuff.h. If you are using Visual C++, you could just do:

#pragma once

which does the same thing at the beginning of the file.

Share this post


Link to post
Share on other sites
Thanks for your input alnite. Actually, I checked that out also and I get the same error. I go the #ifndef--#endif route just to be safe. Does anyone have any ideas? Is my conceptualization off? My syntax? My organization? arrrggghhh, another unslept night.

Share this post


Link to post
Share on other sites
Thanks so much Hedos, it's finally compiling after your advice! now I haven't a clue why it's turning on and then closing without an error message... that'll be tomorrow nights mystery!

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