GLFW how to drag undecorated window?
Asked Answered
N

1

5

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?

Natala answered 10/10, 2015 at 9:52 Comment(0)
R
7

Maybe answer will be useful for someone. Correct code for drag of undecorated window:

#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#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 offset_cpx;
int offset_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){
            glfwGetWindowPos(window, &w_posx, &w_posy);
            glfwSetWindowPos(window, w_posx + offset_cpx, w_posy + offset_cpy);
            offset_cpx = 0;
            offset_cpy = 0;
            cp_x += offset_cpx;
            cp_y += offset_cpy;

        }

        glfwSwapBuffers(window);

        glfwWaitEvents();
     }

    return 0;
}

void cursor_position_callback(GLFWwindow* window, double x, double y){
    if (buttonEvent == 1) {
        offset_cpx = x - cp_x;
        offset_cpy = y - cp_y;
    }
}

void mouse_button_callback(GLFWwindow* window, int button, int action, int mods){
    if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS){
        buttonEvent = 1;
        double x, y;
        glfwGetCursorPos(window, &x, &y);
        cp_x = floor(x);
        cp_y = floor(y);
    }
    if(button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_RELEASE){
        buttonEvent = 0;
        cp_x = 0;
        cp_y = 0;
    }
}
Rident answered 21/1, 2016 at 9:40 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.