Created
March 28, 2021 17:18
-
-
Save JeffM2501/703728379eb6e9d51d33201d1a1fe05d to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/******************************************************************************************* | |
* | |
* raylib [2D] example - world space panning | |
* | |
* Welcome to raylib! | |
* | |
* To test examples, just press F6 and execute raylib_compile_execute script | |
* Note that compiled executable is placed in the same folder as .c file | |
* | |
* You can find all basic examples on C:\raylib\raylib\examples folder or | |
* raylib official webpage: www.raylib.com | |
* | |
* Enjoy using raylib. :) | |
* | |
* This example has been created using raylib 3.5 (www.raylib.com) | |
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details) | |
* | |
* Copyright (c) 2013-2016 Ramon Santamaria (@raysan5) | |
* | |
********************************************************************************************/ | |
#include "raylib.h" | |
#include "raymath.h" | |
int main(void) | |
{ | |
// Initialization | |
//-------------------------------------------------------------------------------------- | |
const int screenWidth = 800; | |
const int screenHeight = 450; | |
InitWindow(screenWidth, screenHeight, "raylib [2d] example - world space panning"); | |
Camera2D cam = { 0 }; | |
cam.zoom = 1; | |
cam.offset.x = GetScreenWidth() / 2.0f; | |
cam.offset.y = GetScreenHeight() / 2.0f; | |
Vector2 prevMousePos = GetMousePosition(); | |
SetTargetFPS(60); // Set our game to run at 60 frames-per-second | |
//-------------------------------------------------------------------------------------- | |
// Main game loop | |
while (!WindowShouldClose()) // Detect window close button or ESC key | |
{ | |
float mouseDelta = GetMouseWheelMove(); | |
float newZoom = cam.zoom + mouseDelta * 0.01f; | |
if (newZoom <= 0) | |
newZoom = 0.01f; | |
cam.zoom = newZoom; | |
Vector2 thisPos = GetMousePosition(); | |
Vector2 delta = Vector2Subtract(prevMousePos, thisPos); | |
prevMousePos = thisPos; | |
if (IsMouseButtonDown(0)) | |
{ | |
Vector2 centerScreenWorld = GetScreenToWorld2D(Vector2Add((Vector2) { GetScreenWidth() / 2.0f, GetScreenHeight() / 2.0f }, delta),cam); | |
cam.target = centerScreenWorld; | |
} | |
if (IsKeyPressed(KEY_LEFT)) | |
cam.rotation += 10; | |
else if (IsKeyPressed(KEY_RIGHT)) | |
cam.rotation -= 10; | |
BeginDrawing(); | |
ClearBackground(RAYWHITE); | |
BeginMode2D(cam); | |
float size = 5000; | |
for (float i = -size; i <= size; i += 10) | |
{ | |
DrawLine(i, -size, i, size, GRAY); | |
DrawLine(-size, i, size, i, GRAY); | |
} | |
DrawLine(-size, 0, size, 0, RED); | |
DrawLine(0, -size, 0, size, RED); | |
EndMode2D(); | |
DrawText(TextFormat("%f %f", delta.x, delta.y),10, 10, 20, BLACK); | |
EndDrawing(); | |
//---------------------------------------------------------------------------------- | |
} | |
// De-Initialization | |
//-------------------------------------------------------------------------------------- | |
CloseWindow(); // Close window and OpenGL context | |
//-------------------------------------------------------------------------------------- | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment