I am programming a card game in GTK. Is there a way to create a loop that would not exit a function (the human_play function for example) until the user clicks on a card? I mean, it would be like waiting for the user to click a card (while doing so, not blocking the quit button and other event widgets) in order for the function to return and continue running subsequent code.
Let me know if I can be more clear. Responses will be very appreciated.
It would seem at first that what you want is a GtkDialog widget to take the user input (choose a card). A GtkDialog would not return to the function that called it until the adequate response is given.
It just a matter of coding your gtkdialog. The user_data gpointer given to the callback that calls the dialog can have a single pointer or a collection of pointers on a GPtrArray to manage the internal data structures of your program.
If your program is purely reactive that should be enough.
If you want the engine “thinking” while the user ponders its decision at the gtkdialog, then you need a separate thread and manage the communication with the callbacks. Byt that is far more complex.
For more info:
Edit:
I miss understood your initial post, hence the
GtkDialogrecomendation.Did you implement a working prototype? I mean for example a text version of your game engine?. Which seams turn based with no dice.
If you have a proper working turn based engine, you should be able to use callbacks from the GTK UI to handle events.
For example if you have some
GtkButtonwith a card images to identify them, connecting a button clicked event to a corresponding callback which does 2 things in order1) Set the parts of the UI which you want to disable as insensitive while the computer thinks. This can be accomplished using the
gtk_widget_set_sensitive(widget,FALSE)function with the relevant widgets. This includes the button just pressed/clicked.2)pass the data of which card was selected to the game engine, the game engine then retrieves the current game status and processes the information, while the engine does not return with an answer your callback will not return either.
3) Once the engine return an answer/move/responde, show it to the user and use again
gtk_widget_set_sensitive()to enable all the user widgets.