diff --git a/core/Input.cpp b/core/Input.cpp new file mode 100644 index 0000000..4e2756e --- /dev/null +++ b/core/Input.cpp @@ -0,0 +1,28 @@ +#include "Input.h" + +Input::Input() {} + +Input *Input::instance = nullptr; + +Input *Input::GetInstance() { + if (!instance) { + instance = new Input(); + } + return instance; +} + +void Input::Use(Window *window){ + glfwSetKeyCallback(window, (GLFWkeyfun)HandleKeys); +} + +void Input::HandleKeys(Window *window, int keys, int code, int action, int mods) { +#ifdef LUA_SCRIPTING + lua_State *L = (lua_State*) glfwGetWindowUserPointer(window); + lua_getglobal(L, "onkey"); + lua_pushinteger(L, keys); + lua_pushinteger(L, code); + lua_pushinteger(L, action); + lua_pushinteger(L, mods); + lua_call(L, 4, 0); +#endif // LUA_SCRIPTING +} diff --git a/core/Input.h b/core/Input.h new file mode 100644 index 0000000..d5459bd --- /dev/null +++ b/core/Input.h @@ -0,0 +1,22 @@ +#include +#include + +#ifdef LUA_SCRIPTING +extern "C" { +#include +#include +#include +} +#endif // LUA_SCRIPTING + +#include "types.h" + +class Input { +public: + static Input *GetInstance(); + void Use(Window *window); +private: + Input(); + static void HandleKeys(Window *window, int key, int code, int action, int mods); + static Input *instance; +}; diff --git a/core/couch.cpp b/core/couch.cpp index a42717e..7e08cad 100644 --- a/core/couch.cpp +++ b/core/couch.cpp @@ -20,6 +20,7 @@ extern "C" { #include "Shader.h" #include "Ball.h" #include "Camera.h" +#include "Input.h" Window *window; @@ -75,8 +76,12 @@ int main() { std::cerr << "Could not find main.lua" << std::endl; return 1; } + glfwSetWindowUserPointer(window, (void*) L); #endif // LUA_SCRIPTING + Input *input = Input::GetInstance(); + input->Use(window); + Camera defaultCamera; Shader shader("shaders/flat.vert", "shaders/flat.frag"); diff --git a/main.lua b/main.lua index b283af4..15a9364 100644 --- a/main.lua +++ b/main.lua @@ -1,14 +1,55 @@ local ball local camera +local LEFT = 263 +local RIGHT = 262 +local UP = 265 +local DOWN = 264 + +local vx = 0.0 +local vz = 0.0 + +local ballvy = -1.0 + function init() camera = couch.Camera() camera:MakeCurrent() ball = couch.Ball() ball:SetupMesh() couch.AddMeshToList(ball) + ball1 = couch.Ball() + ball1:SetupMesh() + couch.AddMeshToList(ball1) + + ball1.transform:Translate(0.0, 3.0, 0.0) end function update(delta) - camera.transform:Translate(0.0, 0.0, 1.0 * delta); + camera.transform:Translate(vx * delta, 0.0, vz * delta) + + local loc = ball1.transform.position + if loc.y > 4.0 then + ballvy = -1.0 + elseif loc.y < 2.0 then + ballvy = 1.0 + end + ball1.transform:Translate(0.0, ballvy * delta, 0.0) +end + +function onkey(key, code, action, mod) + if key == LEFT and action == 1 then + vx = -1.0 + elseif key == RIGHT and action == 1 then + vx = 1.0 + elseif (key == LEFT or key == RIGHT) and action == 0 then + vx = 0.0 + end + + if key == UP and action == 1 then + vz = -1.0 + elseif key == DOWN and action == 1 then + vz = 1.0 + elseif (key == DOWN or key == UP) and action == 0 then + vz = 0.0 + end end diff --git a/scripting/couch.i b/scripting/couch.i index d4ad312..fc0f8b0 100644 --- a/scripting/couch.i +++ b/scripting/couch.i @@ -2,19 +2,20 @@ %typemap(in) cfloat { $1 = (cfloat) lua_tonumber(L, $input); -} - + } %{ -#include "types.h" #include "Transform.h" #include "Mesh.h" #include "Ball.h" #include "Camera.h" extern void AddMeshToList(Mesh &mesh); %} -%include "types.h" +struct Vector3 { + double x, y, z; +}; %include "Transform.h" %include "Mesh.h" %include "Ball.h" %include "Camera.h" + extern void AddMeshToList(Mesh &mesh);