Advertisement
Guest User

HGETileMap.cpp

a guest
Oct 5th, 2013
33
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 9.29 KB | None | 0 0
  1. #include "HGETileMap.h"
  2. #include <vector>
  3. #include <math.h>
  4.  
  5. HGETileMap::~HGETileMap()
  6. {
  7.  
  8. }
  9.  
  10. void HGETileMap::Init()
  11. {
  12.     info.nTiles = 0;
  13.  
  14.     // null the tile array, delete existing tiles if necessary
  15.     for (int i=0; i<MAX_TILES; i++)
  16.         tiles[i] = NULL;
  17. }
  18.  
  19. bool HGETileMap::Frame()
  20. {
  21.     return 1;
  22. }  
  23.  
  24.  
  25. void HGETileMap::Render()
  26. {
  27.  
  28.     // work out the tile range
  29.     //int pxWidth = (TILE_SIZE * vpWidth);
  30.     int xtile = int(vpX / TILE_W);
  31.     int ytile = int(vpY / TILE_H);
  32.  
  33.     // calculate final pixel offset
  34.     int px = vpX % TILE_W;
  35.     int py = vpY % TILE_H;
  36.  
  37.     // Render tiles -  2 different render paths depending on if we're using a different texture size to the tile size
  38.     if (TILE_H == TILETEX_H)
  39.         for (int cy=ytile; cy<ytile+vpHeight; cy++)
  40.             for (int cx=xtile; cx<xtile+vpWidth; cx++)
  41.                 if (tiles[cx+(cy*info.mapwidth)])
  42.                 {
  43.  
  44.                     if (tiles[cx+(cy*info.mapwidth)]->isBackground())
  45.                         tiles[cx+(cy*info.mapwidth)]->background->Render(   drawX-px + (cx-xtile)*TILE_W, drawY - py - (TILETEX_H-TILE_H) + (cy-ytile)*TILE_H );
  46.  
  47.                     if (tiles[cx+(cy*info.mapwidth)]->isGround())
  48.                         tiles[cx+(cy*info.mapwidth)]->ground->Render( drawX-px + (cx-xtile)*TILE_W, drawY - py - (TILETEX_H-TILE_H) + (cy-ytile)*TILE_H );
  49.                        
  50.                 }
  51.  
  52.  
  53.  
  54. }
  55. void HGETileMap::RenderFG()
  56. {
  57.     // work out the tile range
  58.     //int pxWidth = (TILE_SIZE * vpWidth);
  59.     int xtile = int(vpX / TILE_W);
  60.     int ytile = int(vpY / TILE_H);
  61.    
  62.     // calculate final pixel offset
  63.     int px = vpX % TILE_W;
  64.     int py = vpY % TILE_H;
  65.    
  66.     // Render foreground
  67.     if (TILE_H == TILETEX_H)
  68.         for (int cy=ytile; cy<ytile+vpHeight; cy++)
  69.             for (int cx=xtile; cx<xtile+vpWidth; cx++)
  70.                 if (tiles[cx+(cy*info.mapwidth)])
  71.                     if (tiles[cx+(cy*info.mapwidth)]->isForeground())
  72.                         tiles[cx+(cy*info.mapwidth)]->foreground->Render(   drawX-px + (cx-xtile)*TILE_W, drawY - py - (TILETEX_H-TILE_H) + (cy-ytile)*TILE_H );
  73.                    
  74. }
  75.  
  76. void HGETileMap::SetOffset(int x, int y)
  77. {
  78.     vpX = x;
  79.     vpY = y;
  80. }
  81.  
  82. void HGETileMap::SetTileOffset(int x, int y)
  83. {
  84.     vpX = x * TILE_W;
  85.     vpY = y * TILE_H;
  86. }
  87.  
  88. bool HGETileMap::PointCollide(float x, float y)
  89. {
  90.     if (x<0 || y<0) return false;
  91.  
  92.     // calculate which tile this is
  93.     int tx = int(x / TILE_W);
  94.     int ty = int(y / TILE_H);
  95.  
  96.     // tile index
  97.     int idx = (tx)+((ty)*info.mapwidth);
  98.  
  99.     if (tiles[idx])
  100.         return tiles[idx]->collide;
  101.  
  102.     return false;
  103. }
  104.  
  105.  
  106. // TODO: try and remove the checks for - coords, since it shouldn't happen!
  107. // Returns an array of up to 9 potential collisions
  108. bool HGETileMap::RectCollide(hgeRect r, CTile *plats[9],  int *nCols)
  109. {
  110.     int cPlat = 0;
  111.  
  112.     if (r.x1 < 0 || r.x2 < 0 || r.y1 < 0 || r.y2 < 0) return false;
  113.  
  114.     // calculate which tiles this affects
  115.     int tx1 = int(r.x1 / TILE_W);       // start tile x
  116.     int ty1 = int(r.y1 / TILE_H);       // start tile y
  117.     int tx2 = int(r.x2 / TILE_W)+1;     // end tile x
  118.     int ty2 = int(r.y2 / TILE_H)+1;     // end tile y
  119.  
  120.     // build a list of rects to test collision with
  121.     std::vector<hgeRect> cols;
  122.     std::vector<int> colidx;
  123.  
  124.     for (int iy=ty1; iy<ty2; iy++)
  125.         for (int ix=tx1; ix<tx2; ix++)
  126.             if (tiles[ix+(iy*info.mapwidth)])
  127.                 if (tiles[ix+(iy*info.mapwidth)]->collide || tiles[ix+(iy*info.mapwidth)]->collide2 || tiles[ix+(iy*info.mapwidth)]->collide3 || tiles[ix+(iy*info.mapwidth)]->collide4)
  128.                 {
  129.  
  130.                     if (tiles[ix+(iy*info.mapwidth)]->collide3 || tiles[ix+(iy*info.mapwidth)]->collide4)
  131.                     {
  132.                         // if its an upwards slope
  133.  
  134.                         hgeVector player_pos = hgeVector( r.x1 + ((r.x2 - r.x1)/2) , r.y2 );
  135.                         hgeRect tile_pos = hgeRect(ix*TILE_W, iy*TILE_H, (ix*TILE_W)+TILE_W, (iy*TILE_H)+TILE_H);
  136.  
  137.                         if (tiles[ix+(iy*info.mapwidth)]->collide3)
  138.                         {
  139.                             if ( (player_pos.x - tile_pos.x1) > 40-(player_pos.y - tile_pos.y1) )
  140.                             {
  141.                                 cols.push_back(hgeRect(ix*TILE_W, iy*TILE_H, (ix*TILE_W)+TILE_W, (iy*TILE_H)+TILE_H));
  142.                                 colidx.push_back(ix+(iy*info.mapwidth));
  143.                             }
  144.                         }
  145.                         else
  146.                         {
  147.                             if ( (player_pos.x - tile_pos.x1) < (player_pos.y - tile_pos.y1) )
  148.                             {
  149.                                 cols.push_back(hgeRect(ix*TILE_W, iy*TILE_H, (ix*TILE_W)+TILE_W, (iy*TILE_H)+TILE_H));
  150.                                 colidx.push_back(ix+(iy*info.mapwidth));
  151.                             }
  152.                         }
  153.  
  154.                     }
  155.                     else
  156.                     {
  157.                         cols.push_back(hgeRect(ix*TILE_W, iy*TILE_H, (ix*TILE_W)+TILE_W, (iy*TILE_H)+TILE_H));
  158.                         colidx.push_back(ix+(iy*info.mapwidth));
  159.                     }
  160.                 }
  161.  
  162.  
  163.    
  164.     // test for collisions
  165.     for (int i=0; i<cols.size(); i++)
  166.         if (cols[i].Intersect(&r))
  167.         {
  168.             plats[cPlat] = tiles[colidx[i]];
  169.             cPlat ++;
  170.         }
  171.  
  172.     *nCols = cPlat;
  173.  
  174.     // no collision was found  
  175.     return false;
  176. }
  177.  
  178. // Returns a tile at current offset
  179. CTile *HGETileMap::GetOffsetTile(int x, int y)
  180. {
  181.     //calculate the tile offset
  182.     int xtile = int(vpX / TILE_W);
  183.     int ytile = int(vpY / TILE_H);
  184.    
  185.     // tile index
  186.     int idx = (xtile + x)+((ytile + y)*info.mapwidth);
  187.  
  188.     return tiles[idx];
  189. }
  190.  
  191. // Returns a tile from tile coordinates
  192. CTile *HGETileMap::GetTile(int x, int y)
  193. {
  194.     return tiles[x+(y*info.mapwidth)];
  195. }
  196.  
  197. // Returns a tile at a game vector
  198. CTile *HGETileMap::GameGetTile(hgeVector vec)
  199. {
  200.     //calculate the tile offset
  201.     int xtile = int(vec.x / TILE_W);
  202.     int ytile = int(vec.y / TILE_H);
  203.    
  204.     // tile index
  205.     int idx = xtile+(ytile*info.mapwidth);
  206.    
  207.     return tiles[idx];
  208. }
  209.  
  210. void HGETileMap::GetTileOffset(int *x, int *y)
  211. {
  212.     *x = int(vpX / TILE_W);
  213.     *y = int(vpY / TILE_H);
  214. }
  215.  
  216. // Returns map size in pixels
  217. void HGETileMap::GetMapSize(int *w, int *h)
  218. {
  219.     *w = info.mapwidth * TILE_W;
  220.     *h = info.mapheight * TILE_H;
  221. }
  222.  
  223. MapTexture *HGETileMap::AddTexture(std::string texture, int xo, int yo)
  224. {
  225.     // cache the texture name and fetch a texture id
  226.     hgeSprite *spr = NULL;
  227.     HTEXTURE tex = 0;
  228.  
  229.     for (int i=0; i<textures.Max(); i++)
  230.         if (textures.Fetch(i)->path == texture)
  231.         {
  232.             if (textures.Fetch(i)->xo == xo && textures.Fetch(i)->yo == yo)
  233.             {
  234.                 return textures.Fetch(i);
  235.             }
  236.             else
  237.             {
  238.                 tex = textures.Fetch(i)->spr->GetTexture();
  239.                 break;
  240.             }
  241.         }
  242.  
  243.     // create a sprite if not already cached / loaded
  244.     if (!tex)   spr = new hgeSprite(hge->Texture_Load(texture.c_str()), xo, yo, TILETEX_W, TILETEX_H);
  245.     else        spr = new hgeSprite(tex, xo, yo, TILETEX_W, TILETEX_H);
  246.     // add a new texture object. ID starts at 1. 0 denotes a null texture
  247.     textures.Add(new MapTexture(textures.Max()+1, texture, spr, xo, yo));
  248.     return textures.Fetch(textures.Max()-1);
  249. }
  250.  
  251.  
  252. void HGETileMap::LoadMap(std::string filename)
  253. {
  254.     long i, j;
  255.    
  256.     FILE *fil = fopen(filename.c_str(), "rb");
  257.    
  258.     // ======================================
  259.     // Read the LevelInfo header
  260.    
  261.     fread(&info, sizeof(LevelInfo), 1, fil);
  262.    
  263.  
  264.     // Delete texture database TODO: call freetexture?
  265.     textures.clear();
  266.  
  267.     // ======================================
  268.     // Read the texture database
  269.    
  270.     // textures
  271.     char tmp[LEVEL_FILENAME_LEN];
  272.     int xo, yo;
  273.     for (i=0; i<info.nTextures; i++)
  274.     {
  275.         fread(tmp, LEVEL_FILENAME_LEN, 1, fil);
  276.         fread(&xo, sizeof(int), 1, fil);
  277.         fread(&yo, sizeof(int), 1, fil);
  278.         AddTexture(tmp, xo, yo);
  279.     }
  280.    
  281.    
  282.     // null the tile array, delete existing tiles if necessary
  283.     for (i=0; i<MAX_TILES; i++)
  284.         if (tiles[i])
  285.         {
  286.             // remove any entitys
  287.             if (tiles[i]->entity)
  288.                 delete tiles[i]->entity;
  289.  
  290.             delete tiles[i];
  291.             tiles[i] = NULL;
  292.         }
  293.        
  294.    
  295.     // ======================================
  296.     // Read tiles
  297.    
  298.     for (i=0; i<info.nTiles-1; i++)
  299.     {
  300.         fread(&j, sizeof(int), 1, fil);
  301.        
  302.         tiles[j] = new CTile();
  303.        
  304.         tiles[j]->loc = hgeVector( (j % info.mapwidth) * TILE_W , ceil((float)j / info.mapwidth) * TILE_H );
  305.  
  306.         fread(&tiles[j]->bgTexID, sizeof(int), 1, fil);
  307.         fread(&tiles[j]->gTexID, sizeof(int), 1, fil);
  308.         fread(&tiles[j]->fgTexID, sizeof(int), 1, fil);
  309.        
  310.         if (tiles[j]->bgTexID > 0)
  311.             tiles[j]->background = textures.Fetch(tiles[j]->bgTexID-1)->spr;
  312.        
  313.         if (tiles[j]->gTexID > 0)
  314.             tiles[j]->ground = textures.Fetch(tiles[j]->gTexID-1)->spr;
  315.        
  316.         if (tiles[j]->fgTexID > 0)
  317.             tiles[j]->foreground = textures.Fetch(tiles[j]->fgTexID-1)->spr;
  318.        
  319.        
  320.         // Read entitys
  321.         int ent_tmp = 0;
  322.         fread(&ent_tmp, sizeof(int), 1, fil);
  323.         if (ent_tmp)
  324.         {
  325.             char tmp2[ENT_NAME_LEN];
  326.            
  327.             // read entity name
  328.             fread(&tmp2, ENT_NAME_LEN, 1, fil);
  329.            
  330.             // make entity
  331.             tiles[j]->entity = new CEntity(tmp2);
  332.            
  333.             // read number of entity pairs
  334.             unsigned int a;
  335.             fread(&a, sizeof(a), 1, fil);
  336.            
  337.             // read each pair
  338.             for (int k=0; k<a; k++)
  339.             {
  340.                 entity_nv_pair nvpair;
  341.                 fread(nvpair.name, ENT_NAME_LEN, 1, fil);
  342.                 fread(nvpair.value, ENT_VALUE_LEN, 1, fil);
  343.                
  344.                 tiles[j]->entity->AddNVProperty(nvpair.name, nvpair.value);
  345.             }
  346.  
  347.             // Grab the x, y of this tile
  348.             int tmp_x = (j % info.mapwidth) * TILE_W;
  349.             int tmp_y = ceil((float)j / info.mapwidth) * TILE_H;
  350.  
  351.  
  352.             // NOTE: this is where you can write code to make your game register entitys & do something with them.
  353.             // eg:
  354.             //
  355.             // my_game_class->RegisterEntity(tiles[j]->entity, tmp_x, tmp_y);
  356.             //
  357.             // Post on the forum if you require more information..
  358.            
  359.         }
  360.        
  361.        
  362.         // read collision data
  363.         fread(&tiles[j]->collide, sizeof(bool), 1, fil);
  364.         fread(&tiles[j]->collide2, sizeof(bool), 1, fil);
  365.         fread(&tiles[j]->collide3, sizeof(bool), 1, fil);
  366.         fread(&tiles[j]->collide4, sizeof(bool), 1, fil);
  367.         fread(&tiles[j]->collide5, sizeof(bool), 1, fil);
  368.         fread(&tiles[j]->collide6, sizeof(bool), 1, fil);  
  369.     }
  370.    
  371.    
  372.    
  373.    
  374.     fclose(fil);
  375. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement