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.

96 lines
3.3 KiB

  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. Vector2 texcoords[MAX_POINTS] = {
  24. (Vector2){ 0.75f, 0.0f },
  25. (Vector2){ 0.25f, 0.0f },
  26. (Vector2){ 0.0f, 0.5f },
  27. (Vector2){ 0.0f, 0.75f },
  28. (Vector2){ 0.25f, 1.0f},
  29. (Vector2){ 0.375f, 0.875f},
  30. (Vector2){ 0.625f, 0.875f},
  31. (Vector2){ 0.75f, 1.0f},
  32. (Vector2){ 1.0f, 0.75f},
  33. (Vector2){ 1.0f, 0.5f},
  34. (Vector2){ 0.75f, 0.0f} // Close the poly
  35. };
  36. Vector2 points[MAX_POINTS] = { 0 };
  37. // Create the poly coords from the UV's
  38. // you don't have to do this you can specify
  39. // them however you want
  40. for (int i = 0; i < MAX_POINTS; i++)
  41. {
  42. points[i].x = (texcoords[i].x - 0.5f)*256.0f;
  43. points[i].y = (texcoords[i].y - 0.5f)*256.0f;
  44. }
  45. InitWindow(screenWidth, screenHeight, "raylib [textures] example - textured polygon");
  46. Texture texture = LoadTexture("resources/cat.png");
  47. float ang = 0;
  48. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  49. //--------------------------------------------------------------------------------------
  50. // Main game loop
  51. while (!WindowShouldClose()) // Detect window close button or ESC key
  52. {
  53. // Update
  54. //----------------------------------------------------------------------------------
  55. ang++;
  56. Vector2 positions[MAX_POINTS] = { 0 };
  57. for (int i = 0; i < MAX_POINTS; i++) positions[i] = Vector2Rotate(points[i], ang);
  58. //----------------------------------------------------------------------------------
  59. // Draw
  60. //----------------------------------------------------------------------------------
  61. BeginDrawing();
  62. ClearBackground(RAYWHITE);
  63. DrawText("textured polygon", 20, 20, 20, DARKGRAY);
  64. DrawTexturePoly(texture, (Vector2){ GetScreenWidth()/2, GetScreenHeight()/2 },
  65. positions, texcoords, MAX_POINTS, WHITE);
  66. EndDrawing();
  67. //----------------------------------------------------------------------------------
  68. }
  69. // De-Initialization
  70. //--------------------------------------------------------------------------------------
  71. UnloadTexture(texture); // Unload texture
  72. CloseWindow(); // Close window and OpenGL context
  73. //--------------------------------------------------------------------------------------
  74. return 0;
  75. }