Thread: need function help, passing arguments

  1. #1
    Registered User
    Join Date
    Jul 2006
    Posts
    15

    need function help, passing arguments

    It's really simple actually. You don't need to understand what this code does. I just need help figuring out how to write the function declaration, call, and definition in order to pass arguments.

    [edit]
    look for code below
    Last edited by infernosnow; 07-17-2006 at 05:48 AM.

  2. #2
    Crazy Fool Perspective's Avatar
    Join Date
    Jan 2003
    Location
    Canada
    Posts
    2,640
    code tags

  3. #3
    pwns nooblars
    Join Date
    Oct 2005
    Location
    Portland, Or
    Posts
    1,094
    Edit your post and add in [code] tags.

  4. #4
    Rabite SirCrono6's Avatar
    Join Date
    Nov 2003
    Location
    California, US
    Posts
    269
    Code:
    #include <string>
    From C to shining C++!

    Great graphics, sounds, algorithms, AI, pathfinding, visual effects, cutscenes, etc., etc. do NOT make a good game.
    - Bubba

    IDE and Compiler - Code::Blocks with MinGW
    Operating System - Windows XP Professional x64 Edition

  5. #5
    Registered User
    Join Date
    Jul 2006
    Posts
    15
    This is not the entire code. I do in fact have string declared.

  6. #6
    ---
    Join Date
    May 2004
    Posts
    1,379
    Code:
    using namespace std;

  7. #7
    Registered User VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,607
    You have quite a few identifiers there that I cannot find in your code.

    pD3DXMaterialBuffer2 should be a pointer to a material buffer, yet I see no such animal.

    And that code is a mess. Fix the formatting so we can at least see what's going on. This function also has no business not being part of a class. Do yourself a favor and think of a graphics engine in terms of objects and encapsulate the API.

    You can also create your own COM interfaces which wrap calls to the actual COM interfaces, but that might be a bit too far.
    Last edited by VirtualAce; 07-17-2006 at 03:36 AM.

  8. #8
    Registered User
    Join Date
    Jul 2006
    Posts
    15
    I've fooled around with the code a bit. Hope this helps. I need to pass arguments to the function, but I'm doing that wrong somehow. The variables used in the function dealing with arguments are highlighted in red. I do not think I need a class of sorts to make this function work, because it works perfectly fine when I replace the RED items in the function with the actual items being passed. For instance, replacing MATBUF with pD3DXMtrlBuffer2. pD3DXMtrlBuffer2 is what is passed to the function to go in place of MATBUF... or at least that's what's supposed to happen.

    Code:
    //declarations
    LPD3DXMESH		m_pMesh2;
    D3DMATERIAL9*		m_pMeshMaterials2;
    LPDIRECT3DTEXTURE9*	m_pMeshTextures2;
    DWORD			m_dwNumMaterials2;
    LPD3DXBUFFER		pD3DXMtrlBuffer2;
    
    //declartion
    HRESULT InitGeometry(LPD3DXBUFFER, string, DWORD,
    LPD3DXMESH, string, D3DMATERIAL9*, LPDIRECT3DTEXTURE9*);
    
    //call
    if( FAILED( InitGeometry(pD3DXMtrlBuffer2, "myball.x", m_dwNumMaterials2, m_pMesh2,
    "d3dxMaterials2", m_pMeshMaterials2, m_pMeshTextures2) ))
    	return false;
    
    //definition
    HRESULT InitGeometry(LPD3DXBUFFER MATBUF, string FILEN, DWORD NUMMAT,
    LPD3DXMESH MESH, string MAT, D3DMATERIAL9* MESHMAT, LPDIRECT3DTEXTURE9* MESHTEX)
    {
    	if( FAILED( D3DXLoadMeshFromX(FILEN,D3DXMESH_SYSTEMMEM,pd3dDevice,NULL
    ,&MATBUF,NULL,&NUMMAT,&MESH) ) )
    	{
    		MessageBox(NULL, "Could not find FILEN", "XFile", MB_OK);
    		return E_FAIL;
    	}
    	D3DXMATERIAL* MAT = (D3DXMATERIAL*)MATBUF->GetBufferPointer();
    	MESHMAT = new D3DMATERIAL9[NUMMAT];
    	MESHTEX  = new LPDIRECT3DTEXTURE9[NUMMAT];
    	for( DWORD i=0; i<NUMMAT; i++ )
    	{
    		MESHMAT[i] = MAT[i].MatD3D;
    		MESHMAT[i].Ambient = MESHMAT[i].Diffuse;
    		MESHTEX[i] = NULL;
    		if( MAT[i].pTextureFilename != NULL && lstrlen(MAT[i].pTextureFilename) > 0 )
    		{
    			if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, 
    MAT[i].pTextureFilename, &MESHTEX[i] ) ) )
    			{
    				MessageBox(NULL, "Could not find texture map", "XFile", MB_OK);                
    			}
    		}
    	}
    	MATBUF->Release();
    	return S_OK;
    }

  9. #9
    Registered User
    Join Date
    Jul 2006
    Posts
    15
    ahh yes, and using using namespace std did solve a few string problems. Thanks.

  10. #10
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    The problem is when you load the model and texture files...
    You're passing pointers to the arguments to the DX functions, not pointers to your allocated variables - which you should...

    If you change InitGeometry() so it takes all arguments except the std::strings by reference it should work fine. =)

  11. #11
    Registered User
    Join Date
    Jul 2006
    Posts
    15
    And how might I do that good sir?

  12. #12
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    Code:
    HRESULT InitGeometry(LPD3DXBUFFER &MATBUF, string FILEN, DWORD &NUMMAT,
    LPD3DXMESH &MESH, string MAT, D3DMATERIAL9* &MESHMAT, LPDIRECT3DTEXTURE9* &MESHTEX){...}
    ...in C++.

    If you're writing pure C you'll need to use pointers instead (* and not & preceding the arguments) and also pass by adress, instead of value, the respective arguments when calling InitGeometry().

  13. #13
    Registered User
    Join Date
    Jul 2006
    Posts
    15
    This error is talking about the LoadMeshFromX thing in the definition of InitGeometry function
    error C2664: 'D3DXLoadMeshFromXA' : cannot convert parameter 1 from 'std::string' to 'LPCSTR'

    This error deals with: D3DXMATERIAL* MAT = (D3DXMATERIAL*)MATBUF->GetBufferPointer(); (after the LoadMeshFromX thing in the function)
    error C2082: redefinition of formal parameter 'MAT'

    Tihs error deals with: MESHMAT[i] = MAT[i].MatD3D; (inside the FOR loop in the function)
    error C2228: left of '.MatD3D' must have class/struct/union type

    There are other errors, but I think they will all be easily solvable if we just tweek something.

  14. #14
    Registered User Osaou's Avatar
    Join Date
    Nov 2004
    Location
    Stockholm, Sweden
    Posts
    69
    1) std::string isn't the same as LPCSTR (which is a char *, simply). Find some way to convert between the two; or why not use LPCSTR/char* for the InitGeometry() arguments?

    2) You have 2 variables named MAT in play at the same scope: the argument of type std::string and the D3DXMATERIAL* later in the function. Rename one of them - and remember to change their referenced name wherever it's used in the function.

    3) This will be fixed when you take care of nr. 2...



    I could dump this whole code on you, but I'd rather see that you learn something... ^^

  15. #15
    Registered User
    Join Date
    Jul 2006
    Posts
    15
    Well, while I was waiting for your reply, I decided to venture off and try a different method. This one is actually cleaner I think. It doesn't pass anything, save an int which simply tells it how many meshes it needs to load and give textures to. Yes indeed. However, it carries with it a problem of course. It doesn't like things like LPD3DXBUFFER pD3DXMtrlBuffer[g]; where I've tried to make it LPD3DXBUFFER pD3DXMtrlBuffer1; and LPD3DXBUFFER pD3DXMtrlBuffer2; as the FOR loop cycles. It thinks it's an array, though. Unfortunate. In Flash Actionscript, you would use something along the lines of LPD3DXBUFFER eval(pD3DXMtrlBuffer[g]); to "evaluate" pD3DXMtrlBuffer[g] where it would become, not an array, but the string pD3DXMtrlBuffer combined with whatever number was in [g].

    Code:
    HRESULT InitGeometry(int);
    
    if( FAILED( InitGeometry(2) ))
    	return false;
    
    //declared before Main()
    LPD3DXMESH			m_pMesh1;
    D3DMATERIAL9*		m_pMeshMaterials1;
    LPDIRECT3DTEXTURE9*	m_pMeshTextures1;
    DWORD			m_dwNumMaterials1;
    LPD3DXMESH			m_pMesh2;
    D3DMATERIAL9*		m_pMeshMaterials2;
    LPDIRECT3DTEXTURE9*	m_pMeshTextures2;
    DWORD			m_dwNumMaterials2;
    string FILEN1 = "myball.x";
    string FILEN2 = "square.x";
    
    /**************************************
    *InitGeometry FUNCTION
    ***************************************/
    HRESULT InitGeometry(int NUMOFOBJECTS)
    {
    	for(int g=1;g>NUMOFOBJECTS;g++)
    	{
    		LPD3DXBUFFER pD3DXMtrlBuffer[g];
    		if( FAILED( D3DXLoadMeshFromX(
    //I'm hoping this points to the global variables FILEN1 and FILEN2
    //where I declared them above... I'm guessing that's what the "&" does
    			&FILEN[g],
    			D3DXMESH_SYSTEMMEM,
    			pd3dDevice,
    			NULL,
    			&pD3DXMtrlBuffer[g],
    			NULL,
    			&m_dwNumMaterials[g],
    			&m_pMesh[g]) ) )
    		{
    			MessageBox(NULL, "Could not find myball.x", "XFile", MB_OK);
    			return E_FAIL;
    		}
    		D3DXMATERIAL* d3dxMaterials[g] = (D3DXMATERIAL*)pD3DXMtrlBuffer[g]->GetBufferPointer();
    		m_pMeshMaterials[g] = new D3DMATERIAL9[m_dwNumMaterials[g]];
    		m_pMeshTextures[g] = new LPDIRECT3DTEXTURE9[m_dwNumMaterials[g]];
    		for( DWORD i=0; i<m_dwNumMaterials[g]; i++ )
    		{
    			m_pMeshMaterials[g][i] = d3dxMaterials[g][i].MatD3D;
    			m_pMeshMaterials[g][i].Ambient = m_pMeshMaterials[g][i].Diffuse;
    			m_pMeshTextures[g][i] = NULL;
    			if( d3dxMaterials[g][i].pTextureFilename != NULL && lstrlen(d3dxMaterials[g][i].pTextureFilename) > 0 )
    			{
    				if( FAILED( D3DXCreateTextureFromFile( pd3dDevice, d3dxMaterials[g][i].pTextureFilename, &m_pMeshTextures[g][i] ) ) )
    				{
    					MessageBox(NULL, "Could not find texture map", "XFile", MB_OK);                
    				}
    			}
    		}
    		pD3DXMtrlBuffer[g]->Release();
    		return S_OK;
    	}
    }
    So now I will try to do what you said your last post and maybe try to see about fixing this [g] problem.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Beginner Needs help in Dev-C++
    By Korrupt Lawz in forum C++ Programming
    Replies: 20
    Last Post: 09-28-2010, 01:17 AM
  2. Getting an error with OpenGL: collect2: ld returned 1 exit status
    By Lorgon Jortle in forum C++ Programming
    Replies: 6
    Last Post: 05-08-2009, 08:18 PM
  3. In over my head
    By Shelnutt2 in forum C Programming
    Replies: 1
    Last Post: 07-08-2008, 06:54 PM
  4. Passing arguments to another function
    By Wiretron in forum C Programming
    Replies: 2
    Last Post: 12-24-2006, 05:57 AM
  5. passing counters between function
    By BungleSpice in forum C Programming
    Replies: 18
    Last Post: 02-21-2004, 06:16 PM