Balls can dance

This commit is contained in:
Dane Johnson
2021-01-13 18:51:58 -06:00
parent 86f6203efc
commit 3c652884c2
5 changed files with 102 additions and 5 deletions

28
core/Input.cpp Normal file
View File

@@ -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
}

22
core/Input.h Normal file
View File

@@ -0,0 +1,22 @@
#include <GL/glew.h>
#include <GLFW/glfw3.h>
#ifdef LUA_SCRIPTING
extern "C" {
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
}
#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;
};

View File

@@ -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");