Jump to page: 1 2 3
Thread overview
Frontend and backend communication
Jul 27, 2011
Nick Sabalausky
Jul 27, 2011
Nick Sabalausky
Jul 28, 2011
novice2
Jul 28, 2011
Kai Meyer
Jul 28, 2011
Kai Meyer
Jul 29, 2011
Jacob Carlborg
Jul 29, 2011
Jesse Phillips
Aug 10, 2011
Pelle
Nov 29, 2011
Jacob Carlborg
Jul 28, 2011
Pelle
Jul 28, 2011
novice2
July 27, 2011
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?
July 27, 2011
"Dainius (GreatEmerald)" <pastas4@gmail.com> wrote in message news:mailman.1931.1311788506.14074.digitalmars-d-learn@puremagic.com...
> 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.

Why not?


July 27, 2011
Hm, well, at least I don't know how it's possible for a binary to overwrite/capture a library's function. Would you care to give an example?
July 27, 2011
"Dainius (GreatEmerald)" <pastas4@gmail.com> wrote in message news:mailman.1933.1311797423.14074.digitalmars-d-learn@puremagic.com...
> Hm, well, at least I don't know how it's possible for a binary to overwrite/capture a library's function. Would you care to give an example?

I'm not sure what you mean "overwrite/capture". I thought you just needed to call a function in a library?


July 27, 2011
No no. It's the other way round. Shuffle() is in the library
(backend). PlaySound() is in the executable (frontend). Since I don't
want the library to be dependent on any sound libraries, I can't have
PlaySound() in it. And there is no other way that I can think of to
execute PlaySound() just at the end of Shuffle() without capturing
events (since Shuffle() itself is called by a whole lot of different
functions across the library, and not from the executable itself).
July 28, 2011
Dainius (GreatEmerald) Wrote:

> No no. It's the other way round. Shuffle() is in the library
> (backend). PlaySound() is in the executable (frontend). Since I don't
> want the library to be dependent on any sound libraries, I can't have

would you pass playSound() as parameter (callback) to shuffle()?
July 28, 2011
Hmm, there are still a whole lot of functions that call Shuffle(), so it might not be ideal. However, this gives me an idea - if a pointer to a function can be a parameter, can it be a global variable? In that case, the frontend would indeed be able to overwrite the function that the backend calls by simply altering that pointer.
July 28, 2011
On Wed, 27 Jul 2011 19:41:37 +0200, Dainius (GreatEmerald) <pastas4@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();
}
July 28, 2011
Pelle Wrote:

> On Wed, 27 Jul 2011 19:41:37 +0200, Dainius (GreatEmerald) You could use a struct of function pointers to define the interface, if

This is known approach in app, using plugin. For example, then open source FAR (File Archive Manager) exe load pluging dll, it fill strcuct with exe functions pointers, so plugin dll can use it
July 28, 2011
On Wed, 27 Jul 2011 13:41:37 -0400, Dainius (GreatEmerald) <pastas4@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.

Actually, an event loop is a good way to do it.  If your front end is a GUI, it's already likely running an event loop.  All you need to do is stick an event in the event queue.

I'd examine how to create custom events for your front end's probably already existing event loop.

-Steve
« First   ‹ Prev
1 2 3