textures_image_rotate.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Image Rotation
  4. *
  5. * Example originally created with raylib 1.0, last time updated with raylib 1.0
  6. *
  7. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  8. * BSD-like license that allows static linking with closed source software
  9. *
  10. * Copyright (c) 2014-2023 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. #define NUM_TEXTURES 3
  15. //------------------------------------------------------------------------------------
  16. // Program main entry point
  17. //------------------------------------------------------------------------------------
  18. int main(void)
  19. {
  20. // Initialization
  21. //--------------------------------------------------------------------------------------
  22. const int screenWidth = 800;
  23. const int screenHeight = 450;
  24. InitWindow(screenWidth, screenHeight, "raylib [textures] example - texture rotation");
  25. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  26. Image image45 = LoadImage("resources/raylib_logo.png");
  27. Image image90 = LoadImage("resources/raylib_logo.png");
  28. Image imageNeg90 = LoadImage("resources/raylib_logo.png");
  29. ImageRotate(&image45, 45);
  30. ImageRotate(&image90, 90);
  31. ImageRotate(&imageNeg90, -90);
  32. Texture2D textures[NUM_TEXTURES] = { 0 };
  33. textures[0] = LoadTextureFromImage(image45);
  34. textures[1] = LoadTextureFromImage(image90);
  35. textures[2] = LoadTextureFromImage(imageNeg90);
  36. int currentTexture = 0;
  37. //---------------------------------------------------------------------------------------
  38. // Main game loop
  39. while (!WindowShouldClose()) // Detect window close button or ESC key
  40. {
  41. // Update
  42. //----------------------------------------------------------------------------------
  43. if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT) || IsKeyPressed(KEY_RIGHT))
  44. {
  45. currentTexture = (currentTexture + 1)%NUM_TEXTURES; // Cycle between the textures
  46. }
  47. //----------------------------------------------------------------------------------
  48. // Draw
  49. //----------------------------------------------------------------------------------
  50. BeginDrawing();
  51. ClearBackground(RAYWHITE);
  52. DrawTexture(textures[currentTexture], screenWidth/2 - textures[currentTexture].width/2, screenHeight/2 - textures[currentTexture].height/2, WHITE);
  53. DrawText("Press LEFT MOUSE BUTTON to rotate the image clockwise", 250, 420, 10, DARKGRAY);
  54. EndDrawing();
  55. //----------------------------------------------------------------------------------
  56. }
  57. // De-Initialization
  58. //--------------------------------------------------------------------------------------
  59. for (int i = 0; i < NUM_TEXTURES; i++) UnloadTexture(textures[i]);
  60. CloseWindow(); // Close window and OpenGL context
  61. //--------------------------------------------------------------------------------------
  62. return 0;
  63. }