sorting transparent sprites

I know to draw sprites correctly you have to sort them from back to front and use alpha blending.

I wrote some algorithms to sort my sprites but I am having a little trouble wrapping my head around this part. Let’s say you don’t have your textures atlased but you have multiple free textures. Sorting by reverse z index then sorting by texture id I seem to get an odd result. First here is the sorting algorithm.


static int greater(cg_sprite*a, cg_sprite*b) {
    if(a->z_index < b->z_index) {
        return 0;
    }
    else if (a->z_index > b->z_index) {
        return 1;
    }
    if(a->texture_id > b->texture_id) {
        return 0;
    }
    else if(a->texture_id < b->texture_id) {
        return 1;
    }
    return -1;
}


void cg_sprite_back_2_front(cg_sprite** a, int count) {
    cg_sprite* l, *r;

    for (int i = 0; i < count; i++) {
        int swap = 0;
        for (int j = 0; j < (count - i - 1); j++) {
            if(greater(a[j], a[j+1])) {
                cg_sprite_swap_func(&a[j], &a[j + 1]);
                swap++;
            }
        }
        if (swap == 0) {
            break;
        }
    }
}

it seems to only sort along the z index. I currently have about 200 test sprites rendering with 4 different textures just to test the sorting algorithm. I was hoping that I could achieve z index sorting then texture sorting but my output looks like this, where z is z index and id is the texture id


z:-100 id:4
z:-99 id:4
z:-98 id:4
z:-97 id:4
z:-96 id:4
z:-95 id:4
z:-94 id:4
z:-93 id:4
z:-92 id:4
z:-91 id:4
z:-90 id:1
z:-89 id:1
z:-88 id:1
z:-87 id:1
z:-86 id:1
z:-85 id:1
z:-84 id:1
z:-83 id:1
z:-82 id:1
z:-81 id:1
z:-80 id:1
z:-79 id:1
z:-78 id:1
z:-77 id:2
z:-76 id:2
z:-75 id:2
z:-74 id:2
z:-73 id:2
z:-72 id:2
z:-71 id:2
z:-70 id:2
z:-69 id:2
z:-68 id:2
z:-67 id:2
z:-66 id:2
z:-65 id:2
z:-64 id:3
z:-63 id:3
z:-62 id:3
z:-61 id:3
z:-60 id:3
z:-59 id:3
z:-58 id:3
z:-57 id:3
z:-56 id:3
z:-55 id:3
z:-54 id:3
z:-53 id:3
z:-52 id:3
z:-51 id:4
z:-50 id:4
z:-49 id:4
z:-48 id:4
z:-47 id:4
z:-46 id:4
z:-45 id:4
z:-44 id:4
z:-43 id:4
z:-42 id:4
z:-41 id:4
z:-40 id:4
z:-39 id:4
z:-38 id:1
z:-37 id:1
z:-36 id:1
z:-35 id:1
z:-34 id:1
z:-33 id:1
z:-32 id:1
z:-31 id:1
z:-30 id:1
z:-29 id:1
z:-28 id:1
z:-27 id:1
z:-26 id:1
z:-25 id:2
z:-24 id:2
z:-23 id:2
z:-22 id:2
z:-21 id:2
z:-20 id:2
z:-19 id:2
z:-18 id:2
z:-17 id:2
z:-16 id:2
z:-15 id:2
z:-14 id:2
z:-13 id:2
z:-12 id:3
z:-11 id:3
z:-10 id:3
z:-9 id:3
z:-8 id:3
z:-7 id:3
z:-6 id:3
z:-5 id:3
z:-4 id:3
z:-3 id:3
z:-2 id:3
z:-1 id:3
z:0 id:3
z:1 id:4

I set the last sprite having a higher z index so it can be on top for testing purposes. About the sort order, I was hoping that I could get an output where the z index would be sorted from smallest to largest while the texture id’s would all be grouped but even the output below is only sorted by the texture id and not both texture id and z index.


z:1 id:4
z:-98 id:4
z:-97 id:4
z:-96 id:4
z:-95 id:4
z:-94 id:4
z:-93 id:4
z:-92 id:4
z:-91 id:4
z:-51 id:4
z:-50 id:4
z:-49 id:4
z:-48 id:4
z:-47 id:4
z:-46 id:4
z:-45 id:4
z:-44 id:4
z:-43 id:4
z:-42 id:4
z:-41 id:4
z:-40 id:4
z:-39 id:4
z:-64 id:3
z:-63 id:3
z:-62 id:3
z:-61 id:3
z:-60 id:3
z:-59 id:3
z:-58 id:3
z:-57 id:3
z:-56 id:3
z:-55 id:3
z:-54 id:3
z:-53 id:3
z:-52 id:3
z:-12 id:3
z:-11 id:3
z:-10 id:3
z:-9 id:3
z:-8 id:3
z:-7 id:3
z:-6 id:3
z:-5 id:3
z:-4 id:3
z:-3 id:3
z:-2 id:3
z:-1 id:3
z:0 id:3
z:-25 id:2
z:-24 id:2
z:-23 id:2
z:-22 id:2
z:-21 id:2
z:-20 id:2
z:-19 id:2
z:-18 id:2
z:-17 id:2
z:-16 id:2
z:-15 id:2
z:-14 id:2
z:-13 id:2
z:-77 id:2
z:-76 id:2
z:-75 id:2
z:-74 id:2
z:-73 id:2
z:-72 id:2
z:-71 id:2
z:-70 id:2
z:-69 id:2
z:-68 id:2
z:-67 id:2
z:-66 id:2
z:-65 id:2
z:-90 id:1
z:-89 id:1
z:-88 id:1
z:-87 id:1
z:-86 id:1
z:-85 id:1
z:-84 id:1
z:-83 id:1
z:-82 id:1
z:-81 id:1
z:-80 id:1
z:-79 id:1
z:-78 id:1
z:-38 id:1
z:-37 id:1
z:-36 id:1
z:-35 id:1
z:-34 id:1
z:-33 id:1
z:-32 id:1
z:-31 id:1
z:-30 id:1
z:-29 id:1
z:-28 id:1
z:-27 id:1
z:-26 id:1

the texture id grouping isn’t as important as getting the z order correct. Do I have to use multiple arrays to do this type of sorting?

All of your sprites have different Z indices. In the first case, the texture id is only used if both Z indices are equal, which never happens.

What kind of result were you expecting?