我有我想要在C中呈現的實體列表,並且我的呈現緩衝區使用堆插入每個幀以確保實體深度按順序呈現。但是,由於某種原因,堆總是結束未排序。我已經瀏覽了幾十次代碼,讓朋友們看看代碼,而我似乎無法找到爲什麼實體總是出現故障。我希望或許一雙新鮮的眼睛能夠幫助我以我的方式看到錯誤。這裏是我的註釋代碼:堆插入未排序
(請注意,X,Y,Z和深度(未用在這裏)我該如何解決堆插入都存儲爲int
S IN的實體結構
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");
}
。 ???謝謝!
我沒有仔細看看你的代碼,但[堆](https://en.wikipedia.org/wiki/Heap_data_structure)沒有完全排序。 – chtz
歡迎來到StackOverflow。 請參考參考stackoverflow.com/tour, 學習問好的問題stackoverflow.com/help/how-to-ask, 作出一個MCVE stackoverflow.com/help/mcve 如果您正在尋找調試代碼的幫助,請參閱https://ericlippert.com/2014/03/05/how-to-debug-small-programs/ – Yunnosch
考慮引用「二進制堆」而不是「堆」。也許提到https://en.wikipedia.org/wiki/Binary_heap讓回答者進入正確的環境。 – Yunnosch