1
votes

I'm creating a GUI application using the MVC design pattern in C. It is meant to be a game of connect 4.

I want my controller to be able to send data to the GUI. I have a method in my GUI to draw the starting elements, e.g. the board and buttons to select which column to drop a checker in.

At the end of this method I have to call gtk_main() in order for the GUI to show on screen. I want to be able to send data between the view and controller class, but after calling this method, control is NOT given back to the controller class.

To be clear, the main method is in my controller class, and it calls the 'setup' method in my view class, the gtk_main() call is at the end of the 'setup' method.

How do I resolve this?

Also, for example, I have this method in my view:

void drop_checker(GtkWidget *widget, cairo_t *cr, gint col, gint row, int r, int g, int b)

It is easy to call this using a GTK callback, since I have access to the widget and cr variables, however I don't know how I would call this from my controller class.

Is this an error with how the application is designed or is this a technical misunderstanding?

1

1 Answers

2
votes

I think it's a technical misunderstanding which has lead to your application being designed incorrectly. For a GUI application, you want to have the main loop running at all times and your program flow should be something like this

int main (int argc, char **argv)
{
    <do setup>
    <do UI setup>

    gtk_main ();

    return 0;
}

The main loop is only exited whenever you call gtk_main_quit(); and typically this is only done in response to the game being quit via window controls or a menu option. Everything else that happens in your game must happen due to an event from the main loop. You can attach callbacks to signals on widgets like the "clicked" signal on GtkButton, or if you need something to happen independently you can set up callbacks to be called at specific times with g_timeout_add or just when the mainloop is idle with g_idle_add.

(Note that there are times when you do want to run a second loop inside the first, but those are few and far between and not relevant to your current situation and generally GTK+ handles them without you needing to know about the details)