You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

86 lines
3.7 KiB

  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - Texture source and destination rectangles
  4. *
  5. * Example originally created with raylib 1.3, last time updated with raylib 1.3
  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) 2015-2024 Ramon Santamaria (@raysan5)
  11. *
  12. ********************************************************************************************/
  13. #include "raylib.h"
  14. //------------------------------------------------------------------------------------
  15. // Program main entry point
  16. //------------------------------------------------------------------------------------
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [textures] examples - texture source and destination rectangles");
  24. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  25. Texture2D scarfy = LoadTexture("resources/scarfy.png"); // Texture loading
  26. int frameWidth = scarfy.width/6;
  27. int frameHeight = scarfy.height;
  28. // Source rectangle (part of the texture to use for drawing)
  29. Rectangle sourceRec = { 0.0f, 0.0f, (float)frameWidth, (float)frameHeight };
  30. // Destination rectangle (screen rectangle where drawing part of texture)
  31. Rectangle destRec = { screenWidth/2.0f, screenHeight/2.0f, frameWidth*2.0f, frameHeight*2.0f };
  32. // Origin of the texture (rotation/scale point), it's relative to destination rectangle size
  33. Vector2 origin = { (float)frameWidth, (float)frameHeight };
  34. int rotation = 0;
  35. SetTargetFPS(60);
  36. //--------------------------------------------------------------------------------------
  37. // Main game loop
  38. while (!WindowShouldClose()) // Detect window close button or ESC key
  39. {
  40. // Update
  41. //----------------------------------------------------------------------------------
  42. rotation++;
  43. //----------------------------------------------------------------------------------
  44. // Draw
  45. //----------------------------------------------------------------------------------
  46. BeginDrawing();
  47. ClearBackground(RAYWHITE);
  48. // NOTE: Using DrawTexturePro() we can easily rotate and scale the part of the texture we draw
  49. // sourceRec defines the part of the texture we use for drawing
  50. // destRec defines the rectangle where our texture part will fit (scaling it to fit)
  51. // origin defines the point of the texture used as reference for rotation and scaling
  52. // rotation defines the texture rotation (using origin as rotation point)
  53. DrawTexturePro(scarfy, sourceRec, destRec, origin, (float)rotation, WHITE);
  54. DrawLine((int)destRec.x, 0, (int)destRec.x, screenHeight, GRAY);
  55. DrawLine(0, (int)destRec.y, screenWidth, (int)destRec.y, GRAY);
  56. DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);
  57. EndDrawing();
  58. //----------------------------------------------------------------------------------
  59. }
  60. // De-Initialization
  61. //--------------------------------------------------------------------------------------
  62. UnloadTexture(scarfy); // Texture unloading
  63. CloseWindow(); // Close window and OpenGL context
  64. //--------------------------------------------------------------------------------------
  65. return 0;
  66. }