Could someone please help me moving a screen?

I have been working to fix this problem for a long time but I still don't know how to fix it. I have asked this question on Stackoverflow but there was no answer...

This is my problem, I have a window that I created with GLFW and it is undecorated window. It means there is no title border.

My problem is that I want to move this undecorated window but I can't figure out what the problem is...I tried so many different method to solve this but none of them worked successfully...


This is my code...


1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
#include <stdio.h>
#include <math.h>
#include <GL/gl.h>
#include "include/GLFW/glfw3.h"

double c_x;
double c_y;
int cp_x;
int cp_y;
int wrel_cpx;
int wrel_cpy;
int w_posx;
int w_posy;


int main(){
	glfwInit();
	glfwWindowHint(GLFW_DECORATED, 0);
	GLFWwindow *window = glfwCreateWindow(640, 480, "Undecorated Resizable", 0, 0);

	int lcp_x;
	int lcp_y;

	glfwMakeContextCurrent(window);


	while(!glfwWindowShouldClose(window)){
		int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);

		glfwGetWindowPos(window, &w_posx, &w_posy);

		if(state == GLFW_PRESS){
			glfwGetCursorPos(window, &c_x, &c_y);
			cp_x = floor(c_x);
			cp_y = floor(c_y);

			wrel_cpx = w_posx + cp_x;
			wrel_cpy = w_posy + cp_y;
		}

		if(state == GLFW_PRESS){
			if(cp_x != lcp_x || cp_y != lcp_y){
				printf("%d \n", cp_x);
				glfwSetWindowPos(window, wrel_cpx - lcp_x, wrel_cpy - lcp_y);
			}else{
				printf("Nothing \n");
			}
		}

		lcp_x = cp_x;
		lcp_y = cp_y;

		glfwSwapBuffers(window);

		glfwWaitEvents();
	}

	return 0;
}


When I run my program and when I drag my window, my window will keep teleport one position to another instantly...I have no idea why this happens and I have no clue how to fix this problem...

Could someone please help me?
Last edited on
I don't know GLFW, but this 'teleport' usually means that you add (lines 37/38) absolute coordinates. What you need are the relative coordinates from the previous call of glfwGetCursorPos(...). That would the true wrel_... coordinates.

Somthing like this:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
int main(){
	glfwInit();
	glfwWindowHint(GLFW_DECORATED, 0);
	GLFWwindow *window = glfwCreateWindow(640, 480, "Undecorated Resizable", 0, 0);

	glfwGetCursorPos(window, &c_x, &c_y);
	int lcp_x = floor(c_x);
	int lcp_y = floor(c_y);

	glfwMakeContextCurrent(window);


	while(!glfwWindowShouldClose(window)){
		int state = glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);

			glfwGetCursorPos(window, &c_x, &c_y);
			cp_x = floor(c_x);
			cp_y = floor(c_y);


		if(state == GLFW_PRESS){
			if(cp_x != lcp_x || cp_y != lcp_y){
			wrel_cpx = cp_x - lcp_x;
			wrel_cpy = cp_y - lcp_y;
				printf("%d \n", cp_x);
				glfwGetWindowPos(window, &w_posx, &w_posy);
				glfwSetWindowPos(window, w_posx + wrel_cpx, w_posy + wrel_cpy);
			}else{
				printf("Nothing \n");
			}
		}

		lcp_x = cp_x;
		lcp_y = cp_y;

		glfwSwapBuffers(window);

		glfwWaitEvents();
	}

	return 0;
}
See: http://s4.postimg.org/l327q8ffx/window_reposition.png

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
#include <stdio.h>
#include <math.h>

#include <GLFW/glfw3.h>

int main() {
    glfwInit();
    glfwWindowHint(GLFW_DECORATED, 0);
    GLFWwindow *window = glfwCreateWindow(640, 480, "Undecorated Resizable", 0, 0);
    glfwMakeContextCurrent(window);


    int win_x, win_y;
    int mouse_x, mouse_y; 
    bool dragging = false;

    while (!glfwWindowShouldClose(window)) {
        bool left_button_pressed = GLFW_PRESS == glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_LEFT);

        if (!left_button_pressed)
            dragging = false;
        else
        {
            if (!dragging)
            {
                // When we start dragging, we need to set us up the data.
                dragging = true;
                glfwGetWindowPos(window, &win_x, &win_y);

                double m_x, m_y;
                glfwGetCursorPos(window, &m_x, &m_y);
                mouse_x = int(m_x), mouse_y = int(m_y);
            }
            else        // We're already dragging, so we must reposition
            {
                double m_x, m_y;
                glfwGetCursorPos(window, &m_x, &m_y);

                win_x = win_x + int(m_x) - mouse_x;
                win_y = win_y + int(m_y) - mouse_y;

                glfwSetWindowPos(window, win_x, win_y);
            }
        }

        glfwSwapBuffers(window);
        glfwWaitEvents();
    }
}
Topic archived. No new replies allowed.