Hxcpp windows target:: need help with Pointers.

132 views
Skip to first unread message

Tom

unread,
Jan 31, 2015, 3:22:39 PM1/31/15
to haxe...@googlegroups.com
Hi!

I try to port the original example of NanoVG into Michael Bickel's hx-nanovg lib with Haxe, Hxcpp windows platform. It's going well, but...

There is a function in C++:
// Breaks the specified text into lines. If end is specified only the sub-string will be used.
// White space is stripped at the beginning of the rows, the text is split at word boundaries or when new-line characters are encountered.
// Words longer than the max width are slit at nearest character (i.e. no hyphenation).
int nvgTextBreakLines(NVGcontext* ctx, const char* string, const char* end, float breakRowWidth, NVGtextRow* rows, int maxRows);

// the used structure
struct NVGtextRow {
   
const char* start;    // Pointer to the input text where the row starts.
   
const char* end;    // Pointer to the input text where the row ends (one past the last character).
   
const char* next;    // Pointer to the beginning of the next row.
   
float width;        // Logical width of the row.
   
float minx, maxx;    // Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending.
};

... and in Nvg.hx the extern for this:

@:native("::nvgTextBreakLines")
public static function textBreakLines(_ctx:Pointer<NvgContext>, _string:ConstPointer<Char>, _end:ConstPointer<Char>, _breakRowWidth:Float32, _rows:Pointer<NvgTextRow>, _maxRows:Int):Int;

@:native("NVGtextRow")
extern class NvgTextRow {
   
/** Pointer to the input text where the row starts. **/
   
public var start:ConstPointer<Char>;
   
/** Pointer to the input text where the row ends (one past the last character). **/
   
public var end:ConstPointer<Char>;
   
/** Pointer to the beginning of the next row. **/
   
public var next:ConstPointer<Char>;
   
/** Logical width of the row. **/
   
public var width:Float32;
   
/** Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending. **/
   
public var minx:Float32;
   
/** Actual bounds of the row. Logical with and bounds can differ because of kerning and some parts over extending. **/
   
public var maxx:Float32;
}

I have a problem with the _rows:Pointer<NvgTextRow> variable, because the original code of the usage of this function is: (the comments are my interpretation, and maybe not correct)

NVGtextRow rows[3]; // create a 3 length array of NVGtextRow
nvgTextBreakLines
(vg, start, end, width, rows, 3); // this function sets the rows variable

I can't create in Haxe the appropriate rows variable.
I tried:

var rows:Array<NvgTextRow>; // This will be in cpp: Array<Dynamic> type.
Nvg.textBreakLines(vg, start, end, width, Pointer.fromHandle(rows), 3); // error: can't covert Dynamic to NVGtextRow

How can I do this in the right way and pass a pointer of 3 length array? Can it be done in Hxcpp?

Thanks in advance!
Tom









Michael Bickel

unread,
Jan 31, 2015, 5:18:28 PM1/31/15
to haxe...@googlegroups.com
Hi Tom,

It's a bit cunbersome but I think you can achieve this in the following way:

untyped __cpp__("NVGtextRow rows[3];");
Nvg.textBreakLines(vg, "f\nb", untyped null, 50, untyped __cpp__("rows"), 3);
var firstRow:NvgTextRow = untyped __cpp__("rows[0]");
trace(firstRow.width + " " + firstRow.minx + " " + firstRow.maxx);

Maybe Hugh can shed more light on the best practices for such a case.

- Michael 

Tom

unread,
Feb 1, 2015, 6:11:56 AM2/1/15
to haxe...@googlegroups.com
Hi Michael!

Thanks for this. It's working.
There was some trickier part (for me), but working. :)



There are some linecaps which are displaying wrong with NvgLineCap.SQUARE, but the others are working.

Tom

unread,
Feb 1, 2015, 6:13:22 AM2/1/15
to haxe...@googlegroups.com

The image for the previous post.

Michael Bickel

unread,
Feb 1, 2015, 3:09:16 PM2/1/15
to haxe...@googlegroups.com
Hi Tom, 

Thats very nice! Good work! :)

- Michael

Tom

unread,
Feb 1, 2015, 3:56:56 PM2/1/15
to haxe...@googlegroups.com
Thanks!
I created a pull request, so you could add these example to your hx-nanovg project.

By the way, there are some performance comparison how fast this library? Can we use it e.g. display, and zoom some heavy svg?

Hugh

unread,
Feb 1, 2015, 11:23:43 PM2/1/15
to haxe...@googlegroups.com
Yes,the "Arrays" are not really designed for holding structures, only pointers.  Maybe I could make an exception for externs, but it would be some work.
Other possible technique would be to "new" an native array into a pointer, and delete it when you are done.

Hugh
Reply all
Reply to author
Forward
0 new messages