NLS Engine  v0.1
The Next Logical Step in game engine design.
 All Classes Namespaces Files Functions Variables Enumerations Enumerator Defines
TiledMap.cpp
Go to the documentation of this file.
00001 
00012 #include "TiledMap.h"
00013 
00014 // System Library Includes
00015 
00016 // Application Library Includes
00017 #include <EngineConfig.h>
00018 
00019 // Local Includes
00020 #include "../../../sharedbase/Entity.h"
00021 #include "../../AssetCreationHelpers.h"
00022 
00023 // Forward Declarations
00024 
00025 // Typedefs and data structures
00026 
00027 // Constants
00028 
00029 // Class methods
00030 namespace GraphicsCore {
00031   TiledMap::TiledMap(EntitySPTR owner, ModuleInterface* renderModule, LPDIRECT3DDEVICE9 d3dDevice) : RenderableComponent(owner, renderModule) {
00032     D3DXCreateSprite(d3dDevice, &this->sprite);
00033   }
00034 
00035   TiledMap::~TiledMap( void ) {
00036 
00037   }
00038 
00039   void TiledMap::AddTile( float x, float y, std::string texName, int srcX, int srcY ) {
00040     LPDIRECT3DTEXTURE9 tex;
00041     CreateTexture(texName, &tex);
00042     RECT srcRect = {srcX, srcY, TILE_WIDTH, TILE_HEIGHT};
00043     Tile t = {x, y, tex};
00044     t.srcRect = srcRect;
00045     this->tiles.push_back(t);
00046   }
00047 
00048   void TiledMap::Render( LPDIRECT3DDEVICE9 ) {
00049     float offX = this->owner->GetWorldPosition().x;
00050     float offY = this->owner->GetWorldPosition().y;
00051     unsigned int viewportWidth = this->owner->GetParent()->properties.GetPropertyValue<unsigned int>("viewportWidth");
00052     unsigned int viewportHeight = this->owner->GetParent()->properties.GetPropertyValue<unsigned int>("viewportHeight");
00053 
00054     D3DXVECTOR3 pos;
00055     pos.z = 1.0f; // Always 0
00056 
00057     LPDIRECT3DTEXTURE9 tex;
00058     RECT src;
00059     this->sprite->Begin(D3DXSPRITE_ALPHABLEND);
00060     int tilewidth = this->owner->properties.GetPropertyValue<unsigned int>("tileWidth") * -1;
00061     int tileheight = this->owner->properties.GetPropertyValue<unsigned int>("tileHeight") * -1;
00062     for (std::vector<Tile>::iterator itr = this->tiles.begin(); itr != this->tiles.end(); ++itr) {
00063       tex = itr->texture;
00064       src = itr->srcRect;
00065       pos.x = itr->x - offX; // Subtract the owner's offset
00066       pos.y = itr->y - offY; // Subtract the owner's offset
00067 
00068       if ((pos.x >= tilewidth && pos.x < viewportWidth) && (pos.y >= tileheight && pos.y < viewportHeight)) {
00069         this->sprite->Draw(tex, &src, NULL, &pos, 0xFFFFFFFF);
00070       }
00071     }
00072     this->sprite->End();
00073   }
00074 
00075   bool TiledMap::LoadMapData( EnvelopeSPTR data ) {
00076     EnvelopeSPTR tsetsEnv(data.get()->GetDataEnvelopeSPTR(0)); // First envelope is tilesets
00077     std::vector<TileSet> tilesets;
00078     for (unsigned int i = 0; i < tsetsEnv.get()->GetCount(); ++i) { // Loop through each tileset
00079       EnvelopeSPTR tset(tsetsEnv.get()->GetDataEnvelopeSPTR(i)); // Get each tileset
00080       TileSet temptset;
00081       temptset.name = tset.get()->GetDataString(0); // 0 - Name
00082       temptset.across = tset.get()->GetDataUInt(1); // 1 - Across
00083       temptset.down = tset.get()->GetDataUInt(2);   // 2 - Down
00084       temptset.firstid = tset.get()->GetDataUInt(3);  // 3 - Firstid
00085       temptset.lastid = tset.get()->GetDataUInt(4); // 4 - Lastid
00086       tilesets.push_back(temptset);
00087 
00088       LPDIRECT3DTEXTURE9 tex;
00089       CreateTexture(NLS_ENGINE_DATA_PATH + "/maps/" + tilesets[i].name, &tex);
00090       this->textures[tilesets[i].name] = tex;
00091     }
00092 
00093     unsigned int tileWidth = this->owner->properties.GetPropertyValue<unsigned int>("tileWidth");
00094     unsigned int tileHeight = this->owner->properties.GetPropertyValue<unsigned int>("tileHeight");
00095 
00096     EnvelopeSPTR layersEnv(data.get()->GetDataEnvelopeSPTR(1)); // Second envelope is layers
00097     for (unsigned int i = 0; i < layersEnv.get()->GetCount(); ++i) { // Loop through each layer
00098       EnvelopeSPTR layer(layersEnv.get()->GetDataEnvelopeSPTR(i)); // Get each layer
00099       for (unsigned int n = 0; n < layer.get()->GetCount(); n = n + 3) { // Loop through each tile
00100         unsigned int r = layer.get()->GetDataUInt(n);     // 0 - row
00101         unsigned int c = layer.get()->GetDataUInt(n + 1);   // 1 - column
00102         unsigned int tileid = layer.get()->GetDataUInt(n + 2);  // 2 - tileid
00103 
00104         TileSet temptset;
00105         for (auto itr = tilesets.begin(); itr != tilesets.end(); ++itr) {
00106           if ((tileid >= itr->firstid) && (tileid < itr->lastid)) {
00107             temptset = (*itr);
00108           }
00109         }
00110 
00111         Tile t;
00112         t.texture = this->textures[temptset.name];
00113         t.x = c * tileWidth;
00114         t.y = r * tileHeight;
00115 
00116         // Determine the row and column offsets for finding the source locations
00117         int tileOffset = tileid - temptset.firstid; // We need to normalize the offset
00118         int rowOffset = tileOffset / temptset.across; // Next, we determine how many rows are in the id
00119         int colOffset = tileOffset - (temptset.across * rowOffset); // Finally, get the column by subtracting the rows.
00120 
00121         t.srcRect.left = colOffset * tileWidth;
00122         t.srcRect.top = rowOffset * tileHeight;
00123         t.srcRect.right = t.srcRect.left + tileWidth;
00124         t.srcRect.bottom = t.srcRect.top + tileHeight;
00125 
00126         this->tiles.push_back(t);
00127       }
00128     }
00129 
00130     return true;
00131   }
00132 }