Você não pode selecionar mais de 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

114 linhas
5.1 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. * Example originally created with raylib 2.0, last time updated with raylib 2.5
  8. *
  9. * Example contributed by Jorge A. Gomes (@overdev) and reviewed by Ramon Santamaria (@raysan5)
  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) 2018-2024 Jorge A. Gomes (@overdev) and Ramon Santamaria (@raysan5)
  15. *
  16. ********************************************************************************************/
  17. #include "raylib.h"
  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 - N-patch drawing");
  28. // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
  29. Texture2D nPatchTexture = LoadTexture("resources/ninepatch_button.png");
  30. Vector2 mousePosition = { 0 };
  31. Vector2 origin = { 0.0f, 0.0f };
  32. // Position and size of the n-patches
  33. Rectangle dstRec1 = { 480.0f, 160.0f, 32.0f, 32.0f };
  34. Rectangle dstRec2 = { 160.0f, 160.0f, 32.0f, 32.0f };
  35. Rectangle dstRecH = { 160.0f, 93.0f, 32.0f, 32.0f };
  36. Rectangle dstRecV = { 92.0f, 160.0f, 32.0f, 32.0f };
  37. // A 9-patch (NPATCH_NINE_PATCH) changes its sizes in both axis
  38. NPatchInfo ninePatchInfo1 = { (Rectangle){ 0.0f, 0.0f, 64.0f, 64.0f }, 12, 40, 12, 12, NPATCH_NINE_PATCH };
  39. NPatchInfo ninePatchInfo2 = { (Rectangle){ 0.0f, 128.0f, 64.0f, 64.0f }, 16, 16, 16, 16, NPATCH_NINE_PATCH };
  40. // A horizontal 3-patch (NPATCH_THREE_PATCH_HORIZONTAL) changes its sizes along the x axis only
  41. NPatchInfo h3PatchInfo = { (Rectangle){ 0.0f, 64.0f, 64.0f, 64.0f }, 8, 8, 8, 8, NPATCH_THREE_PATCH_HORIZONTAL };
  42. // A vertical 3-patch (NPATCH_THREE_PATCH_VERTICAL) changes its sizes along the y axis only
  43. NPatchInfo v3PatchInfo = { (Rectangle){ 0.0f, 192.0f, 64.0f, 64.0f }, 6, 6, 6, 6, NPATCH_THREE_PATCH_VERTICAL };
  44. SetTargetFPS(60);
  45. //---------------------------------------------------------------------------------------
  46. // Main game loop
  47. while (!WindowShouldClose()) // Detect window close button or ESC key
  48. {
  49. // Update
  50. //----------------------------------------------------------------------------------
  51. mousePosition = GetMousePosition();
  52. // Resize the n-patches based on mouse position
  53. dstRec1.width = mousePosition.x - dstRec1.x;
  54. dstRec1.height = mousePosition.y - dstRec1.y;
  55. dstRec2.width = mousePosition.x - dstRec2.x;
  56. dstRec2.height = mousePosition.y - dstRec2.y;
  57. dstRecH.width = mousePosition.x - dstRecH.x;
  58. dstRecV.height = mousePosition.y - dstRecV.y;
  59. // Set a minimum width and/or height
  60. if (dstRec1.width < 1.0f) dstRec1.width = 1.0f;
  61. if (dstRec1.width > 300.0f) dstRec1.width = 300.0f;
  62. if (dstRec1.height < 1.0f) dstRec1.height = 1.0f;
  63. if (dstRec2.width < 1.0f) dstRec2.width = 1.0f;
  64. if (dstRec2.width > 300.0f) dstRec2.width = 300.0f;
  65. if (dstRec2.height < 1.0f) dstRec2.height = 1.0f;
  66. if (dstRecH.width < 1.0f) dstRecH.width = 1.0f;
  67. if (dstRecV.height < 1.0f) dstRecV.height = 1.0f;
  68. //----------------------------------------------------------------------------------
  69. // Draw
  70. //----------------------------------------------------------------------------------
  71. BeginDrawing();
  72. ClearBackground(RAYWHITE);
  73. // Draw the n-patches
  74. DrawTextureNPatch(nPatchTexture, ninePatchInfo2, dstRec2, origin, 0.0f, WHITE);
  75. DrawTextureNPatch(nPatchTexture, ninePatchInfo1, dstRec1, origin, 0.0f, WHITE);
  76. DrawTextureNPatch(nPatchTexture, h3PatchInfo, dstRecH, origin, 0.0f, WHITE);
  77. DrawTextureNPatch(nPatchTexture, v3PatchInfo, dstRecV, origin, 0.0f, WHITE);
  78. // Draw the source texture
  79. DrawRectangleLines(5, 88, 74, 266, BLUE);
  80. DrawTexture(nPatchTexture, 10, 93, WHITE);
  81. DrawText("TEXTURE", 15, 360, 10, DARKGRAY);
  82. DrawText("Move the mouse to stretch or shrink the n-patches", 10, 20, 20, DARKGRAY);
  83. EndDrawing();
  84. //----------------------------------------------------------------------------------
  85. }
  86. // De-Initialization
  87. //--------------------------------------------------------------------------------------
  88. UnloadTexture(nPatchTexture); // Texture unloading
  89. CloseWindow(); // Close window and OpenGL context
  90. //--------------------------------------------------------------------------------------
  91. return 0;
  92. }