diff --git a/src/helpers.c b/src/helpers.c index 4a16040..3ed87d5 100644 --- a/src/helpers.c +++ b/src/helpers.c @@ -73,3 +73,16 @@ void engine_rendertarget_draw_file(char * fpath) { y += 1; } } + +void engine_position_2d_to_tex(struct ENGINE_VECTOR_TEX * pos, float x, float y, int width, int height, char clamp) { + width = width - 1; + height = height - 1; + pos->x = round(((float)width*0.5) + (((float)width*x) * 0.5)); + pos->y = round(((float)height*0.5) + (((float)height*y) * 0.5)); + if (clamp == 1) { + if (pos->x < 0) { pos->x = 0; } + if (pos->x > width) { pos->x = width; } + if (pos->y < 0) { pos->y = 0; } + if (pos->y > height) { pos->y = height; } + } +} diff --git a/src/lua_manual.c b/src/lua_manual.c index 7f15930..5d988fd 100644 --- a/src/lua_manual.c +++ b/src/lua_manual.c @@ -80,6 +80,19 @@ int engine_luaf_3d_project(lua_State *L) { return 2; } +int engine_luaf_position_2d_to_tex(lua_State *L) { + float x = luaL_checknumber(L,1); + float y = luaL_checknumber(L,2); + int width = luaL_checknumber(L,3); + int height = luaL_checknumber(L,4); + int clamp = luaL_checknumber(L,5); + struct ENGINE_VECTOR_TEX pos; + engine_position_2d_to_tex(&pos,x,y,width,height,clamp); + lua_pushnumber(L,pos.x); + lua_pushnumber(L,pos.y); + return 2; +} + void engine_lua_init_manual() { lua_pushcfunction(engine_lua_state,engine_luaf_lua_event_get_data); lua_setglobal (engine_lua_state,"engine_lua_event_get_data"); @@ -93,4 +106,6 @@ void engine_lua_init_manual() { lua_setglobal (engine_lua_state,"engine_3d_rotate_position"); lua_pushcfunction(engine_lua_state,engine_luaf_3d_project); lua_setglobal (engine_lua_state,"engine_3d_project"); + lua_pushcfunction(engine_lua_state,engine_luaf_position_2d_to_tex); + lua_setglobal (engine_lua_state,"engine_position_2d_to_tex"); } diff --git a/src/values/functions.toml b/src/values/functions.toml index 285c70e..8bfa9e9 100644 --- a/src/values/functions.toml +++ b/src/values/functions.toml @@ -89,6 +89,14 @@ arguments = ["char *"] argNames = ["fpath"] description = "Draw a file into the target." +# POSITION +[engine_position_2d_to_tex] +type = "void" +arguments = ["struct ENGINE_VECTOR_TEXTURE *","float","float","int", "int", "char"] +argNames = ["pos", "x", "y", "width","height","clamp"] +description = "Convert 2D coordinates (range -1 = top/left, 1 = bottom/right) to ENGINE_VECTOR_TEXTURE. If clamp is set to 1, coordinates may not be less than 0, or more than width-1/height-1. Output is put into pos pointer. Pointer not required in Lua." +lua = "manual" + # TIME [engine_time_sleep] type = "void"