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 | |
16 | #include "raylib.h" |
17 | |
18 | int main(void) |
19 | { |
20 | // Initialization |
21 | //-------------------------------------------------------------------------------------- |
22 | const int screenWidth = 800; |
23 | const int screenHeight = 450; |
24 | |
25 | InitWindow(screenWidth, screenHeight, "raylib [textures] example - N-patch drawing" ); |
26 | |
27 | // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required) |
28 | Texture2D nPatchTexture = LoadTexture("resources/ninepatch_button.png" ); |
29 | |
30 | Vector2 mousePosition = { 0 }; |
31 | Vector2 origin = { 0.0f, 0.0f }; |
32 | |
33 | // Position and size of the n-patches |
34 | Rectangle dstRec1 = { 480.0f, 160.0f, 32.0f, 32.0f }; |
35 | Rectangle dstRec2 = { 160.0f, 160.0f, 32.0f, 32.0f }; |
36 | Rectangle dstRecH = { 160.0f, 93.0f, 32.0f, 32.0f }; |
37 | Rectangle dstRecV = { 92.0f, 160.0f, 32.0f, 32.0f }; |
38 | |
39 | // A 9-patch (NPT_9PATCH) changes its sizes in both axis |
40 | NPatchInfo ninePatchInfo1 = { (Rectangle){ 0.0f, 0.0f, 64.0f, 64.0f }, 12, 40, 12, 12, NPT_9PATCH }; |
41 | NPatchInfo ninePatchInfo2 = { (Rectangle){ 0.0f, 128.0f, 64.0f, 64.0f }, 16, 16, 16, 16, NPT_9PATCH }; |
42 | |
43 | // A horizontal 3-patch (NPT_3PATCH_HORIZONTAL) changes its sizes along the x axis only |
44 | NPatchInfo h3PatchInfo = { (Rectangle){ 0.0f, 64.0f, 64.0f, 64.0f }, 8, 8, 8, 8, NPT_3PATCH_HORIZONTAL }; |
45 | |
46 | // A vertical 3-patch (NPT_3PATCH_VERTICAL) changes its sizes along the y axis only |
47 | NPatchInfo v3PatchInfo = { (Rectangle){ 0.0f, 192.0f, 64.0f, 64.0f }, 6, 6, 6, 6, NPT_3PATCH_VERTICAL }; |
48 | |
49 | SetTargetFPS(60); |
50 | //--------------------------------------------------------------------------------------- |
51 | |
52 | // Main game loop |
53 | while (!WindowShouldClose()) // Detect window close button or ESC key |
54 | { |
55 | // Update |
56 | //---------------------------------------------------------------------------------- |
57 | mousePosition = GetMousePosition(); |
58 | |
59 | // Resize the n-patches based on mouse position |
60 | dstRec1.width = mousePosition.x - dstRec1.x; |
61 | dstRec1.height = mousePosition.y - dstRec1.y; |
62 | dstRec2.width = mousePosition.x - dstRec2.x; |
63 | dstRec2.height = mousePosition.y - dstRec2.y; |
64 | dstRecH.width = mousePosition.x - dstRecH.x; |
65 | dstRecV.height = mousePosition.y - dstRecV.y; |
66 | |
67 | // Set a minimum width and/or height |
68 | if (dstRec1.width < 1.0f) dstRec1.width = 1.0f; |
69 | if (dstRec1.width > 300.0f) dstRec1.width = 300.0f; |
70 | if (dstRec1.height < 1.0f) dstRec1.height = 1.0f; |
71 | if (dstRec2.width < 1.0f) dstRec2.width = 1.0f; |
72 | if (dstRec2.width > 300.0f) dstRec2.width = 300.0f; |
73 | if (dstRec2.height < 1.0f) dstRec2.height = 1.0f; |
74 | if (dstRecH.width < 1.0f) dstRecH.width = 1.0f; |
75 | if (dstRecV.height < 1.0f) dstRecV.height = 1.0f; |
76 | //---------------------------------------------------------------------------------- |
77 | |
78 | // Draw |
79 | //---------------------------------------------------------------------------------- |
80 | BeginDrawing(); |
81 | |
82 | ClearBackground(RAYWHITE); |
83 | |
84 | // Draw the n-patches |
85 | DrawTextureNPatch(nPatchTexture, ninePatchInfo2, dstRec2, origin, 0.0f, WHITE); |
86 | DrawTextureNPatch(nPatchTexture, ninePatchInfo1, dstRec1, origin, 0.0f, WHITE); |
87 | DrawTextureNPatch(nPatchTexture, h3PatchInfo, dstRecH, origin, 0.0f, WHITE); |
88 | DrawTextureNPatch(nPatchTexture, v3PatchInfo, dstRecV, origin, 0.0f, WHITE); |
89 | |
90 | // Draw the source texture |
91 | DrawRectangleLines(5, 88, 74, 266, BLUE); |
92 | DrawTexture(nPatchTexture, 10, 93, WHITE); |
93 | DrawText("TEXTURE" , 15, 360, 10, DARKGRAY); |
94 | |
95 | DrawText("Move the mouse to stretch or shrink the n-patches" , 10, 20, 20, DARKGRAY); |
96 | |
97 | EndDrawing(); |
98 | //---------------------------------------------------------------------------------- |
99 | } |
100 | |
101 | // De-Initialization |
102 | //-------------------------------------------------------------------------------------- |
103 | UnloadTexture(nPatchTexture); // Texture unloading |
104 | |
105 | CloseWindow(); // Close window and OpenGL context |
106 | //-------------------------------------------------------------------------------------- |
107 | |
108 | return 0; |
109 | } |
110 | |