Thread: How do I have C Do 2 Things at Once?

  1. #1
    Registered User mr_raging_money's Avatar
    Join Date
    Mar 2012
    Posts
    18

    Question How do I have C Do 2 Things at Once?

    I am writing a simple program using shapes and keyboard control, and I started doing simple AI today for the enemies. I have my program now so that every time I press a key to move the box or shoot, it will then also move the enemy box. Is there a way for the enemy box to move without me pressing a key first, making the program do two things at once?(enemy box is moving and I am not pressing any keys to set everything in motion, ex. player movement, ai movement)

  2. #2
    Registered User
    Join Date
    Dec 2011
    Posts
    795
    I'm not sure how you're making things "move" and how you're getting user input (hint: post code), but the idea of "doing 2 things at once" is easily accomplished with threading.

    For example, you could share a common data struct with two separate threads: one to get user input and move the user's character, and one to move the AI and anything else in the background.

    For starters:
    > Linux Tutorial: POSIX Threads
    > Threads: Basic Theory and Libraries
    > Multithreading in C

  3. #3
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    It doesn't sound like you're looking for threads but simply want to make it appear that both "boxes" are moving at the same time. What you need is a way of reading the keyboard without blocking. That is system dependent, so what OS are you using? Pseudocode:
    Code:
    while (gameOn) {
        testKeyboard();  // read keyboard without blocking and modify player state
        movePlayer();
        moveAI();
    }
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  4. #4
    Registered User mr_raging_money's Avatar
    Join Date
    Mar 2012
    Posts
    18
    I'm using Windows 7 for programing right now. I'm not looking to have the "boxes" move at the same time, that is what I have right now, but have the AI move separately from the player, and not require keyboard input.
    Code:
    while(gameon){
    testKayboard();
    movePlayer();
    moveAI();  //how AI moves is not dependent on keyboard input currently
    }
    That is what I have now.

  5. #5
    - - - - - - - - oogabooga's Avatar
    Join Date
    Jan 2008
    Posts
    2,808
    How are you reading the keyboard without blocking?
    The cost of software maintenance increases with the square of the programmer's creativity. - Robert D. Bliss

  6. #6
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,666
    Is this a console program, or a GUI program?
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  7. #7
    Registered User ledow's Avatar
    Join Date
    Dec 2011
    Posts
    435
    There are several ways of accomplishing what you want.

    The first is to run on loops. Just keep constantly checking the keyboard, moving the enemy, checking the keyboard, moving the enemy and carrying on no matter if anything was pressed or not. This requires a "non-blocking" keyboard reader (i.e. something that returns "no key is pressed" or "key A is pressed" in some fashion and does it immediately without waiting for the user to finish). You just keep looping whether or not it says any particular key was pressed, so your enemies move whether or not you're pressing a key at all. As a user presses a key, you will hopefully "catch" them in the act by just looking at the keyboard often enough.

    The second is to run on timers. You set up the environment to fire a timer every now and then (e.g. 50 times a second) and at that time, you check the keyboard. Otherwise, you just keep running through the loop that moves your enemies. This often uses threading or some OS-based feature such as interrupts and can be OS-specific. As a user presses a key, you are informed and can act on it immediately no matter what else is happening. You can literally "jump" out of your AI code to handle a keypress and then jump back. This requires "non-blocking" keyboard functions too, and is probably the most horrible way to handle things if you're trying to do stuff manually (though some libraries work like this behind-the-scenes).

    The third is to run on events. This is similar to the previous one. You just loop constantly until "something" happens. That something could be a keyboard press, a timer event, some user-thrown event, a window event, etc. This is called an event loop. So as the user presses a key, you act on it at some point during the next loop, which also handles all your other bits too. If the user doesn't press, it just loops around again and checks for events.

    By far the most common is the third method - the event loop - and almost all libraries will provide you with a means to create and handle an event loop (e.g. in SDL, SDL_Events, etc.), and it's not that difficult to roll your own. Basically you loop forever, and when an event is pushed into the event "queue" (just a list of things that have happened since you last looked) you act on it. That might be that 10ms have passed and you need to move the AI units, or that a particular key was pressed, or that the mouse was moved from its previous position or whatever. On each loop, you just check the queue and handle the events in it (if any). All your drawing code is done inside the loop too and when the loop ends, your game is basically finished and will quit. It's not at all uncommon for such a loop to be of the type:

    Code:
    while(TRUE)
    {
        while(there_is_an_event_in_the_queue == TRUE)
        {
            handle_event();
        }
        draw_frame();
    };
    Where handle_event is the code that actually looks at what is pending (the events that are currently needing handling) and acts on them. They could be a keypress event (so you know to move the player's units or open a menu) or a timer event (so you know to increment a clock, decrease the number of resources, move the AI because he needs to move X times per second etc.) or event an event you create for yourself (e.g. the AI has died, so push a special event for that into the queue and handle things like celebrations, going back to the menu, quitting the game, etc. when it's time to do that event).

    You hardly ever do anything "two things at once" using this method, but that's the way 99% of games handle things. To *actually* do things "two things at once" needs threading and/or dual-processors (depending on your definition of "at once") and cause an AWFUL lot of synchronisation and locking problems, but you don't need to because if you're only drawing 60fps (say), then so long as everything that needs to happen happens within one frame, the user CANNOT tell anything is different - and computers are inherently linear and much easier to program when things happen in order.

    Make yourself an event loop, or even just a "brute-force" loop, and find a non-blocking way to read the keyboard (usually platform-specific unfortunately) and loop like crazy acting on whatever you need to and only drawing once you've acted on everything you can. Otherwise, you are sadly limited to waiting for some keyboard function (DON'T USE SCANF!) to say it's finished before you can do anything else.

    - Compiler warnings are like "Bridge Out Ahead" warnings. DON'T just ignore them.
    - A compiler error is something SO stupid that the compiler genuinely can't carry on with its job. A compiler warning is the compiler saying "Well, that's bloody stupid but if you WANT to ignore me..." and carrying on.
    - The best debugging tool in the world is a bunch of printf()'s for everything important around the bits you think might be wrong.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need help on some things
    By TheShape in forum C++ Programming
    Replies: 5
    Last Post: 06-03-2007, 09:36 AM
  2. How are things looking
    By cyberCLoWn in forum C++ Programming
    Replies: 8
    Last Post: 01-15-2004, 02:53 PM
  3. It's the little things my CPU does...
    By SMurf in forum C Programming
    Replies: 1
    Last Post: 12-18-2003, 05:54 PM
  4. I have tried many things...
    By skyruler54 in forum C++ Programming
    Replies: 8
    Last Post: 09-05-2002, 07:17 PM
  5. how do i do these things?
    By knight543 in forum Windows Programming
    Replies: 1
    Last Post: 08-01-2002, 03:36 AM