Drawing to a Native Window

Is it possible to draw OpenFl things to a native view from the operating system (i.e. HWND on windows, NSView* on Mac)?

I have Haxe code linked to a C++ application using hxcpp. The C++ side provides a window and I would like to use this window in Haxe and OpenFl. So, draw things on it, receive events from it, etc.
Is this possible?

Drawing to the window sure, I’ve seen rendering engine that can do that, if the window has an openfl context created the app can simple send commands.

But for events most likely not, the window would send events, but lime/openfl wouldn’t get them, and so can’t give them to the haxe code of your application.

Lime/Openfl use sdl to create a window and get events from it, so if the sdl can do that then it becomes possible I guess. But I’ve never heard of something like that.

Take a look at this sample:


The intention of OpenFL and Lime is to continue to become more modular, and to be easier to integrate into other projects. The standard rendering for native is OpenGL, so if it is given a valid context, it should work properly in many ways.

Are you wanting to do a different kind of rendering instead? Lime is designed to allow it to be fairly flexible for embedding, but real-world use-cases could really help polish/prove out any areas that could be developed to make this easier out of the box


Thanks for the example!

I’m using the WDL-OL framework to create plug-ins for audio software (like Ableton Live, Logic, …). The framework is in C and C++ (which can be very inconvenient), and it provides very little in terms of GUI widgets, asset handling, etc. This is why I’d like to use Haxe and OpenFL.
WDL-OL opens a window for you; and you can place elements onto the window. I’d like to do two things:

  1. Pass mouse/keyboard events from the WDL-OL window to Haxe
  2. Draw the OpenFL stage onto the WDL-OL window

About events: I place one element on the WDL-OL window. The element is similar to an InteractiveObject in openfl. It’s a C++ class with methods like OnMouseDown(int x, int y) etc., which get called whenever the user clicks on the element. Each of these methods can call Haxe code (maybe on an openfl.display.Stage?) which will dispatch the event to its children.

About rendering: I’m not sure how to do this. I can pass an HWND or NSView* pointer to Haxe, but there’s no OpenGL context (WDL-OL doesn’t use OpenGL).
WDL-OL expects the rendered pixels as a byte array. My current idea is: Call Haxe code to draw the stage to a BitmapData object (using BitmapData.draw()), and pass a pointer to its pixel ByteArray back to C++.

Also, right now I’m compiling with haxe and a custom Build.hxml file. I don’t use openfl build mac because it seems to do too much:

  • WDL-OL takes care of audio I/O, so I don’t want Haxe/Lime to create an AudioManager
  • WDL-OL opens a window, so I don’t want Haxe/Lime to open a window

I really want to make this work, and I’ve made good progress already getting things to link correctly etc. I think this could be great, because Haxe is a much more convenient language than C++, the Flash/OpenFL API is very good for the GUI stuff, and since HxCpp generates C++ code, we can even do the audio processing in Haxe with good performance.

If performance is not a huge concern, then I would try testing using an array of bytes. The Lime Image API should let you get/set pixels, etc, allowing some basic testing for pushing graphics back and forth.

The newer OpenFL code uses OpenGL, even for bitmapData.draw, though I’ve been debating whether we should support some standard C++ 2D rendering API, and what that would be. Cairo or DirectFB?

Here’s what I have right now:

class MainStage extends Stage {
    public function new(width:Int, height:Int) {
        super(width, height);
        init(RenderContext.OPENGL(new GLRenderContext()));
        bitmapData = new BitmapData(width, height);
    public function draw() : Void {
        // This throws an exception:
        bitmapData.draw(this, null, new ColorTransform(), BlendMode.NORMAL);

    private var bitmapData:BitmapData;

The draw() call gives a EXC_BAD_ACCESS in GLRenderer_obj::resize, which is trying to call ::lime::graphics::opengl::GL_obj::lime_gl_viewport.

It might be that the lime.ndll is not being loaded. There should be code in Lime/OpenFL projects to search for this in the same directory as the project.

It’s going to fail, though, if GL is not available, I would start, first, with a Lime project, see if it can talk to it’s native binary, then maybe try some basic image operations, first. If Lime works, then OpenFL is more likely to work above it

Could you manage to glue OpenFL with WDL-OL? If yes, how did you do it? I tried it also but seems to be tough to separate WDL-OL from lice. I would be very thankful if you can tell how you did it!