I'm stuck trying to make my window dragable.
Here is my code,
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include "include/GLFW/glfw3.h"
void cursor_position_callback(GLFWwindow* window, double x, double y);
void mouse_button_callback(GLFWwindow *window, int button, int action, int mods);
int cp_x;
int cp_y;
int wrel_cpx;
int wrel_cpy;
int w_posx;
int w_posy;
int buttonEvent;
int main(){
glfwInit();
glfwWindowHint(GLFW_DECORATED, 0);
GLFWwindow *window = glfwCreateWindow(640, 480, "Undecorated Resizable", 0, 0);
int w_width;
int w_height;
int ccp_x;
int ccp_y;
glfwSetCursorPosCallback(window, cursor_position_callback);
glfwSetMouseButtonCallback(window, mouse_button_callback);
glfwMakeContextCurrent(window);
while(!glfwWindowShouldClose(window)){
if(buttonEvent == 1){
glfwSetWindowPos(window, wrel_cpx - cp_x, wrel_cpy - cp_y);
}
glfwSwapBuffers(window);
glfwWaitEvents();
}
return 0;
}
void cursor_position_callback(GLFWwindow* window, double x, double y){
glfwGetWindowPos(window, &w_posx, &w_posy);
wrel_cpx = cp_x + w_posx;
wrel_cpy = cp_y + w_posy;
cp_x = floor(x);
cp_y = floor(y);
}
void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
buttonEvent = 1;
}
if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
buttonEvent = 0;
}
}
When I run my program and try to drag the window, the position of my window will remain its position and does absolutely nothing.
If I change wrel_cpx - cp_x
to something like wrel_cpx + cp_x
, my window moves crazy.
Can someone please help me?