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

View File

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

View File

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