COMMUNITY

Building types

Hello,

I recently discovered Haxe and I want to prototype something in it to see its capabilities. I like to do things myself and I am already used to working with OpenGL.

I want to create my own port for the OpenGL specification but I don’t have the desire to write every existing GL function or typedef by hand. Luckily OpenGL provides a spec XML file that I found out how to parse through a macro. I also found out how to create functions in an empty type by using @:build(…call to my macro…) . Where I am stuck at is adding the typedef’s for the OpenGL language. I wanto to be able to generate with a macro a typedef similar to this:

typedef GLenum = Int;

… but with a macro. I tried build macros and initialization macros but there is not enough documentation or articles on the subject of how to create custom types or classes.

You can create a type using Context.defineType.

For a typedef GLenum = Int; you’d do:

Context.defineType({
	name: "GLenum",
	pack: ["your", "package"],
	fields: [],
	pos: Context.currentPos(),
	kind: TDAlias(macro :Int),
});
1 Like

Wow, very nice! Thanks a lot. Do you also happen to know if I can get intellisence in VS Code for generated typedefs?

vscode should show intellisence for generated typedefs out of the box – curious to know if it’s not

Good luck with your project :slight_smile:

Some links in case they’re handy

For raw OpenGL usage:
GLFW haxe library: GitHub - Sunjammer/linc_glfw: GLFW bindings for Haxe
GL ES 2 API for hxcpp: GitHub - haxiomic/gluon: WebGL implementation for hxcpp
Hello-triangle demo of the above: https://github.com/haxiomic/gluon/files/4791480/gluon-demo.zip (haxe install build.hxml for libs then haxe build.hxml)

For cross-platform windowing, audio and GPUs APIs there’s at least:

1 Like

These are nice! More like I wanted to toy around with the macros and automatic type generation (from what I understand one of the strong points of Haxe).

I even consider to actually write a parser for C++ headers (using libclang) that would produce an intermediate json/xml document that haxe could then use for generating the glue code for calling the native methods.

1 Like

@object71 clang for automatic C++ headers would be soooo nice and a massive addition to the language

I took a look at this earlier in the year and it’s definitely achievable. The main challenge is just learning the clang API well enough. There’s a little thread about this here: Extern generator for C · Issue #15 · haxiomic/haxe-c-bridge · GitHub

I’m on contracts for the next few months so I likely won’t work on that again until later in the year

1 Like

Code completion works on generated types and fields the same way as normal code.

Well, I couldn’t get intellisense working. I tried vscode and haxe develop which are the two IDE environments I am willing to work with.

The compiler doesn’t complain and successfully replaces calls for example to GLint with whatever I put to be the alias. Its just that I don’t get any suggestions while typing or when I request using the ctrl + space shortcut.

I am pasting a link to minimum reproduction project where only typedefs from the gl.xml file are defined (all as strings)
https://drive.google.com/file/d/1QtrxN5fT_yutL_7JwhQaQ71nO2rGuT9g/view?usp=sharing

EDIT: Also I found out that it shows documentation if I set the doc field and hove over a typed field. I just don’t get the suggestion.

EDIT 2: If I generate a field for the OpenGLContext class (from the example) I do get intellisense for the field. I just wanted to metion that its wierd I don’t get it for typedefs.

1 Like

Your macro might be run too late, you should instead use an init macro, that way all of your types will be available as soon as the compilation starts, which should help with ide support.

Sadly that didn’t help (adding --macro opengl.macros.OpenGLTypeBuildMacro.buildTypes() to the .hxml and removing the call from the build() method)

EDIT: Seems the display server is skipping the macro:

Display position: d:\Projects\HaxeGL\src\opengl\OpenGLContext.hx: 0-0
 1,evl: skipping opengl.macros.OpenGLTypeBuildMacro

I don’t know then.

An alternative would be to generate haxe files from your xml ahead of time, instead of running the macro at each compilation. Less flexible but faster compilation.

Is there an easy way of doing this?

Don’t know of any, there are some utilities in haxe.macro.Printer - Haxe 4.2.1 API but I never used them.

Personally for more control I manually create the file string, which is more work, but you can get exactly what you want: extern-generator/Generate.macro.hx at main · HaxeGodot/extern-generator · GitHub