I'm using raylib to make a little game. and here is the problem.
Struct Icon has Loc Struct that has two integer value.
When i put the Struct, Icon as a Pointer into Icons vector. value has been changed.
here is the capture of debuging.
and here is the full code.
// driver.cpp
void init() {
InitWindow(WINDOW_WIDTH, WINDOW_HEIGHT, "Uvuntu");
SetTargetFPS(30);
map<string, Texture2D> mapset = loadAllImages();
setCursor(mapset.find("cursor")->second);
setIcons({ "File Exploer", mapset.find("icon_dir")->second, nullptr });
setIcons({ "File Exploer", mapset.find("icon_dir")->second, nullptr });
runIcons();
}
// icons.cpp
using namespace std;
vector<Icon*> icons;
int default_x = int(WINDOW_WIDTH / 50);
int default_y = int(WINDOW_HEIGHT / 50);
int step = 128;
int icon_x = default_x;
int icon_y = default_y;
void setIcons(Icon icon) {
Loc loc = { icon_x, icon_y };
icon.loc = &loc;
icon_y += step;
if (icon_y > WINDOW_HEIGHT) {
icon_y = default_y;
icon_x += step;
}
cout << icon.loc->x << endl;
cout << icon.loc->y << endl;
icons.push_back(&icon);
}
void runIcons() {
Loc loc = *icons.at(0)->loc; // the problem...
string name = to_string(loc.x) + ", " + to_string(loc.y);
cout << name << endl;
}
// icon.cpp
using namespace std;
struct Icon {
string name;
Texture2D icon_img;
Loc* loc;
};
// loc.cpp
struct Loc {
int x, y;
};