raylib/ports/textures/textures_npatch_drawing

Licence This example has its own licence, rather than Odin's: raylib/ports/LICENSE
These are ported Raylib examples, and as such, they are subject to Raylib's license. Copyright (c) 2013-2024 Ramon Santamaria (@raysan5)
The rest of the licence
This software is provided "as-is", without any express or implied warranty. In no event will the authors be held liable for any damages arising from the use of this software. Permission is granted to anyone to use this software for any purpose, including commercial applications, and to alter it and redistribute it freely, subject to the following restrictions: 1. The origin of this software must not be misrepresented; you must not claim that you wrote the original software. If you use this software in a product, an acknowledgment in the product documentation would be appreciated but is not required. 2. Altered source versions must be plainly marked as such, and must not be misrepresented as being the original software. 3. This notice may not be removed or altered from any source distribution.
A screenshot of textures_npatch_drawing

Code

textures_npatch_drawing.odin ¶
130 linesSource

1/*******************************************************************************************
2*
3*   raylib [textures] example - npatch 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
20package raylib_examples
21
22import rl "vendor:raylib"
23
24//------------------------------------------------------------------------------------
25// Program main entry point
26//------------------------------------------------------------------------------------
27main :: proc() {
28	// Initialization
29	//--------------------------------------------------------------------------------------
30	SCREEN_WIDTH :: 800
31	SCREEN_HEIGHT :: 450
32
33	rl.InitWindow(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [textures] example - npatch drawing")
34
35	// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
36	n_patch_texture := rl.LoadTexture("resources/ninepatch_button.png")
37
38	mouse_position: rl.Vector2
39	origin: rl.Vector2
40
41	// Position and size of the n-patches
42	dstRec1 := rl.Rectangle {480, 160, 32, 32}
43	dstRec2 := rl.Rectangle {160, 160, 32, 32}
44	dstRecH := rl.Rectangle {160, 93, 32, 32}
45	dstRecV := rl.Rectangle {92, 160, 32, 32}
46
47	// A 9-patch (NPATCH_NINE_PATCH) changes its sizes in both axis
48	nine_patch_info_1 := rl.NPatchInfo {{0, 0, 64, 64}, 12, 40, 12, 12, .NINE_PATCH}
49	nine_patch_info_2 := rl.NPatchInfo {{0, 128, 64, 64}, 16, 16, 16, 16, .NINE_PATCH}
50
51	// A horizontal 3-patch (NPATCH_THREE_PATCH_HORIZONTAL) changes its sizes along the x axis only
52	h3_patch_info := rl.NPatchInfo {{0, 64, 64, 64}, 8, 8, 8, 8, .THREE_PATCH_HORIZONTAL}
53
54	// A vertical 3-patch (NPATCH_THREE_PATCH_VERTICAL) changes its sizes along the y axis only
55	v3_patch_info := rl.NPatchInfo {{0, 192, 64, 64}, 6, 6, 6, 6, .THREE_PATCH_VERTICAL}
56
57	rl.SetTargetFPS(60)
58	//---------------------------------------------------------------------------------------
59
60	// Main game loop
61	for !rl.WindowShouldClose() {    // Detect window close button or ESC key
62		// Update
63		//----------------------------------------------------------------------------------
64		mouse_position = rl.GetMousePosition()
65
66		// Resize the n-patches based on mouse position
67		dstRec1.width = mouse_position.x - dstRec1.x
68		dstRec1.height = mouse_position.y - dstRec1.y
69		dstRec2.width = mouse_position.x - dstRec2.x
70		dstRec2.height = mouse_position.y - dstRec2.y
71		dstRecH.width = mouse_position.x - dstRecH.x
72		dstRecV.height = mouse_position.y - dstRecV.y
73
74		// Set a minimum width and/or height
75		if dstRec1.width < 1 {
76			dstRec1.width = 1
77		}
78		if dstRec1.width > 300 {
79			dstRec1.width = 300
80		}
81		if dstRec1.height < 1 {
82			dstRec1.height = 1
83		}
84		if dstRec2.width < 1 {
85			dstRec2.width = 1
86		}
87		if dstRec2.width > 300 {
88			dstRec2.width = 300
89		}
90		if dstRec2.height < 1 {
91			dstRec2.height = 1
92		}
93		if dstRecH.width < 1 {
94			dstRecH.width = 1
95		}
96		if dstRecV.height < 1 {
97			dstRecV.height = 1
98		}
99		//----------------------------------------------------------------------------------
100
101		// Draw
102		//----------------------------------------------------------------------------------
103		rl.BeginDrawing()
104
105			rl.ClearBackground(rl.RAYWHITE)
106
107			// Draw the n-patches
108			rl.DrawTextureNPatch(n_patch_texture, nine_patch_info_2, dstRec2, origin, 0, rl.WHITE)
109			rl.DrawTextureNPatch(n_patch_texture, nine_patch_info_1, dstRec1, origin, 0, rl.WHITE)
110			rl.DrawTextureNPatch(n_patch_texture, h3_patch_info, dstRecH, origin, 0, rl.WHITE)
111			rl.DrawTextureNPatch(n_patch_texture, v3_patch_info, dstRecV, origin, 0, rl.WHITE)
112
113			// Draw the source texture
114			rl.DrawRectangleLines(5, 88, 74, 266, rl.BLUE)
115			rl.DrawTexture(n_patch_texture, 10, 93, rl.WHITE)
116			rl.DrawText("TEXTURE", 15, 360, 10, rl.DARKGRAY)
117
118			rl.DrawText("Move the mouse to stretch or shrink the n-patches", 10, 20, 20, rl.DARKGRAY)
119
120		rl.EndDrawing()
121		//----------------------------------------------------------------------------------
122	}
123
124	// De-Initialization
125	//--------------------------------------------------------------------------------------
126	rl.UnloadTexture(n_patch_texture)       // Texture unloading
127
128	rl.CloseWindow()                // Close window and OpenGL context
129	//--------------------------------------------------------------------------------------
130}

Assets 1

Declarations Used 21