Archived

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

riksweeney

Sorting in C

Recommended Posts

riksweeney    146
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
Fruny    1658
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
riksweeney    146
I''ve knocked up a quick test by creating an array of pointers to point to each location entry and have sorted that successfully.

Thanks for your help.

Share this post


Link to post
Share on other sites