Question on design
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.
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.
while not quit
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
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:
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
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
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).
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,