Mouse control
This commit is contained in:
		
							parent
							
								
									ca8a44a017
								
							
						
					
					
						commit
						35f43e9520
					
				| @ -7,12 +7,16 @@ Input *Input::instance = nullptr; | ||||
| Input *Input::GetInstance() { | ||||
|   if (!instance) { | ||||
|     instance = new Input(); | ||||
|     instance->lastx = 0.0; | ||||
|     instance->lasty = 0.0; | ||||
|   } | ||||
|   return instance; | ||||
| } | ||||
| 
 | ||||
| void Input::Use(Window *window){ | ||||
|   glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED); | ||||
|   glfwSetKeyCallback(window, (GLFWkeyfun)HandleKeys); | ||||
|   glfwSetCursorPosCallback(window, (GLFWcursorposfun)HandleMousePosition); | ||||
| } | ||||
| 
 | ||||
| void Input::HandleKeys(Window *window, int keys, int code, int action, int mods) { | ||||
| @ -26,3 +30,19 @@ void Input::HandleKeys(Window *window, int keys, int code, int action, int mods) | ||||
|   lua_call(L, 4, 0); | ||||
| #endif // LUA_SCRIPTING
 | ||||
| } | ||||
| 
 | ||||
| void Input::HandleMousePosition(Window *window, double xpos, double ypos) { | ||||
|   double relx = xpos - instance->lastx; | ||||
|   double rely = ypos - instance->lasty; | ||||
| #ifdef LUA_SCRIPTING | ||||
|   lua_State *L = (lua_State*) glfwGetWindowUserPointer(window); | ||||
|   lua_getglobal(L, "onmousemotion"); | ||||
|   lua_pushnumber(L, xpos); | ||||
|   lua_pushnumber(L, ypos); | ||||
|   lua_pushnumber(L, relx); | ||||
|   lua_pushnumber(L, rely); | ||||
|   lua_call(L, 4, 0); | ||||
| #endif // LUA_SCRIPTING
 | ||||
|   instance->lastx = xpos; | ||||
|   instance->lasty = ypos; | ||||
| } | ||||
|  | ||||
| @ -16,7 +16,9 @@ public: | ||||
|   static Input *GetInstance(); | ||||
|   void Use(Window *window); | ||||
| private: | ||||
|   double lastx, lasty; | ||||
|   Input(); | ||||
|   static void HandleKeys(Window *window, int key, int code, int action, int mods); | ||||
|   static void HandleMousePosition(Window *window, double xpos, double ypos); | ||||
|   static Input *instance; | ||||
| }; | ||||
|  | ||||
							
								
								
									
										12
									
								
								main.lua
									
									
									
									
									
								
							
							
						
						
									
										12
									
								
								main.lua
									
									
									
									
									
								
							| @ -13,6 +13,7 @@ local vz = 0.0 | ||||
| local ballvy = -1.0 | ||||
| 
 | ||||
| local cam_rot_x = 0.0 | ||||
| local cam_rot_y = 0.0 | ||||
| 
 | ||||
| function init() | ||||
|    camera = couch.Camera() | ||||
| @ -30,7 +31,10 @@ end | ||||
| 
 | ||||
| function update(delta) | ||||
|    camera.transform:Translate(vx * delta, 0.0, vz * delta) | ||||
|    camera.transform.rotation.y = camera.transform.rotation.y + cam_rot_x * delta | ||||
|    camera.transform.rotation.y = camera.transform.rotation.y - cam_rot_x * delta | ||||
|    cam_rot_x = 0.0 | ||||
|    camera.transform.rotation.x = camera.transform.rotation.x - cam_rot_y * delta | ||||
|    cam_rot_y = 0.0 | ||||
| 
 | ||||
|    local loc = ball1.transform.position | ||||
|    if loc.y > 4.0 then | ||||
| @ -46,7 +50,6 @@ function update(delta) | ||||
| end | ||||
| 
 | ||||
| function onkey(key, code, action, mod) | ||||
|    print(key, code, action, mod) | ||||
|    if key == LEFT and action == 1 then | ||||
|       vx = -1.0 | ||||
|    elseif key == RIGHT and action == 1 then | ||||
| @ -71,3 +74,8 @@ function onkey(key, code, action, mod) | ||||
|       end | ||||
|    end | ||||
| end | ||||
| 
 | ||||
| function onmousemotion(_, _, relx, rely) | ||||
|    cam_rot_x = relx | ||||
|    cam_rot_y = rely | ||||
| end | ||||
|  | ||||
		Loading…
	
		Reference in New Issue
	
	Block a user
	 Dane Johnson
						Dane Johnson