textures_polygon.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - Draw Textured Polygon
  4. *
  5. * This example has been created using raylib 3.7 (www.raylib.com)
  6. * raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
  7. *
  8. * Example contributed by Chris Camacho (@codifies - bedroomcoders.co.uk) and
  9. * reviewed by Ramon Santamaria (@raysan5)
  10. *
  11. * Copyright (c) 2021 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
  12. *
  13. ********************************************************************************************/
  14. #include "raylib.h"
  15. #include "raymath.h"
  16. #define MAX_POINTS 11 // 10 points and back to the start
  17. int main(void)
  18. {
  19. // Initialization
  20. //--------------------------------------------------------------------------------------
  21. const int screenWidth = 800;
  22. const int screenHeight = 450;
  23. InitWindow(screenWidth, screenHeight, "raylib [textures] example - textured polygon");
  24. // Define texture coordinates to map our texture to poly
  25. Vector2 texcoords[MAX_POINTS] = {
  26. (Vector2){ 0.75f, 0.0f },
  27. (Vector2){ 0.25f, 0.0f },
  28. (Vector2){ 0.0f, 0.5f },
  29. (Vector2){ 0.0f, 0.75f },
  30. (Vector2){ 0.25f, 1.0f},
  31. (Vector2){ 0.375f, 0.875f},
  32. (Vector2){ 0.625f, 0.875f},
  33. (Vector2){ 0.75f, 1.0f},
  34. (Vector2){ 1.0f, 0.75f},
  35. (Vector2){ 1.0f, 0.5f},
  36. (Vector2){ 0.75f, 0.0f} // Close the poly
  37. };
  38. // Define the base poly vertices from the UV's
  39. // NOTE: They can be specified in any other way
  40. Vector2 points[MAX_POINTS] = { 0 };
  41. for (int i = 0; i < MAX_POINTS; i++)
  42. {
  43. points[i].x = (texcoords[i].x - 0.5f)*256.0f;
  44. points[i].y = (texcoords[i].y - 0.5f)*256.0f;
  45. }
  46. // Define the vertices drawing position
  47. // NOTE: Initially same as points but updated every frame
  48. Vector2 positions[MAX_POINTS] = { 0 };
  49. for (int i = 0; i < MAX_POINTS; i++) positions[i] = points[i];
  50. // Load texture to be mapped to poly
  51. Texture texture = LoadTexture("resources/cat.png");
  52. float angle = 0.0f; // Rotation angle (in degrees)
  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. // Update
  59. //----------------------------------------------------------------------------------
  60. // Update points rotation with an angle transform
  61. // NOTE: Base points position are not modified
  62. angle++;
  63. for (int i = 0; i < MAX_POINTS; i++) positions[i] = Vector2Rotate(points[i], angle*DEG2RAD);
  64. //----------------------------------------------------------------------------------
  65. // Draw
  66. //----------------------------------------------------------------------------------
  67. BeginDrawing();
  68. ClearBackground(RAYWHITE);
  69. DrawText("textured polygon", 20, 20, 20, DARKGRAY);
  70. DrawTexturePoly(texture, (Vector2){ GetScreenWidth()/2, GetScreenHeight()/2 },
  71. positions, texcoords, MAX_POINTS, WHITE);
  72. EndDrawing();
  73. //----------------------------------------------------------------------------------
  74. }
  75. // De-Initialization
  76. //--------------------------------------------------------------------------------------
  77. UnloadTexture(texture); // Unload texture
  78. CloseWindow(); // Close window and OpenGL context
  79. //--------------------------------------------------------------------------------------
  80. return 0;
  81. }