Question

Ok, I wasn't sure how to explain my question in the title but basically what I am trying to achieve is a "Command Line"-esque GUI using Allegro. The graphics work fine but the method of keeping the history isn't working for obvious reasons. I'm using a map to store the values which was really stupid of me to begin with. Every time I add a command to the history that was the same as a previous history, the previous one disappears. What I want to know is, Is there a way that I can store the values in a way that they won't be over-written like they are in the map?

Here is my current method

I have a struct called Point

struct Point {
    float x, y;

    Point() { this->x = 10.0; this->y = 440.0; }
    Point(float x, float y): x(x), y(y) { };
};

I use this to store the points where the text will be displayed which is used by the graphics handling part of my program.

This is my HistoryManager class defined in HistoryManager.h

class HistoryManager {

    public:
        HistoryManager();
        ~HistoryManager();
        bool firstEntry;
        map<string, Point> history;

        void add_to_history(string);

    private:
        void update();
};

And here are the defenitions in HistoryManager.cpp

HistoryManager::HistoryManager() { this->firstEntry = false; }

HistoryManager::~HistoryManager() { }

void HistoryManager::add_to_history(string input) {

    if (!this->firstEntry) {
        this->history[input] = Point(10.0, 440.0);
        this->firstEntry = true;
    } else {
        this->update();
        this->history[input] = Point(10.0, 440.0);
    }
}

void HistoryManager::update() { 

    for (map<string, Point>::iterator i = this->history.begin(); i != this->history.end(); i++) {
        this->history[(*i).first] = Point((*i).second.x, (*i).second.y-10.0);
    }
}

I assume vectors is an option but is there any way of pairing the values together?

Was it helpful?

Solution

Use std::pair

std::vector< std::pair <std::string, Point> > >

Or just declare your own struct

struct HistoryEntry
{
    std::string input;
    Point point;
};

std::vector<HistoryEntry>
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top