[RESOLVED] Endian Switching and Structs on Wii?

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

[RESOLVED] Endian Switching and Structs on Wii?

Post 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
Last edited by LeonBlade on Fri Jun 25, 2010 4:28 pm, edited 1 time in total.
There's no place like ~/
User avatar
avansc
Respected Programmer
Respected Programmer
Posts: 1708
Joined: Sun Nov 02, 2008 6:29 pm

Re: Endian Switching and Structs on Wii?

Post by avansc »

you would have to do it for each individual struct member.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Endian Switching and Structs on Wii?

Post by LeonBlade »

That was a horrible reply, avansc as I have no idea what you're referring to...
There's no place like ~/
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Endian Switching and Structs on Wii?

Post 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.
There's no place like ~/
User avatar
Amarant
Chaos Rift Newbie
Chaos Rift Newbie
Posts: 34
Joined: Wed Nov 05, 2008 9:52 am

Re: Endian Switching and Structs on Wii?

Post 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 ?
177
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Endian Switching and Structs on Wii?

Post 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.
There's no place like ~/
User avatar
Falco Girgis
Elysian Shadows Team
Elysian Shadows Team
Posts: 10294
Joined: Thu May 20, 2004 2:04 pm
Current Project: Elysian Shadows
Favorite Gaming Platforms: Dreamcast, SNES, NES
Programming Language of Choice: C/++
Location: Studio Vorbis, AL
Contact:

Re: Endian Switching and Structs on Wii?

Post 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.
User avatar
LeonBlade
Chaos Rift Demigod
Chaos Rift Demigod
Posts: 1314
Joined: Thu Jan 22, 2009 12:22 am
Current Project: Trying to make my first engine in C++ using OGL
Favorite Gaming Platforms: PS3
Programming Language of Choice: C++
Location: Blossvale, NY

Re: Endian Switching and Structs on Wii?

Post 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 :)
There's no place like ~/
Post Reply