Balls can dance
This commit is contained in:
		
							
								
								
									
										28
									
								
								core/Input.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										28
									
								
								core/Input.cpp
									
									
									
									
									
										Normal 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
									
								
							
							
						
						
									
										22
									
								
								core/Input.h
									
									
									
									
									
										Normal 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; | ||||||
|  | }; | ||||||
| @@ -20,6 +20,7 @@ extern "C" { | |||||||
| #include "Shader.h" | #include "Shader.h" | ||||||
| #include "Ball.h" | #include "Ball.h" | ||||||
| #include "Camera.h" | #include "Camera.h" | ||||||
|  | #include "Input.h" | ||||||
|  |  | ||||||
| Window *window; | Window *window; | ||||||
|  |  | ||||||
| @@ -75,8 +76,12 @@ int main() { | |||||||
|     std::cerr << "Could not find main.lua" << std::endl; |     std::cerr << "Could not find main.lua" << std::endl; | ||||||
|     return 1; |     return 1; | ||||||
|   } |   } | ||||||
|  |   glfwSetWindowUserPointer(window, (void*) L); | ||||||
| #endif // LUA_SCRIPTING | #endif // LUA_SCRIPTING | ||||||
|  |  | ||||||
|  |   Input *input = Input::GetInstance(); | ||||||
|  |   input->Use(window); | ||||||
|  |  | ||||||
|   Camera defaultCamera; |   Camera defaultCamera; | ||||||
|    |    | ||||||
|   Shader shader("shaders/flat.vert", "shaders/flat.frag"); |   Shader shader("shaders/flat.vert", "shaders/flat.frag"); | ||||||
|   | |||||||
							
								
								
									
										43
									
								
								main.lua
									
									
									
									
									
								
							
							
						
						
									
										43
									
								
								main.lua
									
									
									
									
									
								
							| @@ -1,14 +1,55 @@ | |||||||
| local ball | local ball | ||||||
| local camera | 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() | function init() | ||||||
|    camera = couch.Camera() |    camera = couch.Camera() | ||||||
|    camera:MakeCurrent() |    camera:MakeCurrent() | ||||||
|    ball = couch.Ball() |    ball = couch.Ball() | ||||||
|    ball:SetupMesh() |    ball:SetupMesh() | ||||||
|    couch.AddMeshToList(ball) |    couch.AddMeshToList(ball) | ||||||
|  |    ball1 = couch.Ball() | ||||||
|  |    ball1:SetupMesh() | ||||||
|  |    couch.AddMeshToList(ball1) | ||||||
|  |  | ||||||
|  |    ball1.transform:Translate(0.0, 3.0, 0.0) | ||||||
| end | end | ||||||
|  |  | ||||||
| function update(delta) | 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 | end | ||||||
|   | |||||||
| @@ -2,19 +2,20 @@ | |||||||
|  |  | ||||||
| %typemap(in) cfloat { | %typemap(in) cfloat { | ||||||
|   $1 = (cfloat) lua_tonumber(L, $input); |   $1 = (cfloat) lua_tonumber(L, $input); | ||||||
| } |  } | ||||||
|  |  | ||||||
| %{ | %{ | ||||||
| #include "types.h" |  | ||||||
| #include "Transform.h" | #include "Transform.h" | ||||||
| #include "Mesh.h" | #include "Mesh.h" | ||||||
| #include "Ball.h" | #include "Ball.h" | ||||||
| #include "Camera.h" | #include "Camera.h" | ||||||
| extern void AddMeshToList(Mesh &mesh); | extern void AddMeshToList(Mesh &mesh); | ||||||
| %} | %} | ||||||
| %include "types.h" | struct Vector3 { | ||||||
|  |   double x, y, z; | ||||||
|  | }; | ||||||
| %include "Transform.h" | %include "Transform.h" | ||||||
| %include "Mesh.h" | %include "Mesh.h" | ||||||
| %include "Ball.h" | %include "Ball.h" | ||||||
| %include "Camera.h" | %include "Camera.h" | ||||||
|  |  | ||||||
| extern void AddMeshToList(Mesh &mesh); | extern void AddMeshToList(Mesh &mesh); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Dane Johnson
					Dane Johnson