array of enum's? C++

Started by
24 comments, last by graveyard filla 20 years, 1 month ago
OK, that's mostly right. Actually, an eight-bit byte can store 256 possible values, which is 0-255. An int with a value of 128 will be 10000000 in binary (in fact, an int is usually two bytes, but we can ignore that for now). In the enum declaration I listed earlier, I set WALL1 to 128, and the following WALL items would continue from there, 129, 130, 131, etc. In binary, they would look like this:
WALL1 -> 128 -> 10000000
WALL2 -> 129 -> 10000001
WALL3 -> 130 -> 10000010
WALL4 -> 131 -> 10000011
and so on. In fact, that first bit will be unchanged until it gets to 256 (which would be 00000001 00000000).
This statement:
foo & 128
takes the value of foo and does a bitwise AND with the constant value 128.
The AND operation takes two inputs and produces one output. The result table is like this:
0 & 0 = 0
0 & 1 = 0
1 & 0 = 0
1 & 1 = 1
So, only when both inputs are true is the output true. A bitwise operation looks at each bit of the inputs one at a time, and produces the output one bit at a time. So, if foo has a value of, say, 131 (a WALL4), the operation looks like this:
  131   10000011& 128   10000000----------------result  10000000  

The result is what it is because only in the first bit is there a true value in both inputs. Because this result is a non-zero value, it is considered to be a true result for the purposes of an if statement. So effectively, by saying:
if( foo & 128 )
you are simply checking to see if the eighth bit is set in the value of foo. Any value between 128 and 255 will have that bit set, so unless you have more than 127 different types of WALL, that should do the job.

Does that make any sense?

[edited by - Plasmadog on March 24, 2004 10:05:59 PM]
You are not the one beautiful and unique snowflake who, unlike the rest of us, doesn't have to go through the tedious and difficult process of science in order to establish the truth. You're as foolable as anyone else. And since you have taken no precautions to avoid fooling yourself, the self-evident fact that countless millions of humans before you have also fooled themselves leads me to the parsimonious belief that you have too.--Daniel Rutter
Advertisement
quote:Original post by flangazor
I was playing with Java and was curious what the Java equivalent of enums are. (Small topic not worth it''s own thread, I imagine)


There is no real native equivalent. You can roll your own support for enumerated types in a variety of ways, depending on how heavyweight you want to be about it. Some implementations can offer several advantages over the usual C++ way of doing it, but seem rather like serious overkill.

quote:Original post by AndyTX
I''m not sure about your specific situation, but it seems to me that if you''re going to be adding many more walls etc. in the future, you may want to redesign the "enum" idea now to something more dynamic (perhaps loaded at runtime from a resource file?). It really does depend on what you are going to do with this in the long run though



thats true. i do plan on re-using this code in my next games (this is a pretty decent tile based engine) . also, adding more walls is kind of a hassle, well, i have to change the code a bunch of different places, where as good-designed code should only have to be changed in one spot (at least IMO). so does anyone have any other suggestions? i dunno, man.

plasma- thanks for the lesson, im gunna keep re-reading your post untill it sinks in
FTA, my 2D futuristic action MMORPG
actually, i pretty much made it nice to work with. i just seperate Tile_Types{} from Walls[] completely. also i use a const value called MAX_WALLS. so really, if i want to add more walls, its not such a hassle. i just incease the value of MAX_WALLS and of course i have to hard-code the loading in of the images. but i would have to do that regardless. thanks for your guys help
FTA, my 2D futuristic action MMORPG
Here's the way I did my tile engine, if you're interested. You just tell your editor how many parts your tile should have, then have it automatically allocate enough "cells" to hold all of the tile's parts. Walls would be no different than floors. Each "cell" could have it's own properties (such as if drawn after objects). Anyway, this is how I have mine set up:
struct KTileCmp // "Cell"{	LONG		Img;		// Image Index	LONG		x,y;		// Drawing Offset from tile origin	ULONG		Flags;		// Flags *like this comment?*};struct KTile // Actual Tile{	KTileCmp	*Cmp;		// Tile Components in Drawing Order	LONG		nCmp;		// Number of Components allocated	LONG		Ofy;		// Main Tile Offset	LONG		HMap;		// Height Map Index	LONG		Mat;		// Material of the floor only	ULONG		Flags;		// Bitflags for Tile};
Here's a snapshot of the editor. The image is kind of big.

[edited by - Jiia on March 26, 2004 5:13:30 AM]
Sorry to keep adding garbage to to this thread, but I saw that you're trying to learn bitflag operations! I wanted to point out that it's really easy to declare bitflags with hex values.

For example, somewhere in your code, you have a single integer. Call it "int Flags" or whatnot. Below your structure (if "int Flags" is in a structure) you can define the names of each bit in that variable using hex:
#define THIS_IS_BIT_01 0x00000001#define THIS_IS_BIT_02 0x00000002#define THIS_IS_BIT_03 0x00000004#define THIS_IS_BIT_04 0x00000008#define THIS_IS_BIT_05 0x00000010#define THIS_IS_BIT_06 0x00000020#define THIS_IS_BIT_07 0x00000040#define THIS_IS_BIT_08 0x00000080#define THIS_IS_BIT_09 0x00000100 
Do you see the pattern? 1,2,4,8, move left, 1,2,4,8, repeat. Those are declared for a 32-bit variable. Every two digits (in hex) is one-byte. For an 8-bit variable such "BYTE" or "char", you would remove the first 6 digits (0x00000001 would be 0x01). You would of course also use better names for each bit, such as "CAN_WALK", or "IS_A_WALL", not "THIS_IS_BIT_02"

After you define each bit name, you turn them on like this:
Flags |= BIT_NAME; // now this bit is 1Flags |= OTHER_BIT_NAME; // now both bits are 1 
Turn them off like this:
Flags &= ~BIT_NAME; // Turning only this bit off 
To find out if a specific bit is on, you do this:
if(Flags & BIT_NAME) 
That's it. I would play around with it, then try to learn the actual point of each operator, such as &, |, and ~.

[edited by - Jiia on March 26, 2004 5:45:50 AM]

This topic is closed to new replies.

Advertisement