sgdk
object.h File Reference

Base object management unit. More...

#include "pool.h"

Go to the source code of this file.

Classes

struct  Object_
 Base object structure. More...

Defines

#define OBJ_ALLOCATED   0x8000
 Allocated state for object (mainly useful for debugging)

Typedefs

typedef struct Object_ Object
 Base object structure.
typedef void ObjectCallback (Object *obj)
 Object function callback.

Functions

PoolOBJ_createObjectPool (u16 size, u16 objectSize)
 Create and allocate the new object pool (this method is an alias of POOL_create(..))
ObjectOBJ_create (Pool *pool)
 Create a new objet from the given object pool (object must extend basic Object structure)
void OBJ_release (Pool *pool, Object *object, bool maintainCoherency)
 Release an objet from the given object pool (object must extend basic Object structure)
void OBJ_updateAll (Pool *pool)
 Iterate over all active objects from the given object pool and call update method for each of them.

void OBJ_setInitMethod (Object *object, ObjectCallback *initMethod)
 Set the initialization method for the given object.
void OBJ_setUpdateMethod (Object *object, ObjectCallback *updateMethod)
 Set the update method for the given object.
void OBJ_setEndMethod (Object *object, ObjectCallback *endMethod)
 Set the ending method for the given object.

Detailed Description

Base object management unit.

Author:
Stephane Dallongeville
Date:
02/2022

This unit provides methods to manage objects.
It works in concert with the pool.h unit which provide dynamic object allocation.

The idea of Object is that you can use it as base structure for your own object (entity, enemy, character, whatever you want..).
To do that the idea is to declare your new object structure by embedding the Object into it can be anonymous) and always at the first position:

 struct entity_
 {
     Object;
     f32 posX;
     f32 posY;
     Sprite* sprite;
     ...
 };

Doing that your Entity structure can be used through OBJ_xxx methods.


Typedef Documentation

typedef struct Object_ Object

Base object structure.

Parameters:
internalStateObject internal state, you can use it but you should save bit 15 as it's used internally to detect invalid object
typeObject type, can be used to recognize the underlying object / structure type.
initInitialisation function callback, should be only called once after object creation
updateUpdate function callback, usually called once per frame
endEnding function callback, should be only called once before object release
typedef void ObjectCallback(Object *obj)

Object function callback.

Parameters:
objObject

Function Documentation

Object* OBJ_create ( Pool pool)

Create a new objet from the given object pool (object must extend basic Object structure)

Parameters:
poolObject pool to allocate from (see pool.h unit)
Returns:
the created object or NULL if an error occured (no more available object in pool or invalid pool). The returned object is initialized to 0
See also:
OBJ_release(..)
Pool* OBJ_createObjectPool ( u16  size,
u16  objectSize 
)

Create and allocate the new object pool (this method is an alias of POOL_create(..))

Parameters:
sizethe capacity of the pool (in number of object)
objectSizethe size of a single object (usually you should use sizeof(Struct) here)
Returns:
the new created object pool or NULL if there is not enough memory available for that.
See also:
POOL_create(..)
void OBJ_release ( Pool pool,
Object object,
bool  maintainCoherency 
)

Release an objet from the given object pool (object must extend basic Object structure)

Parameters:
poolObject pool allocator to release from (see pool.h unit)
objectObject to release (must extend basic Object structure)
maintainCoherencyset it to TRUE if you want to keep coherency for stack iteration and use OBJ_updateAll().
Set it to FALSE for faster release process if you don't require object iteration through alloc stack.
See also:
OBJ_create(..)
OBJ_updateAll(..)
void OBJ_setEndMethod ( Object object,
ObjectCallback endMethod 
)

Set the ending method for the given object.

Parameters:
objectObject to set end method for
endMethodthe method to set for object destruction (should be called just before object is released)
See also:
OBJ_setInitMethod(..)
OBJ_setUpdateMethod(..)
void OBJ_setInitMethod ( Object object,
ObjectCallback initMethod 
)

Set the initialization method for the given object.

Parameters:
objectObject to set init method for
initMethodthe method to set for object initialization (should be called after object creation)
See also:
OBJ_setUpdateMethod(..)
OBJ_setEndMethod(..)
void OBJ_setUpdateMethod ( Object object,
ObjectCallback updateMethod 
)

Set the update method for the given object.

Parameters:
objectObject to set update method for
updateMethodthe method to set for object update (should be called once per frame)
See also:
OBJ_setInitMethod(..)
OBJ_setEndMethod(..)
void OBJ_updateAll ( Pool pool)

Iterate over all active objects from the given object pool and call update method for each of them.

Parameters:
poolObject pool to update all objects from.
Warning:
You need to always set 'maintainCoherency' to TRUE when using OBJ_release(..) otherwise stack iteration won't work correctly.
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Defines