Hello, everyone! I'm new here so let me introduce myself.
My name is tienery, and I am a contributor to many Haxe projects, and currently I have embarked on making my own wrapper for SFML in the Haxe programming language. Now, before I go forward into explaining my problem, I am no expert with either C++ or Haxe, of course the reason I am here is to learn new things and to resolve my mistakes.
So now I shall talk about the issue I am currently having in Haxe. Because Haxe does not know how to resolve a native C++ enum in its own language, I need to do a comparison with events some other way. One of those ways were to create another C++ file that has some "helper" functions to deal with the abstraction between the `EventType` enum type in SFML, and returning an integer that identifies what event type it was.
Here is the full code of this helper class:
#include "SFML/Window.hpp"
namespace hx {
namespace sfml {
class Event {
public:
static sf::Event createEvent();
static int getEventType(sf::Event &event);
};
sf::Event Event::createEvent()
{
sf::Event event;
return event;
}
int Event::getEventType(sf::Event &event)
{
if (event.type == sf::Event::Closed)
return 1;
else if (event.type == sf::Event::Resized)
return 2;
else
return 0;
}
} //sfml
} //hx
I then make an extern for this helper class so that I can use it in the context of Haxe:
package window;
import cpp.Int32;
import sfml.window.Event in SFMLEvent;
@:include("hx/sfml/Event.cpp")
extern class EventHelper {
@:native("hx::sfml::Event::createEvent") public static function createEvent():SFMLEvent;
@:native("hx::sfml::Event::getEventType") public static function getEventType(event:SFMLEvent):Int32;
}
So the idea is to create an event by declaring it and returning the variable to the context of Haxe, so that we can use that variable to pollEvents in a window and get its type.
So the final test code is the following:
package;
import sfml.window.*;
import window.EventHelper;
@:buildXml('<include name="${haxelib:hxsfml}/../Build.xml" />')
class Main
{
public static function main()
{
var window:Window = Window.create(VideoMode.create(800, 600), "Test 01 - Blank Window");
while (window.isOpen())
{
var event = EventHelper.createEvent();
while (window.pollEvent(event))
{
if (EventHelper.getEventType(event) == 1) {
window.close();
}
}
window.display();
}
return 0;
}
}
For the full code, I have a public repository
on Github with the externs and wrappers. If you wish to test the code, you will need to download
Haxe and a library called hxcpp using the command:
haxelib install hxcpp
So what's the problem? The comparison doesn't work.
Where the if statement checks to identify what the event is, the window does not close. I even traced it in another example and it never returned 1, ultimately resulting in the window closing as it should.
Image result:
I just don't know how to resolve what is otherwise a very simple concept.
Any help would be greatly appreciated.