This topic is now archived and is closed to further replies.

Sorting in C

This topic is 5275 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I''m attempting to sort all the objects in my game and basically have several different structures, one for my vehicles, one for static objects such as buildings and one for projectiles and explosions. I''m looking for a simple way of sorting these items for display on the screen. I was thinking of making an additional structure that dealt with the image and its coordinates, creating an array of it and make each vehicle structure point to one of these images. I can then pass a copy of this array into a function which would then sort it for onscreen generation is this any good? Super quick hacked up structures: typedef struct image { int imagenum,x,y; {...} } image; typedef struct vehcile { {...} image *image; } vehicle; Thanks

Share this post

Link to post
Share on other sites
A few tips

* C has a qsort function you can use.
* If you sort the array of images, the pointers held by the vehicles may become invalid.
* It is generally better to sort an array of pointers to structures than an array of structures.
* Consider whether relying on your graphics card''s Z-buffer is good enough.
* Consider sorting only to keep image numbers in order (to minimize texture switches during rendering) - which only needs to be done when the list changes, and lets you use algorithms for insertion/removal of items in a sorted list.

[ Start Here ! | How To Ask Smart Questions | Recommended C++ Books | C++ FAQ Lite | Function Ptrs | CppTips Archive ]
[ Header Files | File Format Docs | LNK2001 | C++ STL Doc | STLPort | Free C++ IDE | Boost C++ Lib | MSVC6 Lib Fixes ]

Share this post

Link to post
Share on other sites