textures_srcrec_dstrec.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119
  1. /*******************************************************************************************
  2. *
  3. * raylib [core] example - Texture source and destination rectangles (adapted for HTML5 platform)
  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. #if defined(PLATFORM_WEB)
  13. #include <emscripten/emscripten.h>
  14. #endif
  15. //----------------------------------------------------------------------------------
  16. // Global Variables Definition
  17. //----------------------------------------------------------------------------------
  18. int screenWidth = 800;
  19. int screenHeight = 450;
  20. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  21. Texture2D scarfy;
  22. int frameWidth;
  23. int frameHeight;
  24. Rectangle sourceRec;
  25. Rectangle destRec;
  26. Vector2 origin;
  27. int rotation = 0;
  28. //----------------------------------------------------------------------------------
  29. // Module Functions Declaration
  30. //----------------------------------------------------------------------------------
  31. void UpdateDrawFrame(void); // Update and Draw one frame
  32. //----------------------------------------------------------------------------------
  33. // Main Enry Point
  34. //----------------------------------------------------------------------------------
  35. int main()
  36. {
  37. // Initialization
  38. //--------------------------------------------------------------------------------------
  39. InitWindow(screenWidth, screenHeight, "raylib [textures] examples - texture source and destination rectangles");
  40. scarfy = LoadTexture("resources/scarfy.png"); // Texture loading
  41. frameWidth = scarfy.width/6;
  42. frameHeight = scarfy.height;
  43. // NOTE: On PLATFORM_WEB, NPOT textures support is limited
  44. // NOTE: Source rectangle (part of the texture to use for drawing)
  45. sourceRec = (Rectangle){ 0, 0, frameWidth, frameHeight };
  46. // NOTE: Destination rectangle (screen rectangle where drawing part of texture)
  47. destRec = (Rectangle){ screenWidth/2, screenHeight/2, frameWidth*2, frameHeight*2 };
  48. // NOTE: Origin of the texture (rotation/scale point), it's relative to destination rectangle size
  49. origin = (Vector2){ frameWidth, frameHeight };
  50. #if defined(PLATFORM_WEB)
  51. emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
  52. #else
  53. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  54. //--------------------------------------------------------------------------------------
  55. // Main game loop
  56. while (!WindowShouldClose()) // Detect window close button or ESC key
  57. {
  58. UpdateDrawFrame();
  59. }
  60. #endif
  61. // De-Initialization
  62. //--------------------------------------------------------------------------------------
  63. UnloadTexture(scarfy); // Texture unloading
  64. CloseWindow(); // Close window and OpenGL context
  65. //--------------------------------------------------------------------------------------
  66. return 0;
  67. }
  68. //----------------------------------------------------------------------------------
  69. // Module Functions Definition
  70. //----------------------------------------------------------------------------------
  71. void UpdateDrawFrame(void)
  72. {
  73. // Update
  74. //----------------------------------------------------------------------------------
  75. rotation++;
  76. //----------------------------------------------------------------------------------
  77. // Draw
  78. //----------------------------------------------------------------------------------
  79. BeginDrawing();
  80. ClearBackground(RAYWHITE);
  81. // NOTE: Using DrawTexturePro() we can easily rotate and scale the part of the texture we draw
  82. // sourceRec defines the part of the texture we use for drawing
  83. // destRec defines the rectangle where our texture part will fit (scaling it to fit)
  84. // origin defines the point of the texture used as reference for rotation and scaling
  85. // rotation defines the texture rotation (using origin as rotation point)
  86. DrawTexturePro(scarfy, sourceRec, destRec, origin, rotation, WHITE);
  87. DrawLine(destRec.x, 0, destRec.x, screenHeight, GRAY);
  88. DrawLine(0, destRec.y, screenWidth, destRec.y, GRAY);
  89. DrawText("(c) Scarfy sprite by Eiden Marsal", screenWidth - 200, screenHeight - 20, 10, GRAY);
  90. EndDrawing();
  91. //----------------------------------------------------------------------------------
  92. }