How does one resolve a native C++ enum in Haxe?

I’m creating an extern for SFML, just because I feel like it, and I am currently in the process of polling events. One of the members of the Event class is a type which takes the enum EventType.

I am trying to figure out a way for the Haxe compiler to identify that an event is of a given type, since SFML requires that you poll them like so:

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window: exit
            if (event.type == sf::Event::Closed)
                window.close();
        }

        // Clear screen
        window.clear();

        // Draw the sprite
        window.draw(sprite);

        // Draw the string
        window.draw(text);

        // Update the window
        window.display();
    }

The class reference for the sf::Event type can be found here, and my extern library here.

The EventType member is an enum, so I need to resolve it as an enum on Haxe-level code in order to use it properly. Anyone with extern mastery that can help?

Okay, so I’ve resolved the response issue and builds correctly using in the most recent commit:

I’ve made the Event type field an Integer type, and the EventType class at Haxe level is just a bunch of static variables linking to the enum values like so:

@:headerCode('#include "SFML/Window.hpp"')
class EventType {
    public static var CLOSED:Int        = untyped __cpp__("sf::Event::Closed");
}

However, when clicking on the Cross in the corner, the Window does not close as expected in the following example code:

import sfml.window.*;
import window.Event in NativeEvent;

@:buildXml('<include name="${haxelib:hxsfml}/../Build.xml" />')
class Main
{
    
    public static function main()
    {
        var window = Window.create(VideoMode.create(800, 600), "Test 01 - Blank Window");
        
        while (window.isOpen())
        {
            var event = NativeEvent.createEvent();
            if (window.pollEvent(event))
            {
                if (event.type == EventType.CLOSED)
                    window.close();
            }
            
            window.display();
        }
        
        return 0;
    }
    
}

Does anyone have any thoughts on this?

Try tracing event.type and EventType.CLOSED to see what the difference is.

Okay, I just tried it and getting some rather odd behaviour.

They both come back as 0 when tracing it, but upon doing so the application opens and closes immediately, so I’m assuming using Int type to resolve these enums is probably not the way to go.

Commenting the traces results in no immediate crash, but unable to close.

EDIT: tracing either event.type or EventType.CLOSED within the scope of the if statement to check if the event type matches results in nothing occurring, but tracing both causes an immediate close, which is really odd.

I may use pure C++ to do the event type checking for simplicity sake.

Hi @tienery.

I am resolved the problem with next example code:

@:include("includefile.h")
@:unreflective
@:native("MyEnum")
extern class NativeMyEnum { }

abstract MyEnum(Int) from Int to Int
{
	inline public function new(i:Int) { this = i; }

	@:to(NativeMyEnum)
	@:unreflective
	inline public function toNative() return untyped __cpp__("((MyEnum)({0}))", this);

	@:from(NativeMyEnum)
	@:unreflective
	inline static public function fromNative(value:NativeMyEnum) return new MyEnum(untyped value);

	public static var VALUE1(default, null) = new MyEnum(untyped __cpp__("VALUE1"));
	public static var VALUE2(default, null) = new MyEnum(untyped __cpp__("VALUE2"));
}

You can delete from Int to Int if you don’t use it like “flags”.
For example, var v:MyEnum = MyEnum.VALUE1 & MyEnum.VALUE2;.

Cheers!

I’ve already resolved this by simply just using Integers, since most enums just resolve to integers by the C++ compiler.

Thanks anyway, though.