textures_polygon.c 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - Draw Textured Polygon
  4. *
  5. * Example originally created with raylib 3.7, last time updated with raylib 3.7
  6. *
  7. * Example contributed by Chris Camacho (@codifies) and reviewed by Ramon Santamaria (@raysan5)
  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) 2021-2022 Chris Camacho (@codifies) and Ramon Santamaria (@raysan5)
  13. *
  14. ********************************************************************************************/
  15. #include "raylib.h"
  16. #include "raymath.h"
  17. #define MAX_POINTS 11 // 10 points and back to the start
  18. //------------------------------------------------------------------------------------
  19. // Program main entry point
  20. //------------------------------------------------------------------------------------
  21. int main(void)
  22. {
  23. // Initialization
  24. //--------------------------------------------------------------------------------------
  25. const int screenWidth = 800;
  26. const int screenHeight = 450;
  27. InitWindow(screenWidth, screenHeight, "raylib [textures] example - textured polygon");
  28. // Define texture coordinates to map our texture to poly
  29. Vector2 texcoords[MAX_POINTS] = {
  30. (Vector2){ 0.75f, 0.0f },
  31. (Vector2){ 0.25f, 0.0f },
  32. (Vector2){ 0.0f, 0.5f },
  33. (Vector2){ 0.0f, 0.75f },
  34. (Vector2){ 0.25f, 1.0f},
  35. (Vector2){ 0.375f, 0.875f},
  36. (Vector2){ 0.625f, 0.875f},
  37. (Vector2){ 0.75f, 1.0f},
  38. (Vector2){ 1.0f, 0.75f},
  39. (Vector2){ 1.0f, 0.5f},
  40. (Vector2){ 0.75f, 0.0f} // Close the poly
  41. };
  42. // Define the base poly vertices from the UV's
  43. // NOTE: They can be specified in any other way
  44. Vector2 points[MAX_POINTS] = { 0 };
  45. for (int i = 0; i < MAX_POINTS; i++)
  46. {
  47. points[i].x = (texcoords[i].x - 0.5f)*256.0f;
  48. points[i].y = (texcoords[i].y - 0.5f)*256.0f;
  49. }
  50. // Define the vertices drawing position
  51. // NOTE: Initially same as points but updated every frame
  52. Vector2 positions[MAX_POINTS] = { 0 };
  53. for (int i = 0; i < MAX_POINTS; i++) positions[i] = points[i];
  54. // Load texture to be mapped to poly
  55. Texture texture = LoadTexture("resources/cat.png");
  56. float angle = 0.0f; // Rotation angle (in degrees)
  57. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  58. //--------------------------------------------------------------------------------------
  59. // Main game loop
  60. while (!WindowShouldClose()) // Detect window close button or ESC key
  61. {
  62. // Update
  63. //----------------------------------------------------------------------------------
  64. // Update points rotation with an angle transform
  65. // NOTE: Base points position are not modified
  66. angle++;
  67. for (int i = 0; i < MAX_POINTS; i++) positions[i] = Vector2Rotate(points[i], angle*DEG2RAD);
  68. //----------------------------------------------------------------------------------
  69. // Draw
  70. //----------------------------------------------------------------------------------
  71. BeginDrawing();
  72. ClearBackground(RAYWHITE);
  73. DrawText("textured polygon", 20, 20, 20, DARKGRAY);
  74. DrawTexturePoly(texture, (Vector2){ GetScreenWidth()/2, GetScreenHeight()/2 },
  75. positions, texcoords, MAX_POINTS, WHITE);
  76. EndDrawing();
  77. //----------------------------------------------------------------------------------
  78. }
  79. // De-Initialization
  80. //--------------------------------------------------------------------------------------
  81. UnloadTexture(texture); // Unload texture
  82. CloseWindow(); // Close window and OpenGL context
  83. //--------------------------------------------------------------------------------------
  84. return 0;
  85. }