I'm in a situation where I already have a working, high-performance C++ program with GTK and OpenGL that renders data as a real-time graph. The data is saved as a report in a database which can be looked up at a later date from a browser. I now need to display those same graphs from the web-based reports (this wasn't in the original requirements).

I've tried generating the graphs from PHP, but drawing the whole thing with GD takes more than 20 seconds. I'm in the process of rewriting this in Javascript using canvas, which seems to be okay, although now I have to transfer all that data to the browser and parse it, which is still problematic.

But the real problem for me is that I'm actually writing the same program twice: in C++ with OpenGL and in Javascript with canvas.

I'm looking for general advice on how to "reuse" code between different platforms that support different languages, such as between a desktop environment and a web page. Are there tools that can help? Is there any way I can avoid maintaining two different codebases that do the same thing?

My ideas so far, none of which are particularly interesting:

  1. Reuse the code. Find a way of to actually share the code, such as using a C++ program as a CGI. This won't work if the language isn't supported by the platform (such as in a browser).

  2. Reuse the output: Write the program once, generate the output (such as an image) and make the other programs "viewers". This won't work if the output is too large to pass around.

  3. Translate the code: Use some automated tool to translate from one language (or a meta-language) to another. If such a tool exists, I could use recommendations.

  4. Reuse the bytecode: Use a language that compiles to bytecode that can run on various platforms. Apart from Java applets (which most people have disabled nowadays), I don't know of anything else that runs in browsers.

  5. Develop my own universal language: Ain't nobody got time for that.

  • I understand this may be too broad. I'm looking for personal experiences or common practices. – isanae Jun 26 '15 at 18:07
  • How complex are the graphics? On the Web side you'll probably want to look at WebGL, not canvas. But that would still be a re-write. – GrandmasterB Jun 26 '15 at 18:15
  • They are 2D graphs, with grids, several labels and lines. Rather simple, but I have to draw up to 48 of them realtime at 60fps from data received by network, hence C++ and OpenGL. – isanae Jun 26 '15 at 18:31

Sharing functionality between languages is a complex and difficult-to-solve problem, so I doubt you'll find a general solution. The desire to share code is a lot of the motivation behind everything from Node.js to Xamarin.Forms to Emscripten.

I'm not clear from your description where you want to process the data and where the data comes from. In our case, we did a variation of your #1: we modified our C++ plotting code to take an abstract PlotOutput class. For the desktop app, it used a local GUI implementation; for the web app, it ran server-side and used a PlotOutput implementation that turned the plot into Flot JSON for use by a web app. It's worked well for us, although we didn't need anywhere near 60fps. (Switching from JSON to JavaScript typed arrays would be one obvious source of performance improvement.)

Another possible solution is to use Emscripten to compile your C++ to JavaScript. Mozilla's blog has a good overview of porting a game to Emscripten.


If you already have the C++ code, I would err towards #2 (making the C++ a service that the client code calls into). If the output is too large, that generally means the data will be too large to send to the client to render. This approach also would let you have the client specify what size/resolution they actually need, so the server can not send the entire thing over the wire.

As for #3, I know there already exists a C to JavaScript compiler. Vile, I know... I don't know how well it will deal with ui elements, and I don't know if there's a good C++ analog/extension (or if your code is easily portable to C) - but it's maybe worth 15 minutes of google due diligence.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.