25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

88 lines
3.8 KiB

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