Re: bitwise operators

If you need more than 4 booleans in a class, struct, or whatever, it's easier to use an int, using the bitwise operators to interact with the bits. This is also helpful when you don't know in advance how many boolean flags you'll need.
There are some other uses you might come across in game-related libraries, related to efficiently storing data for collisions, or complex properties that can't be reduced to booleans. If I remember correctly, Box2d has two ints for specifying arbitrary custom types for objects. I forget the exact rules, but iirc it's something like only let the objects interact if x.mask1 & y.mask2 is nonzero. This let's you be as complex or simple as you want for a wide variety of projects.

Things I use bitwise operations for all the time:
- I had a class for input configurations (keyboard, joystick, whatever) that assumed something like your typical game controller, with 6-12 buttons. Using bits for this allowed me to use the same constants for controls and movelists.
- Movelists. Since many of my games do some fighting game-like movelists, I use ints to define what state the characters are in, and bits to map those to commands. You could just use something string-based, but somehow this has worked much better for me.
- Lists of properties that could change later. Continuing with the fighting example, a move might involve fire, might use a common set of battlecries, might need to track the sound or not, might be unstunnable, unblockable, cancelable, seeking, weightless, etc, etc. As the game develops, some new properties might become necessary or useful. A giant pile of booleans isn't just inefficient for the machine; it's a time-waster, confusing to navigate, takes up space in your code that affects readability, and if you do things like by-hand cloning or serialization, you'd have to change that every time you add or modify a property. On the other hand, you can just declare "int flags;", and all you need to do is add constants (if you don't just memorize the values, but who does that?).
- Unlockables or variable lists of playable characters. I'll use EC as an example. Each level has a certain list of characters who can be used in that level, and sometimes that list changes. An array or linked list might be viable, but I was getting the list only when needed, so just returning an int was simpler.
- Voxels. I don't have the patience to make a new map editor for every game that requires a different format, so whenever I can just make the map into an array of strings, or at least load from one, that's preferred. That does limit what I can do in terms of 3d, though. One solution for this is to map an int to each unique char on the map, and have that int represent the vertical stack. This lets me make maps with a notetaker whereever I happen to be at the time, without being restricted to 2 dimentions.
- Aiui, mainstream games have used bitmasks to speed up hit detection. Kinda irrelevant if you're using a super optimized physics engine on a super optimized machine, but that's a thing.
- Efficiency, if you have memory limits. You'd be surprised how much you can fit into 8 bits.
- Some libraries do things like represent colors as a single int. Swamp maps do this. If you work with these, you'd need to use bitwise operators to work with red/green/blue individually.

-- 
Audiogames-reflector mailing list
Audiogames-reflector@sabahattin-gucukoglu.com
https://sabahattin-gucukoglu.com/cgi-bin/mailman/listinfo/audiogames-reflector
  • ... AudioGames . net Forum — Developers room : goran via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : goran via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : magurp244 via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : kaigoku via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : CAE_Jones via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : goran via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : leibylucw via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : goran via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : goran via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : Ethin via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : kaigoku via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : kaigoku via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : goran via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : goran via Audiogames-reflector
    • ... AudioGames . net Forum — Developers room : goran via Audiogames-reflector

Reply via email to