Author Topic: Reading tilemap algorithm  (Read 3738 times)

0 Members and 1 Guest are viewing this topic.

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Reading tilemap algorithm
« on: October 22, 2011, 02:06:01 pm »
I have an issue that's been annoying me for some time now. I have to read a file to an bi-dimensional array.

The file looks like this:

Code: [Select]
1111111111111111111111111
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1000000000000000000000001
1111111111111111111111111

I am using the C library ('stdio.h') to read the file, and this is what I have so far.

Code: [Select]
int map[26][24];
int map_width = 26;
int map_height = 24;
int x;
int y;
FILE *map_file = fopen("map.txt", "r");
for (x = 0; x < map_width; x++)
{
  for (y = 0; y map_height; y++)
  {
     fscanf(map_file, "%d", &map[x][y]);
  }
  fscanf(map_file, "\n"); // Skip newlines
}

However, it doesn't really seem to be working, since I'm printing the array right after and it doesn't look like the file:

Code: [Select]
for (x = 0; x < map_width; x++)
{
  for (y = 0; y < map_height; y++)
  {
    printf("%d", map[x][y]);
  }
}

Thanks for the help!

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Reading tilemap algorithm
« Reply #1 on: October 22, 2011, 02:13:18 pm »
Are you casting to an int after you read from the file? Because that might be the problem.

In Daemons I read from a binary file and I read to a memblock equal to the size of the file - 2 bytes for size data.

I then copy that memblock to a vector for use in the game. ;)

Perhaps you could try that. (Is this only going to be C or C++?)

EDIT: Here's my code:

Code: [Select]
fstream map;
    string map_path;//Holds the path to the files

    try
    {


        map_path = "data/maps/visual/" + mapID;

        map.open(map_path.c_str(), ios::in|ios::binary);
        char memblock[2];
        //char* mapblock;
        if(map.is_open())
        {
            //memblock = new char [2];
            //map.seekg(0, ios::beg);
            map.read(memblock, 2);

            MapWidth = memblock[0];
            MapHeight = memblock[1];
            //delete[] memblock;
            char mapblock[MapWidth * MapHeight];
            MapData.resize(MapWidth * MapHeight);
            //map.seekg(2, ios::beg);
            map.read(mapblock, MapWidth * MapHeight);
            for(int i = 0; i < (MapWidth * MapHeight); i++)
                MapData[i] = (int)mapblock[i];
            //cout << MapData[0];
            //cout << map_path.c_str();

            map.close();
        }

}
    catch(exception& e)
        {cout << e.what() << "\n";}
    return;
« Last Edit: October 22, 2011, 02:15:25 pm by HOMER-16 »

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline Munchor

  • LV13 Extreme Addict (Next: 9001)
  • *************
  • Posts: 6199
  • Rating: +295/-121
  • Code Recycler
    • View Profile
Re: Reading tilemap algorithm
« Reply #2 on: October 22, 2011, 02:18:58 pm »
Are you casting to an int after you read from the file? Because that might be the problem.

In Daemons I read from a binary file and I read to a memblock equal to the size of the file - 2 bytes for size data.

I then copy that memblock to a vector for use in the game. ;)

Perhaps you could try that. (Is this only going to be C or C++?)

EDIT: Here's my code:

Code: [Select]
fstream map;
    string map_path;//Holds the path to the files

    try
    {


        map_path = "data/maps/visual/" + mapID;

        map.open(map_path.c_str(), ios::in|ios::binary);
        char memblock[2];
        //char* mapblock;
        if(map.is_open())
        {
            //memblock = new char [2];
            //map.seekg(0, ios::beg);
            map.read(memblock, 2);

            MapWidth = memblock[0];
            MapHeight = memblock[1];
            //delete[] memblock;
            char mapblock[MapWidth * MapHeight];
            MapData.resize(MapWidth * MapHeight);
            //map.seekg(2, ios::beg);
            map.read(mapblock, MapWidth * MapHeight);
            for(int i = 0; i < (MapWidth * MapHeight); i++)
                MapData[i] = (int)mapblock[i];
            //cout << MapData[0];
            //cout << map_path.c_str();

            map.close();
        }

}
    catch(exception& e)
        {cout << e.what() << "\n";}
    return;

Actually, the program is C++, but I thought C would be better for this, due to speed and size issues, I want map loading to be fast.

What do you mean by "Are you casting to an int after you read from the file? Because that might be the problem."?
« Last Edit: October 22, 2011, 02:19:09 pm by ephan »

Offline Scipi

  • Omni Kitten Meow~ =^ω^=
  • LV10 31337 u53r (Next: 2000)
  • **********
  • Posts: 1547
  • Rating: +192/-3
  • Meow :3
    • View Profile
    • ScipiSoftware
Re: Reading tilemap algorithm
« Reply #3 on: October 22, 2011, 02:23:44 pm »
You are reading from a text file, so what you are reading is going to be a char data type. However, the array is an int so there's going to be issues there. I use the code:

Code: [Select]
MapData[i] = (int)mapblock[i];

to fix that in my file loading as it changes the char to an int. (Also, the function posted above is quite fast, speed won't be a problem between C/C++) ;)

Imma Cat! =^_^= :3 (It's an emoticon now!)
Spoiler For Things I find interesting:
Spoiler For AI Programming:
Spoiler For Shameless advertising:

Spoiler For OldSig:





Spoiler For IMPORTANT NEWS!:
Late last night, Quebec was invaded by a group calling themselves, "Omnimaga". Not much is known about these mysterious people except that they all carried calculators of some kind and they all seemed to converge on one house in particular. Experts estimate that the combined power of their fabled calculators is greater than all the worlds super computers put together. The group seems to be holding out in the home of a certain DJ_O, who the Omnimagians claim to be their founder. Such power has put the world at a standstill with everyone waiting to see what the Omnimagians will do...

Wait... This just in, the Omnimagians have sent the UN a list of demands that must be met or else the world will be "submitted to the wrath of Netham45's Lobster Army". Such demands include >9001 crates of peanuts, sacrificial blue lobsters, and a wide assortment of cherry flavored items. With such computing power stored in the hands of such people, we can only hope these demands are met.

In the wake of these events, we can only ask, Why? Why do these people make these demands, what caused them to gather, and what are their future plans...

Offline lkj

  • LV6 Super Member (Next: 500)
  • ******
  • Posts: 485
  • Rating: +58/-1
    • View Profile
Re: Reading tilemap algorithm
« Reply #4 on: October 22, 2011, 02:26:46 pm »
It should be
Code: [Select]
int map[24][26]; instead of your
Code: [Select]
int map[26][24];.
Also there's a typo: for (y = 0; y < map_height; y++)
And I think you have to exchange the x and the y loop.
Correct me if I'm wrong.