Sign in to follow this  

Sprite Lists

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

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!

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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?

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
Guest Anonymous Poster
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
I might be wrong, but I think the discussion got sidetracked from the original problem from the very beginning. The problem is that whaleyboy needs to contain different types in the same container, whatever container that may be. The immediate OOP answer to this is obviously inheritence, which may be something that the OP will need to research further. The basic idea is that you would have a general class called something like c_Alien. You would then derive other subclasses from this class, called things like c_FlyingAlien and c_WalkingAlien. Your container would contain pointers to a class of type c_Alien. You could then use RTTI (Run-Time Type Information) or your own virtual functions to determine what subclass each pointer points to. There's a lot of stuff in there to learn about, though, if you're new to inheritance. But that is the typical method when using OOP.

Share this post


Link to post
Share on other sites

This topic is 4856 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this