The problem: There is a vector of a structure that includes a string, "plantname". It's referenced in a different class to draw it to the screen. However, somewhere between writing it and reading it, it gets wiped.
enum planttype {DOUBLEQUOTE = 34, SINGLEOPENQUOTE = 39, SINGLECLOSEQUOTE = 96};
class SoilMap
{
public:
struct plants
{
string plantname;
void setName(string str) {plantname = str;}
};
void GenerateGrass(); //generate the grass map;.
plants plants_at(int x, int y) {if (y*h + x < plantmap.size()) return plantmap.at(y*w + x); else {plants temptile; return temptile;}}
protected:
vector<plants> plantmap;
};
#include "SoilMap class.h"
void SoilMap::GenerateGrass()
{
plants *temp;
temp = new plants();
/* code that is not relevant */
for (int ycounter = 0; ycounter < h; ycounter++)
for (int xcounter = 0; xcounter < w; xcounter++)
{
/* not relevant */
string tempstr;
if (temp->character == DOUBLEQUOTE)
tempstr.assign("Fescue grass");
else if (temp->character == SINGLECLOSEQUOTE)
tempstr.assign("Ryegrass");
else if (temp->character == SINGLEOPENQUOTE)
tempstr.assign("Ryegrass");
else
tempstr.assign("Unidentified");
temp->setName(tempstr);
}
plantmap.push_back(*temp); //add to plantmap vector
}
class sideDisplay: public GUIHandler
{
public:
sideDisplay();
void draw();
void update(SoilMap &map, Cursor &cursor); //while cursor is visible
protected:
vector<pair<string,pair<TCODColor, TCODColor>>> infovector;
TCODColor darkbrown;
};
void sideDisplay::update(SoilMap &map, Cursor &cursor)
{
SoilMap::tile *tilepointer;
SoilMap::plants *plantpointer;
if (cursor.visible == true)
if ((cursor.getX() < 53) & (cursor.getY() <= 46) & (cursor.getY() >= 10))
{
plantpointer = & map.consoletoplant(cursor.getX(), cursor.getY());
if (plantpointer->plantname.size() != 0)
infovector.push_back(pair<string, pair<TCODColor, TCODColor>>(plantpointer->plantname, pair<TCODColor, TCODColor>(TCODColor::lerp(plantpointer->background, TCODColor::black,0.5f), plantpointer->foreground)));
tilepointer = & map.consoletotile(cursor.getX(), cursor.getY());
infovector.push_back(pair<string, pair<TCODColor, TCODColor>>(tilepointer->getTileName(), pair<TCODColor, TCODColor>(TCODColor::lerp(tilepointer->foreground, TCODColor::black,0.5f), tilepointer->background)));
}
}
void sideDisplay::draw()
{
console->clear();
int counter = 0;
for (auto it = infovector.begin(); it < infovector.end(); it++)
{
console->setDefaultBackground(it->second.first);
console->setDefaultForeground(it->second.second);
console->print(0, counter, "%s", it->first.c_str());
counter++;
}
infovector.clear();
}
Basically what the update() function does is get the coordinates from a cursor, translate them into the Soilmap's coordinates, and fetches the correct plants stored in the vector. It then pushes back the plant name, two TCODColor variables.
Then, the draw() function takes the colors and sets them, then takes the c_str() of the string and blits that to the screen.
Debugging checks that (1) Using a constant string in the update() function makes it work correctly (2) In the update() function, the pointer to the plants object tells me that the plantname variable is empty, (3) An almost identical structure tile works perfectly fine, though it uses a slightly different way of looking up the string (it uses a lookup function, though, not a lookup table).
If this doesn't suffice, I could provide a dropbox link to the entire project. :<
It is using SDL and libtcod.
Question: What could be possible reasons the string in the plants is wiped between writing it and reading it?
This has stumped me for a good hour.