Hey,
Well, I am trying to get a linked list to work in one of my classes. First I create 250 objects of my class 'Block'.
Block BlockObject[250];
Here is my constructor:
Block() {
if(pFirst == 0) {
pFirst = this; // pFirst is a static member.
}
else {
for(Block *pBlock = pFirst; pBlock->pNext; pBlock = pBlock->pNext){
}
pBlock->pNext = this;
}
pNext = 0;
}
//Here is my function I am trying to run:
void Block::DrawBlocks(LPDIRECTDRAWSURFACE7 BackSurface) {
RECT BlockRect;
for(Block *pS = pFirst; pS; pS = pS->pNext) {
BlockRect.bottom = pS->y_pos + pS->height;
BlockRect.top = pS->y_pos;
BlockRect.right = pS->x_pos + pS->width;
BlockRect.left = pS->x_pos;
DDFillRect(BackSurface, &BlockRect, RGB(10,10,1));
}
}
Well basically I am trying to go through each object of the class and draw it on the screen. If someone could help me figure out what is wrong that would be Appreciated. The code is error free, well atleast syntax.
Also, one more thing. How do you do the source view thing in theses forums?
Thanks.
Edited by - JSCFaith on April 18, 2001 9:09:16 AM
AFAIK when you declare a variable inside a for loop like that, it is only valid inside the scope of that block. But unless you have a global or member variable or something else called pBlock that would result in a compiler error.
The other thing would be to draw the blocks in a brighter colour - it may just be that they are too dark. Try 255,255,255 and see if that helps.
I see you are using an array and then sorting it into a list. Is the array only temporary? The entire point of a linked list is to get away from arrays. Maybe you don''t even need the array.
Are you even trying to be intelligent? ''cuz if you are, it ain''t workin'' Down with Tiberia!
I use traditional C for linked lists and I''m too tired to think what kind of "anchors" there should be in RPG. Level anchor for items on ground is one..
There is a bug in your constructor. The for loop always adds the node as the second node in the list and drops all others. So if you have four nodes in the list, after Block's constructor call you will only have two. (Of course, this depends on how you initialize Block's pNext data member.)
What you need is something like this:
if( pFirst == 0 ) { // this is the first node pFirst = this; } else { // get the head of the list Block* pBlock = pFirst;
// move to the end of the list while( pBlock->Next ) pBlock = pBlock->Next;
// tack this one onto the end; pBlock->Next = this; }
If you want to avoid the while loop, store a pointer to the last node (call it pLast). You'll have to update pLast every time you add or remove a node at the end, but it should be much faster as the list size increases.
Edited by - null_pointer on April 20, 2001 8:22:30 AM