Friday, May 17, 2024
HomeGame Developmentc++ - Tilemap drawing - Game Development Stack Exchange

c++ – Tilemap drawing – Game Development Stack Exchange


I’m trying to implement a function that draws a map, but i’m currently having two problems. The first one is when running the code i get an error: Unhandled exception thrown: read access violation. recB was 0xCDCDCDD5.
From what I can tell the program runs a little bit trying to create the map, then returns this error and the vecB’s value is not found. (Maybe that is whats called out of bounds?) The error occured in code provided down below, although the code i was trying to implement was the one i provided in the bottom. I’m not that experienced in c++. Anyone know what’s happening? Also another problem is, when the code is working and drawing the drawing is also immediately destroyed by the deconstructed and this puzzles me as well, hence I have’nt called that function.

#include "Collision.h"
#include "ECS\ColliderComponent.h"

bool Collision::AABB(const SDL_Rect& recA, const SDL_Rect& recB)
{
    if (
        recA.x + recA.w >= recB.x &&
        recB.x + recB.w >= recA.x &&
        recA.y + recA.h >= recB.y && //(x) here is the error marked by the debugger
        recB.y + recB.h >= recA.y 
        )
    {
        return true;
    }
    return false;
}

bool Collision::AABB(const ColliderComponent& colA, const ColliderComponent& colB)
{
    if (AABB(colA.collider, colB.collider))
    {
        std::cout << colA.tag << " hit " << colB.tag << std::endl;
        
        return true;
    }
    else
    {
        return false;
    }
}

Here is the code I’m trying to implement

#include "Map.h"
#include "Game.h"
#include <fstream>
#include <sstream>
#include <vector>
#include <string>

Map::Map(){
    
}

Map::~Map()
{
    
}

void Map::LoadMap(std::string path, int sizeX, int sizeY) 
{
    std::ifstream mapFile(path);
    std::string line;
    std::vector<std::vector<int>> mapData;

    if (!mapFile.is_open()) {
        std::cout << "ERROR: Unable to open file " << path << std::endl;
        return;
    }


    while (std::getline(mapFile, line)) {
        std::istringstream ss(line);
        std::vector<int> row;
        std::string value;

        while (std::getline(ss, value, ',')) {
            row.push_back(std::stoi(value));
        }

        mapData.push_back(row);
    }

    for (int y = 0; y < sizeY; ++y) {
        for (int x = 0; x < sizeX; ++x) {
            int tileCode = mapData[y][x];
            int srcX = tileCode % 10;
            int srcY = tileCode / 10;
            Game::AddTile(srcX * 32, srcY * 32, x * 32, y * 32);
        }
    }
    mapFile.close();
}

/*
{
    char c;
    std::fstream mapFile;
    mapFile.open(path);

    int srcX, srcY;
    
    for (int y = 0; y < sizeY; y++)
    {
        for (int x = 0; x < sizeX; x++)
        {
            mapFile.get(c); 
            srcX = atoi(&c) * 32;
            mapFile.get(c);
            srcY = atoi(&c) * 32;
            Game::AddTile(srcX, srcY, x * 32, y * 32);
            mapFile.ignore();
        }
    }

    mapFile.close();
}*/

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments