I'd like to preface my request for help with some clarification on what I'm looking for. I am looking for someone experienced in C++ to help me figure out what exactly is going wrong. I believe it has to do with object pointers and passing them among member functions. I also have some issues with the code design and I would appreciate help on ways to better structure it. I'm also really not too sure on how to ask for design and coding help, If I excluded something necessary, just let me know and I'll do my best to post it.
I have working on a 2d platformer with Visual Studio C++ 2010 Express in my spare time using the allegro graphics lib. All was going well until I hit a game breaking bug. It happened during the process of restructuring game assets and adding animation. I tried and tried to figure it out but was never successful. I decided to take an extended break from the project in hopes I could come back to it with a clear head and figure it out. I have now come back to the project and much to my dismay, am still not making much progress on finding/fixing the bug.
The first thing I did after familiarizing myself with the code again was rewrite it using single bitmaps for objects (the way it was before it broke, I also restructured the assets the way they were before as well). Unfortunately, the bug is still there. The debugger still stops at the same location with the same symptoms.
When I start the game, it makes it past all initialization and reaches a check for the players shield before crashing. It is a simple bool check and the code itself is fine, it fails because the player object pointer is invalid (this is what I gather from what the debugger shows). The player object is a member of the map object. The debugger shows the map object variables assigned in the constructor are valid, but all member objects are empty or "???" which probably means an invalid pointer or something of the like.
I am fairly certain the issue has to do with how I'm handling object pointers. I'll try to detail the code flow where I believe the problem is.
-declare mapFile* globaly in main.cpp
-call map constructor in main
-set basic data variables in constructor (when debugging, these values are correct)
-next call loadMapFile() in main.cpp
-open file, for each line
-return MapObject* from parseLine() and add it to objects vector, also sets type, x,y cord and bitmap location to object
-call buildMap()
-for each object in object vector
-determine type, create specific object (derived from MapObject class), set x,y cords and bitmap location from MapObject
-add object to array of that type of object
-start main loop
I really just want to fix my game. I know the way I handled object creation could use some work, and would appreciate any detailed responses on better ways of doing it. I'm willing to post any relevant snippets if someone is interested in helping me go through the code. It is entirely too large of a project to post (27 files) so let me know what would be helpful.
Thanks for reading/any help!
EDIT: Including the code for the functions mentioned above. DISCLAIMER: I know there is a total absence of error checking and it would have probably helped the situation to have it there, but in this thread I'm only looking for help on the actual code/design issue.
void MapFile::loadMapFile()
{
std::string line = "";
std::ifstream file;
file.open(map_file_location, std::ios::in);
if (file.is_open())
{
while(file.good())
{
getline(file,line);
objects.push_back(parseLine(line));
}
file.close();
buildMap();
}
}
MapObject* MapFile::parseLine(std::string line)
{
int last_search = 0;
size_t found = 0;
std::string str_type = "";
std::string str_bmp = "";
int x = 0;
int y = 0;
found = line.find(',',0);
str_type = line.substr(0,found);
last_search = found;
//get bitmap
found = line.find(',',found+1);
str_bmp = line.substr(last_search+1,found - (last_search + 1));
last_search = found;
found = line.find(',',found+1);
x = atoi(line.substr(last_search+1,found - (last_search + 1)).c_str());
last_search = found;
found = line.find(',',found+1);
y = atoi(line.substr(last_search+1,found - (last_search + 1)).c_str());
last_search = found;
return new MapObject(str_type,str_bmp,x,y);
}
void MapFile::buildMap()
{
for(int i=0;i<objects.size();i++)
{
if (objects.at(i)->getType() == PLAYER)
{
player = new Player(objects.at(i)->getX(),objects.at(i)->getY(),objects.at(i)->getBitmapStr());
player_created = true;
}
else if (objects.at(i)->getType() == BLOCK)
{
blocks.push_back(new block(objects.at(i)->getX(),objects.at(i)->getY(),objects.at(i)->getBitmapStr()));
}
else if (objects.at(i)->getType() == BOT)
{
bots.push_back(new Bot(objects.at(i)->getX(),objects.at(i)->getY(),objects.at(i)->getBitmapStr()));
}
else if (objects.at(i)->getType() == GOAL)
{
goal = new Goal(objects.at(i)->getX(),objects.at(i)->getY(),objects.at(i)->getBitmapStr());
is_goal = true;
}
else if (objects.at(i)->getType() == SPIKES)
{
spikes.push_back(new SpikePit(objects.at(i)->getX(),objects.at(i)->getY(),objects.at(i)->getBitmapStr()));
}
else if (objects.at(i)->getType() == JUMP_PAD)
{
jump_pads.push_back(new JumpPad(objects.at(i)->getX(),objects.at(i)->getY(),objects.at(i)->getBitmapStr()));
}
}
}