Sunday, August 31, 2014
Monday, November 4, 2013
Friday, October 25, 2013
Monday, August 6, 2012
An “asset” is a relatively large chunk of data used by a game. Examples include sound effects, textures, meshes, music and movies. While it’s important to have a runtime resource management system to avoid duplicate assets residing in memory, it’s also important to have a way to track runtime assets during development. Games tend to have a lot of assets.
One way to ease asset tracking is to reduce the number of assets by rolling some up together. In a 3D game, it’s often necessary to keep individual texture assets separate as they are reused across different meshes. In a 3D game, textures often describe surface materials but rarely do they describe object contours (that’s left to mesh geometry).
In a 2D game, however, textures usually define both surface material *and* contour, and as such there’s less likelihood that textures will be reused across different meshes. One example is sprite sheets. Some characters are defined by sprite sheets that span multiple textures. There is never going to be a case where these sprite-sheet textures have meaning outside the shape/material they represent. As a result, our runtime asset representing the visual characteristics of a character is a single asset: an “animation” asset. While we can “nest” animations, the exposed assets (file-wise) never drop below “animation asset” level (for example, the runtime files visually representing a guy holding a sword are a “guy” and “sword” animation asset). Animation data, mesh data and sprite-sheet textures are generated/rolled into one asset on export from our animation tool.
Similarly, larger backgrounds may span multiple textures at runtime, and thus there is no reason for the runtime to need these textures to be floating around separately. Thus, just like character assets, a background asset accumulates multiple textures on background export and all reside in a single asset file.
Reducing the asset count by rolling up “atomic” asset groups is good practice, both for runtime and for tools.
Tuesday, July 17, 2012
Design patterns have their place in C++. Don’t abuse them! Let’s look at one of the most common design patterns used in games- the “strategy” pattern. Games often define an “Entity” base class which specifies an interface that all game objects adhere to. Game objects derive from this Entity class and, via polymorphism, exhibit different behavior without the underlying engine having to know anything other than the base Entity interface.
It’s tempting to stuff as much common behavior in the base class as possible, and, when that becomes too bloated, create more classes in an inheritance scheme between the base and the final child (for example Entity<---WalkingEntity<---Human).
Don’t get in the habit of stuffing tons of behavior towards base classes! As the behavior is spread out amongst more classes, it’s very hard to track the program flow, and more difficult to anticipate the effects of changing something.
What’s an alternative way to get code reuse? You’ll often hear “use composition” floated. Instead of a deep inheritance chain, you get your reuse via attributes- in the case above, Human would derive from Entity, and then have, as an attribute, an instance of a Walking behavior class. Now, the flow can be more easily tracked at the Human level…there’s no hide and seek with virtual overrides. Components are nice because unintended side effects are low, and program flow can be traced at the Human level more easily.
That being said, in practice I use both. There is some functionality that is *so* widespread and *so* predictable and *so* self-contained that stuffing it in an intermediate class in an inheritance chain makes sense. This is more a walling-off intent- I know that 100% of the children that derive from that class will not override those methods.
Finally, I’ve found a place where neither inheritance nor composition does the trick- AI. I use state machines for AI, and I’ve found two annoyances. One, a lot of boiler-plate code is needed. Two, it’s difficult to get reuse out of state machines. To solve the first problem I rely on macros (yes, #defines, to a hilarious extent- whole state classes in a single macro, and hooking up the states to their owner’s callbacks). The second problem, reuse in state machines, is interesting. There doesn’t seem to be a good solution without destroying the flow. For reuse of actual behavior, I mostly fall back on functional programming. I go oldschool and create functions with no side effects…they operate solely on data passed to them by the AI state execution, and they live independently of everything else. This way, to decipher an AI state’s behavior I at most have to remind myself what those functions do.
Friday, July 6, 2012
Jonathan Blow (developed Braid) recently tweeted that building stuff yourself is the way to go, time and money permitting. It was nice to hear him chime in, as those with strong opinions usually don’t have a game to back it up (hint…don’t listen to me!!).
Here’s how having our own tools/engine has been useful so far:
- We “own” it
- We know how it works
- We can enhance it quickly (a single tool programmer working on exactly what is needed is better than a team of tool programmers trying to satisfy every possible use).
- We can fix bugs in it quickly
- Customization for the type of games we hope to make is a priority
- User interface fits our preferences
- Feels good- morale boost.
The drawback is time and money. One-time fixed cost in my humble opinion. Don’t take my word for it though. Up next, tool shiznits…
Sunday, November 6, 2011
C++ requires one to manage heap memory. Memory management can be tedious and error-prone. If different things reference this memory, and you de-allocate the memory without someone knowing, bad things can happen. If you forget to de-allocate the memory, you have a memory leak.
One solution is to use smart pointers. They let a programmer use similar syntax as normal pointers, with the benefit of memory being automatically cleaned up when nothing references that memory.
What you’ve gained by using smart pointers:
- Anxiety reduction as you believe you are insulated from having to think about memory management
- Potentially less typing
What you’ve lost by using smart pointers:
- Stuff is now happening behind the scenes, but the syntax looks as though nothing is happening behind the scenes. Operators are overloaded. Evil is in the air!
- The timing of memory de-allocations is implicit. Hidden both in time and in the code.
Normally I’m fine with memory management being done behind the scenes. Our tools are written in C#, a language with a memory manager built in, and I could care less about memory. Tools can be memory hogs to a certain (certainly huge) extent.
However, our runtime game needs to care. I want the easiest, most transparent way to manage memory.
To ease memory management in C++ I do the following:
- Memory allocations using “new” are only allowed when a chunk (or level) of the game is loaded. Not frame to frame. Likewise, de-allocations are only allowed when a chunk (or level) of the game is unloaded.
- I use a naming convention for class attributes. An “m” prefix means member, an “mp” prefix means a member that is a pointer, and that the class is responsible for allocating and de-allocating its memory. Finally, I use “mpe” for member pointers for whose memory the class is responsible for neither allocating nor de-allocating. The “e” means external.
- I use simple macros to ease the typing related to allocation/de-allocation.
So, I centralize (in time) allocations and de-allocations, choose names that immediately identify a class’s responsibility, and wrap some of the clunkiness (such as checking for null before a de-allocation, setting to null after a de-allocation) in macros.
Some people use memory pools to achieve centralizing allocations and preventing a ton of frame-to-frame allocations. If I weren’t the only programmer on this project I would research this. As it stands my solution is easy to implement and transparent, and I can’t waste time researching “best” solutions to a problem that’s under control.