Page 1 of 1

[RESOLVED] Endian Switching and Structs on Wii?

Posted: Thu Jun 24, 2010 8:18 pm
by LeonBlade
Hello, yet again.

I have a few questions and problems with this.
I'm porting my engine (as it is so far) to my Wii, and I've encountered some errors.
If I use structs like I normally do, it throws some errors at me.

For example:
Output Log wrote: file.c: In function 'main':
file.c:137: error: 'MapHeader' undeclared (first use in this function)
file.c:137: error: (Each undeclared identifier is reported only once
file.c:137: error: for each function it appears in.)
file.c:137: error: expected ';' before 'mapHeader'
file.c:146: error: 'mapHeader' undeclared (first use in this function)
Here are my structs:

Code: Select all

struct MapHeader
{
	unsigned int		fileLength;
	unsigned short		tilesX;
	unsigned short		tilesY;
};

struct MapData
{
	unsigned char		tileData[4];
};
And here is where I'm calling it.
Line 137:

Code: Select all

MapHeader mapHeader;
Line 146:

Code: Select all

memset(&mapHeader, 0, 8);
I can't use sizeof() like I normally would in replace of 8 because it doesn't work...

So, instead, I called my map header like this:

Code: Select all

struct MapHeader
{
	unsigned int		fileLength;
	unsigned short		tilesX;
	unsigned short		tilesY;
} mapHeader;
Now, this is okay because it compliles and running it on my Wii, I get this output.
Wii Output wrote: BBE Map File Reader

File length: 636 bytes
Tiles X: 12
Tiles Y: 13
Which works just fine, but I'd like to just have my struct like I normally do, and create an instance of it.
So if anyone knows how I would go about that, please let me know.

Next what I would do, is read in my map data, however, this has some problems.
Here is my output:
Output log wrote: file.c: In function 'main':
file.c:154: error: 'MapData' undeclared (first use in this function)
file.c:154: error: (Each undeclared identifier is reported only once
file.c:154: error: for each function it appears in.)
file.c:155: error: expected expression before ')' token
This is my code for those lines.
Line 154:

Code: Select all

unsigned int tileBufSize = tileCount * sizeof(MapData);
Line 155:

Code: Select all

mapData = (MapData*)malloc(tileBufSize);
Basically, that's because there is no struct I can pull MapData from.
So sizeof() wont work... and I can't do line 155 either because of it.

So this is an issue I really need to get over, otherwise I wont be able to read in my map data.
And after this, how would I switch the endian of what I read in from the map data.

This is where I read it in:

Code: Select all

unsigned int tileCount = mapHeader.tilesX * mapHeader.tilesY;
unsigned int tileBufSize = tileCount * sizeof(MapData);
mapData = (MapData*)malloc(tileBufSize);
fread(mapData, tileBufSize, 1, file);
Here is the map data struct again.

Code: Select all

struct MapData
{
	unsigned char		tileData[4];
}
And this is how I would normally call it in my code:

Code: Select all

MapData * mapData;
I switch my other endians with these functions:

Code: Select all

unsigned int swapEndianUI(unsigned int ui)
{
	ui = (ui >> 24) |
		((ui << 8) & 0x00FF0000) |
		((ui >> 8) & 0x0000FF00) |
		(ui << 24);
	return ui;
}

unsigned short swapEndianUS(unsigned short us)
{
	us = (us >> 8) |
		(us << 8);
	return us;
}
But how would I do it for my map data?

Thanks guys,
LeonBlade

Re: Endian Switching and Structs on Wii?

Posted: Thu Jun 24, 2010 8:25 pm
by avansc
you would have to do it for each individual struct member.

Re: Endian Switching and Structs on Wii?

Posted: Thu Jun 24, 2010 8:33 pm
by LeonBlade
That was a horrible reply, avansc as I have no idea what you're referring to...

Re: Endian Switching and Structs on Wii?

Posted: Thu Jun 24, 2010 9:43 pm
by LeonBlade
I got my friend to help me out, I changed the structs to this instead:

Code: Select all

typedef struct
{
	unsigned int		fileLength;
	unsigned short		tilesX;
	unsigned short		tilesY;
} MapHeader;

typedef struct
{
	unsigned char		tileData[4];
} MapData;
And after a few debugging got my final product!
Check my post in Game Development to see a screen shot.

Re: Endian Switching and Structs on Wii?

Posted: Fri Jun 25, 2010 3:15 am
by Amarant
Instead of using typedefs (which I prefer btw) you could also use your previous struct declarations. To use those you would have to add the struct keyword every time you're referring to the structs you've declared.

Example:

Code: Select all

struct MapHeader mapHeader;
memset(&mapHeader, 0, sizeof(struct MapHeader));
unsigned int tileBufSize = tileCount * sizeof(struct MapData);
struct MapData * mapData;
etc...
Btw. What kind of compiler do you normally use ?

Re: Endian Switching and Structs on Wii?

Posted: Fri Jun 25, 2010 3:54 am
by LeonBlade
Amarant wrote:Instead of using typedefs (which I prefer btw) you could also use your previous struct declarations. To use those you would have to add the struct keyword every time you're referring to the structs you've declared.

Example:

Code: Select all

struct MapHeader mapHeader;
memset(&mapHeader, 0, sizeof(struct MapHeader));
unsigned int tileBufSize = tileCount * sizeof(struct MapData);
struct MapData * mapData;
etc...
Btw. What kind of compiler do you normally use ?
I use the devkitPPC toolchain to build on Wii.

Re: Endian Switching and Structs on Wii?

Posted: Fri Jun 25, 2010 7:47 am
by Falco Girgis
Dude, your entire issue boils down to one thing: the syntax for structs in C is different than C++.

If what you had compiled before, it was because your .c files were being compiled by a C++ compiler. In the C language, when you instantiate/use a struct, you must precede it by the struct keyword:

Code: Select all

struct MyClass myClass;
sizeof(struct MyClass);
By typedeffing at declaration, you are essentially doing this:

Code: Select all

struct MyStruct {
    //somebullshit;
};

typedef struct MyStruct MyStruct;
And I'm not sure what you are doing with endian-ness. I'm pretty sure that unless you are actually manipulating your data at the bit levels, that is completely unnecessary.

Edit: oh just realized your map is saved in binary.

Re: Endian Switching and Structs on Wii?

Posted: Fri Jun 25, 2010 4:28 pm
by LeonBlade
Haha, yeah Gyro.
I don't know why it took me so long to figure that out... probably because I haven't used structs very much.
The issue is resolved now though, it kind of just poped up in my head and I felt stupid... but oh well!

And I didn't need to swap the last bits for the map data, so everything works just fine :)