VgaGames 3 - VgaGames3-object man-pages

[.. upper level ..]

VG3_ofunc_get_objvar()

[added in version 1.4]

Return structure with object-variables for a VgaGames3-object.
SYNTAX
void * VG3_ofunc_get_objvar(struct vg3_ofunc *ofstruct, const char *oid)

FUNCTION PARAMETERS
ofstruct Common VgaGames3-object structure
oid Object-ID

RETURN VALUE
Returns the structure with object-variables, or NULL if not found.

DESCRIPTION
Return structure with object-variables for a VgaGames3-object with object-ID = oid. This structure had to be filled out with a getovar_*() function in this object, refer to ofunc_new(). Object-Variables are variables for an VgaGames3-object rather than for object-instances. All instances share the same object-variables.

EXAMPLE

A very simple VgaGames3-object with object-variables:
  

myobj.h

#ifndef MYOBJ_H_ #define MYOBJ_H_ struct myobj { /* private structure for object */ int xm, ym; /* position on window (centered) */ struct vg3_image *img; /* image */ }; struct myobj_vars { /* structure for object-variables */ unsigned int seed; /* initial seed-number for all instances */ struct vg3_hash *hs; /* any hashmap */ }; #endif

myobj.c

/* myobj object */ #include "game.h" /* defines struct game and OID_MYOBJ */ #include "myobj.h" static void free_myvars(void *); static struct vg3_ofunc_object * f_new(void *, unsigned int, ...); static void f_free(void *, struct vg3_ofunc_object *); static void f_run(void *, struct vg3_ofunc_object *); /* fill out the passed structure for object-functions of this VgaGames3-object */ void getofc_myobj(struct vg3_ofunc_objfunc *ofc) { if (ofc == NULL) { return; } snprintf(ofc->oid, sizeof(ofc->oid), "%s", OID_MYOBJ); ofc->f_new = f_new; ofc->f_free = f_free; ofc->f_run = f_run; } /* fill out the passed structure for object-variables of this VgaGames3-object */ void getovar_myobj(struct vg3_ofunc_objvar *ovar) { static struct myobj_vars myvars; /* must be static */ if (ovar == NULL) { return; } /* initialize myvars, * but not the initial seed-value, * it may not be set unless network-connect has happened, so we do it in f_new() */ memset(&myvars, 0, sizeof(myvars)); myvars.hs = VG3_hash_new(); snprintf(ovar->oid, sizeof(ovar->oid), "%s", OID_MYOBJ); ovar->ovptr = &myvars; ovar->free = free_myvars; } /* free-function for the hashmap of the individual structure */ static void free_myvars(void *vmyvars) { struct myobj_vars *myvars = (struct myobj_vars *)vmyvars; VG3_hash_free(myvars->hs); } /* now define the functions */ static struct vg3_ofunc_object * f_new(void *vgame, unsigned int parent_objid, ...) { struct game *gstruct = (struct game *)vgame; struct myobj_vars *myvars; [...] /* set the initial seed-value, if still unset */ myvars = VG3_ofunc_get_objvar(gstruct->ofstruct, OID_MYOBJ); if (myvars != NULL && myvars->seed == 0) { const int seed_modifier = 171; /* arbitrary number, valid for this object */ myvars->seed = VG3_nw_random_initseed(seed_modifier); } [...] } /* move an object-instance */ static void f_run(void *vgame, struct vg3_ofunc_object *objp) { struct game *gstruct = (struct game *)vgame; struct myobj_vars *myvars; [...] /* get a new random value using the common seed-value of the object myobj */ myvars = VG3_ofunc_get_objvar(gstruct->ofstruct, OID_MYOBJ); if (myvars != NULL) { VG3_nw_random_initseed(1, 10, &myvars->seed); } [...] }

SEE ALSO
VG3_ofunc_objlist_newlist() VG3_ofunc_objlist_freelist()

SEE ALSO
VG3_ofunc_get_objfunc() VG3_ofunc_get_objobjfunc()