-
Notifications
You must be signed in to change notification settings - Fork 51
States and Sprites
Zalo edited this page Aug 5, 2019
·
2 revisions
Before you start coding anything it is important that you understand what States and Sprites are for ZGB
-
States: you can think of States as the current main loop of your program (they are similar to Scenes in Unity3d). There can only be one of them running at a time and everytime you change from one to another all resources are discarded. Every declared State must contain 2 functions:
- Start: that handles the initialization of the State. Here is where you should load all the resources you are going to need and perform all variables initialization
- Uptate: called one per frame, for global interactions
-
Sprites: these are the objects that you see moving on screen (like GameObjects in Unity). 8x16, 16x16 ans 32x32 sprites are supported and no matter the size you cannot have more than 20 sprites at a time or the rom will crash. Every declared Sprite must contain 3 functions:
- Start: that handles the initialiation of the Sprite. It is called inmediatelly after the Sprite is created
- Update: called one per frame
- Destroy: called before deleting and Sprite
every Sprite has a struct associated with it that contains its current data. This struct can be accesed using the THIS variable inside any of its functions and contains the next variables:
- anim_data: a pointer to an UINT8 array containing a list of frames for the current animation. The first element must be the number of frames
- x, y: current world position of the Sprite
- flags: right now only used for sprite mirroring, setting it to OAM_VERTICAL_FLAG will mirror the Sprite and setting it to 0 will leave it as it is
- lim_x, lim_y: by default sprites are destroyed when going offscreen. Using these vars you can control how far can they go without being destroyed
- custom_data: 8 bytes for storing custom information
Sprites are managed by the SpriteManager which is responsible for the update of every one of them (you don't need to worry about that, it happens automatically). Some useful functions of the SpriteManager are: - SpriteManagerAdd: for adding new Sprites - SpriteManagerRemove: for removing them - SPRITEMANAGER_ITERATE: a useful macro for iterating through all active sprites