Skip to content

Commit

Permalink
Generators: Minor refactorings and updated apidoc
Browse files Browse the repository at this point in the history
  • Loading branch information
danij-deng committed Mar 9, 2012
1 parent 4577ad0 commit a396126
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 36 deletions.
39 changes: 26 additions & 13 deletions doomsday/engine/portable/include/generators.h
Expand Up @@ -42,9 +42,9 @@ typedef struct generators_s Generators;
/**
* Constructs a new generators collection. Must be deleted with Generators_Delete().
*
* @param sectorCount Number of sectors the collection must support.
* @param listCount Number of lists the collection must support.
*/
Generators* Generators_New(uint sectorCount);
Generators* Generators_New(uint listCount);

/**
* Destructs the generators collection @a generators.
Expand All @@ -53,11 +53,11 @@ Generators* Generators_New(uint sectorCount);
void Generators_Delete(Generators* generators);

/**
* Clear all references to any ptcgen_t instances currently owned by this
* collection.
* Clear all ptcgen_t references in this collection.
*
* @warning Does nothing about any memory allocated for said instances.
* It is therefore the caller's responsibilty to
*
* @param generators Generators instance.
*/
void Generators_Clear(Generators* generators);

Expand Down Expand Up @@ -107,36 +107,49 @@ ptcgen_t* Generators_Unlink(Generators* generators, ptcgen_t* generator);
ptcgen_t* Generators_Link(Generators* generators, ptcgenid_t slot, ptcgen_t* generator);

/**
* Clear all sector --> generator links.
* Empty all generator link lists.
*
* @param generators Generators instance.
*/
void Generators_ClearSectorLinks(Generators* generators);
void Generators_EmptyLists(Generators* generators);

/**
* Link the a sector with a generator.
*
* @param generators Generators instance.
* @param generator Generator to link with the identified sector.
* @param sectorIndex Index of the sector to link the generator with.
* @param generator Generator to link with the identified list.
* @param listIndex Index of the list to link the generator on.
*
* @return Same as @a generator for caller convenience.
*/
ptcgen_t* Generators_LinkToSector(Generators* generators, ptcgen_t* generator, uint sectorIndex);
ptcgen_t* Generators_LinkToList(Generators* generators, ptcgen_t* generator, uint listIndex);

/**
* Walk the entire list of generators.
* Iterate over all generators in the collection making a callback for each.
* Iteration ends when all generators have been processed or a callback returns
* non-zero.
*
* @param generators Generators instance.
* @param callback Callback to make for each iteration.
* @param parameters User data to be passed to the callback.
*
* @return @c 0 iff iteration completed wholly.
*/
int Generators_Iterate(Generators* generators, int (*callback) (ptcgen_t*, void*), void* parameters);

/**
* Walk the list of sector-linked generators.
* Iterate over all generators in the collection which are present on the identified
* list making a callback for each. Iteration ends when all targeted generators have
* been processed or a callback returns non-zero.
*
* @param generators Generators instance.
* @param listIndex Index of the list to traverse.
* @param callback Callback to make for each iteration.
* @param parameters User data to be passed to the callback.
*
* @return @c 0 iff iteration completed wholly.
*/
int Generators_IterateSectorLinked(Generators* generators, uint sectorIndex,
int Generators_IterateList(Generators* generators, uint listIndex,
int (*callback) (ptcgen_t*, void*), void* parameters);

#endif /// LIBDENG_MAP_GENERATORS
26 changes: 13 additions & 13 deletions doomsday/engine/portable/src/generators.c
Expand Up @@ -43,17 +43,17 @@ struct generators_s {
uint listsSize;
};

Generators* Generators_New(uint sectorCount)
Generators* Generators_New(uint listCount)
{
Generators* gens = Z_Malloc(sizeof(*gens), PU_MAP, 0);
if(!gens) Con_Error("Generators_New: Failed on allocation of %lu bytes for new Generators instance.", (unsigned long) sizeof(*gens));

memset(gens->activeGens, 0, sizeof(gens->activeGens));

gens->listsSize = sectorCount;
gens->lists = Z_Calloc(sizeof(listnode_t*) * sectorCount, PU_MAP, 0);
gens->listsSize = listCount;
gens->lists = Z_Calloc(sizeof(listnode_t*) * listCount, PU_MAP, 0);

// We can link 64 generators each into four sectors each before running out of links.
// We can link 64 generators each into four lists each before running out of links.
gens->linkStoreSize = 4 * GENERATORS_MAX;
gens->linkStore = Z_Malloc(sizeof(listnode_t) * gens->linkStoreSize, PU_MAP, 0);
gens->linkStoreCursor = 0;
Expand All @@ -72,7 +72,7 @@ void Generators_Delete(Generators* gens)
void Generators_Clear(Generators* gens)
{
assert(gens);
Generators_ClearSectorLinks(gens);
Generators_EmptyLists(gens);
memset(gens->activeGens, 0, sizeof(gens->activeGens));
}

Expand Down Expand Up @@ -152,18 +152,18 @@ ptcgen_t* Generators_Link(Generators* gens, ptcgenid_t slot, ptcgen_t* gen)
return gen;
}

ptcgen_t* Generators_LinkToSector(Generators* gens, ptcgen_t* gen, uint sectorIndex)
ptcgen_t* Generators_LinkToList(Generators* gens, ptcgen_t* gen, uint listIndex)
{
listnode_t* link, *it;
assert(gens);
// Sanity check - generator is one from this collection.
assert(Generators_GeneratorId(gens, gen) >= 0);

// Must check that it isn't already there...
for(it = gens->lists[sectorIndex]; it; it = it->next)
for(it = gens->lists[listIndex]; it; it = it->next)
{
if(it->gen == gen) return gen; // No, no...
/*Con_Error("Generators_LinkToSector: Attempted repeat link of generator %p to sector %u.", (void*)gen, sectorIndex);
/*Con_Error("Generators_LinkToList: Attempted repeat link of generator %p to list %u.", (void*)gen, listIndex);
exit(1); // Unreachable.*/
}

Expand All @@ -172,13 +172,13 @@ ptcgen_t* Generators_LinkToSector(Generators* gens, ptcgen_t* gen, uint sectorIn
if(link)
{
link->gen = gen;
link->next = gens->lists[sectorIndex];
gens->lists[sectorIndex] = link;
link->next = gens->lists[listIndex];
gens->lists[listIndex] = link;
}
return gen;
}

void Generators_ClearSectorLinks(Generators* gens)
void Generators_EmptyLists(Generators* gens)
{
assert(gens);
if(!gens->lists) return;
Expand All @@ -204,12 +204,12 @@ int Generators_Iterate(Generators* gens, int (*callback) (ptcgen_t*, void*), voi
return false; // Continue iteration.
}

int Generators_IterateSectorLinked(Generators* gens, uint sectorIndex,
int Generators_IterateList(Generators* gens, uint listIndex,
int (*callback) (ptcgen_t*, void*), void* parameters)
{
listnode_t* it;
assert(gens);
for(it = it = gens->lists[sectorIndex]; it; it = it->next)
for(it = it = gens->lists[listIndex]; it; it = it->next)
{
int result = callback(it->gen, parameters);
if(result) return result;
Expand Down
18 changes: 9 additions & 9 deletions doomsday/engine/portable/src/p_particle.c
Expand Up @@ -200,7 +200,7 @@ static int linkGeneratorParticles(ptcgen_t* gen, void* parameters)
if(gen->ptcs[i].stage < 0) continue;

/// @fixme Do not assume sector is from the CURRENT map.
Generators_LinkToSector(gens, gen, GameMap_SectorIndex(theMap, gen->ptcs[i].sector));
Generators_LinkToList(gens, gen, GameMap_SectorIndex(theMap, gen->ptcs[i].sector));
}
return false; // Continue iteration.
}
Expand All @@ -223,7 +223,7 @@ void P_CreatePtcGenLinks(void)

BEGIN_PROF(PROF_PTCGEN_LINK);

Generators_ClearSectorLinks(gens);
Generators_EmptyLists(gens);

if(useParticles)
{
Expand Down Expand Up @@ -1401,13 +1401,6 @@ static int findDefForGenerator(ptcgen_t* gen, void* parameters)
return i+1;
}

// A state generator?
if(gen->source && def->state[0] &&
gen->source->state - states == Def_GetStateNum(def->state))
{
return i+1;
}

// A flat generator?
if(gen->plane && def->material)
{
Expand Down Expand Up @@ -1450,6 +1443,13 @@ static int findDefForGenerator(ptcgen_t* gen, void* parameters)
}
}
}

// A state generator?
if(gen->source && def->state[0] &&
gen->source->state - states == Def_GetStateNum(def->state))
{
return i+1;
}
}

return 0; // Not found.
Expand Down
2 changes: 1 addition & 1 deletion doomsday/engine/portable/src/rend_particle.c
Expand Up @@ -209,7 +209,7 @@ void Rend_ParticleMarkInSectorVisible(sector_t* sector)
gens = GameMap_Generators(theMap);
if(!gens) return;

Generators_IterateSectorLinked(gens, GameMap_SectorIndex(theMap, sector), markPtcGenVisible, NULL/*no parameters*/);
Generators_IterateList(gens, GameMap_SectorIndex(theMap, sector), markPtcGenVisible, NULL/*no parameters*/);
}

/**
Expand Down

0 comments on commit a396126

Please sign in to comment.