Original Post
I am working on the framework for a game. At the moment, I am stuck on the graphics system. I am loading and displaying text instead of graphics because it will use the same framework as graphics, but be easier to debug and think out. When the problem occoured, I isolated the graphics into it's own system, trying to simplify, but the problem kept occuring. I'll post the code at the end for reference. The first problem I observed was that the program get's stuck after printing the initialization messages for display. when I run it through the debugger, the blasted thing segfaults, and dumps random charachters all over the screen and makes the system bell beep repeatedly. I found that it crashed on the call to graphics->draw(renderable). I traced into this, and the next line was the same line, and the one after that was in the middle of display::getmodel(string filename). My question is this: why doesn't that trace end up at the beginning of display::draw? graphics.cpp graphics.h [Edited by - NIm on September 28, 2006 11:12:45 PM]
#include "graphics.h"
#include <iostream>
#include <fstream>
using namespace std;
class model /*Did I do this right? I put this here becaus enothing outside of graphics needs to know it exists.*/
{
protected:
string image;
public:
boost::shared_ptr<model> load(string filename);
friend int display::draw(renderable r);
// std::string getdata() {return image;}
};
display::display()
{
cout<<"Graphics: Initializing..."<<endl;
cout<<"Graphics: Initialized"<<endl;
}
display::~display()
{
cout<<"Graphics: shut down"<<endl;
}
int display::draw(renderable r)
{
cout<<"drawing..."<<endl;
string m;
m = r.image->image;
cout<<m<<endl;
}
int display::draw()
{
cout<<"object drawn"<<endl;
}
boost::shared_ptr<model> display::getmodel(string filename)
{
if (modelmap.find(filename) != modelmap.end())
{
return modelmap[filename];
}
else
{
modelmap[filename] = boost::shared_ptr<model>(new model);
boost::shared_ptr<model> m = modelmap[filename]->load(filename);
if (modelmap[filename].get() != NULL)
{
boost::shared_ptr<model> m = modelmap[filename];
return modelmap[filename];
}
else
{
cout<<"Failed to get model"<<endl;
boost::shared_ptr<model> p;
p.reset();
return p;
}
}
}
renderable::renderable()
{
//null and useless rendable
}
renderable::renderable(boost::shared_ptr<display> disp, string filename)
{
image = disp->getmodel(filename);
parent = disp;
position = 0; //placeholders. change these to be meaningful according to whatever physics system gets implemented.
orientation = 0;
}
boost::shared_ptr<model> model::load(const string filename)
{
fstream file;
file.open(filename.c_str());
if (!file.is_open())
{
cout<<"Failed to open "<<filename<<endl;
boost::shared_ptr<model> p;
p.reset();
return p;
}
file>>image; //placeholder untill graphics gets real.
return boost::shared_ptr<model> (this);
}
/*test main*/
main()
{
boost::shared_ptr<display> graphics;
graphics = boost::shared_ptr<display>(new display);
std::string str = "(<?>)";
renderable r(graphics, "command-module.txt");
graphics->draw(r);
int i;
cin>>i;
}
#ifndef GRAPHICS_H
#define GRAPHICS_H
#include <boost/shared_ptr.hpp>
#include <string>
#include <map>
class renderable;
class model;
class display
{
private:
std::map<std::string, boost::shared_ptr<model> > modelmap;
public:
display();
~display();
int draw(renderable r);
boost::shared_ptr<model> getmodel(std::string filename);
};
class renderable /*a renderable will be altered by a module and then passed to the graphics system for rendering.*/
{
protected:
boost::shared_ptr<model> image;
boost::shared_ptr<display> parent;
int position;
int orientation;
public:
friend int display::draw(renderable r);
renderable();
renderable(boost::shared_ptr<display> disp, std::string filename);
};
/* each model doesn't know about the rest of the program; it just knows how to
load from a file. display keeps track of models, and draw them.*/
#endif /*GRAPHICS_H*/