mirror of
https://github.com/glfw/glfw.git
synced 2024-11-25 22:14:34 +00:00
examples/boing: Add ball-follow-cursor functionality
Make it so that in the boing example, when the user clicks, the ball follows the mouse cursor.
This commit is contained in:
parent
7d373477ed
commit
819e148510
@ -44,6 +44,8 @@ void init( void );
|
|||||||
void display( void );
|
void display( void );
|
||||||
void reshape( GLFWwindow* window, int w, int h );
|
void reshape( GLFWwindow* window, int w, int h );
|
||||||
void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods );
|
void key_callback( GLFWwindow* window, int key, int scancode, int action, int mods );
|
||||||
|
void mouse_button_callback( GLFWwindow* window, int button, int action, int mods );
|
||||||
|
void cursor_position_callback( GLFWwindow* window, double x, double y );
|
||||||
void DrawBoingBall( void );
|
void DrawBoingBall( void );
|
||||||
void BounceBall( double dt );
|
void BounceBall( double dt );
|
||||||
void DrawBoingBallBand( GLfloat long_lo, GLfloat long_hi );
|
void DrawBoingBallBand( GLfloat long_lo, GLfloat long_hi );
|
||||||
@ -80,8 +82,12 @@ typedef enum { DRAW_BALL, DRAW_BALL_SHADOW } DRAW_BALL_ENUM;
|
|||||||
typedef struct {float x; float y; float z;} vertex_t;
|
typedef struct {float x; float y; float z;} vertex_t;
|
||||||
|
|
||||||
/* Global vars */
|
/* Global vars */
|
||||||
|
int width, height;
|
||||||
GLfloat deg_rot_y = 0.f;
|
GLfloat deg_rot_y = 0.f;
|
||||||
GLfloat deg_rot_y_inc = 2.f;
|
GLfloat deg_rot_y_inc = 2.f;
|
||||||
|
GLboolean override_pos = GL_FALSE;
|
||||||
|
GLfloat cursor_x = 0.f;
|
||||||
|
GLfloat cursor_y = 0.f;
|
||||||
GLfloat ball_x = -RADIUS;
|
GLfloat ball_x = -RADIUS;
|
||||||
GLfloat ball_y = -RADIUS;
|
GLfloat ball_y = -RADIUS;
|
||||||
GLfloat ball_x_inc = 1.f;
|
GLfloat ball_x_inc = 1.f;
|
||||||
@ -251,6 +257,37 @@ void key_callback( GLFWwindow* window, int key, int scancode, int action, int mo
|
|||||||
glfwSetWindowShouldClose(window, GL_TRUE);
|
glfwSetWindowShouldClose(window, GL_TRUE);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void set_ball_pos ( GLfloat x, GLfloat y )
|
||||||
|
{
|
||||||
|
ball_x = (width / 2) - x;
|
||||||
|
ball_y = y - (height / 2);
|
||||||
|
}
|
||||||
|
|
||||||
|
void mouse_button_callback( GLFWwindow* window, int button, int action, int mods )
|
||||||
|
{
|
||||||
|
if (button != GLFW_MOUSE_BUTTON_LEFT)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (action == GLFW_PRESS)
|
||||||
|
{
|
||||||
|
override_pos = GL_TRUE;
|
||||||
|
set_ball_pos(cursor_x, cursor_y);
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
override_pos = GL_FALSE;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
void cursor_position_callback( GLFWwindow* window, double x, double y )
|
||||||
|
{
|
||||||
|
cursor_x = x;
|
||||||
|
cursor_y = y;
|
||||||
|
|
||||||
|
if ( override_pos )
|
||||||
|
set_ball_pos(cursor_x, cursor_y);
|
||||||
|
}
|
||||||
|
|
||||||
/*****************************************************************************
|
/*****************************************************************************
|
||||||
* Draw the Boing ball.
|
* Draw the Boing ball.
|
||||||
*
|
*
|
||||||
@ -341,6 +378,9 @@ void BounceBall( double delta_t )
|
|||||||
GLfloat sign;
|
GLfloat sign;
|
||||||
GLfloat deg;
|
GLfloat deg;
|
||||||
|
|
||||||
|
if ( override_pos )
|
||||||
|
return;
|
||||||
|
|
||||||
/* Bounce on walls */
|
/* Bounce on walls */
|
||||||
if ( ball_x > (BOUNCE_WIDTH/2 + WALL_R_OFFSET ) )
|
if ( ball_x > (BOUNCE_WIDTH/2 + WALL_R_OFFSET ) )
|
||||||
{
|
{
|
||||||
@ -574,7 +614,6 @@ void DrawGrid( void )
|
|||||||
int main( void )
|
int main( void )
|
||||||
{
|
{
|
||||||
GLFWwindow* window;
|
GLFWwindow* window;
|
||||||
int width, height;
|
|
||||||
|
|
||||||
/* Init GLFW */
|
/* Init GLFW */
|
||||||
if( !glfwInit() )
|
if( !glfwInit() )
|
||||||
@ -591,6 +630,8 @@ int main( void )
|
|||||||
|
|
||||||
glfwSetFramebufferSizeCallback(window, reshape);
|
glfwSetFramebufferSizeCallback(window, reshape);
|
||||||
glfwSetKeyCallback(window, key_callback);
|
glfwSetKeyCallback(window, key_callback);
|
||||||
|
glfwSetMouseButtonCallback(window, mouse_button_callback);
|
||||||
|
glfwSetCursorPosCallback(window, cursor_position_callback);
|
||||||
|
|
||||||
glfwMakeContextCurrent(window);
|
glfwMakeContextCurrent(window);
|
||||||
glfwSwapInterval( 1 );
|
glfwSwapInterval( 1 );
|
||||||
|
Loading…
Reference in New Issue
Block a user