How do you simulate a scrolling effect?

Started by
15 comments, last by thyrgle 13 years, 9 months ago
x and y are the current location of the mouse. And mouseX and mouseY serve as an origin. So it can tell me how much to move by.
Advertisement
basing this of what I think your trying to do, it works: give it a test in a new console window and see if you cant figure out where your going wrong:

#include <Windows.h>#include <iostream>POINT mouse, old;void MoveMouse( ){	static bool g = false;	if (::GetAsyncKeyState(VK_LBUTTON) & 0x8000)	{		::GetCursorPos(&mouse);		if (!g)		{			old = mouse;			g = true;		}		else		{			int cx, cy;			// cx = old.x - mouse.x; woops wrong way round			// cy = old.y - mouse.y;			cx = mouse.x - old.x;			cy = mouse.y - old.y;			std::cout << "Mouse = (" << cx << ", " << cy << ")" << std::endl;			system("cls");		}	}	else	{		g = false;		std::cout << "Mouse = (0, 0)" << std::endl;		system("cls");	}}int main(int argc, char** argv){	while (true)		MoveMouse( );	return 0;}


Almost forgot, your on a MAC (sucks lol), just replace the windows specific stuff with whatever APIs you are using.

::GetAsyncKeyState - is just what is used to find out what key is being held down (in this case I test for VK_LBUTTON which is the left mouse button)

::GetCursorPos - just fills a point structure with the current absolute coordinates of the mouse cursor.

POINT is just a simple struct like this:
typedef struct _POINT {
INT x, y;
} POINT, *LPPOINT;

And im not sure, but I dont think system("cls") is portable. try it though
I think the way your doing it, it should be reversed.

x - mouseX;y - mouseY;/* x = new position, mouseX = old position */also are your screen coordinates setup so zero is in the center? if so your probably going to need to translate them when you do the calculation, so that they run from 0 -> screenWidth and 0 -> screenHeight. else -x - -mouseX is going to cause undesired behavior.
[ dev journal ]
[ current projects' videos ]
[ Zolo Project ]
I'm not mean, I just like to get to the point.
@CodeCriminal:

Hi, thanks again but, first off what is the 0x8000 for? And also system("cls") does not work on Macs.

@Freeworld:

Thanks for mentioning that but, I am getting 0 either way... So there is another problem.
The 0x8000 is a bit mask that gets the most significant bit of the return code from GetAsyncKeyState which indicates that the button is being pressed.
It's not a bug... it's a feature!
Dom_152 how do I translate that into OpenGL then? Or does it work as is? I'm not good with bit masking. :(
I figured it out with pure OpenGL and if anyone wants to know here it is:

Function "mouseMovement" is passed as the "glutMouseFunc" paramater:

[source lang = "cpp"]void mouseMovement(int button, int state, int x, int y){	if (button == GLUT_LEFT_BUTTON && state == GLUT_DOWN)	{		printf("Here");		mouseX = x;		mouseY = y;		move = true;	}	if (state == GLUT_UP)	{		printf("Here1");		move = false;	}}


Function "scroll" is passed as a parameter to "glutMotionFunc":

[source lang = "cpp"]void scroll(int x, int y){	if (move == true)	{		cameraX = mouseX- x;		cameraY = y - mouseY;	}	glutPostRedisplay();}


Assume "move" is a global bool. And at the very begging of my drawing function passed as a paramter to "glutDisplayFunc" I have "glTranslatef(cameraX*0.1, cameraY*0.1, 0);".


Thanks for everyones help.

[Edited by - thyrgle on July 12, 2010 2:31:32 PM]

This topic is closed to new replies.

Advertisement