From de028fc056b26e03ee00324888f870f64e28c756 Mon Sep 17 00:00:00 2001 From: bigfoot547 Date: Fri, 5 May 2017 15:07:36 -0500 Subject: [CSM] Add camera API (#5609) * [CSM] Add camera API roper rebase & squash * Address nerzhul's review --- src/script/lua_api/l_camera.cpp | 206 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 206 insertions(+) create mode 100644 src/script/lua_api/l_camera.cpp (limited to 'src/script/lua_api/l_camera.cpp') diff --git a/src/script/lua_api/l_camera.cpp b/src/script/lua_api/l_camera.cpp new file mode 100644 index 000000000..e6229ffe8 --- /dev/null +++ b/src/script/lua_api/l_camera.cpp @@ -0,0 +1,206 @@ +#include "script/common/c_converter.h" +#include "l_camera.h" +#include "l_internal.h" +#include "content_cao.h" +#include "camera.h" + +LuaCamera::LuaCamera(Camera *m) +{ + m_camera = m; +} + +void LuaCamera::create(lua_State *L, Camera *m) +{ + LuaCamera *o = new LuaCamera(m); + *(void **) (lua_newuserdata(L, sizeof(void *))) = o; + luaL_getmetatable(L, className); + lua_setmetatable(L, -2); + + int camera_object = lua_gettop(L); + + lua_getglobal(L, "core"); + luaL_checktype(L, -1, LUA_TTABLE); + int coretable = lua_gettop(L); + + lua_pushvalue(L, camera_object); + lua_setfield(L, coretable, "camera"); +} + +int LuaCamera::l_set_camera_mode(lua_State *L) +{ + Camera *camera = getobject(L, 1); + GenericCAO *playercao = getClient(L)->getEnv().getLocalPlayer()->getCAO(); + if (!camera) + return 0; + sanity_check(playercao); + if (!lua_isnumber(L, 2)) + return 0; + + camera->setCameraMode((CameraMode)((int)lua_tonumber(L, 2))); + playercao->setVisible(camera->getCameraMode() > CAMERA_MODE_FIRST); + playercao->setChildrenVisible(camera->getCameraMode() > CAMERA_MODE_FIRST); + return 0; +} + +int LuaCamera::l_get_camera_mode(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + lua_pushnumber(L, (int)camera->getCameraMode()); + + return 1; +} + +int LuaCamera::l_get_fov(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + lua_newtable(L); + lua_pushnumber(L, camera->getFovX() * core::DEGTORAD); + lua_setfield(L, -2, "x"); + lua_pushnumber(L, camera->getFovY() * core::DEGTORAD); + lua_setfield(L, -2, "y"); + lua_pushnumber(L, camera->getCameraNode()->getFOV() * core::RADTODEG); + lua_setfield(L, -2, "actual"); + lua_pushnumber(L, camera->getFovMax() * core::RADTODEG); + lua_setfield(L, -2, "max"); + return 1; +} + +int LuaCamera::l_get_pos(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + push_v3f(L, camera->getPosition()); + return 1; +} + +int LuaCamera::l_get_offset(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + push_v3s16(L, camera->getOffset()); + return 1; +} + +int LuaCamera::l_get_look_dir(lua_State *L) +{ + LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer(); + sanity_check(player); + + float pitch = -1.0 * player->getPitch() * core::DEGTORAD; + float yaw = (player->getYaw() + 90.) * core::DEGTORAD; + v3f v(cos(pitch) * cos(yaw), sin(pitch), cos(pitch) * sin(yaw)); + + push_v3f(L, v); + return 1; +} + +int LuaCamera::l_get_look_horizontal(lua_State *L) +{ + LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer(); + sanity_check(player); + + lua_pushnumber(L, (player->getYaw() + 90.) * core::DEGTORAD); + return 1; +} + +int LuaCamera::l_get_look_vertical(lua_State *L) +{ + LocalPlayer *player = getClient(L)->getEnv().getLocalPlayer(); + sanity_check(player); + + lua_pushnumber(L, -1.0 * player->getPitch() * core::DEGTORAD); + return 1; +} + +int LuaCamera::l_get_aspect_ratio(lua_State *L) +{ + Camera *camera = getobject(L, 1); + if (!camera) + return 0; + + lua_pushnumber(L, camera->getCameraNode()->getAspectRatio()); + return 1; +} + +LuaCamera *LuaCamera::checkobject(lua_State *L, int narg) +{ + luaL_checktype(L, narg, LUA_TUSERDATA); + + void *ud = luaL_checkudata(L, narg, className); + if (!ud) + luaL_typerror(L, narg, className); + + return *(LuaCamera **) ud; +} + +Camera *LuaCamera::getobject(LuaCamera *ref) +{ + return ref->m_camera; +} + +Camera *LuaCamera::getobject(lua_State *L, int narg) +{ + LuaCamera *ref = checkobject(L, narg); + assert(ref); + Camera *camera = getobject(ref); + if (!camera) + return NULL; + return camera; +} + +int LuaCamera::gc_object(lua_State *L) +{ + LuaCamera *o = *(LuaCamera **) (lua_touserdata(L, 1)); + delete o; + return 0; +} + +void LuaCamera::Register(lua_State *L) +{ + lua_newtable(L); + int methodtable = lua_gettop(L); + luaL_newmetatable(L, className); + int metatable = lua_gettop(L); + + lua_pushliteral(L, "__metatable"); + lua_pushvalue(L, methodtable); + lua_settable(L, metatable); + + lua_pushliteral(L, "__index"); + lua_pushvalue(L, methodtable); + lua_settable(L, metatable); + + lua_pushliteral(L, "__gc"); + lua_pushcfunction(L, gc_object); + lua_settable(L, metatable); + + lua_pop(L, 1); + + luaL_openlib(L, 0, methods, 0); + lua_pop(L, 1); +} + +const char LuaCamera::className[] = "Camera"; +const luaL_reg LuaCamera::methods[] = { + luamethod(LuaCamera, set_camera_mode), + luamethod(LuaCamera, get_camera_mode), + luamethod(LuaCamera, get_fov), + luamethod(LuaCamera, get_pos), + luamethod(LuaCamera, get_offset), + luamethod(LuaCamera, get_look_dir), + luamethod(LuaCamera, get_look_vertical), + luamethod(LuaCamera, get_look_horizontal), + luamethod(LuaCamera, get_aspect_ratio), + + {0, 0} +}; -- cgit v1.2.3 From a024042bf5ad487685e952da7b96ffa845cd7731 Mon Sep 17 00:00:00 2001 From: Loic Blot Date: Sat, 6 May 2017 21:30:27 +0200 Subject: Fix codestyle since CSM Camera API --- src/script/lua_api/l_camera.cpp | 62 +++++++++++++++++++---------------------- src/script/lua_api/l_camera.h | 26 ++++++++--------- 2 files changed, 41 insertions(+), 47 deletions(-) (limited to 'src/script/lua_api/l_camera.cpp') diff --git a/src/script/lua_api/l_camera.cpp b/src/script/lua_api/l_camera.cpp index e6229ffe8..0bdf4907b 100644 --- a/src/script/lua_api/l_camera.cpp +++ b/src/script/lua_api/l_camera.cpp @@ -12,16 +12,16 @@ LuaCamera::LuaCamera(Camera *m) void LuaCamera::create(lua_State *L, Camera *m) { LuaCamera *o = new LuaCamera(m); - *(void **) (lua_newuserdata(L, sizeof(void *))) = o; + *(void **)(lua_newuserdata(L, sizeof(void *))) = o; luaL_getmetatable(L, className); lua_setmetatable(L, -2); - + int camera_object = lua_gettop(L); - + lua_getglobal(L, "core"); luaL_checktype(L, -1, LUA_TTABLE); int coretable = lua_gettop(L); - + lua_pushvalue(L, camera_object); lua_setfield(L, coretable, "camera"); } @@ -35,7 +35,7 @@ int LuaCamera::l_set_camera_mode(lua_State *L) sanity_check(playercao); if (!lua_isnumber(L, 2)) return 0; - + camera->setCameraMode((CameraMode)((int)lua_tonumber(L, 2))); playercao->setVisible(camera->getCameraMode() > CAMERA_MODE_FIRST); playercao->setChildrenVisible(camera->getCameraMode() > CAMERA_MODE_FIRST); @@ -47,9 +47,9 @@ int LuaCamera::l_get_camera_mode(lua_State *L) Camera *camera = getobject(L, 1); if (!camera) return 0; - + lua_pushnumber(L, (int)camera->getCameraMode()); - + return 1; } @@ -58,7 +58,7 @@ int LuaCamera::l_get_fov(lua_State *L) Camera *camera = getobject(L, 1); if (!camera) return 0; - + lua_newtable(L); lua_pushnumber(L, camera->getFovX() * core::DEGTORAD); lua_setfield(L, -2, "x"); @@ -76,7 +76,7 @@ int LuaCamera::l_get_pos(lua_State *L) Camera *camera = getobject(L, 1); if (!camera) return 0; - + push_v3f(L, camera->getPosition()); return 1; } @@ -86,7 +86,7 @@ int LuaCamera::l_get_offset(lua_State *L) Camera *camera = getobject(L, 1); if (!camera) return 0; - + push_v3s16(L, camera->getOffset()); return 1; } @@ -127,7 +127,7 @@ int LuaCamera::l_get_aspect_ratio(lua_State *L) Camera *camera = getobject(L, 1); if (!camera) return 0; - + lua_pushnumber(L, camera->getCameraNode()->getAspectRatio()); return 1; } @@ -135,12 +135,12 @@ int LuaCamera::l_get_aspect_ratio(lua_State *L) LuaCamera *LuaCamera::checkobject(lua_State *L, int narg) { luaL_checktype(L, narg, LUA_TUSERDATA); - + void *ud = luaL_checkudata(L, narg, className); if (!ud) luaL_typerror(L, narg, className); - - return *(LuaCamera **) ud; + + return *(LuaCamera **)ud; } Camera *LuaCamera::getobject(LuaCamera *ref) @@ -160,7 +160,7 @@ Camera *LuaCamera::getobject(lua_State *L, int narg) int LuaCamera::gc_object(lua_State *L) { - LuaCamera *o = *(LuaCamera **) (lua_touserdata(L, 1)); + LuaCamera *o = *(LuaCamera **)(lua_touserdata(L, 1)); delete o; return 0; } @@ -171,36 +171,32 @@ void LuaCamera::Register(lua_State *L) int methodtable = lua_gettop(L); luaL_newmetatable(L, className); int metatable = lua_gettop(L); - + lua_pushliteral(L, "__metatable"); lua_pushvalue(L, methodtable); lua_settable(L, metatable); - + lua_pushliteral(L, "__index"); lua_pushvalue(L, methodtable); lua_settable(L, metatable); - + lua_pushliteral(L, "__gc"); lua_pushcfunction(L, gc_object); lua_settable(L, metatable); - + lua_pop(L, 1); - + luaL_openlib(L, 0, methods, 0); lua_pop(L, 1); } const char LuaCamera::className[] = "Camera"; -const luaL_reg LuaCamera::methods[] = { - luamethod(LuaCamera, set_camera_mode), - luamethod(LuaCamera, get_camera_mode), - luamethod(LuaCamera, get_fov), - luamethod(LuaCamera, get_pos), - luamethod(LuaCamera, get_offset), - luamethod(LuaCamera, get_look_dir), - luamethod(LuaCamera, get_look_vertical), - luamethod(LuaCamera, get_look_horizontal), - luamethod(LuaCamera, get_aspect_ratio), - - {0, 0} -}; +const luaL_reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode), + luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov), + luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset), + luamethod(LuaCamera, get_look_dir), + luamethod(LuaCamera, get_look_vertical), + luamethod(LuaCamera, get_look_horizontal), + luamethod(LuaCamera, get_aspect_ratio), + + {0, 0}}; diff --git a/src/script/lua_api/l_camera.h b/src/script/lua_api/l_camera.h index 82ab6a47f..04921ad03 100644 --- a/src/script/lua_api/l_camera.h +++ b/src/script/lua_api/l_camera.h @@ -5,42 +5,40 @@ class Camera; -class LuaCamera : public ModApiBase { -private: - +class LuaCamera : public ModApiBase +{ +private: static const char className[]; static const luaL_Reg methods[]; - + // garbage collector static int gc_object(lua_State *L); - + static int l_set_camera_mode(lua_State *L); static int l_get_camera_mode(lua_State *L); - + static int l_get_fov(lua_State *L); - + static int l_get_pos(lua_State *L); static int l_get_offset(lua_State *L); static int l_get_look_dir(lua_State *L); static int l_get_look_vertical(lua_State *L); static int l_get_look_horizontal(lua_State *L); static int l_get_aspect_ratio(lua_State *L); - + Camera *m_camera; - -public: +public: LuaCamera(Camera *m); ~LuaCamera() {} - + static void create(lua_State *L, Camera *m); - + static LuaCamera *checkobject(lua_State *L, int narg); static Camera *getobject(LuaCamera *ref); static Camera *getobject(lua_State *L, int narg); - + static void Register(lua_State *L); - }; #endif // L_CAMERA_H -- cgit v1.2.3 From d0678948165768472fc940c03e78cba787f49ea5 Mon Sep 17 00:00:00 2001 From: T0ny2 Date: Sun, 7 May 2017 00:37:37 +0100 Subject: Replace occurrence of luaL_reg in l_camera.cpp Related to commit 41c5483. Replace an occurrence of luaL_reg in src/script/lua_api/l_camera.cpp (added by commit de028fc). --- src/script/lua_api/l_camera.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'src/script/lua_api/l_camera.cpp') diff --git a/src/script/lua_api/l_camera.cpp b/src/script/lua_api/l_camera.cpp index 0bdf4907b..862384198 100644 --- a/src/script/lua_api/l_camera.cpp +++ b/src/script/lua_api/l_camera.cpp @@ -191,7 +191,7 @@ void LuaCamera::Register(lua_State *L) } const char LuaCamera::className[] = "Camera"; -const luaL_reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode), +const luaL_Reg LuaCamera::methods[] = {luamethod(LuaCamera, set_camera_mode), luamethod(LuaCamera, get_camera_mode), luamethod(LuaCamera, get_fov), luamethod(LuaCamera, get_pos), luamethod(LuaCamera, get_offset), luamethod(LuaCamera, get_look_dir), -- cgit v1.2.3