Jump to content
  • Advertisement

Archived

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

Athelena

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

This topic is 5214 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

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!