shapes_triangle_strip.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. /*******************************************************************************************
  2. *
  3. * raylib [shapes] example - triangle strip
  4. *
  5. * Example complexity rating: [★★☆☆] 2/4
  6. *
  7. * Example originally created with raylib 5.6-dev, last time updated with raylib 5.6-dev
  8. *
  9. * Example contributed by Jopestpe (@jopestpe)
  10. *
  11. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  12. * BSD-like license that allows static linking with closed source software
  13. *
  14. * Copyright (c) 2025 Jopestpe (@jopestpe)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  18. #include <math.h>
  19. #define RAYGUI_IMPLEMENTATION
  20. #include "raygui.h" // Required for GUI controls
  21. //------------------------------------------------------------------------------------
  22. // Program main entry point
  23. //------------------------------------------------------------------------------------
  24. int main(void)
  25. {
  26. // Initialization
  27. //--------------------------------------------------------------------------------------
  28. const int screenWidth = 800;
  29. const int screenHeight = 450;
  30. InitWindow(screenWidth, screenHeight, "raylib [shapes] example - triangle strip");
  31. Vector2 points[122] = { 0 };
  32. Vector2 center = { (screenWidth/2.0f) - 125.0f, screenHeight/2.0f };
  33. float segments = 6.0f;
  34. float insideRadius = 100.0f;
  35. float outsideRadius = 150.0f;
  36. bool outline = true;
  37. SetTargetFPS(60); // Set our game to run at 60 frames-per-second
  38. //--------------------------------------------------------------------------------------
  39. // Main game loop
  40. while (!WindowShouldClose()) // Detect window close button or ESC key
  41. {
  42. // Update
  43. //----------------------------------------------------------------------------------
  44. int pointCount = (int)(segments);
  45. float angleStep = (360.0f/pointCount)*DEG2RAD;
  46. for (int i = 0, i2 = 0; i < pointCount; i++, i2 += 2)
  47. {
  48. float angle1 = i*angleStep;
  49. points[i2] = (Vector2){ center.x + cosf(angle1)*insideRadius, center.y + sinf(angle1)*insideRadius };
  50. float angle2 = angle1 + angleStep/2.0f;
  51. points[i2 + 1] = (Vector2){ center.x + cosf(angle2)*outsideRadius, center.y + sinf(angle2)*outsideRadius };
  52. }
  53. points[pointCount*2] = points[0];
  54. points[pointCount*2 + 1] = points[1];
  55. //----------------------------------------------------------------------------------
  56. // Draw
  57. //----------------------------------------------------------------------------------
  58. BeginDrawing();
  59. ClearBackground(RAYWHITE);
  60. for (int i = 0; i < pointCount; i++)
  61. {
  62. Vector2 a = points[i*2];
  63. Vector2 b = points[i*2 + 1];
  64. Vector2 c = points[i*2 + 2];
  65. Vector2 d = points[i*2 + 3];
  66. float angle1 = i*angleStep;
  67. DrawTriangle(c, b, a, ColorFromHSV(angle1*RAD2DEG, 1.0f, 1.0f));
  68. DrawTriangle(d, b, c, ColorFromHSV((angle1 + angleStep/2)*RAD2DEG, 1.0f, 1.0f));
  69. if (outline)
  70. {
  71. DrawTriangleLines(a, b, c, BLACK);
  72. DrawTriangleLines(c, b, d, BLACK);
  73. }
  74. }
  75. DrawLine(580, 0, 580, GetScreenHeight(), (Color){ 218, 218, 218, 255 });
  76. DrawRectangle(580, 0, GetScreenWidth(), GetScreenHeight(), (Color){ 232, 232, 232, 255 });
  77. // Draw GUI controls
  78. //------------------------------------------------------------------------------
  79. GuiSliderBar((Rectangle){ 640, 40, 120, 20}, "Segments", TextFormat("%.0f", segments), &segments, 6.0f, 60.0f);
  80. GuiCheckBox((Rectangle){ 640, 70, 20, 20 }, "Outline", &outline);
  81. //------------------------------------------------------------------------------
  82. DrawFPS(10, 10);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. CloseWindow(); // Close window and OpenGL context
  89. //--------------------------------------------------------------------------------------
  90. return 0;
  91. }