# Game 1024: Advanced Game Programming Lesson 7: Super Magnificent Hyper Game Features Hardcore Extreme Black Turbo!

 Sana 11.03.2018 Hajmi 474 b.

• ## Write your name either…

• In binary ASCII format or …
• Spelled backwards

• ## How do you tell when a player is in the enemy’s view cone?

• The Dot Product of two normalized direction vectors placed tail to tail equals the cosine of the angle between the vectors
• ## How do you find the axis of rotation for something that needs to rotate at an odd angle, like a guided missile following a target?

• The Cross Product of two vectors placed tail to tail is a third vector that is perpendicular to the plane defined by both source vectors
• The direction of the cross product vector is the same as your thumb when you curl your hand inward from the first vector to the second vector… whether it is your right hand or left hand depends on whether you are using the right-handed or left-handed coordinate system (look it up if you want to know more about the right/left handedness of coordinate systems).

• ## Why?

• “operator new” (or malloc) is more expensive than assigning pointers to pre-allocated heap memory
• On a console, you want full control over how much RAM you use and you want to be warned when you pass the limit, because consoles have no virtual memory.
• Fragmented memory leads to slower access due to having to juggle the contents of the CPU cache to access things that would normally be next to each other in memory
• ## Basic Recipe (The web is full of various implementations of this):

• Pre-allocate a huge block of memory on the heap at the very beginning of the game
• Instead of allocating new chunks off the global heap when you need a new object, Assign a pointer to an address within your big giant block by…
• Implementing “operator new” as a member of the classes that you want allocated within this big giant block or…
• #define your own version of “new” and “delete” that recognizes your big giant block or…
• #define your own version of “NEW” and “DELETE” that recognizes your big giant block and gets used only when you specifically use it… this way, you don’t collide with other people’s custom memory allocator usages in your third party libraries
• C++ Tip: “Placement new” works like regular “new”, except it constructs your object at an address you specify

• ## What is it?

• Loading a single large file from the disc that contains the contents of multiple resources that were packed together into a single memory-mapped file.
• This giant memory-mapped file can be compressed (zipped). This makes loading faster because there is less data to load, and decompression of a zipped file is typically faster than the extra time it takes to load the uncompressed file.
• A memory mapped file is a file that starts with name and offset information followed by consecutive blocks of resource data packed together so that whatever loads the file can access the resource data it is looking for
• ## Why use it?

• Because it discourages users from mucking with the assets and posting screenshots of their “modded” content on the internet
• ## Basic Recipe:

• Make sure all resources in the game (graphics, sound, etc.) can be converted to/from generic blocks of memory (address + size)
• Creating the Memory-Mapped Resource File
• Create an external tool that can pack these files into a single file for you (i.e. wad files used in DOOM)… this external tool could either be your level editor or a specific tool made just for resource packing
• OR write code inside the game to have the game dump all mappable resources into a special resource file for later loading when you tell it to
• Load the block file at the start of the game before the other systems get set up
• Instead of reading individual files for a resource, have the game’s initilization code grab the resource data from this managed resource block to set up its sprites, sounds, textures, models, etc.

• ## What is it?

• Separating source data from instance data so that multiple objects can use the same loaded resource for separate instances.
• An instance is a unique manifestation of a resource in the game. In the example of a sprite…
• Source data on a sprite is the actual bitmap of the sprite. This can be shared between multiple sprites
• Instance data on a sprite is position, size, and rotation information about that sprite
• ## Why use it?

• Sharing data instead of duplicating data uses up less RAM
• ## Basic Recipe:

• Figure out what resources can be shared
• Centralize the storage of these shared resources in a central “database” that can be accessed by other systems
• Have the entities (Sprites, AudioBuffers, etc.) that use these shared resources store only a pointer or reference to the resource, but not own it

• ## What is it?

• Background loading while the game is still active (as seen in city-roaming games such as the recent Grand Theft Auto series)

• ## Basic Recipe:

• Separate your game’s dependency on having these dynamic resources in memory so that it can still run even if no dynamic resources were currently loaded… in other words, make sure that the game logic does not depend on these dynamic resources

• ## Basic Recipe:

• Define your game state. A game state is a set of properties that define the state of the game at any one time
• Make it so that the game state can be stored into a file
• Make it so that the game can read the game state from a file and set all relevant parts of the game to the proper state
• Maintain this functionality during development as new features come into the game
• Expose this functionality with a user interface that the user can use to save/load the game

• ## Milestone 3 (Your syllabus has changed, BTW)

• Optional Bonus: Write a resource manager that loads all bitmaps used in the program into a single memory mapped file. Make it so that when you call your program with a cook parameter, it basically creates one giant super-bitmap that holds all bitmaps used in the game. When the game runs and detects a “cooked” bitmap, it will load one large file instead of a bunch of tiny files. All sprites will share a spot on the bitmap so that no graphics data is duplicated.
• Optional Bonus: Write a custom memory allocator which you will use with your versions of NEW and DELETE to ensure that no memory is leaked and that the number of heap allocation occurences are minimized
• Optional Bonus: Integrate your custom memory allocator with STL or whatever template library you are using