store States to heap so that State memory adresses stay same

This commit is contained in:
jettis
2024-12-06 12:33:58 +02:00
parent 688def7e01
commit 38931cf65e
2 changed files with 24 additions and 5 deletions

View File

@@ -22,6 +22,7 @@ Animal::Animal(string skin) {
cur = 0; cur = 0;
viime_event = 0; viime_event = 0;
state_changed = false; state_changed = false;
animation_i = 0;
// set rand seed // set rand seed
SDL_srand(0); SDL_srand(0);
@@ -31,6 +32,11 @@ Animal::Animal(string skin) {
} }
Animal::~Animal() { Animal::~Animal() {
for (unsigned i = 0; i < states.size(); i++) {
if (states[i]) {
delete states[i];
}
}
} }
SDL_Surface *Animal::readImg(string skin, string state_name, int num) { SDL_Surface *Animal::readImg(string skin, string state_name, int num) {
@@ -79,6 +85,12 @@ void Animal::readSkin(string skin) {
State feat; State feat;
char prev_chr; char prev_chr;
for (unsigned i = 0; i < states.size(); i++) {
if (states[i]) {
delete states[i];
}
}
states.clear(); states.clear();
special_states.clear(); special_states.clear();
random_states.clear(); random_states.clear();
@@ -150,11 +162,11 @@ void Animal::readSkinStep(string skin, char chr, string &buf, string &key, State
prev_chr = chr; prev_chr = chr;
} }
void Animal::readSkinFill(string skin, State feat) { void Animal::readSkinFill(string skin, State &feat) {
states.push_back(feat); cur_state = new State(feat);
states.push_back(cur_state);
// Load images to memory // Load images to memory
cur_state = &states.back();
for (int x = 0; x < cur_state->count; x++) { for (int x = 0; x < cur_state->count; x++) {
cur_state->imgs.push_back(NULL); cur_state->imgs.push_back(NULL);
cur_state->imgs[x] = readImg(skin, cur_state->img_name, x); cur_state->imgs[x] = readImg(skin, cur_state->img_name, x);

View File

@@ -30,6 +30,13 @@ class State {
vector<SDL_Surface *> imgs; vector<SDL_Surface *> imgs;
SoundData sound_data; SoundData sound_data;
~State() {
for (unsigned i = 0; i < imgs.size(); i++) {
if (imgs[i]) {
SDL_DestroySurface(imgs[i]);
}
}
}
}; };
class Animal { class Animal {
@@ -40,7 +47,7 @@ class Animal {
SDL_Surface *readImg(string skin, string state_name, int num); SDL_Surface *readImg(string skin, string state_name, int num);
void readSkin(string file); void readSkin(string file);
void readSkinStep(string skin, char chr, string &buf, string &key, State &state, char &prev_chr); void readSkinStep(string skin, char chr, string &buf, string &key, State &state, char &prev_chr);
void readSkinFill(string skin, State state); void readSkinFill(string skin, State &state);
void createShape(int num, int w, int h); void createShape(int num, int w, int h);
void move(); void move();
void animate(bool reset); void animate(bool reset);
@@ -57,7 +64,7 @@ class Animal {
SDL_Rect disp_pos; // has display width and height and window posiion on it SDL_Rect disp_pos; // has display width and height and window posiion on it
vector<State> states; vector<State *> states;
vector<State *> special_states; vector<State *> special_states;
State *cur_state; State *cur_state;
vector<State *> random_states; vector<State *> random_states;