Advertisement

Syntax Question for Tile-Based Engine

Started by April 05, 2000 01:24 PM
59 comments, last by Nazrix 24 years, 6 months ago
Here's a way it can be done (which could pretty easily be adjusted to suit your own format):

boolean read_level_file(const char* filename)
{
int rows = 0, cols = 0;
FILE *fp = NULL;

if((fp = fopen(filename, "rt")) == NULL) return FALSE;

else
{
fscanf(fp, "%d", &pacman.direction);

for(rows = 0; rows < (TILE_ROWS); ++rows) {
for(cols = 0; cols < (TILE_COLUMNS); ++cols) {

fscanf(fp, "%d", (int*)&current_level.tile[rows][cols].type);

}
}
}

fclose(fp);
return TRUE;
}



It looks much nicer with indents, but the forum removes them

/. Muzzafarath

Edited by - Muzzafarath on 4/10/00 11:28:31 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
excuse my ignorance but I'm not sure if I understand what
&pacman.direction is...

and I don't totally understand this line:
fscanf(fp, "%d", (int*)¤t_level.tile[rows][cols].type);

Thanks for any info....







Edited by - Nazrix on 4/15/00 8:41:11 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Advertisement
Pacman is my main character in my game. I store all of his information in this struct:

struct sprite
{
unsigned int score;
unsigned int level;

unsigned int x;
unsigned int y;

unsigned int direction;

char name[50];
}pacman;


So Pacman is a sprite structure, and direction is just an integer in the sprite struct that tells which way he is facing. Whenever I load a level, I read what way he should be facing.

fscanf(fp, "%d", (int*)current_level.tile[rows][cols].type);


fscanf() is a function that takes a printf() like format string and read what you wanted it from a file. "current_level.tile[rows][cols].type" refers to my level structure. I cast it to an int* so I won''t get a stupid warning... So what I do is that the line will read an integer from a file and store it in current_level.tile[rows][cols].type.

/. Muzzafarath
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Okay, I figured that was what pacman was

I tried this code, and it didn't seem to work..but am I at all on the right track?

FILE *fp = NULL;
fopen("map10-9.map", "rt");
for (int j=0; j<=21; j++){fscanf(fp,"%d", world [j];}



I tried %s and %c instead of %d in the fscanf function but to no avail.

It said that fscanf was null a few times...and it locked up a few times too.

Thanks for your help



Edited by - Nazrix on 4/18/00 1:04:36 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
I'm not sure what kind of variable world[j] is supposed to be, but you need to pass a pointer to fscanf.

Like so:
int myNumber;
FILE *someFile;
fscanf(someFile, "%d", &myNumber);
(Don't forget the '&' in front of the variable.)

Of course, you need to open the file before you read from it, but I'm guessing that part's already working for you.

Edited by - Jesse Chounard on 4/16/00 2:32:58 PM
well I initialize world like this
char *world[21]=
{"11111111111111111",
"11111111111122333",
and so on until there's 21 lines...

So, I wanted to have a file that had the 21 rows of numbers and read them into the world arrays world[0] through world[21]...

yes, I believe it's opening it okay...

So perhaps if I made it
for (int j=0; j<=21; j++){fscanf(fp,"%d", &world [j]);}
by added the '&'

I'm at work so I cannot try it yet

Thanks again






Edited by - Nazrix on 4/16/00 5:57:27 PM

Edited by - Nazrix on 4/16/00 6:20:00 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
Advertisement
Say your file looks like this:

111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111
111111111111111111111111111111

To read it you can do this:


FILE *fp = NULL;
fp = fopen("map10-9.map", "rt");
if(fp == NULL) exit(1); //File didn't open correctly

for (int j=0; j<=21; j++) { fscanf(fp, "%s", world [j]; } //Read one line from the text file


Since world[] is a pointer, there's no need to put the & infront of it when you call fscanf() since it already contains an address to a char.

That should clear things up.

/. Muzzafarath

Edited by - Muzzafarath on 4/17/00 9:43:59 AM
I'm reminded of the day my daughter came in, looked over my shoulder at some Perl 4 code, and said, "What is that, swearing?" - Larry Wall
Yes, that helps to clear things up.
That code makes perfect sense, however I tried it and it's not able to open the file. I checked the filename and it is correct. I even tried entering the exact path to the filename. I tried opening it with a "r" for reading and tried "r+" also. I'm not too sure what I'm doing wrong this time. As soon as I call the function to load the file it just exits the program. Sorry to keep bother you guys.

Edited by - Nazrix on 4/17/00 5:46:01 PM
Need help? Well, go FAQ yourself. "Just don't look at the hole." -- Unspoken_Magi
------------ Nazrix -------------

char *world[21]=
{
"111111111111111111111111111111",
"100000000000000000000000000001",
"100002222220000000000000077701",
"100002222223333333333000077701",
"100002222227777777773000070001",
"100002222227777777773000070001",
"100000000377777777773000070001",
"107777700377777777773000070001",
"177777770377777777773000770001",
"107777700377777777773007700001",
"100777770377777777773777000001",
"100000707377777777773000000001",
"100007777377777777773000000001",
"100000000302222777773000000001",
"100000000332222777773000000001",
"100000000002222333333000000001",
"100000666666666666666666600001",
"100000800000000000000000800001",
"100000800000000000000000800001",
"100000000000000000000000000001",
"111111111111111111111111111111",
};

----------- END ----------------

Ok, I see that none of these guys answered the question right. First of all, this is just a 2 dimensionnal array. It can be accessed as world[1][1]="1";// change the first character or world[1]="111111111111111111111111111111";// Change the first line. This is the way to access these types of variable....



Cyberdrek
Headhunter Soft
DLC Multimedia
Two Guys Soft
[Cyberdrek | ]
somebody should make a level editor tutorial. Or a file i/o tutorial(but there''s probably a few of those around though)

This topic is closed to new replies.

Advertisement