Advertisement
Guest User

HGETileMap.h

a guest
Oct 5th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 5.88 KB | None | 0 0
  1.  
  2. #ifndef HGETileMap_H
  3. #define HGETileMap_H
  4.  
  5. #include <string>
  6.  
  7. #include <hge.h>
  8. #include <hgesprite.h>
  9. #include <hgevector.h>
  10.  
  11. #include "HGETileMapDatabases.h"
  12.  
  13.  
  14. // ===================================================================
  15. // Game-Specific defines - change these to match your tile size!
  16.  
  17.  
  18. #define TILE_W 40
  19. #define TILE_H 40
  20.  
  21. // Adjust these if you want to use different sized textures to the tile size
  22. #define TILETEX_W TILE_W
  23. #define TILETEX_H TILE_H
  24.  
  25.  
  26. // ===================================================================
  27. // Map File Struct
  28.  
  29. #define LEVEL_FILENAME_LEN 128
  30.  
  31. struct LevelInfo {
  32.     int nTextures;
  33.     int nTiles;
  34.     int nEntitys;
  35.    
  36.     char filename[LEVEL_FILENAME_LEN];
  37.    
  38.     int tilew;  // in pixels
  39.     int tileh;  // in pixels
  40.    
  41.     int texw;   // texture in pixels - usually will be the same as tile size.
  42.     int texh;   // texture height in pixels
  43.    
  44.     int mapwidth;   // in tiles
  45.     int mapheight;  // in tiles
  46. };
  47.  
  48. // ===================================================================
  49. // Map Texture Class
  50.  
  51. struct MapTexture
  52. {
  53.     unsigned int id;
  54.     std::string path;
  55.     hgeSprite *spr;
  56.  
  57.     int xo, yo; // offsets
  58.  
  59.     MapTexture() { id = 0; path= ""; spr = NULL; }
  60.     MapTexture(int i, std::string p, hgeSprite *s, int offsetx, int offsety) { id = i; path= p; spr = s; xo=offsetx; yo=offsety; }
  61. };
  62.  
  63.  
  64.  
  65. // ===================================================================
  66. // Map Entity Class
  67.  
  68. #define ENT_NAME_LEN 32
  69. #define ENT_VALUE_LEN 128
  70.  
  71. struct entity_nv_pair
  72. {
  73.     char name[ENT_NAME_LEN];
  74.     char value[ENT_VALUE_LEN];
  75.  
  76.     entity_nv_pair() { return; }
  77.     entity_nv_pair(std::string nam, std::string val) { strcpy(name, nam.c_str()); strcpy(value, val.c_str()); }
  78.  
  79.     // Copy constructor
  80.     entity_nv_pair(const entity_nv_pair &b)
  81.     {
  82.         strncpy(name, b.name, ENT_NAME_LEN);
  83.         strncpy(value, b.value, ENT_VALUE_LEN);
  84.     }
  85. };
  86.  
  87. class CEntity
  88. {
  89. public:
  90.     char name[ENT_NAME_LEN];
  91.  
  92.     // retrieves a value from an existing property
  93.     std::string GetValue(std::string name)
  94.     {
  95.         for (int i=0; i<nvals.Max(); i++)
  96.             if (std::string(nvals.Fetch(i).name) == name)
  97.                 return std::string(nvals.Fetch(i).value);
  98.        
  99.         return "";
  100.     }
  101.  
  102.     // adds value to an existing property
  103.     bool AddValue(std::string name, std::string value)
  104.     {
  105.         for (int i=0; i<nvals.Max(); i++)
  106.             if (!strcmp(nvals.Fetch(i).name, name.c_str()))
  107.             {
  108.                 sprintf(nvals.FetchPointer(i)->value, "%s", value.c_str());
  109.                 return true;
  110.             }
  111.  
  112.         return false;  
  113.     }
  114.  
  115.     // creates a property
  116.     void AddNVProperty(std::string name, std::string value)
  117.     {
  118.         nvals.Add(entity_nv_pair(name, value));
  119.     }
  120.  
  121.     // returns the number of nval pairs
  122.     unsigned int GetNVCount() { return nvals.Max(); }  
  123.  
  124.     // returns a nval pair
  125.     entity_nv_pair GetNVProperty(int index) { return nvals.Fetch(index); }
  126.  
  127.     // Copies from another entity to this one
  128.     void Copy(CEntity *ent)
  129.     {
  130.         strcpy(name, ent->name);
  131.  
  132.         for (int i=0; i<ent->GetNVCount(); i++)
  133.         {
  134.             entity_nv_pair tmp = ent->GetNVProperty(i);
  135.             AddNVProperty(tmp.name, tmp.value);
  136.         }
  137.     }
  138.  
  139.     CEntity(std::string nam) { strcpy(name, nam.c_str()); }
  140.  
  141. private:
  142.    
  143.     // TODO: code these!!
  144.     //entity_nv_pair nvpairs[10];
  145.     //int nPairs;
  146.  
  147.     CSDatabase<entity_nv_pair> nvals;
  148. };
  149.  
  150.  
  151. // ===================================================================
  152. // Map Tile Class
  153.  
  154. class CTile
  155. {
  156. public:
  157.    
  158.     // for rendering
  159.     hgeSprite *background;
  160.     hgeSprite *ground;
  161.     hgeSprite *foreground;
  162.     CEntity *entity;
  163.  
  164.     bool collide;
  165.     bool collide2;
  166.     bool collide3;
  167.     bool collide4;
  168.     bool collide5;
  169.     bool collide6;
  170.  
  171.     // for the editor only:
  172.     unsigned int bgTexID;
  173.     unsigned int gTexID;
  174.     unsigned int fgTexID;
  175.  
  176.     // for the game only! set at load
  177.     hgeVector loc;
  178.  
  179.    
  180.     CTile()
  181.     {
  182.         background = NULL;
  183.         ground = NULL;
  184.         foreground = NULL;
  185.         entity = NULL;
  186.         bgTexID = 0;
  187.         gTexID = 0;
  188.         fgTexID = 0;
  189.         collide = false;
  190.         collide2 = false;
  191.         collide3 = false;
  192.         collide4 = false;
  193.         collide5 = false;
  194.         collide6 = false;
  195.     }
  196.    
  197.     inline bool isBackground() { if (background) return true; else return false; }
  198.     inline bool isGround() { if (ground) return true; else return false; }
  199.     inline bool isForeground() { if (foreground) return true; else return false; }
  200.  
  201.    
  202. };
  203.  
  204.  
  205. // ===================================================================
  206. // Map Class. Creates, Stores, Saves, Loads & Renders maps
  207.  
  208. #define MAX_TILES 512000
  209.  
  210. class HGETileMap
  211. {
  212. private:
  213.  
  214.     HGE *hge;
  215.  
  216.     CTile *tiles[MAX_TILES];
  217.  
  218.  
  219.     // for the editor - texture name database
  220.     CPDatabase<MapTexture> textures;
  221.  
  222.  
  223.     int drawX, drawY; // x y offset of the map itself. internal
  224.  
  225.     // camera variables
  226.     int vpWidth, vpHeight; // viewport width in tiles
  227.     int vpX, vpY; // current viewport in pixels
  228.  
  229.     // Caches textures in the internal texture database. Returns texture ID
  230.     MapTexture *AddTexture(std::string texture, int xo, int yo);
  231.  
  232.  
  233. public:
  234.  
  235.     // level info header from file
  236.     LevelInfo info;
  237.  
  238.  
  239.     // Takes: Game pointer, X + Y offset to draw at, Level width and Height in tiles
  240.     HGETileMap(HGE *hgep, int x, int y, int levelwidth, int levelheight)
  241.     {
  242.         hge = hgep;
  243.         drawX = x;
  244.         drawY = y;
  245.  
  246.         vpX = 0;
  247.         vpY = 0;
  248.         vpWidth = levelwidth + 1;
  249.         vpHeight = levelheight + 1;
  250.        
  251.         return;
  252.     }
  253.     ~HGETileMap();
  254.  
  255.  
  256.     void LoadMap(std::string);
  257.  
  258.  
  259.     CTile *GetOffsetTile(int, int);
  260.     CTile *GetTile(int, int);
  261.  
  262.     // Gets tile from a game vector
  263.     CTile *GameGetTile(hgeVector);
  264.  
  265.     // Sets camera offset in pixels
  266.     void SetOffset(int, int);
  267.  
  268.     // Sets camera offset in tiles
  269.     void SetTileOffset(int, int);
  270.  
  271.     // Returns the viewport offset in tiles
  272.     void GetTileOffset(int *, int *);
  273.  
  274.     // Returns the map size in pixels
  275.     void GetMapSize(int *, int *);
  276.  
  277.     // returns true if a point collides with a tile
  278.     bool PointCollide(float, float);
  279.  
  280.     bool RectCollide(hgeRect r, CTile *plats[9],  int *nCols);
  281.  
  282.  
  283.     void Init();
  284.     bool Frame();
  285.     void Render();
  286.     void RenderFG();
  287.  
  288. };
  289.  
  290. #endif
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement