Sign in to follow this  
NIm

oop problem

Recommended Posts

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
#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)
{
    string m;
    m = r.image->image;
    cout<<m<<endl;
}

boost::shared_ptr<model> display::getmodel(string filename)
{
    if (modelmap[filename])
    {
        return modelmap[filename];
    }
    else
    {
        modelmap[filename] = boost::shared_ptr<model>(new model);
        if (modelmap[filename])
        {
            modelmap[filename]->load(filename);
            return modelmap[filename];
        }
        else
        {
            cout<<"Failed to get model"<<endl;
            boost::shared_ptr<model> p;
            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 meaning ful accordign 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;
        return p;
    }
    file>>image;    //placeholder untill graphics gets real. 
    return boost::shared_ptr<model> (this);
}
/*testmain*/
main()
{
    boost::shared_ptr<display> graphics;
    graphics = boost::shared_ptr<display>(new display);
    
    renderable r(graphics, "command-module.txt");
    graphics->draw(r);
    int i;
    cin>>i;
}
graphics.h
#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*/

Share this post


Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this