How do i use the lists?

If you are a new Irrlicht Engine user, and have a newbie-question, this is the forum for you. You may also post general programming questions here.
Post Reply
dgrafix
Posts: 116
Joined: Sun Nov 18, 2007 2:36 pm

How do i use the lists?

Post by dgrafix »

Im trying to store several bullets of class bullet. In my other language (blitzmax) i did this using a TList.

I want to do this in c++

An example, its fairly self explanatory:

Code: Select all

Global tweaponlist:TList=New TList


Type tweapon

	Field originX#, originY#, power, h:Iscenenode
	Field dir:Byte,style:Byte,friendly:Byte

	Method updatebullets()
		dg_Movenode h,1,0,0
		If DG_NodeX(h)>680 drop();Return
		If DG_NodeX(h)<-20 drop();Return
		If DG_NodeY(h)>500 drop();Return
		If DG_NodeY(h)<-20 drop();Return		
	End Method

	Method drop()
		h.remove()
		tweaponlist.remove(Self)
	End Method

End Type

Function UpdateAllWeapons()
	bulletcount=0
	For Local w:tweapon=EachIn tweaponlist
		bulletcount :+ 1
		Select w.style
			Case WEP_BULLET
				w.updatebullets()
		End Select
	Next
End Function

Im particulary interested in the lines:

Code: Select all

Global tweaponlist:TList=New TList
For  w : tweapon=EachIn tweaponlist
tweaponlist.remove(Self)
I have been looking through the API and have stumbled across core::list which seems to be similar. Is this what im looking for and is there any decent c++ examples anywhere showing this in use?
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com :)
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

core::list should be what you're looking for.

See examples directory of the SDK for code examples.

Any other questions probably would be answered in the API (check docs directory of the SDK) and by searching the forum.

Don't forget this forum.
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
WorldSmith
Posts: 13
Joined: Sat Apr 19, 2008 5:05 pm
Location: Switzerland
Contact:

Post by WorldSmith »

Have a look at these links:

http://richardbowles.tripod.com/cpp/lin ... nklist.htm

http://www.daniweb.com/code/snippet738.html

This one provides you with neat explanations.
http://www.fortunecity.com/skyscraper/f ... klist.html

Here some source again for linked lists
http://sig9.com/node/312

And here a linked list that's using quick sort, which I used in former project:

Code: Select all

typedef struct QuickList
{
	CBeing* data;

	// Pointers
	struct QuickList *pPrev;
	struct QuickList *pNext;
} QuickList;

// Pointers to First and Last items
QuickList *g_pFirstItem = NULL;
QuickList *g_pLastItem = NULL;

// Add List Item
void AddListItem(CBeing* b)
{
	// Create a new list item
	QuickList *pItem = new QuickList;
	
	// Set Integer value
	pItem->data = b;

	// Add to list
	// If this is the first item added
	if (g_pFirstItem == NULL)
	{
		g_pFirstItem = g_pLastItem = pItem;
		pItem->pNext = pItem->pPrev = NULL;
	}
	else
	{
		// Add item to the end of the list
		g_pLastItem->pNext = pItem;
		pItem->pPrev = g_pLastItem;
		g_pLastItem = pItem;
		pItem->pNext = NULL;
	}
}

// Remove all items
void RemoveAllItems()
{
	QuickList *pDelItem, *pItem = g_pFirstItem;

	// Loop while there are items
	while (pItem != NULL)
	{
		// Set the delete item
		pDelItem = pItem;

		// Move to the next item
		pItem = pItem->pNext;

		// Delete the item
		delete pDelItem;
	}

	// Set global pointers to NULL
	g_pFirstItem = g_pLastItem = NULL;
}

// Print List
void PrintList()
{
	QuickList *pItem = g_pFirstItem;

	// Loop while there are items
	while (pItem != NULL)
	{
		// Print this item
		printf("%d %d %d\n", pItem->data->getID(), pItem->data->getX(), pItem->data->getY());

		// Move to the next item
		pItem = pItem->pNext;
	}
}

// Quick Sort List
void QuickSortList(QuickList *pLeft, QuickList *pRight)
{
	QuickList *pStart;
	QuickList *pCurrent; 
	CBeing* copyBeing;

	// If the left and right pointers are the same, then return
	if (pLeft == pRight) return;

	// Set the Start and the Current item pointers
	pStart = pLeft;
	pCurrent = pStart->pNext;

	// Loop forever (well until we get to the right)
	while (1)
	{
		// If the start item is less then the right
		if (pStart->data->getX() < pCurrent->data->getX())
		{
			// Swap the items
			copyBeing = pCurrent->data;
			pCurrent->data = pStart->data;
			pStart->data = copyBeing;
		}	
		
		// Check if we have reached the right end
		if (pCurrent == pRight) break;

		// Move to the next item in the list
		pCurrent = pCurrent->pNext;
	}

	// Set the Start and the Current item pointers
	pStart = pLeft;
	pCurrent = pStart->pNext;
	// Loop forever (well until we get to the right)
	while (1)
	{
		// If the start item is less then the right
		if (pStart->data->getY() < pCurrent->data->getY())
		{
			// Swap the items
			copyBeing = pCurrent->data;
			pCurrent->data = pStart->data;
			pStart->data = copyBeing;
		}	
		
		// Check if we have reached the right end
		if (pCurrent == pRight) break;

		// Move to the next item in the list
		pCurrent = pCurrent->pNext;
	}

	// Swap the First and Current items
	copyBeing = pLeft->data;
	pLeft->data = pCurrent->data;
	pCurrent->data = copyBeing;

	// Save this Current item
	QuickList *pOldCurrent = pCurrent;

	// Check if we need to sort the left hand size of the Current point
	pCurrent = pCurrent->pPrev;
	if (pCurrent != NULL)
	{
		if ((pLeft->pPrev != pCurrent) && (pCurrent->pNext != pLeft))
			QuickSortList(pLeft, pCurrent);
	}

	// Check if we need to sort the right hand size of the Current point
	pCurrent = pOldCurrent;
	pCurrent = pCurrent->pNext;
	if (pCurrent != NULL)
	{
		if ((pCurrent->pPrev != pRight) && (pRight->pNext != pCurrent))
			QuickSortList(pCurrent, pRight);
	}
}

// Quick Sort List
void QuickSortList2(QuickList *pLeft, QuickList *pRight)
{
	QuickList *pStart;
	QuickList *pCurrent; 
	CBeing* copyBeing;

	// If the left and right pointers are the same, then return
	if (pLeft == pRight) return;

	// Set the Start and the Current item pointers
	pStart = pLeft;
	pCurrent = pStart->pNext;

	// Loop forever (well until we get to the right)
	while (1)
	{
		// If the start item is less then the right
		if (pStart->data->getY() < pCurrent->data->getY())
		{
			// Swap the items
			copyBeing = pCurrent->data;
			pCurrent->data = pStart->data;
			pStart->data = copyBeing;
		}	
		
		// Check if we have reached the right end
		if (pCurrent == pRight) break;

		// Move to the next item in the list
		pCurrent = pCurrent->pNext;
	}

	// Swap the First and Current items
	copyBeing = pLeft->data;
	pLeft->data = pCurrent->data;
	pCurrent->data = copyBeing;

	// Save this Current item
	QuickList *pOldCurrent = pCurrent;

	// Check if we need to sort the left hand size of the Current point
	pCurrent = pCurrent->pPrev;
	if (pCurrent != NULL)
	{
		if ((pLeft->pPrev != pCurrent) && (pCurrent->pNext != pLeft))
			QuickSortList(pLeft, pCurrent);
	}

	// Check if we need to sort the right hand size of the Current point
	pCurrent = pOldCurrent;
	pCurrent = pCurrent->pNext;
	if (pCurrent != NULL)
	{
		if ((pCurrent->pPrev != pRight) && (pRight->pNext != pCurrent))
			QuickSortList(pCurrent, pRight);
	}
}
Hope that helps. :)
MasterGod
Posts: 2061
Joined: Fri May 25, 2007 8:06 pm
Location: Israel
Contact:

Post by MasterGod »

Oh and there's always STL :wink: (using namespace std).
Image
Dev State: Abandoned (For now..)
Requirements Analysis Doc: ~87%
UML: ~0.5%
dgrafix
Posts: 116
Joined: Sun Nov 18, 2007 2:36 pm

Post by dgrafix »

great thx :)

-> check docs directory of the SDK
You do man irrlichts sdk right? It is C++ and irrlight im trying to do this in, not bmax :)
I cant find it :?
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com :)
Ion Dune
Posts: 453
Joined: Mon Nov 12, 2007 8:29 pm
Location: California, USA
Contact:

Post by Ion Dune »

It's included in the irrlicht download under "doc" and its on the main irrlicht webpage under "API."
dgrafix
Posts: 116
Joined: Sun Nov 18, 2007 2:36 pm

Post by dgrafix »

Thats what was confusing me :) I cant seem to find list examples under the api though. Just technical references.


think ive got it now though.

Thanks for the help.
C++/Irrlicht Noob pl3se B p4t1ent.
Visit www.d-grafix.com :)
Post Reply