views:

203

answers:

3

This is a thought problem, and one I have been wrestling with for about a day.

I am writing an engine for a game in C (open source, for fun), and all of the interface (read GUI) elements deal with mouse input in a function interfaceMouse(). Basically, I call interfaceMouse() on a regular basis to see if the user has acted with the interface.

I would like this function to take latest mouse click, see if it interacts with any GUI element, and then return how that mouse click interacted with the gui element.

Given two different gui elements:

  • buttons (user clicked button A)
  • select menus (user selected option 1 of menu A)

I'm trying to figure out how best to return an interface event to the calling function. My best guess right now is linked lists for each type of event (buttonEvents, selectEvents), and functions like getNextButtonEvent(), which returns an event-specific value. interfaceMouse() returns a value dependent on the type of event the mouse event triggered, and then the calling function will have to retrieve that event from the respective list with getNextTypeEvent().

I'm not very satisfied with this approach, and was wondering if anyone has a better idea or could provide some additional foresight?

+1  A: 

You could return a pointer to the function that's going to treat the event, and then just execute it when it's the right time for it? You could even have a FIFO of function pointers awaiting to be executed in order, with interfaceMouse() feeding it, and you just execute the ones in front when you can. I also see that you are using a polling mechanism with interfaceMouse(), is this the best option? I assume you do this in the game loop or something like that.

machielo
Yes, in the game loop. I'm guessing if the first element in each struct was something like int type that kept track of what kind of event this was, this solution could work.
endeavormac
But the function that calls interfaceMouse() will do something according to the event returned, right? If you are going to treat the last event right away, then why won't return a function pointer that treats that particular event? This complicates a bit interfaceMouse(), but the calling function will just execute the returned function.Alternatively, if you want a "pool" of the last unprocessed events, interfaceMouse() could just feed a FIFO with function pointers to treat the events. The caller could execute interfaceMouse() once, and then process as many events as possible from the FIFO.
machielo
Correction to the alternative in previous comment: the function that processes the events will not call interfaceMouse(), he will just process as many events as possible from the FIFO, when you want to treat them. interfaceMouse() gets called in the game loop normally to feed the FIFO of events.
machielo
+1  A: 

From your description, I've made a couple of assumptions, but here is how I would handle it:

interfaceMouse() determines what needs to occur from the mouse interaction, but does not process that actual interaction (or, processes the interaction but wants to explicitly return what was done). What I would do is have interfaceMouse() return a struct defined similar to:

struct 
{
    int event_type; /* Enumerated type */
    void* element; /* Pointer to the element, however it's defined in the code */
    void* event_data; /* Pointer to the data about the event */
 } event_result;

You can define the calls by accessing the element based on type (say, another struct with function pointers, or have event_data hold the function pointer and element be passed to it, etc.).

C is one of those languages where it requires a lot more thought of your data modeling, but can end up being as elegant or convoluted as you'd like.

Matt
+2  A: 

Use a correct programming model should work. for example below is a Staged Programming Model:

     +-------------------------------+
     | Stage():                      |
     |    +-OnButtonAClick(Event)    |
     |    +-OnMenuSelect(Event)      |
     |    +-...                      |
     +------+-------------+----------+
            | Pop()       |
            |             |
          +-V-+         +-V-+ 
          | Q |         | Q |     ...           Logic-Half
   -------| 0 |         | 1 |-----------------------------
          |   |         |   |                     I/O-Half
          +-^-+         +-^-+
            | Push()      |
       +----+---+     +---+----+  
       |Event of|     |Event of|  ...
       |  Mouse |     |  Menu  |
       +--------+     +--------+

All OnXXX() will be executed in Stage() sequentially; and your data was attached to each event accordingly from bottom and goes upwards. Every event was driven and handled asynchronously.

See my comments in another thread:http://stackoverflow.com/questions/1606952/viewing-data-in-a-circular-buffer-in-real-time for the details about Staged.

EffoStaff Effo