Advertisement
Guest User

C++ - Internal leakage

a guest
Dec 13th, 2010
204
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.41 KB | None | 0 0
  1. void DirectXRenderer::render(Scene* a_scene)
  2. {
  3.     m_D3DDevice->Clear( 0, NULL, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
  4.                          D3DCOLOR_XRGB( 100, 255, 100 ), 1.0f, 0 );
  5.  
  6.     model=new Model("test",L"filename",this->getDevice());
  7.     Texture* texture = new Texture("Banana", L"banana.bmp", m_D3DDevice);
  8.    
  9.  
  10.         // Begin the scene
  11.         if( SUCCEEDED( m_D3DDevice->BeginScene() ) )
  12.         {
  13.             // Setup the world, view, and projection matrices// Set up world matrix
  14.             D3DXMATRIXA16 matWorld;
  15.             D3DXMatrixIdentity( &matWorld );
  16.             D3DXMatrixRotationX( &matWorld, 130 );
  17.             m_D3DDevice->SetTransform( D3DTS_WORLD, &matWorld );
  18.  
  19.             // Set up our view matrix. A view matrix can be defined given an eye point,
  20.             // a point to lookat, and a direction for which way is up. Here, we set the
  21.             // eye five units back along the z-axis and up three units, look at the
  22.             // origin, and define "up" to be in the y-direction.
  23.             D3DXVECTOR3 vEyePt( 0.0f, 5.0f,-20.0f );
  24.             D3DXVECTOR3 vLookatPt( 0.0f, 5.0f, 0.0f );
  25.             D3DXVECTOR3 vUpVec( 0.0f, 1.0f, 0.0f );
  26.             D3DXMATRIXA16 matView;
  27.             D3DXMatrixLookAtLH( &matView, &vEyePt, &vLookatPt, &vUpVec );
  28.             m_D3DDevice->SetTransform( D3DTS_VIEW, &matView );
  29.  
  30.             // For the projection matrix, we set up a perspective transform (which
  31.             // transforms geometry from 3D view space to 2D viewport space, with
  32.             // a perspective divide making objects smaller in the distance). To build
  33.             // a perpsective transform, we need the field of view (1/4 pi is common),
  34.             // the aspect ratio, and the near and far clipping planes (which define at
  35.             // what distances geometry should be no longer be rendered).
  36.             D3DXMATRIXA16 matProj;
  37.             D3DXMatrixPerspectiveFovLH( &matProj, D3DX_PI / 4, 1.0f, 1.0f, 100.0f );
  38.             m_D3DDevice->SetTransform( D3DTS_PROJECTION, &matProj );
  39.             //SetupMatrices();
  40.  
  41.             // Meshes are divided into subsets, one for each material. Render them in
  42.             // a loop
  43.         /*
  44.         frames++;
  45.             for( DWORD i = 0; i < g_dwNumMaterials; i++ )
  46.             {
  47.                     // Set the material and texture for this subset
  48.                     g_pd3dDevice->SetMaterial( &g_pMeshMaterials[i] );
  49.                     g_pd3dDevice->SetTexture( 0, g_pMeshTextures[i] );
  50.            
  51.             //D3DTRANSFORMSTATETYPE tst = //g_pd3dDevice->SetTransform(?, ?);
  52.  
  53.                     // Draw the mesh subset
  54.             D3DXMATRIXA16 matWorld2;
  55.             D3DXMatrixRotationY( &matWorld2, timeGetTime() / 1000.0f );
  56.             g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld2 );
  57.             g_pMesh->DrawSubset( i );
  58.  
  59.             D3DXMATRIXA16 matPosition, matRotationY, matRotationX, matWorld;
  60.             D3DXMatrixTranslation(&matPosition, 0.0f, 1.0f, 0.0f);//x,y,z
  61.             D3DXMatrixRotationY( &matRotationY, (frames/60)*0.1f);
  62.             D3DXMatrixRotationX( &matRotationX, frames / 500.0f );
  63.             matWorld = matPosition*matRotationY*matRotationX;
  64.             g_pd3dDevice->SetTransform( D3DTS_WORLD, &matWorld );
  65.                     g_pMesh->DrawSubset( i );
  66.            
  67.             }*/
  68.         model->showModel();
  69.         texture->show(m_VertexBuffer);
  70.  
  71.  
  72.             // End the scene
  73.             m_D3DDevice->EndScene();
  74.         }
  75.  
  76.         // Present the backbuffer contents to the display
  77.         m_D3DDevice->Present( NULL, NULL, NULL, NULL );
  78.     //this->getWindow()->update();
  79.     delete model;
  80.     delete texture;
  81. }
  82.  
  83. Model::~Model(void)
  84. {
  85.     if(NULL != m_Mesh)
  86.     {
  87.         m_Mesh->Release();
  88.         m_Mesh = NULL;
  89.     }
  90.     //delete m_Mesh;
  91.     if(NULL != m_Material)
  92.     {
  93.         //m_Material->Release();
  94.         delete m_Material;
  95.     }
  96.    
  97.     m_Material = NULL;
  98.     if(NULL != m_Texture)
  99.     {
  100.         //(&m_Texture)->Release();
  101.         delete m_Texture;
  102.     }
  103.    
  104.     m_Texture = NULL;
  105. }
  106.  
  107. HRESULT Model::load()
  108. {
  109.    
  110.     LPD3DXBUFFER materialBuffer;
  111.  
  112.     m_FileName=L"Tiger.x";
  113.     if(FAILED(D3DXLoadMeshFromX(m_FileName, D3DXMESH_SYSTEMMEM, m_Device, NULL, &materialBuffer, NULL, &m_NumMaterials, &m_Mesh)))
  114.     {
  115.         return E_FAIL;
  116.     }
  117.     D3DXMATERIAL* d3dxMaterials=(D3DXMATERIAL*)materialBuffer->GetBufferPointer();
  118.     m_Material=new D3DMATERIAL9[m_NumMaterials];
  119.     if(NULL == m_Material)
  120.         return E_OUTOFMEMORY;
  121.     m_Texture=new LPDIRECT3DTEXTURE9[m_NumMaterials];
  122.     if(NULL == m_Texture)
  123.         return E_OUTOFMEMORY;
  124.  
  125.     for( DWORD i=0; i < m_NumMaterials; i++)
  126.     {
  127.         m_Material[i]=d3dxMaterials[i].MatD3D;
  128.  
  129.         m_Material[i].Ambient = m_Material[i].Diffuse;
  130.  
  131.         m_Texture[i]=NULL;
  132.  
  133.         if(d3dxMaterials[i].pTextureFilename != NULL && lstrlenA(d3dxMaterials[i].pTextureFilename) > 0)
  134.         {
  135.             if(FAILED(D3DXCreateTextureFromFileA(m_Device, d3dxMaterials[i].pTextureFilename, &m_Texture[i])))//Internal leaks caused here
  136.             {
  137.  
  138.                 const CHAR* strPrefix = "..\\";
  139.                 CHAR strTexture[MAX_PATH];
  140.                 strcpy_s( strTexture, MAX_PATH, strPrefix );
  141.                 strcat_s( strTexture, MAX_PATH, d3dxMaterials[i].pTextureFilename);
  142.                
  143.                 if( FAILED( D3DXCreateTextureFromFileA( m_Device,
  144.                     strTexture,
  145.                     &m_Texture[i] ) ) )
  146.                 {
  147.                     MessageBox( NULL, L"Could not find texture map", L"Meshes.exe", MB_OK );
  148.                 }
  149.             }
  150.            
  151.         }
  152.     }
  153.     materialBuffer->Release();
  154.     return S_OK;
  155. }
  156.  
  157. LPD3DXMESH Model::getMesh()
  158. {
  159.     return m_Mesh;
  160. }
  161.  
  162. DWORD Model::getNumMaterials()
  163. {
  164.     return m_NumMaterials;
  165. }
  166.  
  167. D3DMATERIAL9* Model::getMaterial()
  168. {
  169.     return m_Material;
  170. }
  171.  
  172.  
  173. LPDIRECT3DTEXTURE9* Model::getTexture()
  174. {
  175.     return m_Texture;
  176. }
  177.  
  178. void Model::showModel()
  179. {
  180.     for(DWORD i=0; i < m_NumMaterials; i++)
  181.     {
  182.         m_Device->SetMaterial(&m_Material[i]);
  183.         m_Device->SetTexture(0,m_Texture[i]);
  184.  
  185.         m_Mesh->DrawSubset(i);
  186.     }
  187. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement