summaryrefslogtreecommitdiffhomepage
path: root/examples/web/textures/textures_npatch_drawing.c
diff options
context:
space:
mode:
authorRay <[email protected]>2019-05-18 01:27:54 +0200
committerRay <[email protected]>2019-05-18 01:27:54 +0200
commit210d5ec72bbc4ba426b21f69e6f65b6d75923d05 (patch)
treec01f6e14d673504534602c7ae9609b3dff248806 /examples/web/textures/textures_npatch_drawing.c
parent85b11a6baf64a2b4e02c81d4d47b15eb578dc074 (diff)
downloadraylib.com-210d5ec72bbc4ba426b21f69e6f65b6d75923d05.tar.gz
raylib.com-210d5ec72bbc4ba426b21f69e6f65b6d75923d05.zip
Update examples to raylib 2.5 -WIP-
Remove old examples
Diffstat (limited to 'examples/web/textures/textures_npatch_drawing.c')
-rw-r--r--examples/web/textures/textures_npatch_drawing.c140
1 files changed, 140 insertions, 0 deletions
diff --git a/examples/web/textures/textures_npatch_drawing.c b/examples/web/textures/textures_npatch_drawing.c
new file mode 100644
index 0000000..757f627
--- /dev/null
+++ b/examples/web/textures/textures_npatch_drawing.c
@@ -0,0 +1,140 @@
+/*******************************************************************************************
+*
+* raylib [textures] example - N-patch drawing
+*
+* NOTE: Images are loaded in CPU memory (RAM); textures are loaded in GPU memory (VRAM)
+*
+* This example has been created using raylib 2.0 (www.raylib.com)
+* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
+*
+* Example contributed by Jorge A. Gomes (@overdev) and reviewed by Ramon Santamaria (@raysan5)
+*
+* Copyright (c) 2018 Jorge A. Gomes (@overdev) and Ramon Santamaria (@raysan5)
+*
+********************************************************************************************/
+
+#include "raylib.h"
+
+#if defined(PLATFORM_WEB)
+ #include <emscripten/emscripten.h>
+#endif
+
+//----------------------------------------------------------------------------------
+// Global Variables Definition
+//----------------------------------------------------------------------------------
+int screenWidth = 800;
+int screenHeight = 450;
+
+// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
+
+Texture2D nPatchTexture;
+
+Vector2 mousePosition = { 0 };
+Vector2 origin = { 0.0f, 0.0f };
+
+// Position and size of the n-patches
+Rectangle dstRec1 = { 480.0f, 160.0f, 32.0f, 32.0f };
+Rectangle dstRec2 = { 160.0f, 160.0f, 32.0f, 32.0f };
+Rectangle dstRecH = { 160.0f, 93.0f, 32.0f, 32.0f };
+Rectangle dstRecV = { 92.0f, 160.0f, 32.0f, 32.0f };
+
+// A 9-patch (NPT_9PATCH) changes its sizes in both axis
+NPatchInfo ninePatchInfo1 = { (Rectangle){ 0.0f, 0.0f, 64.0f, 64.0f }, 12, 40, 12, 12, NPT_9PATCH };
+NPatchInfo ninePatchInfo2 = { (Rectangle){ 0.0f, 128.0f, 64.0f, 64.0f }, 16, 16, 16, 16, NPT_9PATCH };
+
+// A horizontal 3-patch (NPT_3PATCH_HORIZONTAL) changes its sizes along the x axis only
+NPatchInfo h3PatchInfo = { (Rectangle){ 0.0f, 64.0f, 64.0f, 64.0f }, 8, 8, 8, 8, NPT_3PATCH_HORIZONTAL };
+
+// A vertical 3-patch (NPT_3PATCH_VERTICAL) changes its sizes along the y axis only
+NPatchInfo v3PatchInfo = { (Rectangle){ 0.0f, 192.0f, 64.0f, 64.0f }, 6, 6, 6, 6, NPT_3PATCH_VERTICAL };
+
+//----------------------------------------------------------------------------------
+// Module Functions Declaration
+//----------------------------------------------------------------------------------
+void UpdateDrawFrame(void); // Update and Draw one frame
+
+//----------------------------------------------------------------------------------
+// Main Enry Point
+//----------------------------------------------------------------------------------
+int main()
+{
+ // Initialization
+ //--------------------------------------------------------------------------------------
+ InitWindow(screenWidth, screenHeight, "raylib [textures] example - N-patch drawing");
+
+ // NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
+ nPatchTexture = LoadTexture("resources/ninepatch_button.png");
+
+#if defined(PLATFORM_WEB)
+ emscripten_set_main_loop(UpdateDrawFrame, 0, 1);
+#else
+ SetTargetFPS(60); // Set our game to run at 60 frames-per-second
+ //--------------------------------------------------------------------------------------
+
+ // Main game loop
+ while (!WindowShouldClose()) // Detect window close button or ESC key
+ {
+ UpdateDrawFrame();
+ }
+#endif
+
+ // De-Initialization
+ //--------------------------------------------------------------------------------------
+ UnloadTexture(nPatchTexture); // Texture unloading
+
+ CloseWindow(); // Close window and OpenGL context
+ //--------------------------------------------------------------------------------------
+
+ return 0;
+}
+
+//----------------------------------------------------------------------------------
+// Module Functions Definition
+//----------------------------------------------------------------------------------
+void UpdateDrawFrame(void)
+{
+ // Update
+ //----------------------------------------------------------------------------------
+ mousePosition = GetMousePosition();
+
+ // Resize the n-patches based on mouse position
+ dstRec1.width = mousePosition.x - dstRec1.x;
+ dstRec1.height = mousePosition.y - dstRec1.y;
+ dstRec2.width = mousePosition.x - dstRec2.x;
+ dstRec2.height = mousePosition.y - dstRec2.y;
+ dstRecH.width = mousePosition.x - dstRecH.x;
+ dstRecV.height = mousePosition.y - dstRecV.y;
+
+ // Set a minimum width and/or height
+ if (dstRec1.width < 1.0f) dstRec1.width = 1.0f;
+ if (dstRec1.width > 300.0f) dstRec1.width = 300.0f;
+ if (dstRec1.height < 1.0f) dstRec1.height = 1.0f;
+ if (dstRec2.width < 1.0f) dstRec2.width = 1.0f;
+ if (dstRec2.width > 300.0f) dstRec2.width = 300.0f;
+ if (dstRec2.height < 1.0f) dstRec2.height = 1.0f;
+ if (dstRecH.width < 1.0f) dstRecH.width = 1.0f;
+ if (dstRecV.height < 1.0f) dstRecV.height = 1.0f;
+ //----------------------------------------------------------------------------------
+
+ // Draw
+ //----------------------------------------------------------------------------------
+ BeginDrawing();
+
+ ClearBackground(RAYWHITE);
+
+ // Draw the n-patches
+ DrawTextureNPatch(nPatchTexture, ninePatchInfo2, dstRec2, origin, 0.0f, WHITE);
+ DrawTextureNPatch(nPatchTexture, ninePatchInfo1, dstRec1, origin, 0.0f, WHITE);
+ DrawTextureNPatch(nPatchTexture, h3PatchInfo, dstRecH, origin, 0.0f, WHITE);
+ DrawTextureNPatch(nPatchTexture, v3PatchInfo, dstRecV, origin, 0.0f, WHITE);
+
+ // Draw the source texture
+ DrawRectangleLines(5, 88, 74, 266, BLUE);
+ DrawTexture(nPatchTexture, 10, 93, WHITE);
+ DrawText("TEXTURE", 15, 360, 10, DARKGRAY);
+
+ DrawText("Move the mouse to stretch or shrink the n-patches", 10, 20, 20, DARKGRAY);
+
+ EndDrawing();
+ //----------------------------------------------------------------------------------
+}