Sprite Lists

Started by
10 comments, last by GameDev.net 19 years, 7 months ago
And for my next problem...;) Right, basically I need a way to keep track of sprites contained within different classes. For instance I have a flying_alien class and a walking_alien class. I have to be able to run through a list of all the sprites of a certain kind to create and delete them as needed, etc. I've heard that linked lists are the way to go for this. Can anyone give a quick overview on using linked lists for this type of thing. I've done a search of the forums and have seen STL mentioned quite a bit regarding this same topic. What is STL and can I use it for this purpose? Thanks!
Advertisement
The STL is a library of basic data types and algorithms. While there are many such libraries, the STL is the most common, and to my knowledge has recently been made part of "official" C++.

And yes, many of the tools it provides will be useful to you in solving this problem.
Quote:Original post by whaleyboy
Can anyone give a quick overview on using linked lists for this type of thing.


a linked list is just a dynamic data structure that can grow & shrink at run-time, they are normally used to implement an abstract data types (ADTs) such as a lists so dont get the two concepts confused. There are loads of information on the net about linked-lists & lists in general google is your friend.

But you proabably wont a list of sprites as a opposed to a linked-list of sprites which wouldn't be as useful especially in terms of abstraction.

Quote:Original post by whaleyboy
I've done a search of the forums and have seen STL mentioned quite a bit regarding this same topic. What is STL


STL = standard template library part of the standard C++ library every compiler is required (meaning they must) to have it, its part of the language, when some body says STL they usually mean the standard library containers (containers are objects that contain other objects) & algorithms that work with them. For more information look here

Quote:Original post by whaleyboy
and can I use it for this purpose?


Yes the standard library has a list type and i recommend you do use it over trying to write your own.

Quote:Original post by Telastyn
the STL is the most common, and to my knowledge has recently been made part of "official" C++.


It wasn't that recent, STL was always considered part of the standardized C++ since the first draft of the standard in 1994 i think it was and become it when C++ was offically standardized in 1998 so its not that recent.

[Edited by - snk_kid on August 26, 2004 6:25:50 PM]
how much do you need to know to use stl, I have tried but it's like learning c++ all over again, like the original poster I would like to make a list of things for my game, but does that mean I have to now spend 3 or 4 monthes learning stl now?
Quote:Original post by Anonymous Poster
how much do you need to know to use stl


depends on how much c++ know, but not much to just to use them, for that probably you would need to know how to write & use functions, classes, little bit about function templates & class templates, understand what typedefs & reference types are. Understand what are code scopes and how they effect your code, know about dynamically memory de/allocation.

It would also help if you knew about data structures & algorithms to help you better select the wright container & algorihtm for the job but its not necessary.

But this depends on what you already know & fast you understand things, everyone is different.

Quote:Original post by Anonymous Poster
I have tried but it's like learning c++ all over again


Then it would seem you never really learn't c++ and it sounds like you've only learn't the low-level parts of c++ basically C and maybe somethings on classess.

Its not your fault, its the old c++ books start off teaching c++ the wrong way.

Quote:Original post by Anonymous Poster
like the original poster I would like to make a list of things for my game, but does that mean I have to now spend 3 or 4 monthes learning stl now?


Nah don't worry so much the standard library containers do alot for you they make you code on a high-level (as c++ should be for application development) meaning you write less but efficent code, they also handle memory management for you, i would say just use them and refer to documentation such as SGI's doc on STL here for minor details.
Spend 3-4 months learning the STL or a similar amount of time learning how to make linked lists so they don't blow up?

I don't know. Honestly, I don't know any of the STL. I'm stubborn.
thankyou both for your replies, thanks snk_kid for the link, I guess I will need to look into it :). About how much I know already, I've got as far as pointers & stuff, but I've spent alot of time also learning windows & directx as well, -like every one, you can't do it all at once. I've got this tutorial that tries to teach linked lists, but it's pages of code, I don't know if they were showing you the sort of code that actually goes into a list, (stuff you wouldn't have to do when just using them), or whether that is what you have to actually use.
Anyway thankyou.
Well, conceptually, linked lists are structures/classes that use pointers to make a list.

The simpilest is:

struct linked_list{    void         *data;    linked_list  *next;};


And that's it.

void *data can be replaced with whatever data you want to be in the list.

To form this little struct into a list, simply make "next" point to another linked_list struct in memory. It will eventually look like this conceptually:

void         *data;linked_list  *next  ------>   void         *data;                              linked_list  *next  ------>   void         *data;                                                            linked_list  *next  ------> NULL


To loop through the list, the syntax becomes

linked_list   *already_made_list,*ptr;/* code here to malloc/new the already_made_list pointer */for (ptr=already_made_list; ptr; ptr=ptr->next){}


That of course is the simplest form. They can be made into classes to automagically add or remove something from the list. The "data" can be another data structure [or template] to allow for multiple types.

STL's vector is already done and [to my knowledge] bug free. The slightly weird syntax is to allow you to specify the "data" type instead of having to make seperate structures for each and every possible stuff you want to make into a list.
Quote:Original post by Telastyn
Well, conceptually, linked lists are structures/classes that use pointers to make a list.

The simpilest is:

struct linked_list{    void         *data;    linked_list  *next;};


And that's it.

void *data can be replaced with whatever data you want to be in the list.


This is the one way you would write a linked-list node in C but this is not type-safe & potentially dangerous if not handled correctly, it is also unnecessarilly low-level in c++ this is where templates & the standard template library comes in, an example of a type-safe simplified linked-list node:

template< typename T >struct list_node {    typedef list_node<T>* node_ptr;       node_ptr      succ;   T             data;};


Quote:Original post by Telastyn
To form this little struct into a list, simply make "next" point to another linked_list struct in memory.


Just remember that a lists are a higher-level entity than a linked-lists, linked-lists are just dynamic data structures, lists are abstract data types and could be implementated by a linked-list it could just aswell be implementated by an array or any other data structure. lists are usually implementated with a linked list thou so you can expect it to be the case.

Quote:Original post by Telastyn
STL's vector is already done and [to my knowledge] bug free.


vectors are not lists, vectors are resizeable arrays. This is what people should be using in c++ instead of raw C dynamically allocated arrays.
Yes, sorry if anything I said was not in proper computer science terms. I've not enjoyed formal instruction.

This topic is closed to new replies.

Advertisement