Page 1 of 2
Loading tiles from a file
Posted: Sat Aug 07, 2010 11:43 am
by PaperDuckyFTW
heeeeeeeeeeeeeeeeeeeeey
Im having a bit of a problem with loading a tilemap froma .txt file. Everything from drawing to collision works when i use an array, but whenever i try to read it from a file, the compiler gives some:
main.obj : error LNK2001: unresolved external symbol __imp___CrtDbgReportW
fatal error LNK1120: 1 unresolved externals
Anyway, can anyone refer me to a tutorial or give some help? This is how the text file looks like.
00000
00020
01220
00000
00000
Code: Select all
This is what it loks like in the array version:
void draw_map()
{
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
//if we read a number, draw respective tile
if( map1[y][x] == 0 )
{
draw_tile( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, map[0][0].id );
}
if( map1[y][x] == 1 )
{
draw_tile( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, map[1][1].id );
}
if( map1[y][x] == 2 )
{
draw_tile( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, map[2][2].id );
}
}
}
}
This is what Ive got in the reading file version
void LoadLevel( const char* filename )
{
std::ifstream file("Levels/level0.txt");
char temp;
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
file >> temp;
if( temp == '0' )
{
draw_tile( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, map[0][0].id );
}
if( temp == '1' )
{
draw_tile( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, map[1][1].id );
}
if( temp == '2' )
{
draw_tile( x * 32 - camera.x, y * 32 - camera.y, tiles, screen, map[2][2].id );
}
}
}
file.close();
}
The map[x][x].id is just the sprite from the tilesheet that is allocated to the given tile. If anyone knows how to fix this, or give a better example, please do =] Have fun and best of luck with your devving
,V,,
Re: Loading tiles from a file
Posted: Sat Aug 07, 2010 12:46 pm
by ibly31
Well for one, why are you calling draw_tile in your load function? Your load function should read the character at the specific point in the file, then set map[y][x] to that, not draw it. Also you have map[0][0] then map[1][1], then map[2][2] etc. Im not sure if your trying to do that, but that ends up forming a diagonal line like this:
10000
01000
00100
00010
00001
Re: Loading tiles from a file
Posted: Sat Aug 07, 2010 2:12 pm
by JaxDragon
ibly31 wrote:
10000
01000
00100
00010
00001
Yeah, I don't think you're going for an identity matrix
Re: Loading tiles from a file
Posted: Sat Aug 07, 2010 2:26 pm
by X Abstract X
Why does LoadLevel() take a parameter if you don't use it?
Why are you drawing in your loading function?
You should check to make sure the stream is valid before you try to read from it.
You're wastefully comparing the value of temp more times than necessary. Use else if statements or a switch statement.
In your draw_map() function you should try to operate on the data in a linear fashion, for performance reasons.
A 2D array is really just a 1D array in memory. Right now you're doing something like:
foo[0]...
foo[40]...
foo[80]...
That is probably SEVERAL TIMES SLOWER than:
foo[0]...
foo[1]...
foo[2]...
So, your loading function should end up looking
something like this:
Code: Select all
bool loadMap(const std::string& filename) {
std::ifstream file;
file.open(filename.c_str());
if (!file)
return false;
for (unsigned int x = 0; x < MAP_WIDTH; ++x)
for (unsigned int y = 0; y < MAP_HEIGHT; ++y)
file >> map[x][y];
file.close();
return true;
}
Re: Loading tiles from a file
Posted: Sat Aug 07, 2010 4:07 pm
by ultimatedragoon69
This is how i originally loaded my map information from text files. (Mind you that there isn't any event information here, this is my old loading function).
Code: Select all
void gameMovementCoord::setAreaDiameters(int areaNumber)
{
loadMapNames();
string fileName;
ifstream parameter;
ifstream parameterColor;
ifstream parameterCollision;
fileName = mapNames[areaNumber] + ".txt";
parameter.open(fileName.c_str(), ios::in);
fileName = mapNames[areaNumber] + "Color.txt";
parameterColor.open(fileName.c_str(), ios::in);
fileName = mapNames[areaNumber] + "Collision.txt";
parameterCollision.open(fileName.c_str(), ios::in);
for ( int j = 0; j < 24; j++ )
{
for ( int i = 0; i < 74; i++ )
{
if(!parameter.eof())
parameter >> map[j][i];
if(!parameterColor.eof())
parameterColor >> mapColor[j][i];
if(!parameterCollision.eof())
parameterCollision >> mapPassable[j][i];
else
return;
} printf("\n");
}
parameterCollision.close();
parameterColor.close();
parameter.close();
}
I currently use a completely different method where i wrote a tile object a map object, saved each individual tile object in a single binary file and reloaded that binary file with all the information i needed.
of course this is just numbers not dimensions but might help anyway who knows.
Re: Loading tiles from a file
Posted: Sat Aug 07, 2010 8:03 pm
by PaperDuckyFTW
Thanks for your replies =] To clear some things up the map[0][0] is part of a struct that has:
map[0][0].id, which is the image of the tile, and
map[0][0].solid, which sets the tile to a solid or wall tile.
It all works perfectly fine and there isnt any problems. The only real reason why it is an array, is to assign a tile from the desired sprite sheet. It doesnt form a diagonal line as the array parameters arent the position. But I completely understand how you could have thought it was.
I hope that all makes sence ^.^" In short, dont worry about the map[0][0], everything works perfectly fine, and the god sent genious
who helped me with tile collision, used that. Seriously he is a genious - i was stuck on tile collision for almost a month ='(
As for drawing in my load function - I had no idea what I was doing tbh. I got the base of the function off some source code given to me from a friend, however its not the same as what I need for my project. However it shows reading from a file, so I can just edit the loading and drawning method. I didnt really think it was needed to load the tiles, then separately draw them. How would any of you read and draw tiels from a txt file?
EDIT: Sorry, I just thought of a more clearer, correct way to explain map[0][0] - its not the game map, its the tiles. Im embarrasingly sorry that it looks like the map and now I know where you were all coming from. Its the tile...yer i shuld probably change that to avoid confusion
Re: Loading tiles from a file
Posted: Sat Aug 07, 2010 8:52 pm
by PaperDuckyFTW
===== UPDATE =====
The main thing that was bothering me was it wasnt compiling and giving me stupid errors. Turns out all I had to do was to remove the _DEBUG line in the C/C++ Preprocessor Definitions section of project properties =] So it compiles now and works fine. As you said, in the LoadLevel function, there was an error with just drawing the tiles. It reads that there are tiles, but i need a separate function to draw the tiles
Im gonig to work on that, but can anyone help me with that? Im not to sure as of yet how i can do it but ill look into it. Thanks for all your help and suggestions :D
Re: Loading tiles from a file
Posted: Sat Aug 07, 2010 11:49 pm
by ibly31
Btw, I know errors always seem dumb and that they are the annoying thing about programming but, don't ever say stupid errors. People will just laugh at you. If you think about it logically, the computer is doing EXACTLY, i mean EXACTLY what you are telling it. And when you think that it is dumb for doing what you told it... I think that sentence can finish itself.
Just ignoring different parts of the code that someone gave you isn't very smart. I always try to make absolutely sure I understand every last character of every code file I have. If you don't know exactly how it works, it'll be a million times harder to debug.
I'm still confused about your tile system. You are saying that map[0][0] holds the tiles? My interpretation of that is that that array holds the Texture Variables and such. But that doesn't seem to be what you are using it for. I know you think you have it figured out, but trust me there is something wrong with your code. Before you try to change it, try to understand it first. Ask questions like: what does [XXX] mean?.
Sorry if I come of as harsh or that I think I'm amazing(trust me, I've asked a bunch of stupid questions), it's just that I was once in your position with code that I didn't really get and it all went down hill from there.
Re: Loading tiles from a file
Posted: Sun Aug 08, 2010 12:58 am
by PaperDuckyFTW
the errors were with the compiler, it wasnt from my code. Changing a compiler option removed the error.
Code: Select all
This is the tile system:
struct CTile
{
int id; //refers to a SDL_Rect of a tile sheet
bool solid; //test to see if you can walk through or not
};
//instance of the tile struct, 40 tiles horinzontally, 30 vertically
CTile map[40][30];
So far it reads the file, but everyway I try, I cant get it to draw any tiles. Can someone please post a link to where you learned how to load tiles from a file? This thing is starting to piss me off, no matter how childish it sounds to be pissed over some code. Im trying to do something that i cant find a tutorial on, and so far its all been guess work. But hey, I know why they made Left for Dead 2. So coders can relieve anger at their projects by killing zombies. "F*** YOU WITCH!! Thats for screwing up my file laoding function!@@$"
And dont worry, im still very, very noobie (pretty obvious though) I dont find critisism personal unless they say "omg stfu you suc balls and your method is confusing" Im happy to get critisism as it allows me to learn more.
I just tested another piece of code. I only drew the wall tile to see if it would actually draw it. Turns out it just fills the screen with the sky tile, and doesnt draw the wall tile. I just realised this is more of a log then a question, but help is always appreciated
Re: Loading tiles from a file
Posted: Sun Aug 08, 2010 1:05 am
by PaperDuckyFTW
I now know whats been cocking up. I feel incredibly stupid, but i wasnt drawing the tile whenever the file saw its respective number. I was just filling the screen with the tile
My new question is: in the LoadLevel function, it opens and reads the file perfectly fine, no issues there. How can I draw the tiles to the screen, after the LoadLevel function reads them?
Re: Loading tiles from a file
Posted: Sun Aug 08, 2010 5:39 am
by MrDeathNote
PaperDuckyFTW wrote:I now know whats been cocking up. I feel incredibly stupid, but i wasnt drawing the tile whenever the file saw its respective number. I was just filling the screen with the tile
My new question is: in the LoadLevel function, it opens and reads the file perfectly fine, no issues there. How can I draw the tiles to the screen, after the LoadLevel function reads them?
Like a few other ppl have shown in their code fragments, you need to populate your map array from the text file in your load level function (not try and draw the tiles) and then call draw_map().
Re: Loading tiles from a file
Posted: Sun Aug 08, 2010 6:25 am
by PaperDuckyFTW
Oh i understand. Would i need to do anything other than
Code: Select all
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
file >> temp;
map[y][x].id = temp;
cout<<temp;
//so for example, if the txt file has a 0 in it
//will it set the map[y][x].id to 0?
}
}
Then
Code: Select all
//if the array has a '0' in it
if( map[y][x].id == 0 )
{
draw_tile( x, y, tile, screen, tileFromSheet);
}
If it helps, I can use the map[0][0].id array without the .id. If so, how do you allocate a file's numbers to an array?
Re: Loading tiles from a file
Posted: Sun Aug 08, 2010 12:13 pm
by MrDeathNote
PaperDuckyFTW wrote:Oh i understand. Would i need to do anything other than
Code: Select all
for( int x = 0; x < 40; x++ )
{
for( int y = 0; y < 30; y++ )
{
file >> temp;
map[y][x].id = temp;
cout<<temp;
//so for example, if the txt file has a 0 in it
//will it set the map[y][x].id to 0?
}
}
Then
Code: Select all
//if the array has a '0' in it
if( map[y][x].id == 0 )
{
draw_tile( x, y, tile, screen, tileFromSheet);
}
If it helps, I can use the map[0][0].id array without the .id. If so, how do you allocate a file's numbers to an array?
I'm not even sure why your using .id you don't need to. Just set the array value like this:-
Re: Loading tiles from a file
Posted: Sun Aug 08, 2010 6:59 pm
by PaperDuckyFTW
*sigh of relief* I can now say i FULLY understand why it wasnt working. It reads the file all as '0', so I can draw tiles with the tile sprite allocated to tile number 0. However it doesnt read any other number. That was why it wasnt working.
Anyone know why its not reading any number other then 0? I even filled the whole file with '2' to make sure it only read the '0's, but its not readin any other number then 0.
Thanks for everyone's time and patience, its muchly greatly superly appreciated
Re: Loading tiles from a file
Posted: Sun Aug 08, 2010 9:05 pm
by ultimatedragoon69
Are you checking to make sure the file was opened? Is the file in the right location? Did you double check the name of the file? Try printing out the array to see if it populated correctly. ~Some code of how your loading thing the text files might help~
It could be reading only 0's because it's not reading the file correctly.