Update everything serially? Not sure I understand, I can't get them to attack at the same time without threading... can I? |
That depends on what you mean by "at the same time".
'Serially' = everything takes turns, going one at a time.
'Parallel' = everything runs at the same time.
The problem with parallel is that it's tricky. Damn tricky. And in cases where multiple jobs are sharing the same information (like a map... or another object's position)... you almost always have to guard accesses so that they don't actually happen in parallel.
Since games tend to have lots of moving parts that all interact with each other, writing parallel code is extraordinarily difficult. Which is why most games, AFAIK, don't do it (at least not for the game logic).
So my point is... you don't really want things to happen at the exact same time in your game. You just want them to happen really really close to each other.
Let's back up a minute and look at what I suspect you're thinking of doing:
1 2 3 4 5 6 7 8 9
|
// Called once by a separate thread
void DoEnemyStuff()
{
while( alive )
{
AttackPlayer();
Sleep(1000);
}
}
|
The idea being that the enemy attacks the player, waits a second, attacks again, waits, etc.
This might seem straightforward and simple... but really it's
horrible. Pretty much the only way to make this approach work would be to use a separate thread. But then you'd need to put each enemy in its own thread.. and you'd have to carefully synchronize dozens or even hundreds of threads (not easy).
So instead of crap like that... you typically update things serially. The idea being that your enemy just takes a small step through his logic (aka: an "update")... rather than doing all of it at once. Then your main program can merely update all objects every frame.
Something like this:
1 2 3 4 5 6 7 8 9 10 11 12
|
// Called once every frame
void DoEnemyStuff()
{
if( time_to_next_attack < 0 ) // if it's time to attack
{
AttackPlayer(); // attack the player
time_to_next_attack += 1000; // then wait 1 second before attacking again
}
time_to_next_attack -= time_since_last_update; // count down based on how much
// time has passed
}
|
Typical, simplistic game loop:
1 2 3 4 5 6
|
while(game_is_running)
{
ProcessUserInputAndWindowMessages();
UpdateAllObjects();
DrawTheScene();
}
|
In normal games, the entire screen is redrawn every frame. If you have a fixed framerate (like say 60 FPS), then you can simplify things and update all your objects once per frame.
Wshew. That was probably a lot wordier than it needed to be, but hopefully you get the idea.
ok, how do I specify the coords for the rect? |
RECTs have a 'left', 'top', 'right', and 'bottom' value. Just set those to whatever you want:
1 2 3 4
|
rc.left = 0;
rc.top = 0;
rc.right = 200;
rc.bottom = 100;
|
Now the text will be drawn in a 200x100 rectangle in the upper-left corner of the screen (position 0,0)