Frontend and backend communication

Pelle pelle.mansson at gmail.com
Thu Jul 28 01:52:19 PDT 2011


On Wed, 27 Jul 2011 19:41:37 +0200, Dainius (GreatEmerald)  
<pastas4 at gmail.com> wrote:

> I have one program design problem and I wonder if anyone here could
> give any suggestions about it. The situation is like this: I am
> splitting a game into a frontend (a library) and a backend (an
> executable). The backend is going to handle all the game mechanics,
> while the frontend is going to handle I/O. But there are certain
> problems. For example, now I have a function Shuffle() that calls
> PlaySound(SHUFFLE). Shuffle() is a backend function, while PlaySound()
> is a frontend one, so obviously it won't work that way after the
> split. It would be ideal if there was a way to create hooks - say an
> empty PlaySound() function that the frontend could receive calls to.
> But I can't see a way to do that. Another way to do that that was
> suggested to me was to use an event loop - set a global variable, then
> have the frontend monitor it for changes and then respond as
> necessary, but that just isn't a very clean way to do it.
>
> And then there is the fact that the backend is going to be written in
> D (right now it's a mix of C and D), while the frontend will be in C
> (one of the frontends, anyway - the second one will also be in D). Any
> suggestions about this?

You could use a struct of function pointers to define the interface, if
you need it to work both in C and D.

extern (C) struct FrontEndFunctions {
     void function(sound) playSound;
     ...
}

backend does myFrontEndFunctions.playSound(SHUFFLE);

The front end:

void myPlaySound(...) { ... }

void main() {
     FrontEndFunctions functions;
     functions.playSound = &myPlaySound;
     ... etc for other functions ...;
     backend.initialize(functions);
     backend.run();
}


More information about the Digitalmars-d-learn mailing list