My question now, is how to have a loop without a wait??? I always get an engine crash without one...
Hanging != crashing!
Crashing means that all computation stops, hanging just means that you do computation without giving the window event handler any time to do its own processing, which the OS will then take as: Alright, it's not responding.
This, will hang your program, but not crash it:
Soo... How do you write loops that don't hang? Well, have them terminate at some point!
Take this strcpy function as an example:
char *strcpy(char *dst, const char *src)
{
char *d = dst;
while(1)
{
*dst = *src;
if(*src == '\0')
break;
dst ++;
src ++;
}
return d;
}
The loop will automatically terminate once the source string reached the end. So, why shouldn't you put a wait in there? Quite simply, first of all because it has a return value and that would never reach the caller with a wait (remember, wait doesn't allow the callee to return values), and secondly because it's superfluous in the first place!
You don't want your string copy operation to take multiple frames, you want to call it and have the result immediately. Your program won't hang, even for long strings, because copying the memory around is insanely fast especially once the CPUs branch predictor kicks in.
Of course, that doesn't mean wait in loops doesn't have its use case! Everything that you want to get periodically done every frame or so, well, use wait, that's what it's there for!