Thread: Question on design

  1. #1
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472

    Question on design

    hi all,

    I am stuck with a little problem, i am rewriting a program to include a GUI.

    I have experimented with various options and because of the program only requiring limited interaction i am just writing the interface myself using SDL as otherwise i will (i think) be spending ages on back to basics stuff with other GUI options available.

    My problem is once the user decides to 'start' after they set up play, then control passes to an algorithm that can take a long time to execute depending on the settings.

    I need to be able to still get user events if they decide to 'stop' for example.

    apart from this even if the settings mean it will execute quickly i still need the event monitoring there to avoid frustration if somebody clicks and it does not immediately respond.

    i was planning to use a state machine (thanks to lazy foo) which i have done before a couple of times with success, but the requirements here mean it might not be as appropriate.

    Code:
    while not quit
    {
    currentstate->handleevents
    currentstate->do logic
    currentstate->changestate
    currentstate->render
    }
    This would be fine but in order to get the event handling i wish for when the algorithm starts then i can only see the option to put another event polling loop into the algorithm loop but that messes with my design ideal.

    and i think it might slow things down quite a bit also.
    Every time i have used sdlpollevent then i see 50%cpu usage even in an otherwise empty program, tried on various computers.

    Is it a viable idea to run my algorithm in a seperate thread, can i 'pause' it or control it from the event loop in my main program thread?

    sorry for the ramble, there is no code yet either
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  2. #2
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    i got some advice in the gameDev forum, though this is not really game related, anyway the advice prompted me to this thought >


    It did cross my mind to try piggyback the main program loop as the complexalgorithm loop...

    the idea would be:
    (as usual)

    check events,
    do logic
    change state
    render

    then, while the current state is the 'complexState' (for example) when i enter the do_logic call i would also do the next cycle of the algorithm after also checking the logic for any 'mouse_over' or 'button_down' rendering and updating the rects accordingly.

    if i get a 'mouse_up' on a button then it has to be a pause and i divert flow from the algorithm.
    if i then get another button up, and it is not 'continue' i switch the algorithm off altogether and changestate

    the complexalgorithm() function would depend on static local variables, and preferably all member variables to avoid unwanted resets on each function call.

    Overall reset when algorithm is switched off would be mostly acheived with destruction then construction next time it is required
    Last edited by rogster001; 09-14-2010 at 09:06 AM.
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

  3. #3
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Two threads seems like a good idea, one does the work and the other is the UI.

    A simple state machine for them to communicate with one another should suffice
    - start
    - stopping
    - stopped

    Once running, the algorithm thread should check for a stopping state around once a second say (or whatever is convenient in that sort of time frame).
    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.

  4. #4
    Registered User rogster001's Avatar
    Join Date
    Aug 2006
    Location
    Liverpool UK
    Posts
    1,472
    Yea thats definitely what i was hoping to have happen, as the program was initially a console app and required minimum event handling apart from a keyhit detect here and there, it looked after itself and could have all the resource to itself also.

    now i have to accomodate an attention seeking GUI and marry the two up.. So it would be the solution in my head to just have a thread that manages UI events and then the other can happily bash the numbers
    thats what i will attempt methinks,

    Ta
    Thought for the day:
    "Are you sure your sanity chip is fully screwed in sir?" (Kryten)
    FLTK: "The most fun you can have with your clothes on."

    Stroustrup:
    "If I had thought of it and had some marketing sense every computer and just about any gadget would have had a little 'C++ Inside' sticker on it'"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Game design question
    By h3ro in forum Game Programming
    Replies: 6
    Last Post: 02-28-2008, 08:20 AM
  2. Question about engine design.
    By Shamino in forum Game Programming
    Replies: 9
    Last Post: 01-29-2008, 10:53 AM
  3. question about class design and overloading
    By l2u in forum C++ Programming
    Replies: 7
    Last Post: 12-13-2007, 02:02 PM
  4. database app design question
    By MPSoutine in forum Windows Programming
    Replies: 4
    Last Post: 12-02-2003, 10:13 PM
  5. design question: opinion
    By ggs in forum C Programming
    Replies: 2
    Last Post: 01-29-2003, 11:59 AM