Darknez
Darknez

Reputation: 39

GTK after main loop

I have a problem as i cannot find proper solution in reference for making something happen after gtk_main(). This is my function for a simple bot that solves game but it won't run because it's not even starting. If i place gtk_main() after bot is done i will get a solved game so it works. Is there a function in gtk that lets me operate as i want in the gtk main loop? Here is the code:

void RunBot(struct Packet *packet){
gtk_main();
while(LookForWin(packet)==0){
    packet->data->color_number=ColorPredictor(packet);
    CheckColors(packet);
    RefreshBoard(packet->essentials,board,colors,BoardButtons,Board);
    printf("Running..\n");
}   }

Thanks in advance :)

Upvotes: 1

Views: 2091

Answers (2)

user4815162342
user4815162342

Reputation: 154911

As andlabs and mame98 pointed out, GUI toolkits expect your logic to be executed from callbacks that carry out a small amount of work and quickly relinquish control to the main loop. There is no "work after main loop", because the end of the main loop typically coincides application's exit.

Restructuring existing code to an event-driven style can be very hard when working with a large legacy code base. Fortunately, there is another option.

You can launch a thread (using glib to do it portably) and run your game logic in that thread, separate from the GUI thread. Inside the game thread, use gdk_threads_add_idle() to occasionally inform your GUI of changes. That should allow both the game code and the GUI thinking each runs the show.

Upvotes: 1

mame98
mame98

Reputation: 1341

So in GTK you have events,signals and timeouts.

So if you want to call a function every x milliseconds, you should use

g_timeout_add (guint interval,
               GSourceFunc function,
               gpointer data);

Just look into the official documentation for more details about this function.

gtk_main will run until you call gtk_main_quit(); but you should only do this at the end of your application.

Upvotes: 0

Related Questions