core_scissor_test.c 2.8 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Scissor test
  4. *
  5. * This example has been created using raylib 2.5 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Chris Dill (@MysteriousSpace) and reviewed by Ramon Santamaria (@raysan5)
  9. *
  10. * Copyright (c) 2019 Chris Dill (@MysteriousSpace)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. int main(void)
  15. {
  16. // Initialization
  17. //--------------------------------------------------------------------------------------
  18. const int screenWidth = 800;
  19. const int screenHeight = 450;
  20. InitWindow(screenWidth, screenHeight, "raylib [core] example - scissor test");
  21. Rectangle scissorArea = { 0, 0, 300, 300 };
  22. bool scissorMode = true;
  23. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  24. //--------------------------------------------------------------------------------------
  25. // Main game loop
  26. while (!WindowShouldClose()) // Detect window close button or ESC key
  27. {
  28. // Update
  29. //----------------------------------------------------------------------------------
  30. if (IsKeyPressed(KEY_S)) scissorMode = !scissorMode;
  31. // Centre the scissor area around the mouse position
  32. scissorArea.x = GetMouseX() - scissorArea.width/2;
  33. scissorArea.y = GetMouseY() - scissorArea.height/2;
  34. //----------------------------------------------------------------------------------
  35. // Draw
  36. //----------------------------------------------------------------------------------
  37. BeginDrawing();
  38. ClearBackground(RAYWHITE);
  39. if (scissorMode) BeginScissorMode((int)scissorArea.x, (int)scissorArea.y, (int)scissorArea.width, (int)scissorArea.height);
  40. // Draw full screen rectangle and some text
  41. // NOTE: Only part defined by scissor area will be rendered
  42. DrawRectangle(0, 0, GetScreenWidth(), GetScreenHeight(), RED);
  43. DrawText("Move the mouse around to reveal this text!", 190, 200, 20, LIGHTGRAY);
  44. if (scissorMode) EndScissorMode();
  45. DrawRectangleLinesEx(scissorArea, 1, BLACK);
  46. DrawText("Press S to toggle scissor test", 10, 10, 20, BLACK);
  47. EndDrawing();
  48. //----------------------------------------------------------------------------------
  49. }
  50. // De-Initialization
  51. //--------------------------------------------------------------------------------------
  52. CloseWindow(); // Close window and OpenGL context
  53. //--------------------------------------------------------------------------------------
  54. return 0;
  55. }