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.

81 lines
3.4 KiB

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