Prettify and embetter the mouse callback in scene.cpp

This commit is contained in:
Peder Bergebakken Sundt 2019-03-21 23:58:28 +01:00
parent 8801414e34
commit 8f7f0313f5
3 changed files with 28 additions and 27 deletions

View File

@ -22,29 +22,25 @@ typedef unsigned int uint;
sf::Sound* sound;
sf::SoundBuffer* buffer;
void mouseCallback(GLFWwindow* window, double x, double y) {
int windowWidth, windowHeight;
glfwGetWindowSize(window, &windowWidth, &windowHeight);
glViewport(0, 0, windowWidth, windowHeight);
void mouse_callback(GLFWwindow* window, double x, double y) {
static bool mouse_mode = false;
int winw, winh;
glfwGetWindowSize(window, &winw, &winh);
glViewport(0, 0, winw, winh);
double mx = (x - winw/2) / double(winh) * 2; // winh instead of winw, like the hudNode space
double my = (winh/2 - y) / double(winh) * 2;
mouse_position_cb(x, y, windowWidth, windowHeight);
bool reset_mouse = mouse_position_handler(mx, my, winh/2);
/*
if(padPositionX > 1) {
padPositionX = 1;
glfwSetCursorPos(window, windowWidth, y);
} else if(padPositionX < 0) {
padPositionX = 0;
glfwSetCursorPos(window, 0, y);
if (reset_mouse)
glfwSetCursorPos(window, winw/2, winh/2);
if (reset_mouse != mouse_mode) {
mouse_mode = reset_mouse;
glfwSetInputMode(window, GLFW_CURSOR, (reset_mouse)
? GLFW_CURSOR_DISABLED
: GLFW_CURSOR_NORMAL);
}
if(padPositionY > 1) {
padPositionY = 1;
glfwSetCursorPos(window, x, windowHeight);
} else if(padPositionY < 0) {
padPositionY = 0;
glfwSetCursorPos(window, x, 0);
}
*/
}
void initRenderer(GLFWwindow* window, CommandLineOptions options) {
@ -53,8 +49,7 @@ void initRenderer(GLFWwindow* window, CommandLineOptions options) {
return;
}
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
glfwSetCursorPosCallback(window, mouseCallback);
glfwSetCursorPosCallback(window, mouse_callback);
init_scene(options);
@ -85,7 +80,6 @@ void updateNodeTransformations(SceneNode* node, mat4 transformationThusFar, mat4
// step
void updateFrame(GLFWwindow* window, int windowWidth, int windowHeight) {
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
double timeDelta = getTimeDeltaSeconds();
float aspect = float(windowWidth) / float(windowHeight);

View File

@ -155,16 +155,19 @@ void init_scene(CommandLineOptions options) {
hudNode->children.push_back(textNode);
}
void mouse_position_cb(double x, double y, int winw, int winh) {
float mousePositionX = x / double(winw); // like the hudNode space
float mousePositionY = y / double(winh);
// returns true if mouse should be centered and invisible
bool mouse_position_handler(double mx, double my, int scale) {
//cout << mx << "\t" << my << endl;
return false;
}
void step_scene(double timeDelta) {
static double timeAcc = 0; // shrug
timeAcc += timeDelta;
cout << "td: " << timeDelta << " " << 1/timeDelta << endl;
plainNode->uvOffset.x += timeDelta*0.5;
plainNode->uvOffset.y -= timeDelta*0.5;
if (boxNode) boxNode->rotation.z += timeDelta;

View File

@ -14,5 +14,9 @@ extern glm::vec3 cameraLookAt;
extern glm::vec3 cameraUpward;
void init_scene(CommandLineOptions options);
void mouse_position_cb(double x, double y, int winw, int winh);
// same coords as hudNode, returns true if mouse should be disabled and centered.
// `scale` is window_height/2
bool mouse_position_handler(double mx, double my, int scale);
void step_scene(double timeDelta);