J'ai une liste d'entités que je veux rendre en C, et mon tampon de rendu utilise heap insère chaque image pour m'assurer que les profondeurs des entités sont rendues dans l'ordre. Cependant, pour une raison quelconque, le tas finit toujours non trié. J'ai regardé le code des douzaines de fois, ai eu des amis regardent le code, et je ne peux pas sembler trouver pourquoi les entités sont toujours dans le désordre. J'espère que peut-être une nouvelle paire d'yeux peut m'aider à voir l'erreur dans mes moyens. Voici mon code commenté:L'insertion de tas n'est pas triée
(S'il vous plaît noter que x, y, z, et la profondeur (non utilisée ici) sont tous stockés comme int
s dans la struct entité
void AEC_RenderCatalogToBuffer(AEC_EntityCatalog* entityCatalog,
AEC_SpriteBuffer* spriteBuffer)
{
//The values we'll be using
unsigned int heap_size = 0;
unsigned int heap_at = 1;
int frame = spriteBuffer->frame + 1;
int x, y, z;
int temp_depth;
//Loop through all the entities
for (int search = 0; search < AEC_ENTITY_COUNT; search++)
{
// If an entity is drawable and it has an x, y, z,
// insert it into the buffer for rendering
if ( entityCatalog
->entity_components[search].component_mask[AEC_DRAWABLE]
&& entityCatalog
->entity_components[search].component_mask[AEC_DISPLACEMENT])
{
//Prepare data for heap insertion
temp_depth = AEC_GetIsoDepth(entityCatalog, search);
x = entityCatalog->displacement[search].x;
y = entityCatalog->displacement[search].y;
z = entityCatalog->displacement[search].z;
//Increase the heap size by 1, save the size as the end node
heap_size++;
heap_at = heap_size;
spriteBuffer->is_filled[heap_at] = frame;
// If the parent node is greater than 0,
// has a larger or equal y (depth)
// and is being drawn in the current frame
while ( (heap_at - 1)/2 > 0
&& spriteBuffer->y[(heap_at - 1)/2] >= y
&& spriteBuffer->is_filled[(heap_at - 1)/2] == frame
)
{
spriteBuffer->entity[heap_at]
= spriteBuffer->entity[(heap_at - 1)/2];
spriteBuffer->depth[heap_at]
= spriteBuffer->depth[(heap_at - 1)/2];
spriteBuffer->x[heap_at] = spriteBuffer->x[(heap_at - 1)/2];
spriteBuffer->y[heap_at] = spriteBuffer->y[(heap_at - 1)/2];
spriteBuffer->z[heap_at] = spriteBuffer->z[(heap_at - 1)/2];
heap_at = (heap_at - 1)/2;
}
// Place the new entity's information into
// the correct place in the array
spriteBuffer->is_filled[heap_at] = frame;
spriteBuffer->x[heap_at] = x;
spriteBuffer->y[heap_at] = y;
spriteBuffer->z[heap_at]= z;
spriteBuffer->entity[heap_at] = search;
spriteBuffer->depth[heap_at] = temp_depth;
}
}
// Once all the entities have submitted their draw depth
// and have been sorted by y-index,
// save the heap size and the current frame
spriteBuffer->size = heap_size;
spriteBuffer->frame = frame;
printf("Checking: ");
for (int q=0;q<heap_size+1;q++)
{
if (spriteBuffer->is_filled[q] == frame)
{
printf("%d ", spriteBuffer->y[q]);
}
}
printf("\n");
}
Comment puis-je fixer l'insert en tas. ??? Merci!
Je n'ai pas examiné de plus près votre code, mais [Heaps] (https://en.wikipedia.org/wiki/Heap_data_structure) n'est pas complètement trié. – chtz
Bienvenue dans StackOverflow. S'il vous plaît prendre la tournée stackoverflow.com/tour, apprendre à poser de bonnes questions stackoverflow.com/help/how-to-ask, faire un MCVE stackoverflow.com/help/mcve Si vous cherchez de l'aide avec le code de débogage voir https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Yunnosch
Pensez à faire référence à "tas binaire" au lieu de simplement "tas". Peut-être mentionner https://en.wikipedia.org/wiki/Binary_heap pour mettre les répondeurs dans le bon contexte. – Yunnosch