• Advertisement

Dan Caranfil

  • Content count

  • Joined

  • Last visited

Community Reputation

122 Neutral

About Dan Caranfil

  • Rank
  1. Smart pointers in a DirectX game

    snk_kid: Thanks for advices.
  2. Smart pointers in a DirectX game

    Thanks for your replies Quote: You have a list of meshes. Where do you store the textures for each mesh? What does your copy constructor do? I don't really follow what you're saying or what the problem is, but if you're asking about what copying a smart COM pointer does: It will typically increment the reference count of the underlying interface. I'm loading a mesh with textures from HDD and then do 'push_back' to place it in the stack. I do this many times for same mesh. This way I get many instances of the mesh in the container. Then what I want to do is change a texture on a particular mesh instance. If I don't use a copy constructor the texture is being applied on all instances of same type. If I use a copy constructor I can change the textures for each mesh individually. Here's my mesh class: function definition: #include "MeshObject.h" #include "FileUtil.h" #include "../common/dhUtility.h" #include "criptare.h" #include "utility.h" MeshObjMold::MeshObjMold() { m_dwNumMaterials = 0; m_pMaterialsBuffer = NULL; m_pMaterials = NULL; m_pTextures = NULL; m_pMesh = NULL; Clone = false; } MeshObjMold::~MeshObjMold() { string Message; string war = "WARNING: Mesh Object was not released. Mesh: "; Message = war + Name; if(Full) { Log(Message.c_str()); Log("\n"); } } HRESULT MeshObjMold::Initialise(string Id, char *szMeshFile, LPDIRECT3DDEVICE9 &pDevice) { Name = Id; Full = true; D3Ddev = pDevice; HRESULT rslt = D3D_OK; // First, load the .X file into a mesh object, and fill out the materials buffer rslt = D3DXLoadMeshFromX(szMeshFile, D3DXMESH_MANAGED, pDevice, NULL, &m_pMaterialsBuffer, NULL, &m_dwNumMaterials, &m_pMesh); if(FAILED(rslt)) { LogDX("Failed to load mesh from file",rslt); } // Next, get a pointer to the first element of data in the materials buffer, ready // to read out. D3DXMATERIAL* pMaterials = (D3DXMATERIAL*)m_pMaterialsBuffer->GetBufferPointer(); // Create 2 arrays, 1 for textures and 1 for materials. D3DXLoadMeshFromX puts the // number of materials/textures (always the same) into m_dwNumMaterials above. m_pMaterials = new D3DMATERIAL9[m_dwNumMaterials]; m_pTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials]; // Next, iterate through the pMaterials buffer. { for(int iCount = 0; iCount < (int)m_dwNumMaterials; iCount++) { // For each material buffer element, copy the D3DMATERIAL into this class' array. m_pMaterials[iCount] = pMaterials[iCount].MatD3D; m_pMaterials[iCount].Ambient = m_pMaterials[iCount].Diffuse; if (IsFileExist2(pMaterials[iCount].pTextureFilename)) // Check if there is a file under that name on disk and if yes make a texture out of it { if (DEBUG_V){Log("Loading texture from file: ");Log(pMaterials[iCount].pTextureFilename);Log("\n");} //Decrypt the texture file decrypt(pMaterials[iCount].pTextureFilename,50); // Finally, create a texture from the file specified in the mateirals buffer and put // it in the m_pTextures array. rslt=D3DXCreateTextureFromFile(pDevice, pMaterials[iCount].pTextureFilename, &m_pTextures[iCount]); if (DEBUG_V){if(FAILED(rslt)) {Log("\n");LogDX("Failed to load mesh texture from file",rslt); }} //Encrypt the texture file encrypt(pMaterials[iCount].pTextureFilename,50); if (DEBUG_V) {Log("Texture successifully loaded");Log("\n");} } else { if (DEBUG_V) //DEBUG_V code VV { Log("-------------------------------!ERROR!--------------------------------- \n"); if (pMaterials[iCount].pTextureFilename != NULL) { Log("Could not load file: "); Log(pMaterials[iCount].pTextureFilename); Log("\n"); Log("-------------------------------------------------------------------------\n"); } if (pMaterials[iCount].pTextureFilename == NULL) { Log("Bad X file format "); Log("\n"); Log("-------------------------------------------------------------------------\n"); } } //DEBUG_V code ^^ } } } // m_pMaterialsBuffer is a COM object created by D3DXLoadMeshFromX. We only need it when // reading the data from the X file - now that we've populated our mesh, materials and // textures we can get rid of it. m_pMaterialsBuffer->Release(); m_pMaterialsBuffer = NULL; return D3D_OK; } MeshObjMold::MeshObjMold(const MeshObjMold &OrigM) { Clone =true; // Mark object as clone (this is required for the ShutDown -> the m_pMesh is not released for clones; D3Ddev = OrigM.D3Ddev; Name = OrigM.Name; m_dwNumMaterials = OrigM.m_dwNumMaterials; m_pMesh = OrigM.m_pMesh; m_pMaterials = new D3DMATERIAL9[m_dwNumMaterials]; m_pTextures = new LPDIRECT3DTEXTURE9[m_dwNumMaterials]; for(int iCount = 0; iCount < (int)m_dwNumMaterials; iCount++) { m_pMaterials[iCount] = OrigM.m_pMaterials[iCount]; m_pTextures[iCount] = OrigM.m_pTextures[iCount]; } Identity = OrigM.Identity; } HRESULT MeshObjMold::ShutDown() { Full = false; Log("Shuting down "); Log(Name.c_str()); Log("\n"); // Free the materials array if(m_pMaterials) delete [] m_pMaterials; // If there are textures... if(m_pTextures) { // ...index through the texture array & release the texture object for(int iCount = 0; iCount < (int)m_dwNumMaterials; iCount++) { if(m_pTextures[iCount] != NULL) { m_pTextures[iCount]->Release(); m_pTextures[iCount] = NULL; } } // Don't forget to delete the array as well delete [] m_pTextures; } // Release the mesh object if(m_pMesh) { m_pMesh->Release(); m_pMesh = NULL; } m_dwNumMaterials = 0; m_pMaterialsBuffer = NULL; m_pMaterials = NULL; return D3D_OK; } HRESULT MeshObjMold::Render(LPDIRECT3DDEVICE9& pDevice) { HRESULT rslt = S_OK; // Rendering a mesh is very easy. Simply iterate through each material... { for(int iCount = 0; iCount < (int)m_dwNumMaterials; iCount++) { //... set the material and texture in the normal way rslt = D3Ddev->SetMaterial(&m_pMaterials[iCount]); if(FAILED(rslt)) { LogDX("Set Material Failed",rslt);} rslt = D3Ddev->SetTexture(0, m_pTextures[iCount]); if(FAILED(rslt)) {LogDX("Set Texture Failed",rslt);} //... then call ID3DXMesh::DrawSubset to draw the vertices in the mesh // that have this material and texture applied to them. rslt = m_pMesh->DrawSubset(iCount); if(FAILED(rslt)) { LogDX("Draw Subset Failed",rslt);} } } return S_OK; } class header file #ifndef MESHOBJECT_H #define MESHOBJECT_H #include "utility.h" #include <string> using namespace std; class MeshObjMold { private: // The actual mesh object LPD3DXBUFFER m_pMaterialsBuffer; // Receives the materials when the X file is loaded D3DMATERIAL9* m_pMaterials; // Array of materials extracted from m_pMaterialsBuffer DWORD m_dwNumMaterials; // The number of materials in this mesh bool Clone; bool Full; public: string Name,Identity; LPDIRECT3DDEVICE9 D3Ddev; LPDIRECT3DTEXTURE9* m_pTextures;// Array of textures extracted from m_pMaterialsBuffer MeshObjMold( const MeshObjMold & m); LPD3DXMESH m_pMesh; MeshObjMold(); ~MeshObjMold(); HRESULT Initialise(string Id,char* szMeshFile, LPDIRECT3DDEVICE9& pDevice); HRESULT Render(LPDIRECT3DDEVICE9& pDevice); HRESULT ShutDown(); }; #endif To change a texture I do this: list <ScnItm> ObjectList; // ScnItm inherits from MeshObjMold ScnItm ColorO[5]; //some code for (list <ScnItm>::iterator i = ObjectList.begin(); i != ObjectList.end(); ++i) { if (someting) i->m_pTextures[0] = ColorO[0].m_pTextures[0]; } [Edited by - Dan Caranfil on March 13, 2006 6:30:39 AM]
  3. I'm making a game that will use a lot of LPD3DXMESH meshes. After doing some research I found claims that the best method to avoid COM-specific memory leaks is using smart pointers. So I have this questions: 1. Is is easier to use smart pointers instead of AddRef - Release method? 2. Since I can't write a smart pointer myself yet, I plan using ATL CComPtr class. Do this instructions have to be followed in each ATL + DirectX project? 3. I'm staking my meshes in a STL list and want to be able to modify textures for each mesh in the stack. I managed putting up a copy constructor that gives me this behavior I want. My question is how will the smart pointer affect the copy constructor and do I need a copy constructor at all if I plan using a smart pointer?
  4. The problem I'm having is that it's impossible to alocate memory for a LPD3DXMESH object, new LPD3DXMESH doesn't work. How do you do this? Also does anyone know any resource about copy contructors for DirectX objects (I'm interested in a copy constructor for a mesh object of course)?
  5. 'const char *' to 'char *'

    Thanks! It worked just fine.
  6. 'const char *' to 'char *'

    Do you mean my method shold work or I should pass something else in my function? If the later how do I convert the Message.c_str() to a const char*. I googled for 'const char *' to 'char *' but nothing helpful came up.
  7. I'm trying to build a Log file but I keep getting this error: Quote: error C2664: 'Log' : cannot convert parameter 1 from 'const char *' to 'char *' Conversion loses qualifiers The problem is that my Log function takes as argument a 'char*' and I want to pass a std string. I'm trying this but it doesn't work: string Message; Log(Message.c_str()); Can someone point how this should be done?
  8. Find path to a Folder (C++)

    Quote: TCHAR szPath[MAX_PATH]; if(SUCCEEDED(SHGetFolderPath(NULL, CSIDL_MYPICTURES|CSIDL_FLAG_CREATE, NULL, 0, szPath))) { // do whatever you want sith szPath... } Thats exactly what I was looking for, thanks so much!
  9. I want to make my game save the screenshots to a folder in 'MyPictures'. How do I make my program do this? I have an approximate idea how it should be done but, if posible, I would like not the reinvent the wheel. Does anyone have any tutorial on this?
  10. Can't track down memory leaks

    Thanks for answer. I didn't managed to solve the problem though. I've commented out the DrawPrimitive call but then the debugger breaks on Present or another DrawPrimitive call. When I tried introducing the lowest AllocID it would break at a Win32Api call ( ShowWindow ). My application doesn't crash so I'll leave this issue aside for now.
  11. When I quit my application the debugger reports a number of memory leaks. If I enter the highest AllocID in the DirectX debugger panel,the Aplication breaks in a DrawPrimitive or a Present call wich is not helping at all. How can I find the source of this memory leaks? Thanks in advance.
  12. Debugging questions

    Thank you for link, that page has a lot of information.
  13. Debugging questions

    Hello. I have two questions: Could someone point some resources on debugging a DirectX application. (a tutorial or something similar). I have come across memory leaks several times and it takes a lot of time to figure out the problem. Many times I have to start over from a previous phaze of the project and rewrite everything line by line. Some sort of general debugging guide would be really helpful. My second question is how do you enable the Enhanced D3DDebugging. I keep getting this error: "D3D9 Helper: Enhanced D3DDebugging disabled; Application was not compiled with D3D_DEBUG_INFO" and I would like to have this Enhanced D3DDebugging turned on. As a side note I am already using the Debugging Runtime. Thanks in advance.
  14. Thanks for advice. I`ll keep that in mind.
  • Advertisement