diff --git a/src/gl.c b/src/gl.c index aea90d3..f449db7 100644 --- a/src/gl.c +++ b/src/gl.c @@ -146,7 +146,7 @@ int heliGLIsIntegerClipping() { return TRUE; } -int heliGLBackTransform(double *x, double *y) { +int heliGLTransform(double *x, double *y, int reverse) { double proj[16] = {}; double model[16] = {}; double viewproj[16] = {}; @@ -158,7 +158,7 @@ int heliGLBackTransform(double *x, double *y) { { *x, *y, 1, 1 } }; double vv[2][4] = {}; *x = 0; *y = 0; - + int rect[4] = {}; // x, y, w, h glGetIntegerv(GL_VIEWPORT, rect); double hw = 0.5*rect[2], hh = 0.5*rect[3]; @@ -173,9 +173,15 @@ int heliGLBackTransform(double *x, double *y) { heliMatrix4Mult(viewproj, view, proj); heliMatrix4Mult(viewmodel, viewproj, model); - if (!heliMatrix4Invert(inv, viewmodel)) return FALSE; - heliMatrix4MultVec(vv[0], inv, v[0]); - heliMatrix4MultVec(vv[1], inv, v[1]); + if (reverse) { + if (!heliMatrix4Invert(inv, viewmodel)) + return FALSE; + heliMatrix4MultVec(vv[0], inv, v[0]); + heliMatrix4MultVec(vv[1], inv, v[1]); + } else { + heliMatrix4MultVec(vv[0], viewmodel, v[0]); + heliMatrix4MultVec(vv[1], viewmodel, v[1]); + } double l = vv[0][2] - vv[1][2]; if (fabs(l) <= HELI_PRECISION) return FALSE; diff --git a/src/private.h b/src/private.h index 7fcfa66..e7301d7 100644 --- a/src/private.h +++ b/src/private.h @@ -172,7 +172,7 @@ extern unsigned int heliGLWindowFramebufferDrawId; double heliGLGetAAResolution(); double heliGLGetPixelSize(); int heliGLIsIntegerClipping(); -int heliGLBackTransform(double *x, double *y); +int heliGLTransform(double *x, double *y, int reverse); void heliGLGetCommonState(HeliGLCommonState *state, unsigned int flags); void heliGLSetCommonState(const HeliGLCommonState *state); diff --git a/src/window.c b/src/window.c index 333d4cf..8c4c945 100644 --- a/src/window.c +++ b/src/window.c @@ -229,16 +229,31 @@ double mouseY() double mouseTransformedX() { double x = mouseX(), y = mouseY(); - heliGLBackTransform(&x, &y); + heliGLTransform(&x, &y, TRUE); return x; } double mouseTransformedY() { double x = mouseX(), y = mouseY(); - heliGLBackTransform(&x, &y); + heliGLTransform(&x, &y, TRUE); return y; } +void mouseWarp(double x, double y) { + if (isnan(x)) x = 0; + if (isnan(y)) y = 0; + if (started && !stopped && window) + SDL_WarpMouseInWindow(window, (int)x, (int)y); + _mouseX = x; + _mouseY = y; +} + +void mouseTransformedWarp(double x, double y) { + heliGLTransform(&x, &y, FALSE); + mouseWarp(x, y); +} + + static void resize(int w, int h) { w = w > minWidth ? w : minWidth; h = h > minHeight ? h : minHeight; diff --git a/src/window.h b/src/window.h index ec90965..9fdfc14 100644 --- a/src/window.h +++ b/src/window.h @@ -39,9 +39,10 @@ int mouseScrolledX(); int mouseScrolledY(); double mouseX(); double mouseY(); - double mouseTransformedX(); double mouseTransformedY(); +void mouseWarp(double x, double y); +void mouseTransformedWarp(double x, double y); void messageBox(const char *message); int questionBox(const char *question, const char *answer0, const char *answer1);