FPG class samples
This section shows common samples of how to create and manage DIV Games Studio graphic package files.
DIV Games Studio FPG file is a simple file package format designed to store multiple MAP graphics sharing a common PAL color palette, is nearly an atlas like we used on modern game engines to group various sprites in a single file. In DIV Games Studio this feature ease the manage of animations and group of related sprites when need to manage in code.
Note
The FPG files required that each MAP graphic has an unique graph id value, from 1 to 999 (the FPG files are limited to 1000 MAP graphics).
Not is necessary that all graph ids are sequentially. You can create different series of ids or set jumps between ids (e.g.: 1, 2, 3, 16, 24, 32, 48, 64, 96, 100, 101, 102, 200, 300, 301, 302, 600...).
By default, DIV Games Studio not sorted the MAP graphics by his graph id value (that is useful to easy locates them in the built-in visor) but DIV2.Format.Exporter does sort them when you save the FPG file to disk.
Create a new FPG
This code shows how to create an empty FPG instance initialized with a specific PAL instance:
var palette = new PAL("DIV.PAL");
var fpg = new FPG(palette);
Load a FPG file
This code shows how to load an existing FPG file:
var fpg = new FPG("PLAYER.FPG");
Also you can load a FPG file from a byte array:
byte[] buffer = System.IO.File.ReadAllBytes("PLAYER.FPG");
var fpg = new FPG(buffer);
Read a MAP graphic
This code shows how to read an existing MAP from a FPG instance using his index position:
var map = fpg[16]; // Gets the MAP in the FPG at index 16.
And also, you can use a foreach loop to read all MAPs:
foreach (MAP map in fpg)
{
Console.WriteLine(map); // Prints the current MAP properties in console.
}
Add a new MAP graphic
This code shows how to add a new MAP to the FPG instance:
var map = new MAP("RUN101.MAP");
fpg.Add(map);
Also you can load a MAP file from a byte array and add them in the same way:
byte[] buffer = System.IO.File.ReadAllBytes("RUN101.MAP");
fpg.Add(buffer);
Or simply load by filename:
fpg.Add("RUN101.MAP");
The Add method, and his overloads, checks the graphId before add the MAP and thrown an exception if already exists a MAP with the same graphId.
Note
The Add method, and his overloads, always performs a color conversion if the palette is different from the FPG instance to ensure the MAP graphic shows properly with the current palette.
Remove a MAP graphic
This code shows how to remove a MAP from a FPG instance using different ways:
var map = new MAP("RUN101.MAP");
fpg.Remove(map); // Removes the MAP that match with this instance.
fpg.Remove(101); // Removes the MAP with graphId 101.
fpg.RemoveAt(16); // Removes the MAP at index 16.
Check if a FPG contains an specific MAP graphic
This code shows how to check if a FPG instance contains an specific MAP:
var map = new MAP("RUN101.MAP");
bool exists = fpg.Contains(map);
bool exists = fpg.Contains(101); // Checks if the FPG contains any MAP with graphId 101.
Replace an existing MAP graphic
This code shows how to replace MAP from a FPG instance:
// How to replace the MAP with graphId 101 on a FPG file:
var fpg = new FPG("PLAYER.FPG");
// Loads the new MAP to replace an existing one.
// For this sample, we assume that this MAP has the graphId 101:
var map = new MAP("RUN101.MAP");
// Removes the current MAP with graphId 101:
fpg.Remove(101);
// Adds the new MAP:
fpg.Add(map);
Removes all MAP graphics
You can removes all MAPs from a FPG instance using the following call:
fpg.Clear();
Save FPG to file
You can easily save your changes to a FPG file using the following call:
fpg.Save("PLAYER.FPG");