sprite_sorty ldy sorted_indexz+1 ; first iteration only compares element 0 & 1 in the table, so no loop needed lda object_yl,y ; y position of object in position 1 ldx sorted_indexz ; get pointer to the object in position 0 cmp object_yl,x ; compare y position of object 0 to y position of object 1 (stored in AR) bcs @second ; if Obj 1 > Obj 0 we are done stx sorted_indexz+1 ; otherwise swap them sty sorted_indexz @second ldx #2 ; get first position to be compared @loop stx current_pos ; position in index table we are finding the right place for ldy sorted_indexz,x ; get pointer (offset) to current object to find the right place for sty current_index ; store current index number dynamically in code for later storage lda object_yl,y ; y position of current object into AR @in_right_place ldy sorted_indexz-1,x ; check if object is already in the right place ... saves time if list is already partially sorted cmp object_yl,y ; ... if it is we dont need to swap any positions but can move right on bcs @next_position sty sorted_indexz,x dex @compare ldy sorted_indexz-1,x ; get pointer to the object we are comparing current one with cmp object_yl,y ; compare y position of this object to y position of current object (stored in AR) bcs @found_place ; is y of current object (in AR) >= y of compare object sty sorted_indexz,x ; move the compared to index one right in the table dex ; loop to check next position bne @compare @found_place lda current_index ; index to current object sta sorted_indexz,x ; store to the right of the one it is >= of ldx current_pos @next_position inx cpx #NUM_SPR_OBJ ; end of table (number of sprite objects)? bcc @loop @end rts
@loop sty current_pos ; position in the index lda (sorted_indexz),y ; y coordinate of object ldx sorted_indexz,y ; object number/index stx current_index @in_right_place cmp (sorted_indexz-2),y ; compare to y coordinate of next object in index bcs @next_position ldx sorted_indexz-2,y ; move compared objects index to the right sorted index list stx sorted_indexz,y dey dey @compare cmp (sorted_indexz-2),y bcs @found_place ldx sorted_indexz-2,y stx sorted_indexz,y dey dey bne @compare @found_place ldx current_index stx sorted_indexz,y @next_position ldy current_pos iny iny cpy #2*NUM_SPR_OBJ bcc @loop