vendredi 1 mai 2020

Exception 0xc0000005 i.e. Access Violition in C++

So I have been trying to eradicate this error for a few hours now but I was unable to. I am making a 2D game where I am reading different maps for different screens.

This is where I get the error. I call this in my main. Here, curr_map_ is a string and mapper is an object of Map.

curr_map_ = mapper.GetMapLabels();

Here is my Map class.

#include <mylibrary/map.h>
#include <iostream>
#include <fstream>
#include <string>
#include <vector>

using mylibrary::Direction;
using mylibrary::Location;

int prev_row = 0;
int prev_col = 0;

namespace mylibrary {

Map::Map() = default;

Map::Map(std::vector<std::vector<char>> game_screen) {
  for (int i = 0; i < 16; i++) {
    for (int j = 0; j < 16; j++) {
      this->coordinates_[i][j] = game_screen[i][j];
    }
  }
}

void Map::ReadImageLabels() {
  std::string map_label_file = "C:/Users/monty/CLionProjects/cinder_0.9.2_vc2015/projects/Break/assets/background.txt";
  std::ifstream file(map_label_file);

  while(!file.eof()) {
    std::string map_label;
    std::getline(file, map_label);
    map_labels_.push_back(map_label);
  }
}

void Map::ReadGameScreens() {
  int map_line_count = 0;
  std::string maps_file = "C:/Users/monty/CLionProjects/cinder_0.9.2_vc2015/projects/Break/assets/maze.txt";
  std::ifstream file(maps_file);
  while (!file.eof()) {
    std::string map_line;
    std::getline(file, map_line);
    if (!map_line.empty()) {
      SetupMap(map_line);
      map_line_count++;


      if (map_line_count == 16) {
        Map game_screen = Map(map_);
        game_maps_.push_back(game_screen);

        map_line_count = 0;
        map_.clear();
      }
    }
  }
}

void Map::SetupMap(std::string map_line) {
  std::vector<char> map_line_char;
  map_line_char.reserve(16);
  for (int i = 0; i < 16; i++) {
    map_line_char.push_back(map_line.at(i));
  }
  map_.push_back(map_line_char);
}

std::string Map::GetMapLabels() {
  for (int i = 0; i < map_labels_.size(); i++) {
    if (i == screen_num_) {
      return map_labels_[i];
    }
  }
}

std::vector<Map> Map::GetScreen() {
  return game_maps_;
}

bool Map::IsScreenChange() {
  return is_screen_change_;
}

int Map::GetNewScreenNum() {
  return screen_num_;
}

Location Map::GetPlayerNewLoc(const Map& curr_map, Engine engine) {
  Location location = engine.GetPrisoner().GetLoc();
  int curr_row = location.Col();
  int curr_col = location.Row();

  for (int j = 0; j < entry_points_.size(); j++) {
    if (curr_map.coordinates_[curr_row][curr_col] == entry_points_.at(j)) {
      screen_num_ = GetTransitionScreenNum(GetCurrScreenNum(curr_map),
                                           entry_points_.at(j));
      is_screen_change_ = true;
      if (engine.GetDirection() == Direction::kUp) {
        return {curr_col, 11};
      } else if (engine.GetDirection() == Direction::kDown) {
        return {curr_col, 1};
      } else if (engine.GetDirection() == Direction::kLeft) {
        return {14, curr_row};
      } else if (engine.GetDirection() == Direction::kRight) {
        return {1, curr_row};
      }
    }
  }

}

int Map::GetCurrScreenNum(const Map& curr_map) {
  int count = 0;
  for (int i = 0; i < game_maps_.size(); i++) {
    for (int j = 0; j < 16; j++) {
      for (int k = 0; k < 16; k++) {
        if (game_maps_[i].coordinates_[j][k] == curr_map.coordinates_[j][k]) {
          count++;
        } else {
          count = 0;
          goto outerloop;
        }
        if (count == 256) {
          return i;
        }
      }
    }
    outerloop:;
  }
}

int Map::GetTransitionScreenNum(int num, char entry) {
  for (int i = 0; i < game_maps_.size(); i++) {
    if (i != num) {
      for (int j = 0; j < 16; j++) {
        for (int k = 0; k < 16; k++) {
          if (game_maps_[i].coordinates_[j][k] == entry) {
            return i;
          }
        }
      }
    }
  }
}

}  // namespace mylibrary

My text files are -

maze.txt

1111111111111111
0000000100100101
1011110100000101
1010010001110001
1010110001010111
1011010100000001
1010010111101101
1000010100100101
1011011100101100
1001000000110001
1001001110001111
1011101000000001
1010001000110111
1011011011100111
10000100101000d1
1111111111111111

aaaaaaaaaaaaaaaa
bbbbbbbabbabbaba
abaaaababbbbbaba
ababbabbbaaabbba
ababaabbbababaaa
abaabababbbbbbba
ababbabaaaabaaba
abbbbababbabbaba
abaabaaabbabaaab
abbabbbbbbaabbba
abbabbaaabbbaaaa
abaaababbbbbbbba
ababbbabbbaabaaa
abaabaabaaabbadd
ddbbbabbababbbdd
aaaaaaaaaaaaaaaa

background.txt

maze1.png
maze2.png

Any help will be appreciated.

Aucun commentaire:

Enregistrer un commentaire