• Announcements

    • khawk

      Download the Game Design and Indie Game Marketing Freebook   07/19/17

      GameDev.net and CRC Press have teamed up to bring a free ebook of content curated from top titles published by CRC Press. The freebook, Practices of Game Design & Indie Game Marketing, includes chapters from The Art of Game Design: A Book of Lenses, A Practical Guide to Indie Game Marketing, and An Architectural Approach to Level Design. The GameDev.net FreeBook is relevant to game designers, developers, and those interested in learning more about the challenges in game development. We know game development can be a tough discipline and business, so we picked several chapters from CRC Press titles that we thought would be of interest to you, the GameDev.net audience, in your journey to design, develop, and market your next game. The free ebook is available through CRC Press by clicking here. The Curated Books The Art of Game Design: A Book of Lenses, Second Edition, by Jesse Schell Presents 100+ sets of questions, or different lenses, for viewing a game’s design, encompassing diverse fields such as psychology, architecture, music, film, software engineering, theme park design, mathematics, anthropology, and more. Written by one of the world's top game designers, this book describes the deepest and most fundamental principles of game design, demonstrating how tactics used in board, card, and athletic games also work in video games. It provides practical instruction on creating world-class games that will be played again and again. View it here. A Practical Guide to Indie Game Marketing, by Joel Dreskin Marketing is an essential but too frequently overlooked or minimized component of the release plan for indie games. A Practical Guide to Indie Game Marketing provides you with the tools needed to build visibility and sell your indie games. With special focus on those developers with small budgets and limited staff and resources, this book is packed with tangible recommendations and techniques that you can put to use immediately. As a seasoned professional of the indie game arena, author Joel Dreskin gives you insight into practical, real-world experiences of marketing numerous successful games and also provides stories of the failures. View it here. An Architectural Approach to Level Design This is one of the first books to integrate architectural and spatial design theory with the field of level design. The book presents architectural techniques and theories for level designers to use in their own work. It connects architecture and level design in different ways that address the practical elements of how designers construct space and the experiential elements of how and why humans interact with this space. Throughout the text, readers learn skills for spatial layout, evoking emotion through gamespaces, and creating better levels through architectural theory. View it here. Learn more and download the ebook by clicking here. Did you know? GameDev.net and CRC Press also recently teamed up to bring GDNet+ Members up to a 20% discount on all CRC Press books. Learn more about this and other benefits here.
Sign in to follow this  
Followers 0
pressgreen

while loop MADNESS

16 posts in this topic

Ok so i am working on this program using PhysX and one of the samples that comes with the library has a while loop that runs in the rendercall back function. like so

 

 

    int nbActors = gScene->getNbActors();
    NxActor** actors = gScene->getActors();
    while(nbActors--){

 

NxActor* actor = *actors++;

///render all the stuff with physics :)

 

 

}

 

What i dont understand is that while loop. whats going on there exactly. from what i would guess its like incrementing through the number of actors and running the loop for each one but im not sure. could some one explaine this use of a while loop and or perhaps the use of the decrementer as something being passed like that.

 

Thanks

1

Share this post


Link to post
Share on other sites

so like is this loop is going to loop until nbActors-- reaches 0 before it breaks out?

 

also

 

If i wanted to specify a specific actor in that array " *actors++ " and start looping upwards from there how would that be done? for example if i wanted to start at index 5 and then ++ from there how would that be done?

0

Share this post


Link to post
Share on other sites

So swiftcoder, thank you very much well written and that helped me indeed.

 

but in Line B if i wanted to start at index 4 of actors, then increment up from there, how would i write that?

I know its a simple answer I am just not understanding or thinking about it correctly.

while(nbActors--){ // Line A

     NxActor* actor = *actors++; // Line B


    ///render all the stuff with physics

}

Edited by greenzone
0

Share this post


Link to post
Share on other sites

// Like this

actors += 4;   // start from element 4 instead of 0
nbActors -= 4; // still got the same number though so reduce initial loop count

while(nbActors--){ // Line A

     NxActor* actor = *actors++; // Line B


    ///render all the stuff with physics

}

EDIT: You need to check that nbActors (after subtracting 4) is >= 1 before you enter the while loop bit though otherwise bad things will happen.

Edited by Paradigm Shifter
1

Share this post


Link to post
Share on other sites

oh I am sorry, but i should have included that I am using the first 3 values in that array within that loop. so imagine if actors[0] - actors[4] are being used and passed to some other function or variable with in the loop. Then after I am wanting to take the rest of the actors in the array, actors[4] and upward, and increment upwards as in Line B.

 

 

How would I then increment upwards knowing i dont want to disturb actors 0-4 in the array?

 

for example:

while(nbActors--){ // Line A

    NxActor* actor0 = *actors[0]; 
    NxActor* actor1 = *actors[1]; 
    NxActor* actor2 = *actors[2]; 
    NxActor* actor3 = *actors[3]; 


     NxActor* actor = *actors++; // Line B     I want this line to up date every loop 
                                            // with new actors number counting upward from 4


    ///render all the stuff with physics

}
Edited by greenzone
0

Share this post


Link to post
Share on other sites


How would I then increment upwards knowing i dont want to disturb actors 0-4 in the array?

 

Like this:


NxActor* actor0 = actors[0];
NxActor* actor1 = actors[1];
NxActor* actor2 = actors[2];
NxActor* actor3 = actors[3];

actors += 4; // start from element 4 instead of 0
nbActors -= 4; // still got the same number though so reduce initial loop count

if(nbActors > 0)
{
    while(nbActors--){ // Line A
        NxActor* actor = *actors++; // Line B     

    ///render all the stuff with physics
    }
}

Alternatively:

 

 

NxActor* actor0 = *actors++;
NxActor* actor1 = *actors++;
NxActor* actor2 = *actors++;
NxActor* actor3 = *actors++;

nbActors -= 4; // still got the same number though so reduce initial loop count

if(nbActors > 0)
{
    while(nbActors--){ // Line A
        NxActor* actor = *actors++; // Line B

    ///render all the stuff with physics
    }
}
 
Edited by Paradigm Shifter
1

Share this post


Link to post
Share on other sites
int nbActors = gScene->getNbActors();
NxActor** actors = gScene->getActors();
while(nbActors--) {
  NxActor* actor = *actors++;
  ///render all the stuff with physics :)
}

 

...

 

SOMEONE GET THIS MAN A FOR-LOOP!

Edited by Khatharr
1

Share this post


Link to post
Share on other sites

Ok so i am working on this program using PhysX and one of the samples that comes with the library has a while loop that runs in the rendercall back function. like so

 

 

    int nbActors = gScene->getNbActors();
    NxActor** actors = gScene->getActors();
    while(nbActors--){

 

NxActor* actor = *actors++;

///render all the stuff with physics smile.png

 

 

}

 

What i dont understand is that while loop. whats going on there exactly. from what i would guess its like incrementing through the number of actors and running the loop for each one but im not sure. could some one explaine this use of a while loop and or perhaps the use of the decrementer as something being passed like that.

That while-loop is an obfuscated way of writing this for-loop.

 

int nbActors = gScene->getNbActors();
for (int i = 0; i < nbActors; ++i)
{
    NxActor* actor = actors[i];
    // ...
}

the world is full of Klever Koders.

This. Looks like a way to purposely obfuscate a loop more than anything else. Its almost "Coding Horrors" subforum worthy.

Edited by TheChubu
1

Share this post


Link to post
Share on other sites

Hmm, not really. It's just counting backwards, which was a win on the PS1 cos you had a hard wired register that always read zero so comparing to zero was faster than checking the loop condition. If the optimiser doesn't do it, you should compare against zero anyway since you don't need to do a compare you just check the zero flag... The optimiser probably does it though...

1

Share this post


Link to post
Share on other sites

Hmm, not really. It's just counting backwards, which was a win on the PS1 cos you had a hard wired register that always read zero so comparing to zero was faster than checking the loop condition. If the optimiser doesn't do it, you should compare against zero anyway since you don't need to do a compare you just check the zero flag... The optimiser probably does it though...

And if this snippet was from a tutorial on optimising loops for the PS1, fair enough.

 

Since it's meant to be a tutorial on writing physics callbacks, not so much...

2

Share this post


Link to post
Share on other sites

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  
Followers 0