home *** CD-ROM | disk | FTP | other *** search
/ Programming an RTS Game with Direct3D / Programming an RTS Game with Direct3D.iso / Examples / Chapter 15 / Example 15.2 / terrain.h < prev    next >
Encoding:
C/C++ Source or Header  |  2006-07-17  |  2.9 KB  |  131 lines

  1. #ifndef _TERRAIN_
  2. #define _TERRAIN_
  3.  
  4. #include <d3dx9.h>
  5. #include <vector>
  6. #include <fstream>
  7. #include "heightmap.h"
  8. #include "debug.h"
  9. #include "shader.h"
  10. #include "object.h"
  11.  
  12. class CAMERA;
  13. class MAPOBJECT; 
  14.  
  15. struct TERRAINVertex
  16. {
  17.     TERRAINVertex(){}
  18.     TERRAINVertex(D3DXVECTOR3 pos, D3DXVECTOR3 norm, D3DXVECTOR2 _uv1, D3DXVECTOR2 _uv2)
  19.     {
  20.         position = pos;
  21.         normal = norm;
  22.         uv1 = _uv1;
  23.         uv2 = _uv2;
  24.     }
  25.  
  26.     D3DXVECTOR3 position, normal;
  27.     D3DXVECTOR2 uv1, uv2;
  28.  
  29.     static const DWORD FVF;
  30. };
  31.  
  32. struct PATCH{
  33.     PATCH();
  34.     ~PATCH();
  35.     void Release();
  36.     HRESULT CreateMesh(TERRAIN &ter, RECT source, IDirect3DDevice9* Dev);
  37.     void Render();
  38.  
  39.     IDirect3DDevice9* m_pDevice;
  40.     ID3DXMesh *m_pMesh;
  41.     RECT m_mapRect;
  42.     BBOX m_BBox;
  43. };
  44.  
  45. struct MAPTILE{
  46.     MAPTILE()    //Set everything to 0
  47.     {
  48.         m_type = m_set = 0; 
  49.         m_height = m_cost = 0.0f;
  50.         m_walkable = false;
  51.         m_pMapObject = NULL;
  52.         m_pParent = NULL;
  53.  
  54.         for(int i=0;i<8;i++)
  55.             m_pNeightbors[i] = NULL;
  56.     }
  57.  
  58.     int m_type, m_set;
  59.     float m_height, m_cost;
  60.     bool m_walkable;
  61.     MAPTILE* m_pNeightbors[8];
  62.     MAPOBJECT *m_pMapObject;
  63.  
  64.     // Pathfinding variables
  65.     INTPOINT m_mappos;
  66.     float f,g;
  67.     bool open, closed;
  68.     MAPTILE *m_pParent;
  69. };
  70.  
  71. class TERRAIN{
  72.     friend struct PATCH;
  73.     friend class MOUSE;
  74.     friend class CAMERA;
  75.     friend class APPLICATION;
  76.     public:
  77.         TERRAIN();        
  78.         void Init(IDirect3DDevice9* Dev, INTPOINT _size);
  79.         void Release();
  80.         void GenerateRandomTerrain(int numPatches);
  81.         void CreatePatches(int numPatches);
  82.         void CalculateAlphaMaps();
  83.         void CalculateLightMap(bool allWhite);
  84.         D3DXVECTOR3 GetNormal(int x, int y);
  85.         void AddObject(int type, INTPOINT mappos);
  86.         void Render(CAMERA &camera);        
  87.         void Progress(std::string text, float prc);
  88.  
  89.         //Pathfinding
  90.         bool Within(INTPOINT p);    //Test if a point is within the bounds of the terrain
  91.         void InitPathfinding();
  92.         void UpdatePathfinding(RECT *r);
  93.         void CreateTileSets();
  94.         std::vector<INTPOINT> GetPath(INTPOINT start, INTPOINT goal, bool considerUnits);
  95.         MAPTILE* GetTile(int x, int y);
  96.         MAPTILE* GetTile(INTPOINT p){return GetTile(p.x, p.y);}
  97.         D3DXVECTOR3 GetWorldPos(INTPOINT mappos);
  98.         INTPOINT GetClosestFreeTile(INTPOINT to, INTPOINT from);
  99.  
  100.         //Save and Load Map
  101.         void SaveTerrain(char fileName[]);
  102.         void LoadTerrain(char fileName[]);
  103.  
  104.         //Public variables
  105.         MAPTILE *m_pMapTiles;
  106.         INTPOINT m_size;
  107.         std::vector<OBJECT> m_objects;
  108.         bool m_updateTerrain;
  109.         HEIGHTMAP *m_pHeightMap;
  110.  
  111.     private:
  112.                 
  113.         IDirect3DDevice9* m_pDevice; 
  114.         ID3DXFont *m_pProgressFont;
  115.         
  116.         std::vector<PATCH*> m_patches;
  117.         std::vector<IDirect3DTexture9*> m_diffuseMaps;        
  118.         IDirect3DTexture9* m_pAlphaMap;
  119.         IDirect3DTexture9* m_pLightMap;
  120.  
  121.         SHADER m_terrainPS, m_terrainVS;
  122.         SHADER m_objectsPS, m_objectsVS;
  123.  
  124.         D3DXVECTOR3 m_dirToSun;
  125.         D3DXHANDLE m_vsMatW, m_vsMatVP, m_vsDirToSun;
  126.         D3DXHANDLE m_objMatW, m_objMatVP, m_objDirToSun, m_objMapSize;
  127.  
  128.         D3DMATERIAL9 m_mtrl;
  129. };
  130.  
  131. #endif