Improve mouse logic
This commit is contained in:
parent
8881e597f0
commit
067b52a159
|
|
@ -9,12 +9,12 @@
|
||||||
*
|
*
|
||||||
* Example originally created with raylib 2.5, last time updated with raylib 4.0
|
* Example originally created with raylib 2.5, last time updated with raylib 4.0
|
||||||
*
|
*
|
||||||
* Example contributed by eggmund (@eggmund) and reviewed by Ramon Santamaria (@raysan5)
|
* Example contributed by Josh Colclough (@joshcol9232) and reviewed by Ramon Santamaria (@raysan5)
|
||||||
*
|
*
|
||||||
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
|
||||||
* BSD-like license that allows static linking with closed source software
|
* BSD-like license that allows static linking with closed source software
|
||||||
*
|
*
|
||||||
* Copyright (c) 2019-2023 eggmund (@eggmund) and Ramon Santamaria (@raysan5)
|
* Copyright (c) 2019-2023 Josh Colclough (@joshcol9232) and Ramon Santamaria (@raysan5)
|
||||||
*
|
*
|
||||||
********************************************************************************************/
|
********************************************************************************************/
|
||||||
|
|
||||||
|
|
@ -46,8 +46,8 @@ int main(void)
|
||||||
//--------------------------------------------------------------------------------------
|
//--------------------------------------------------------------------------------------
|
||||||
const int screenWidth = 800;
|
const int screenWidth = 800;
|
||||||
const int screenHeight = 450;
|
const int screenHeight = 450;
|
||||||
|
const float zoomSpeed = 1.005f;
|
||||||
|
|
||||||
//SetConfigFlags(FLAG_WINDOW_HIGHDPI);
|
|
||||||
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia sets");
|
InitWindow(screenWidth, screenHeight, "raylib [shaders] example - julia sets");
|
||||||
|
|
||||||
// Load julia set shader
|
// Load julia set shader
|
||||||
|
|
@ -64,8 +64,6 @@ int main(void)
|
||||||
float offset[2] = { -(float)GetScreenWidth()/2, -(float)GetScreenHeight()/2 };
|
float offset[2] = { -(float)GetScreenWidth()/2, -(float)GetScreenHeight()/2 };
|
||||||
float zoom = 1.0f;
|
float zoom = 1.0f;
|
||||||
|
|
||||||
Vector2 offsetSpeed = { 0.0f, 0.0f };
|
|
||||||
|
|
||||||
// Get variable (uniform) locations on the shader to connect with the program
|
// Get variable (uniform) locations on the shader to connect with the program
|
||||||
// NOTE: If uniform variable could not be found in the shader, function returns -1
|
// NOTE: If uniform variable could not be found in the shader, function returns -1
|
||||||
int cLoc = GetShaderLocation(shader, "c");
|
int cLoc = GetShaderLocation(shader, "c");
|
||||||
|
|
@ -97,6 +95,7 @@ int main(void)
|
||||||
{
|
{
|
||||||
if (IsKeyPressed(k))
|
if (IsKeyPressed(k))
|
||||||
{
|
{
|
||||||
|
// If this key is pressed, set the point of interest of the corresponding key.
|
||||||
c[0] = pointsOfInterest[k - KEY_ONE][0];
|
c[0] = pointsOfInterest[k - KEY_ONE][0];
|
||||||
c[1] = pointsOfInterest[k - KEY_ONE][1];
|
c[1] = pointsOfInterest[k - KEY_ONE][1];
|
||||||
SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
|
SetShaderValue(shader, cLoc, c, SHADER_UNIFORM_VEC2);
|
||||||
|
|
@ -112,30 +111,28 @@ int main(void)
|
||||||
if (IsKeyPressed(KEY_RIGHT)) incrementSpeed++;
|
if (IsKeyPressed(KEY_RIGHT)) incrementSpeed++;
|
||||||
else if (IsKeyPressed(KEY_LEFT)) incrementSpeed--;
|
else if (IsKeyPressed(KEY_LEFT)) incrementSpeed--;
|
||||||
|
|
||||||
|
// If either left or right button is pressed, zoom in/out.
|
||||||
|
const bool leftMouseButtonDown = IsMouseButtonDown(MOUSE_BUTTON_LEFT);
|
||||||
// TODO: The idea is to zoom and move around with the mouse
|
if (leftMouseButtonDown ^ IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
|
||||||
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT) || IsMouseButtonDown(MOUSE_BUTTON_RIGHT))
|
|
||||||
{
|
{
|
||||||
if (IsMouseButtonDown(MOUSE_BUTTON_LEFT)) zoom += zoom * 0.003f;
|
// Change zoom. If Mouse left -> zoom in. Mouse right -> zoom out.
|
||||||
if (IsMouseButtonDown(MOUSE_BUTTON_RIGHT)) zoom -= zoom * 0.003f;
|
zoom *= leftMouseButtonDown ? zoomSpeed : 1.0f / zoomSpeed;
|
||||||
|
|
||||||
const Vector2 mousePos = GetMousePosition();
|
const Vector2 mousePos = GetMousePosition();
|
||||||
|
Vector2 offsetVelocity;
|
||||||
|
offsetVelocity.x = zoom * (mousePos.x - (float)screenWidth / 2.0);
|
||||||
|
offsetVelocity.y = zoom * (mousePos.y - (float)screenHeight / 2.0);
|
||||||
|
|
||||||
offsetSpeed.x = zoom * (mousePos.x - (float)screenWidth / 2.0);
|
// Apply move velocity to camera
|
||||||
offsetSpeed.y = zoom * (mousePos.y - (float)screenHeight / 2.0);
|
offset[0] += GetFrameTime() * offsetVelocity.x;
|
||||||
|
offset[1] += GetFrameTime() * offsetVelocity.y;
|
||||||
// Slowly move camera to targetOffset
|
|
||||||
offset[0] += GetFrameTime() * offsetSpeed.x;
|
|
||||||
offset[1] += GetFrameTime() * offsetSpeed.y;
|
|
||||||
}
|
}
|
||||||
else offsetSpeed = (Vector2){ 0.0f, 0.0f };
|
|
||||||
|
|
||||||
SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
|
SetShaderValue(shader, zoomLoc, &zoom, SHADER_UNIFORM_FLOAT);
|
||||||
SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
|
SetShaderValue(shader, offsetLoc, offset, SHADER_UNIFORM_VEC2);
|
||||||
|
|
||||||
// Increment c value with time
|
// Increment c value with time
|
||||||
float amount = GetFrameTime()*incrementSpeed*0.0005f;
|
float amount = GetFrameTime() * incrementSpeed * 0.0005f;
|
||||||
c[0] += amount;
|
c[0] += amount;
|
||||||
c[1] += amount;
|
c[1] += amount;
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue
Block a user