• Advertisement

Archived

This topic is now archived and is closed to further replies.

Is there possiblility that running 2 loops at the same time using C++?

This topic is 5035 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

Is there any possiblility that running 2 loops at the same time using C++? Actually, i wanted to have 2 different things running at the same time, but i dont know how to do it. If can, please provide me example...please.... ty for your information and help [edited by - Athelena on May 8, 2004 4:05:24 AM] [edited by - Athelena on May 8, 2004 4:06:00 AM]

Share this post


Link to post
Share on other sites
Advertisement
Have you looked into multi-threading?
What, specifically, do you want to do with multiple simultaneous loops?

-Nik

Share this post


Link to post
Share on other sites
Yes. It''s called multithreading. It''s a ... complex subject.

Share this post


Link to post
Share on other sites
I presume you refer to multi-threading. To get multi-threading to work you need to design your program from the beginning with multi-threading in mind.

Keep in mind that for multi-threading to have true usefulness (yes, some benefits to multi-threading exist without these reqs) you need at least SMP or HT (eg 2 cpu''s +, or virtual cpus with HT) and an operating system that supports multi-processors. *nix and winxp do. So unless you have a problem you can''t solve without using multi-threading (eg music decoding/playing without you thinking about it), you really have to consider if it is worth the time (assuming you don''t have SMP). If you have SMP or HT, then it is always worth it =)

The best thing to find (assuming windows) is information on the API''s for "Processes and Threads".

You''ll need to learn about thread management and memory sharing before jumping in, but it pays.

Share this post


Link to post
Share on other sites
Actually, i wanted to write a simple game - Space Invader, as my pratice for C++.

For now, I've done the part where my ship, and the bullets that shot out from the ship can be move at the same time. But, when I wanted to let the Alien Ship to move from left to right, it works, but, when the Alien Ship is moving, and when my bullets shot out, the loop for the Alien Ship stop for the Bullets to shot out.....

Can help over here...? ty

oh Nik02, where can i find the tutorial for multi-threading?
ty for your help and information~

[edited by - Athelena on May 8, 2004 4:18:14 AM]

Share this post


Link to post
Share on other sites
You must use one single loop for your entire game.

you'll have a function where you update the state of all the objects in your game. You'll have variables (global to the function's scope) representing bullets and aliens positions and you will increase/decrease them all one after another in this function.

something pseudo-like:


int alien_x_pos;
int alien_y_pos;
bool alien_visible;

int bullet_x_pos;
int bullet_y_pos;
bool bullet_visible;

Initialize()
{
alien_x_pos = 10;
alien_y_pos = 2;
alien_visible = true;

bullet_x_pos = ship_x_pos;
bullet_y_pos = ship_y_pos;
bullet_visible = false;
}

ProcessPosition ()
{
if (bullet_visible)
bullet_y_pos--;

if (alien_y_pos+alien_width == bullet_y_pos)
alien_visible = false;
else
alien_x_pos++; // or --

}

int main()
{
Initialize();
  
while(running)
{
ProcessPosition();
DrawObjects;
}
}


EDIT: Damn code tags..




[edited by - owl on May 8, 2004 4:29:06 AM]

Share this post


Link to post
Share on other sites
dear owl:

I''ve done the part already......currently i only missing the Alien Ship moving while my bullets shot out

ty for your information and help

Share this post


Link to post
Share on other sites
quote:
You must use one single loop for your entire game.


Why "must" ?

Share this post


Link to post
Share on other sites
quote:
Original post by Fruny
quote:
You must use one single loop for your entire game.


Why "must" ?


Because that''s the first word I came up when I translated my thought''s to english.

Pick one: should, could, would, will, have to... any will work.

Have you any advice for the OP or are you only trolling around?

Share this post


Link to post
Share on other sites
I use a while loop for pending for user input key
eg:
SpaceBar, Left, and Right

that's why I have a big single loop for the entire game.
Whithin the loop, I'll have to draw all the object, with 2D array (Done), pending for user input key - where for ship to move and shot (done), bullets shot out from ship (done - but when bullets shot out, it stop the alien moving loop)

Basically, i have multiple loop within a single loop, but some i need to run at the same time, just dont know how to do it....

again, thx for your information and help
ty


[edited by - Athelena on May 8, 2004 4:44:25 AM]

Share this post


Link to post
Share on other sites
quote:
Original post by Athelena
Basically, i have multiple loop within a single loop, but some i need to run at the same time, just dont know how to do it...


That''s the wrong way of doing it. You don''t need multiple loops to move your objects. One loop will do the job. Re-read my post.

And look up in the articles section if my example isn''t clear enough.

Share this post


Link to post
Share on other sites
Perhaps you could just keep track of the bullets in a dynamic list (or limit the bullets for easier coding =))

That way when you draw your screen you move the bullets x,y then store that and draw the bullet on the screen.

Btw, when you say ''wait for user input'', are you stopping the loop until user input comes in? If so, investigate ways to not do that (eg peekmessage).

Share this post


Link to post
Share on other sites
for my case, the Alien Ship is not in the "Pending for user input" loop, it's another loop.

The "Pending for user input" loop is for the user to press "left,right,spacebar" in order to perform some actions - move left,right, and shot.

Therefore, it's 2 different loop, and i wanted both to run togethers~

emm~~... can provide me some code please...?

ty for your information and help
~Smile~

[edited by - Athelena on May 8, 2004 5:05:33 AM]

Share this post


Link to post
Share on other sites
you can get banned for doing this if you are bugging on purpose.

Share this post


Link to post
Share on other sites
What you mean "bugging on purpose"? actually, i''m new to this forum, just sign up today, so still i dont know the rule here.

Share this post


Link to post
Share on other sites
You have to ask yourself, "Wouldn''t it be easier to actually query and process the input and move the bullets/aliens/ship in the same main loop?"

A hint: yes...

Kind rgds,
-Nik

Share this post


Link to post
Share on other sites
Click here. This probably isn''t what you are looking for, but in may give you some ideas. Like all of the enemy movements could be handled in the process method and input could be handled in the input method, and so on. The goal of programming in modern operating systems is to share. what you need to do is instead of getting two loops to run concurently, they both need to update a little bit, then return control to the system.

Btw, what OS, and what API?

Share this post


Link to post
Share on other sites
quote:
Original post by Athelena
dear owl:

I''ve done the part already......currently i only missing the Alien Ship moving while my bullets shot out

ty for your information and help


whoa... I didn''t see this one. Sorry.

Share this post


Link to post
Share on other sites
Ah, I think the key to remember is that everything should be given a chance to do any movement or processing that it needs to do *before* the next frame is drawn. That will allow multiple things to be able to move at the same time, which is what it sounds like you want.

In reality (as in looking at it from the pre processing), one is moving before the other. But since all that pre-processing happens before it is rendered to the screen, and any reactions that must take place because of those interactions also happens before it is rendered to the screen, the result is that the end user, even if the end user could flip through one frame at a time at his leisure, would never know that and they are in fact effectively happening at once.

I hope that helps!

Share this post


Link to post
Share on other sites
Thx to everyone''s help and infomation

Now I''ll to do it....but using the information that you all given..

Once again, ty very much

Share this post


Link to post
Share on other sites

  • Advertisement