4 bytes can pack a whole lot.
Moderator: Coders of Rage
4 bytes can pack a whole lot.
So after I saw Falco talk about having his tile just be a char that indicates what the texture was, I started to think how much data can you actually pack into a small package.
At first i used a short (thats 2 Bytes), but felt I didn't give me everything I needed, so I used an int. (4 Bytes.)
Okay, so lets see what we would like out tile to have. (I'm not worried about location because you should be able to deduce that depending on your tile size and position in your array)
- texture info
:I'm gonna take a page from Falco and give it 1 byte, that give me 256 possible textures.
- collision info
:there are the two simple cases of solid or no collision, then there are 6 other possible one, each edge of the tile, and two diagonal.
so 3 bits are enough to represent this.
- elevation info - (like stairs and ramps)
if a tile is a stair or a incline, it will go up in one of 3 direction, left,right or up, (down would be hard to make a tile that looked good, or even to spot stairs, so im going to omit that on)
so 2 bits for this 00 no ele, 01 left, 10 right, 11 up.
- height info - (how high above the bottom a tile is)
for this i would give it 5 bits, thats 32 levels, but they way i would make it is that each level is 64 pixels, (cause your character is probably 32*64), but each height level is only 32, this just makes the transition on stairs not so abrupt. so we only end up with 16 real levels, which is still plenty.
- lighting data
5 bits will give you 32 levels of lighting.
- indoors or outdoors - (if you are indoors you don;t wanna draw any layers above, like the roof or floors above you..)
only 1 bit needed for this.
so out total comes out to 24 bits for that, leaving us with 8 bits or a byte that can still be used for something else. lighting color, or teleport info.
You would have to know a bit about how to work with bit wise operators but its not to hard. (for most systems this really is not necessary, but i still think a pretty good thing to learn about)
At first i used a short (thats 2 Bytes), but felt I didn't give me everything I needed, so I used an int. (4 Bytes.)
Okay, so lets see what we would like out tile to have. (I'm not worried about location because you should be able to deduce that depending on your tile size and position in your array)
- texture info
:I'm gonna take a page from Falco and give it 1 byte, that give me 256 possible textures.
- collision info
:there are the two simple cases of solid or no collision, then there are 6 other possible one, each edge of the tile, and two diagonal.
so 3 bits are enough to represent this.
- elevation info - (like stairs and ramps)
if a tile is a stair or a incline, it will go up in one of 3 direction, left,right or up, (down would be hard to make a tile that looked good, or even to spot stairs, so im going to omit that on)
so 2 bits for this 00 no ele, 01 left, 10 right, 11 up.
- height info - (how high above the bottom a tile is)
for this i would give it 5 bits, thats 32 levels, but they way i would make it is that each level is 64 pixels, (cause your character is probably 32*64), but each height level is only 32, this just makes the transition on stairs not so abrupt. so we only end up with 16 real levels, which is still plenty.
- lighting data
5 bits will give you 32 levels of lighting.
- indoors or outdoors - (if you are indoors you don;t wanna draw any layers above, like the roof or floors above you..)
only 1 bit needed for this.
so out total comes out to 24 bits for that, leaving us with 8 bits or a byte that can still be used for something else. lighting color, or teleport info.
You would have to know a bit about how to work with bit wise operators but its not to hard. (for most systems this really is not necessary, but i still think a pretty good thing to learn about)
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: 4 bytes can pack a whole lot.
For anyone who doesn't know how to use bit fields:
This seems like an awesome way to store tile information.
Code: Select all
struct Element
{
unsigned texture:8;
unsigned collision:3;
unsigned elevation:2;
unsigned height:5;
unsigned lighting:5;
unsigned indoors:1;
};
Re: 4 bytes can pack a whole lot.
pretty efficient too I would say :D
Re: 4 bytes can pack a whole lot.
Wait, wait. Why have I never seen this before? That fits perfectly for network stuff, too.RyanPridgeon wrote:For anyone who doesn't know how to use bit fields:
This seems like an awesome way to store tile information.Code: Select all
struct Element { unsigned texture:8; unsigned collision:3; unsigned elevation:2; unsigned height:5; unsigned lighting:5; unsigned indoors:1; };
Re: 4 bytes can pack a whole lot.
yeah those are bitfields,
i dont like them for 2 reasons,
1. you cant have pointers to them, and 2. (i think) they are not portable in the sense that you can load them from file, C can not garuntee that they are ordered in any particular way.
i just use int data; as the declaration, i know that that gives me 4 bytes.
#define SET_HEIGHT(j,k) k > 31 ? 0 :j |= k<<13
#define GET_HEIGHT(j) (j & 0x03E000)>>13
that would be to set and get the height field in the data var.
but they are WAY more convenient, and i suggest using that if you are not familiar with working with bits of blocks of memory.
i dont like them for 2 reasons,
1. you cant have pointers to them, and 2. (i think) they are not portable in the sense that you can load them from file, C can not garuntee that they are ordered in any particular way.
i just use int data; as the declaration, i know that that gives me 4 bytes.
#define SET_HEIGHT(j,k) k > 31 ? 0 :j |= k<<13
#define GET_HEIGHT(j) (j & 0x03E000)>>13
that would be to set and get the height field in the data var.
but they are WAY more convenient, and i suggest using that if you are not familiar with working with bits of blocks of memory.
Some person, "I have a black belt in karate"
Dad, "Yea well I have a fan belt in street fighting"
Dad, "Yea well I have a fan belt in street fighting"
- lotios611
- Chaos Rift Regular
- Posts: 160
- Joined: Sun Jun 14, 2009 12:05 pm
- Current Project: Game engine for the PC, PSP, and maybe more.
- Favorite Gaming Platforms: Gameboy Micro
- Programming Language of Choice: C++
Re: 4 bytes can pack a whole lot.
I've never seen that before either. I wonder why they don't mention it anywhere in all of the tutorials I've read.K-Bal wrote:Wait, wait. Why have I never seen this before? That fits perfectly for network stuff, too.RyanPridgeon wrote:For anyone who doesn't know how to use bit fields:
This seems like an awesome way to store tile information.Code: Select all
struct Element { unsigned texture:8; unsigned collision:3; unsigned elevation:2; unsigned height:5; unsigned lighting:5; unsigned indoors:1; };
"Why geeks like computers: unzip, strip, touch, finger, grep, mount, fsck, more, yes, fsck, fsck, fsck, umount, sleep." - Unknown
- hurstshifter
- ES Beta Backer
- Posts: 713
- Joined: Mon Jun 08, 2009 8:33 pm
- Favorite Gaming Platforms: SNES
- Programming Language of Choice: C/++
- Location: Boston, MA
- Contact:
Re: 4 bytes can pack a whole lot.
I learned about bitfields in a previous software development class but never really thought about a practical use for them until now. Cool.
"Time is an illusion. Lunchtime, doubly so."
http://www.thenerdnight.com
http://www.thenerdnight.com
-
- Respected Programmer
- Posts: 387
- Joined: Fri Dec 19, 2008 3:33 pm
- Location: Dallas
- Contact:
Re: 4 bytes can pack a whole lot.
There's a TON of application there where it comes to encryption, run time encoding, file compression, texture compression, network compression, your mom compression.
The important thing is to not view data types so much as concrete types that have ONE application, but rather just as a bitfield of anything that you can possibly represent. This has TREMENDOUS implications in graphics.
As an example, there is a technique abound that requires 4 offscreen full 32 bit resolution floating point render targets. That's 4x4 bytes (16 per pixel). Massive. Needless to say rendering to these consumes massive amounts of bandwidth. Two such targets store information about a lookup value to a material, surface normal, and surface depth across 2 textures. With these types of techniques, the entirety of those parameters can be encoded into a single integer texture and decompressed after lookup.
Alot of file formats also use variable bit length streams in which only the maximum number of bits require to represent an object are actually use and are tightly packed.
Lots and lots and lots of application here.
The important thing is to not view data types so much as concrete types that have ONE application, but rather just as a bitfield of anything that you can possibly represent. This has TREMENDOUS implications in graphics.
As an example, there is a technique abound that requires 4 offscreen full 32 bit resolution floating point render targets. That's 4x4 bytes (16 per pixel). Massive. Needless to say rendering to these consumes massive amounts of bandwidth. Two such targets store information about a lookup value to a material, surface normal, and surface depth across 2 textures. With these types of techniques, the entirety of those parameters can be encoded into a single integer texture and decompressed after lookup.
Alot of file formats also use variable bit length streams in which only the maximum number of bits require to represent an object are actually use and are tightly packed.
Lots and lots and lots of application here.
- RyanPridgeon
- Chaos Rift Maniac
- Posts: 447
- Joined: Sun Sep 21, 2008 1:34 pm
- Current Project: "Triangle"
- Favorite Gaming Platforms: PC
- Programming Language of Choice: C/C++
- Location: UK
- Contact:
Re: 4 bytes can pack a whole lot.
That's weird, I remember the way I learnt about them was doing an exercise where I loaded data into them from a file. I just remember doing something whereby I padded each line up to a byte with empty space, for exampleavansc wrote: 1. you cant have pointers to them, and 2. (i think) they are not portable in the sense that you can load them from file, C can not garuntee that they are ordered in any particular way.
.
Code: Select all
struct Something {
unsigned a:4, b:3, :1;
unsigned c:2, d:3, e:3;
unsigned f:2, g:3, :3;
};
- Milch
- Chaos Rift Junior
- Posts: 241
- Joined: Sat Jul 11, 2009 5:55 am
- Programming Language of Choice: C++
- Location: Austria, Vienna
Re: 4 bytes can pack a whole lot.
Pretty cool!
Learned about that in school a half year ago, but never thought about using it this way
Learned about that in school a half year ago, but never thought about using it this way
Follow me on twitter!