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.

116 rivejä
5.2 KiB

5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
5 vuotta sitten
  1. /*******************************************************************************************
  2. *
  3. * raylib [textures] example - N-patch drawing
  4. *
  5. * Example complexity rating: [] 3/4
  6. *
  7. * NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
  8. *
  9. * Example originally created with raylib 2.0, last time updated with raylib 2.5
  10. *
  11. * Example contributed by Jorge A. Gomes (@overdev) and reviewed by Ramon Santamaria (@raysan5)
  12. *
  13. * Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
  14. * BSD-like license that allows static linking with closed source software
  15. *
  16. * Copyright (c) 2018-2025 Jorge A. Gomes (@overdev) and Ramon Santamaria (@raysan5)
  17. *
  18. ********************************************************************************************/
  19. #include "raylib.h"
  20. //------------------------------------------------------------------------------------
  21. // Program main entry point
  22. //------------------------------------------------------------------------------------
  23. int main(void)
  24. {
  25. // Initialization
  26. //--------------------------------------------------------------------------------------
  27. const int screenWidth = 800;
  28. const int screenHeight = 450;
  29. InitWindow(screenWidth, screenHeight, "raylib [textures] example - N-patch drawing");
  30. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  31. Texture2D nPatchTexture = LoadTexture("resources/ninepatch_button.png");
  32. Vector2 mousePosition = { 0 };
  33. Vector2 origin = { 0.0f, 0.0f };
  34. // Position and size of the n-patches
  35. Rectangle dstRec1 = { 480.0f, 160.0f, 32.0f, 32.0f };
  36. Rectangle dstRec2 = { 160.0f, 160.0f, 32.0f, 32.0f };
  37. Rectangle dstRecH = { 160.0f, 93.0f, 32.0f, 32.0f };
  38. Rectangle dstRecV = { 92.0f, 160.0f, 32.0f, 32.0f };
  39. // A 9-patch (NPATCH_NINE_PATCH) changes its sizes in both axis
  40. NPatchInfo ninePatchInfo1 = { (Rectangle){ 0.0f, 0.0f, 64.0f, 64.0f }, 12, 40, 12, 12, NPATCH_NINE_PATCH };
  41. NPatchInfo ninePatchInfo2 = { (Rectangle){ 0.0f, 128.0f, 64.0f, 64.0f }, 16, 16, 16, 16, NPATCH_NINE_PATCH };
  42. // A horizontal 3-patch (NPATCH_THREE_PATCH_HORIZONTAL) changes its sizes along the x axis only
  43. NPatchInfo h3PatchInfo = { (Rectangle){ 0.0f, 64.0f, 64.0f, 64.0f }, 8, 8, 8, 8, NPATCH_THREE_PATCH_HORIZONTAL };
  44. // A vertical 3-patch (NPATCH_THREE_PATCH_VERTICAL) changes its sizes along the y axis only
  45. NPatchInfo v3PatchInfo = { (Rectangle){ 0.0f, 192.0f, 64.0f, 64.0f }, 6, 6, 6, 6, NPATCH_THREE_PATCH_VERTICAL };
  46. SetTargetFPS(60);
  47. //---------------------------------------------------------------------------------------
  48. // Main game loop
  49. while (!WindowShouldClose()) // Detect window close button or ESC key
  50. {
  51. // Update
  52. //----------------------------------------------------------------------------------
  53. mousePosition = GetMousePosition();
  54. // Resize the n-patches based on mouse position
  55. dstRec1.width = mousePosition.x - dstRec1.x;
  56. dstRec1.height = mousePosition.y - dstRec1.y;
  57. dstRec2.width = mousePosition.x - dstRec2.x;
  58. dstRec2.height = mousePosition.y - dstRec2.y;
  59. dstRecH.width = mousePosition.x - dstRecH.x;
  60. dstRecV.height = mousePosition.y - dstRecV.y;
  61. // Set a minimum width and/or height
  62. if (dstRec1.width < 1.0f) dstRec1.width = 1.0f;
  63. if (dstRec1.width > 300.0f) dstRec1.width = 300.0f;
  64. if (dstRec1.height < 1.0f) dstRec1.height = 1.0f;
  65. if (dstRec2.width < 1.0f) dstRec2.width = 1.0f;
  66. if (dstRec2.width > 300.0f) dstRec2.width = 300.0f;
  67. if (dstRec2.height < 1.0f) dstRec2.height = 1.0f;
  68. if (dstRecH.width < 1.0f) dstRecH.width = 1.0f;
  69. if (dstRecV.height < 1.0f) dstRecV.height = 1.0f;
  70. //----------------------------------------------------------------------------------
  71. // Draw
  72. //----------------------------------------------------------------------------------
  73. BeginDrawing();
  74. ClearBackground(RAYWHITE);
  75. // Draw the n-patches
  76. DrawTextureNPatch(nPatchTexture, ninePatchInfo2, dstRec2, origin, 0.0f, WHITE);
  77. DrawTextureNPatch(nPatchTexture, ninePatchInfo1, dstRec1, origin, 0.0f, WHITE);
  78. DrawTextureNPatch(nPatchTexture, h3PatchInfo, dstRecH, origin, 0.0f, WHITE);
  79. DrawTextureNPatch(nPatchTexture, v3PatchInfo, dstRecV, origin, 0.0f, WHITE);
  80. // Draw the source texture
  81. DrawRectangleLines(5, 88, 74, 266, BLUE);
  82. DrawTexture(nPatchTexture, 10, 93, WHITE);
  83. DrawText("TEXTURE", 15, 360, 10, DARKGRAY);
  84. DrawText("Move the mouse to stretch or shrink the n-patches", 10, 20, 20, DARKGRAY);
  85. EndDrawing();
  86. //----------------------------------------------------------------------------------
  87. }
  88. // De-Initialization
  89. //--------------------------------------------------------------------------------------
  90. UnloadTexture(nPatchTexture); // Texture unloading
  91. CloseWindow(); // Close window and OpenGL context
  92. //--------------------------------------------------------------------------------------
  93. return 0;
  94. }