0

I am having an issue wherein private class members do not hold the value assigned to them in a member function.

I initialize the mMapWidth and mMapHeight values to 0, and then I update them to 5 in the load method. Shortly thereafter I call the draw method, but in the for tests mMapWidth and mMapHeight have a value of 0, and mMapCells is empty.

I assign the WorldMap object as follows:

main.h

WorldMap worldMap;

main.cpp

WorldMap worldMap(graphics, &camera, &textureTileset);
worldMap.load();
worldMap.draw();

The WorldMap object is declared and defined as follows:

worldmap.h

class WorldMap
{
public:
    WorldMap();
    WorldMap(Graphics*, Camera*, TextureManager*);
    ~WorldMap();

    void draw();
    void load();

private:
    int mMapWidth = 0;
    int mMapHeight = 0;
    Graphics* pGraphics;
    TextureManager* pTileSet;
    Camera* pCamera;
};

worldmap.cpp

WorldMap::WorldMap(){}
WorldMap::WorldMap(Graphics* graphics, Camera* camera, TextureManager* tileset)
{
    pGraphics = graphics;
    pCamera = camera;
    pTileSet = tileset;

    mMapHeight = 0;
    mMapWidth = 0;
}


WorldMap::~WorldMap(){}

void WorldMap::draw()
{
  for (int tileY = 0; tileY < mMapHeight; ++tileY)
  {
    for (int tileX = 0; tileX < mMapWidth; ++tileX)
    {
      mMapCells[tileX + (tileY * mMapWidth)].draw();
    }
  }
}


void WorldMap::load()
{
    mTileSize = 32;
    mMapWidth = 5;
    mMapHeight = 5;

    mMapCells.reserve(mMapWidth * mMapHeight);

    for (int tileY = 0; tileY < mMapHeight; ++tileY)
    {
      for (int tileX = 0; tileX < mMapWidth; ++tileX)
      {
        Cell* cell = new Cell();

        cell->initialize(pGraphics, 32, 32, 5, pTileSet);
        cell->setX(tileX * 32);
        cell->setY(tileY * 32);
        cell->setCurrentFrame(1);

        mMapCells.push_back(*cell);
      }
    }
}

Thank you for your attention.

Sergio Porres
  • 57
  • 1
  • 10
  • 3
    I guess you are calling `load` and `draw` on two different instances. Please show us the declaration of the `WorldMap` used and where you invoke methods on it. – Jack Nov 15 '13 at 00:47
  • "shortly thereafter"? What happens in between? – clcto Nov 15 '13 at 00:50
  • 1
    I expect that what is happening is you expected `worldMap` to be global just because you declared it in `main.h`. And yes, that instance is global. However, it's not the same as the one you declare later on with a different constructor. – paddy Nov 15 '13 at 00:53

2 Answers2

0

try this.

change this line from main.h

WorldMap worldMap;

to WorldMap* worldMap;

in main.cpp

do this

worldMap = new WorldMap(graphics, &camera, &textureTileset);
worldMap->load();
worldMap->draw();
0

Fist you just define your worldMap twice, first in main.h and then in main.cpp. However you just want one global worldMap, you can write like this in your main.h: extern WorldMap worldMap;

Then other files which include your main.h can use this global worldMap as you wish.

yanchong
  • 276
  • 1
  • 9
  • I did not select this as the answer because `extern` was not directly relevant to the case at hand - the worldMap is only used in main. But thank you for your time and expertise. – Sergio Porres Nov 15 '13 at 01:52