CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 10 of 10
  1. #1
    Join Date
    Dec 2010
    Posts
    907

    Basic Data Persistance question

    Code:
    struct Mesh
    {
    public:
         // a pointer to a series of frames
         LPD3DXFRAME m_Root;
    };
    
     
    struct Shape
    {
             // Get the best mesh suited for the camera's viewpoint, allows LOD.
             virtual const Mesh* getMesh( const CCamera& camera ) const = 0;
    		 virtual void setMesh(Mesh *mesh) = 0;
    };
    
    struct WarehouseShape : public Shape
    {
    
    	const Mesh* getMesh( const CCamera& camera ) const { return m_pMesh; }
    	void setMesh(Mesh *mesh) { m_pMesh = mesh; }
    
    	Mesh *m_pMesh;
    	
    };
    
    struct Object
    {
    	 
        Shape* shape;
        bool Load(TCHAR* pFilename);
        D3DXMATRIX transform;
        LPD3DXANIMATIONCONTROLLER m_pAnimCtrl;
    };
    
    bool Object::Load(TCHAR* pFilename)
    {
    	LPD3DXFRAME frames;
    	CAllocMeshHierarchy Alloc;
    	HRESULT hr = E_FAIL;
    	D3DXLoadMeshHierarchyFromX( pFilename, D3DXMESH_MANAGED, g_pDevice,
                                              &Alloc, NULL, &frames, &m_pAnimCtrl );
    
    	Mesh mesh;
    
    	// Set the whole bunch of frames 
            // have tried to copy it using memcpy (&mesh.m_Root, &frames, sizeof(frames)); 
    	mesh.m_Root = frames;
         
    
    	shape = new WarehouseShape();
    
    	shape->setMesh(&mesh);
    
    	//shape->setMesh(frames);
    	return true;
    }
    
     
    extern CD3DRenderer g_D3DRenderer;
    extern LPDIRECT3DDEVICE9 g_pDevice;
     
    class Scene
    {
    public:
    	Scene() { SetCurCam(0); }
    	~Scene() { }
    
    	void Load()
    	{
    			m_Cam[0].Load(g_pDevice, _T("Data\\Others\\Demo1.cam"));
    			m_Cam[1].Load(g_pDevice, _T("Data\\Others\\Demo2.cam"));
    			m_Cam[2].Load(g_pDevice, _T("Data\\Others\\Demo3.cam"));
    
    			m_WarehouseObject.Load(_T("Data\\DemoWarehouse.x"));
    
    			m_pRenderObjects.push_back(&m_WarehouseObject);
    
    		 
    
    			
    	}
    
    	void SetCurCam(int i)
    	{
    		m_iCurCam = i;
    	}
    
    
    	void render()
    	{
    		for (int i = 0; i < m_pRenderObjects.size(); i++)
    		{
    			const Mesh& mesh = *m_pRenderObjects[i]->shape->getMesh(m_Cam[m_iCurCam]);
    			Matrix4 mat = m_pRenderObjects[i]->transform;
    			g_D3DRenderer.render(mesh, mat, m_Cam[m_iCurCam]);
    		}
    	}
    
     
    	Object		m_WarehouseObject;
    	Object		m_CBObject;
     	Object		m_OperatorObject;
    
    	CCamera     m_Cam[3];
    
    	int			m_iCurCam;
    
    	std::vector<Object *> m_pRenderObjects;
    };
    Problem identified:
    for (int i = 0; i < m_pRenderObjects.size(); i++)
    {
    const Mesh& mesh = *m_pRenderObjects[i]->shape->getMesh(m_Cam[m_iCurCam]);
    Matrix4 mat = m_pRenderObjects[i]->transform;
    g_D3DRenderer.render(mesh, mat, m_Cam[m_iCurCam]);
    }

    shape has a value of 0xcccccccc
    Does anyone know where the problem is?
    Thanks
    Jack

  2. #2
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Basic Data Persistance question

    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  3. #3
    Join Date
    Dec 2010
    Posts
    907

    Re: Basic Data Persistance question

    I discover that when I setMesh(&mesh);
    I cannot transfer the whole object (deep copy) of the Mesh object to "Shape*"
    Where should I put a copy constructor to?
    Thanks
    Jack

  4. #4
    Join Date
    Oct 2006
    Location
    Sweden
    Posts
    3,654

    Re: Basic Data Persistance question

    If I don't remember wrong you have been told in other threads to use stl containers and smart pointers instead of trying to handle dynamic memory yourself?
    Debugging is twice as hard as writing the code in the first place.
    Therefore, if you write the code as cleverly as possible, you are, by
    definition, not smart enough to debug it.
    - Brian W. Kernighan

    To enhance your chance's of getting an answer be sure to read
    http://www.codeguru.com/forum/announ...nouncementid=6
    and http://www.codeguru.com/forum/showthread.php?t=366302 before posting

    Refresh your memory on formatting tags here
    http://www.codeguru.com/forum/misc.php?do=bbcode

    Get your free MS compiler here
    https://visualstudio.microsoft.com/vs

  5. #5
    Join Date
    Dec 2010
    Posts
    907

    Re: Basic Data Persistance question

    Yes, I was. I tried my best and still cannot completely grasp the concept. Sorry for the waste of effort. :*(
    Smart pointers would manage the copying of resources?
    Also, should I replace raw pointers with shared_ptr's as much as possible?
    Thirdly, shared_ptr's would share the ownerships between multiple objects?
    I don't quite get the benefits of smart pointers anyways in general if it still collapses the program when I use it
    Last edited by lucky6969b; September 24th, 2012 at 04:26 AM.

  6. #6
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Basic Data Persistance question

    Quote Originally Posted by lucky6969b View Post
    Does anyone know where the problem is?
    The problem is that shape has an invalid pointer.
    Code:
    bool Object::Load(TCHAR* pFilename)
    {
        LPD3DXFRAME frames;
        CAllocMeshHierarchy Alloc;
        HRESULT hr = E_FAIL;
        D3DXLoadMeshHierarchyFromX( pFilename, D3DXMESH_MANAGED, g_pDevice,
                                              &Alloc, NULL, &frames, &m_pAnimCtrl );
    
        Mesh mesh;
        mesh.m_Root = frames;
        shape = new WarehouseShape();
        shape->setMesh(&mesh);
        return true;
    }
    What you see in red are all local variables and your usage of them. When that Load function returns, those variables no longer exist. Are you still attempting to use "frames" or "mesh" in another function? If so, then that is no good. Local variables are just that -- local. Trying to point to them and use them after they've gone up in smoke is not going to work.

    Also, you should have posted a main() program showing exactly how you're using these classes. Just showing classes and code where we have no idea what happened previously doesn't help.

    Regards,

    Paul McKenzie
    Last edited by Paul McKenzie; September 24th, 2012 at 12:04 PM.

  7. #7
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Basic Data Persistance question

    I discover that when I setMesh(&mesh);
    I cannot transfer the whole object (deep copy) of the Mesh object to "Shape*"
    All I see is a function called "setMesh" that takes a pointer to a Mesh object and all it does is assign the passed-in pointer to a member variable. What does this have to do with a copy constructor?

    Regards,

    Paul McKenzie

  8. #8
    Join Date
    Dec 2010
    Posts
    907

    Re: Basic Data Persistance question

    Quote Originally Posted by Paul McKenzie View Post
    The problem is that shape has an invalid pointer.
    What you see in red are all local variables and your usage of them. When that Load function returns, those variables no longer exist. Are you still attempting to use "frames" or "mesh" in another function? If so, then that is no good. Local variables are just that -- local. Trying to point to them and use them after they've gone up in smoke is not going to work.

    Also, you should have posted a main() program showing exactly how you're using these classes. Just showing classes and code where we have no idea what happened previously doesn't help.
    Code:
     
    //
    
    #include "stdafx.h"
    #include "PerfectSim V5.0.h"
    #include "CMyApp.h"
    
     
    int APIENTRY _tWinMain(HINSTANCE hInstance,
                         HINSTANCE hPrevInstance,
                         LPTSTR    lpCmdLine,
                         int       nCmdShow)
    {
    	UNREFERENCED_PARAMETER(hPrevInstance);
    	UNREFERENCED_PARAMETER(lpCmdLine);
    
     	CMyApp GameApp;
    	
    	if(!GameApp.Create(hInstance))
    		return false;
    
    	if(!GameApp.Run())
    		return false;
    
    	return false;
    }


    if I put Mesh mesh inside Object, it seems to be redundant, cos Shape* is already storing a reference to the Mesh.
    Where should I store the actual solid object of Mesh, in your opinion?
    Thanks
    Jack

  9. #9
    Join Date
    Apr 1999
    Posts
    27,449

    Re: Basic Data Persistance question

    Quote Originally Posted by lucky6969b View Post
    Where should I store the actual solid object of Mesh, in your opinion?
    The issue is that your "solid object" is not solid. It is a local variable that goes out of scope on return. The code you wrote is honestly just plain wrong. It isn't even a matter of design -- you can't return or refer to addresses or references to local variables after the function block that declares them is exited.

    Either create it dynamically (preferably using a smart pointer), or one of your objects must have as a member a Mesh, so that it does not go out of scope.
    Code:
    #include <memory>
    
    typedef std::shared_ptr<Mesh> MeshPtr;
    typedef std::shared_ptr<Shape> ShapePtr;
    
    struct WarehouseShape : public Shape
    {
        const Mesh* getMesh( const CCamera& camera ) const { return m_pMesh.get(); }
        void setMesh(MeshPtr mesh) { m_pMesh = mesh; }
        MeshPtr m_pMesh;
    };
    
    struct Object
    {
        ShapePtr shape;
        LPD3DXFRAME m_frames;
        MeshPtr m_Mesh;
        bool Load(TCHAR* pFilename);
        D3DXMATRIX transform;
        LPD3DXANIMATIONCONTROLLER m_pAnimCtrl;
    };
    //...
    bool Object::Load(TCHAR* pFilename)
    {
        CAllocMeshHierarchy Alloc;
        HRESULT hr = E_FAIL;
        D3DXLoadMeshHierarchyFromX( pFilename, D3DXMESH_MANAGED, g_pDevice,
                                              &Alloc, NULL, &m_frames, &m_pAnimCtrl );
    
        m_mesh = MeshPtr(new Mesh);
        m_mesh->m_Root = frames;
        shape = ShapePtr(new WarehouseShape());
        shape->setMesh(mesh);
        return true;
    }
    And this is just off the top of my head.

    Regards,

    Paul McKenzie

  10. #10
    Join Date
    Dec 2010
    Posts
    907

    Re: Basic Data Persistance question

    Thanks Paul, I'll think thru your code snippet.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured