@@ -20,18 +20,7 @@ namespace HostApi {
2020 return JS_NewObject (ctx);
2121 }
2222
23- JSValue create_draw_render_object (JSContext* ctx) {
24- Utils::ScopedJSValue render2d_obj (ctx, JS_NewObject (ctx));
25-
26- // --- FPS Binding ---
27- JS_SetPropertyStr (ctx, render2d_obj, " drawFPS" , JS_NewCFunction (ctx, [](JSContext* c, JSValueConst, int argc, JSValueConst* argv) -> JSValue {
28- if (argc < 1 ) return JS_ThrowTypeError (c, " drawFPS requires a Vector2 position argument" );
29- const auto pos = Utils::get_opaque_or<JSVector2>(c, argv[0 ], js_vector2_class_id, {0 , 0 });
30- ::DrawFPS (static_cast <int >(pos.x), static_cast<int>(pos.y));
31- return JS_UNDEFINED ;
32- }, " drawFPS" , 1 ));
33-
34- // --- Shapes Sub-Object ---
23+ static JSValue create_shapes_object (JSContext* ctx) {
3524 Utils::ScopedJSValue shape_obj (ctx, JS_NewObject (ctx));
3625
3726 JS_SetPropertyStr (ctx, shape_obj, " drawPixel" , JS_NewCFunction (ctx, [](JSContext* c, JSValueConst, int argc, JSValueConst* argv) -> JSValue {
@@ -93,7 +82,56 @@ namespace HostApi {
9382 return JS_UNDEFINED ;
9483 }, " drawEllipse" , 4 ));
9584
96- JS_SetPropertyStr (ctx, render2d_obj, " shapes" , shape_obj.release ());
85+ return shape_obj.release ();
86+ }
87+
88+ static JSTextOptions parse_text_options (JSContext* ctx, JSValueConst optionsObj) {
89+ JSTextOptions options;
90+ if (JS_IsObject (optionsObj)) {
91+ Utils::try_get_opaque_property<JSFont>(ctx, optionsObj, " font" , js_font_class_id, options.font );
92+ Utils::try_get_opaque_property<JSColor>(ctx, optionsObj, " color" , js_color_class_id, options.color );
93+ Utils::try_get_float_property (ctx, optionsObj, " rotation" , options.rotation );
94+ Utils::try_get_float_property (ctx, optionsObj, " fontSize" , options.fontSize );
95+ Utils::try_get_float_property (ctx, optionsObj, " spacing" , options.spacing );
96+ Utils::try_get_opaque_property<JSVector2>(ctx, optionsObj, " origin" , js_vector2_class_id, options.origin );
97+ }
98+ return options;
99+ }
100+
101+ static JSValue create_text_object (JSContext* ctx) {
102+ Utils::ScopedJSValue text_obj (ctx, JS_NewObject (ctx));
103+ JS_SetPropertyStr (ctx, text_obj, " drawText" , JS_NewCFunction (ctx, [](JSContext* c, JSValueConst, int argc, JSValueConst* argv) -> JSValue {
104+ if (argc < 2 ) return JS_ThrowTypeError (c, " drawText requires position and text string arguments" );
105+ const auto pos = Utils::get_opaque_or<JSVector2>(c, argv[0 ], js_vector2_class_id, {0 , 0 });
106+
107+ const std::string txt_str = Utils::js_to_std_string (c, argv[1 ]);
108+ JSTextOptions options = parse_text_options (c, argc > 2 ? argv[2 ] : JS_UNDEFINED );
109+ Font fontToUse = GetFontDefault ();
110+ if (options.font .font_ptr && options.font .font_ptr ->texture .id != 0 ) {
111+ fontToUse = *options.font .font_ptr ;
112+ }
113+ ::DrawTextPro (fontToUse, txt_str.c_str(), pos, options.origin, options.rotation, options.fontSize, options.spacing, options.color);
114+ return JS_UNDEFINED ;
115+ }, " drawText" , 3 ));
116+
117+ return text_obj.release ();
118+ }
119+
120+ JSValue create_draw_render_object (JSContext* ctx) {
121+
122+ Utils::ScopedJSValue render2d_obj (ctx, JS_NewObject (ctx));
123+
124+ // Add FPS
125+ JS_SetPropertyStr (ctx, render2d_obj, " drawFPS" , JS_NewCFunction (ctx, [](JSContext* c, JSValueConst, int argc, JSValueConst* argv) -> JSValue {
126+ if (argc < 1 ) return JS_ThrowTypeError (c, " drawFPS requires a Vector2 position argument" );
127+ const auto pos = Utils::get_opaque_or<JSVector2>(c, argv[0 ], js_vector2_class_id, {0 , 0 });
128+ ::DrawFPS (static_cast <int >(pos.x), static_cast<int>(pos.y));
129+ return JS_UNDEFINED ;
130+ }, " drawFPS" , 1 ));
131+
132+ // Add Sub Objects
133+ JS_SetPropertyStr (ctx, render2d_obj, " shapes" , create_shapes_object (ctx));
134+ JS_SetPropertyStr (ctx, render2d_obj, " text" , create_text_object (ctx));
97135
98136 // --- Layer Wrappers ---
99137 const JSValue render_obj = JS_NewObject (ctx);
0 commit comments