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.

109 lines
4.8 KiB

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