Jump to content
  • Advertisement


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


Sorting in C

This topic is 5491 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
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

  • Advertisement

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!