Archived

This topic is now archived and is closed to further replies.

Bezzant

Loading An Array

Recommended Posts

Bezzant    122
Hi all, Im making a tile based rpg and I want to be able to load the array that stores the tile info from a text file that is created with my level editor, This has been causing me problems for ages and I can''t get it to work. Could anybody help me out? My level editor saves the data like this: { {4,4,4,8,7,4,4,4,4,4}, {3,3,4,8,7,4,1,1,6,4}, {3,3,4,8,0,4,6,1,1,4}, {2,2,4,8,7,4,1,1,1,4}, {2,2,4,8,7,4,1,1,6,4}, {6,2,4,8,7,4,6,1,1,4}, {6,2,4,8,7,4,1,1,1,4}, {6,2,4,8,7,4,1,1,6,4}, {2,2,4,8,7,4,6,1,1,4}, }; Any help would be very helpful. Many Thanks Alan IF YA SMELL... WHAT THE BEZZ IS COOKIN'' Check Out My Site #Programming

Share this post


Link to post
Share on other sites
Pete Bassett    122
Hi.

Presumably your writing the array out to file in binary mode using somthing like this

// write the X size of the map
file.write(&lngXSize, sizeof(lngXSize));
// write the Y size of the map
file.write(&lngYSize, sizeof(lngYSize));
// write the array out
file.write(array, sizeof(array));

For this sort of 2d map lngXSize * lngYSize == sizeof(array) / sizeof(array[0])

You can thus read it back in by doing this

// read in the X size of the map
file.read(&lngXSize, sizeof(lngXSize));
// read in the Y size of the map
file.read(&lngYSize, sizeof(lngYSize));
// read in the array
file.read(array, lngXSize * lngYSize * sizeof(array[0]));

Hope this helps

Pete

Share this post


Link to post
Share on other sites
Bezzant    122
Sorry. That doesn''t help. I have written the level editor in VB it just creates a normal text file that contains the info for the tiles. I can read a file in a console app by using the fstream header file to read and display the file. Is there a way in which this method can be used in my opengl game. Heres how I load a text file in a dos window:

#include
main()
{
char ch;
char filename[20]="a:level.txt"; //location on disk
int mode = ios::in;
fstream fin( filename, mode);
if(!fin)
cout<<"Unable to open file!";
while (fin.get(ch))
{
cout< }
fin.close();
return(0);
}

Is there a way to use this method to load a text file as the array?

Many Thanks

Alan

IF YA SMELL... WHAT THE BEZZ IS COOKIN''

Check Out My Site #Programming

Share this post


Link to post
Share on other sites
Pete Bassett    122
Oh, ok. I think I get you.

Which file statements are you using in VB? Are you wirting binary data or does the array come out a a string of numbers? Can you post the VB file code?

Share this post


Link to post
Share on other sites
archetype    122
void Map::load(string filename)
{
int mode = ios::in;
fstream fin( filename, mode);
if(!fin)
logError("Unable to open file!");

// in your global section, or class
// you should have an array type thing,
// i am calling it m_array
m_array.initialize(width, height);
int count1=0;
int count2=0
while (fin.get(ch))
{
if(!(ch==''{''||ch==''}''||ch=='','')){
m_array[count1,count2]=ch;
if(++count2>width){
count1++;count2=0;
}
}
fin.close();
}


AHHHHHHHHHHHH, headaches!! Ouch, I hate array''s.

well basically, filter out the ''{ }'' brackets and , commas,
keep track of the width and height of your map, and fill up your array with only the data.

Sorry if that made little sense.

PS, if there is a bug in my code, sorry, but that is the general idea of things.

Share this post


Link to post
Share on other sites
Pete Bassett    122
You mean that

{
{4,4,4,8,7,4,4,4,4,4},
{3,3,4,8,7,4,1,1,6,4},
{3,3,4,8,0,4,6,1,1,4},
{2,2,4,8,7,4,1,1,1,4},
{2,2,4,8,7,4,1,1,6,4},
{6,2,4,8,7,4,6,1,1,4},
{6,2,4,8,7,4,1,1,1,4},
{6,2,4,8,7,4,1,1,6,4},
{2,2,4,8,7,4,6,1,1,4},
};

is how the data is saved to the file? Including all the brackets and commas?

Now thats what I call making life hard for yourself! haha.

In this case, do what archetype said. (Or just write the data out in binary form. Nice and easy to read it back in then!)

Pete

Share this post


Link to post
Share on other sites
Bezzant    122
Thanks everyone,
I''ll have a good look at all the good provided. It seems as though a text file was not the best choice so I have just made a quick program to convert the text files my VB level editor makes and this lil'' program converts them to binary for me, so that will make things easier. I''ll try testing out everything tommorrow....I need sleep.

1 quick question. When I write the binary file should I leave out all the comma''s and braces?

Many Thanks For The Help

Alan

IF YA SMELL... WHAT THE BEZZ IS COOKIN''

Check Out My Site #Programming

Share this post


Link to post
Share on other sites
zedzeek    528
yes in binary u normally leave out the commas

personally im a great believer in txt files i try to use them as much as possible they save u so much coding time (very important when youve developing someit)

if i was u ill stick to using the txt format

http://members.xoom.com/myBollux

Share this post


Link to post
Share on other sites
Pete Bassett    122
Hi again.

I know that the binary vs Text file debate has been done to death but I have to ask some questions.

Zedzeek : You say that using textfiles saves you coding time. How? The code to utilise a specifically formatted text file could be fairly simple but the power of text files come from using a Property=Value like format. You query the file class for a value and it parses the file for the value associated with that Property. This is how ini files work.

The code to write this (without using os functions) is much more obese that a nice standard binary file. The same can be said for a binary file.

Which is easier to code, a text file parser using fscanf or a set of ordered binary reads from the file? I say the second option.

Which is easier to change, or to eyeball? The text file and this does have great advantages while debugging.

This brings me to another point. Debugging of a file loader should be fairly simple. Once its working, you dont care what format your files are in. So why not write it in the most simple way.

Also, reading binary files is generally faster than parsing a text file.

Just my opinion

Pete

Share this post


Link to post
Share on other sites
Why not have a combination text/binary parser. It recognises which so you can use text files for development (easy to mess with) then change these to binary files for release (faster to load).
You only have to write the parser once, and then you can re-use the same code.

Share this post


Link to post
Share on other sites
zedzeek    528
i remeber treading recently carmack saying for the upand coming doom3 they want everything to be textfiles

>>Zedzeek : You say that using textfiles saves you coding time. How?<<

eg just yesterday i was working on this (part of a text file)

bolluxname49
{
+SKIN
TEX0 grafix/grounds/flacky_dirt256.tga
TEX1 grafix/grounds/grass_alpha_speckled3.tga
-SKIN
}

now i was having a problem i thought it could of been cause by the second texture so i went

bolluxname49
{
+SKIN
TEX0 grafix/grounds/flacky_dirt256.tga
// TEX1 grafix/grounds/grass_alpha_speckled3.tga
-SKIN
}


ie commented it out (resaved it) and ran the program again and when the textfile got read the parser came to the // and ignored the rest of the line (simple + quick)

try doing that as easily with a binary file.
this is just one example but when you''re developing someit u should try and save as much time as possible , which leaves u with more time working on the real problems


http://members.xoom.com/myBollux

Share this post


Link to post
Share on other sites
Pete Bassett    122
Ok. But didn''t the time required to write the nice text parsing code overshadow the time saved by this example.

Dont get me wrong. I like text files, I really do. But, I know what it like to get a really robust and extensible (That what I really want) tetx parser. I know its hard. I wrong an interpreter for a non-object oriented version of VB last year and it was not trivial to do correctly I can tell you.

Carmack may well want everything in text files? But I guarentee that he wont put everything in text files. E.g the BSP files. I think not.

Anyway, I''m getting off topic. The point is, for a simple example like the grid based map that Bezzant wants, a binary file is the way to go, at least for now.

Pete

Share this post


Link to post
Share on other sites
zedzeek    528
>>Carmack may well want everything in text files? But I guarentee that he wont put everything in text files. E.g the BSP files. I think not.<<

why not. text compresses great

(edit just found this its carmack btw)

>> The decisions to integrate all tools (editor and map processing) directly into the executable, and to make map source data required for loading in addition to derived data, make the new game far and away the easiest to create content for. Every installation that can play the game can edit the game.
Also, I have banished the last of the binary file formats, so everything (except standard data files like .wav and .tga) is now in easily explored and understood text files. <<

so it looks like the level format (bsp) will be text , which is logical.
its laziness that creates binary formats for games, im very guilty of this as well

http://members.xoom.com/myBollux

Edited by - zedzeek on February 2, 2001 4:40:47 PM

Share this post


Link to post
Share on other sites
Guest Anonymous Poster   
Guest Anonymous Poster
quote:
Original post by Pete Bassett

Ok. But didn''t the time required to write the nice text parsing code overshadow the time saved by this example.



Duh!!!! Have you never heard of the ability to re-use code once it''s written. Obviously not.

Share this post


Link to post
Share on other sites