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

View File

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

View File

@ -14,5 +14,9 @@ extern glm::vec3 cameraLookAt;
extern glm::vec3 cameraUpward; extern glm::vec3 cameraUpward;
void init_scene(CommandLineOptions options); 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); void step_scene(double timeDelta);