March 28, 2024, 08:28:44 AM

News:

Own IWBasic 2.x ? -----> Get your free upgrade to 3.x now.........


Pointers

Started by J B Wood (Zumwalt), December 06, 2006, 09:07:43 PM

Previous topic - Next topic

0 Members and 1 Guest are viewing this topic.

J B Wood (Zumwalt)

Does this look right?
Going to be a bit before my code is compilable, just wanted to make sure I was on the right track, near to 1300 more lines to go through so...
If this is ok, then I know what to do for the rest of the code.



' setup a new game
sub NewGame(totalLives as int)
'BASS_ChannelPlay(sndLevelStart,true)
/* list to hold our player lives */
if(ShipList != null)
listremoveall(ShipList)
ShipList=listCreate()
else
ShipList-listCreate()
endif
BuildPlayerLives(ShipList,Screen,totalLives)
ActivateShip(ShipList)

/* list to hold our space invaders */
if(InvadersList != null)
listremoveall(InvadersList)
InvadersList=listcreate()
else
InvadersList=listcreate()
endif
BuildSaucer(InvadersList,Screen)

' impliment the list of all invaders except the saucer
if(SpaceInvadersList != null)
listremoveall(SpaceInvadersList)
SpaceInvadersList=listcreate()
else
SpaceInvadersList=listcreate()
endif
BuildAllInvaders(SpaceInvadersList,Screen)

/* list to hold our bunkers */
if(BunkerList != null)
listremoveall(BunkerList)
BunkerList=listcreate()
else
BunkerList=listcreate()
endif
/* impliment the list and setup 4 bunkers */
BuildAllBunkers(BunkerList,Screen)

if(pScore != null)
delete pScore
pScore = new(sScore,1)
else
pScore = new(sScore,1)
endif

#<sScore>pScore[0].CurrentScore=0
#<sScore>pScore[0].CurrentScore=0
#<sScore>pScore[0].tLives=totalLives
#<sScore>pScore[0].rLives=totalLives

if(pLevel != null)
delete pLevel
pLevel = new(sLevel,1)
else
pLevel = new(sLevel,1)
endif
#<sLevel>pLevel[0].CurrentLevel=1

if(BulletInvader != null)
listremoveall(BulletInvader)
BulletInvader=listcreate()
else
BulletInvader=listcreate()
endif

buildBullets(BulletInvader,4+pLevel->CurrentLevel,1)

if(BulletPlayer != null)
listremoveall(BulletPlayer)
BulletPlayer=listcreate()
else
BulletPlayer=listcreate()
endif
buildBullets(BulletPlayer,5,0)

if(BulletSaucer != null)
listremoveall(BulletSaucer)
BulletSaucer=listcreate()
else
BulletSaucer=listcreate()
endif

buildBullets(BulletSaucer,5,2)
endsub


Ionic Wind Support Team

Shouldn't they be:

listremoveall(ShipList,TRUE)

?
Ionic Wind Support Team

J B Wood (Zumwalt)

December 06, 2006, 09:39:15 PM #2 Last Edit: December 06, 2006, 09:40:48 PM by Jonathan (zumwalt) Wood
Wasn't for sure about that since the manual says:
ListRemoveAll(list as POINTER,OPT bDelete=0 as INT)

Which is kind of contradictory of the definition of the method:
Description
Removes all nodes from the list and deletes the list.

Parameters
list - The linked list returned by ListCreate
bDelete - Optional. If 1 then DELETE is called for all data pointers in the list.



To me, if its removed from the list, it doesn't exist in the list, so it will delete the list, but I will use TRUE to be on the safe side. Shouldn't be optional, if its removed, it should be deleted automatically.

What about this section, does it look good?

if(pScore != null)
delete pScore
pScore = new(sScore,1)
else
pScore = new(sScore,1)
endif

#<sScore>pScore[0].CurrentScore=0
#<sScore>pScore[0].CurrentScore=0
#<sScore>pScore[0].tLives=totalLives
#<sScore>pScore[0].rLives=totalLives

if(pLevel != null)
delete pLevel
pLevel = new(sLevel,1)
else
pLevel = new(sLevel,1)
endif
#<sLevel>pLevel[0].CurrentLevel=1

Ionic Wind Support Team

The node is removed, bDelete specifes whether the data you added is also deleted.  Remember a linked list is a chain of linked structures:

type list
  pointer next
  pointer data
endtype

The 'data' pointer gets set when you use listadd.  The allocated structure itself is the node.  So A list add does (in pseudo code):

last_node->next = new(list,1)
last_node->next->data = the data you pass to listadd.

When a node in the list is removed it deletes the memory it manages, meaning the counterpart to new(list,1).  And if you specify it deletes the 'data' pointer as well, as a convenience.

Paul.
Ionic Wind Support Team

J B Wood (Zumwalt)

Sounds great.
Well, if its all good to go, I'll impliment the same changes across the board.
Here is the updated version..



' setup a new game
sub NewGame(totalLives as int)
'BASS_ChannelPlay(sndLevelStart,true)
/* list to hold our player lives */
if(ShipList != null)
listremoveall(ShipList,TRUE)
ShipList=listCreate()
else
ShipList=listCreate()
endif
BuildPlayerLives(ShipList,totalLives)
ActivateShip(ShipList)

/* list to hold our space invaders */
if(InvadersList != null)
listremoveall(InvadersList,TRUE)
InvadersList=listcreate()
else
InvadersList=listcreate()
endif
BuildSaucer(InvadersList)

' impliment the list of all invaders except the saucer
if(SpaceInvadersList != null)
listremoveall(SpaceInvadersList,TRUE)
SpaceInvadersList=listcreate()
else
SpaceInvadersList=listcreate()
endif
BuildAllInvaders(SpaceInvadersList)

/* list to hold our bunkers */
if(BunkerList != null)
listremoveall(BunkerList,TRUE)
BunkerList=listcreate()
else
BunkerList=listcreate()
endif
/* impliment the list and setup 4 bunkers */
BuildAllBunkers(BunkerList)

if(pScore != null)
delete pScore
pScore = new(sScore,1)
else
pScore = new(sScore,1)
endif

#<sScore>pScore[0].CurrentScore=0
#<sScore>pScore[0].CurrentScore=0
#<sScore>pScore[0].tLives=totalLives
#<sScore>pScore[0].rLives=totalLives

if(pLevel != null)
delete pLevel
pLevel = new(sLevel,1)
else
pLevel = new(sLevel,1)
endif
#<sLevel>pLevel[0].CurrentLevel=1

if(BulletInvader != null)
listremoveall(BulletInvader,TRUE)
BulletInvader=listcreate()
else
BulletInvader=listcreate()
endif

buildBullets(BulletInvader,4+pLevel->CurrentLevel,1)

if(BulletPlayer != null)
listremoveall(BulletPlayer,TRUE)
BulletPlayer=listcreate()
else
BulletPlayer=listcreate()
endif
buildBullets(BulletPlayer,5,0)

if(BulletSaucer != null)
listremoveall(BulletSaucer,TRUE)
BulletSaucer=listcreate()
else
BulletSaucer=listcreate()
endif

buildBullets(BulletSaucer,5,2)
endsub



That should work, don't see why it wouldn't.
Thanks again Paul.