Sorting in C

Started by
1 comment, last by riksweeney 20 years, 9 months ago
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
Advertisement
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 ]
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
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.

This topic is closed to new replies.

Advertisement