Welcome, Guest. Please login or register. Did you miss your activation email?

Author Topic: Problem with tile system  (Read 1784 times)

0 Members and 1 Guest are viewing this topic.

OutlawLee

  • Jr. Member
  • **
  • Posts: 50
  • This is my personal text. Dont read it.
    • View Profile
    • Email
Problem with tile system
« on: May 12, 2012, 11:29:45 am »
Hi guys, im trying to load a map with rapidxml, it compiles alright but when i call the function , i get a error.

Here is the code:

Code: [Select]
void LoadMap(std::string mapname, Block *blocks[] )
{
//Loads a level from xml file
//Load the file
std::ifstream mapFile(mapname);

if(!mapFile)
throw "Could not load tileset: " + mapname;

//Dump contents of file into a string
std::string xmlContents;

//Blocked out of preference
{
std::string line;
while(std::getline(mapFile, line))
xmlContents += line;
}

//Convert string to rapidxml readable char*
std::vector<char> xmlData = std::vector<char>(xmlContents.begin(), xmlContents.end());
    xmlData.push_back('\0');

//Create a parsed document with &xmlData[0] which is the char*
rapidxml::xml_document<> doc;
doc.parse<rapidxml::parse_no_data_nodes>(&xmlData[0]);

//Get the root node
rapidxml::xml_node<>* root = doc.first_node();

std::string imagepath;

//Load each necessary tileset
rapidxml::xml_node<>* tileset = root->first_node("tileset");
while(tileset)
{
imagepath = tileset->first_attribute("name")->value();
}
sf::Texture texture;
texture.loadFromFile(imagepath);

//Columns and rows (of tileset image)
    int columns = texture.getSize().x / 32;
    int rows = texture.getSize().y / 32;

std::vector <sf::Rect<int> > subRects;
    for (int y = 0; y < rows; y++)
    {
        for (int x = 0; x < columns; x++)
        {
            sf::Rect <int> rect;
            rect.top = y * 32;
            rect.height = y * 32 + 32;
            rect.left = x * 32;
            rect.width = x * 32 + 32;
            subRects.push_back(rect);
        }
    }
rapidxml::xml_node<>* tile = root->first_node("tile");
int block_type = -1;
int x = 0;
int y = 0;
int n = 0;

while(tile)
{
block_type = atoi(tile->first_attribute("gid")->value());

blocks[n] = new Block( n, block_type, x, y);

x += 32;

cout<<"ID: "<<n<<" Block type: "<<block_type<<" X,Y: "<<x<<","<<y;

        if( x >= SCREEN_WIDTH)
        {
            x = 0;
            y += 32;
        }

n++;
tile = tile->next_sibling("tile");
}
mapFile.close();
}

This is how i call it:

Block* blocks[TOTAL_TILES];
LoadMap("spawn.tmx", blocks);

Debug error: R6010
-abort() has been called

and i can only click abort, retry and ignore
« Last Edit: May 12, 2012, 11:32:44 am by OutlawLee »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11032
    • View Profile
    • development blog
    • Email
Re: Problem with tile system
« Reply #1 on: May 13, 2012, 12:42:03 am »
Learn how to use a debugger and the call stack, because it will:
  • Help you find solutions on your own.
  • Teach you one of the most important basis of programming.

Additionally try to write safe code. This means don't use arrays, use std::vector. Try to avoid pointers and use either references or smart pointers, depending on the situation.

For the code; often it's the same as with other texts: TL;DR.  ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/