diff --git a/public/usage-examples/utilities/split_text-1-example-oop.cs b/public/usage-examples/utilities/split_text-1-example-oop.cs new file mode 100644 index 000000000..30c1af678 --- /dev/null +++ b/public/usage-examples/utilities/split_text-1-example-oop.cs @@ -0,0 +1,82 @@ +using System; +using SplashKitSDK; + +namespace SplitTextExample +{ + public class SplitText + { + private Window _window; + private string _text; + private string[] _parts; + private Color _topColor; + private Color _bottomColor; + + public SplitText() + { + // creating window + _window = new Window("Split Text Example", 800, 400); + + // I am defining the text to split + _text = "apple,banana,carrot"; + + // I am splitting the string into parts using SplashKit's split() function + _parts = SplashKit.Split(_text, ','); + + // colors for background gradient + _topColor = SplashKit.RGBColor(245, 251, 255); + _bottomColor = SplashKit.RGBColor(200, 230, 255); + } + + private Color BlendColors(Color c1, Color c2, double t) + { + // linear interpolation for RGB channels + int r = (int)((1 - t) * SplashKit.RedOf(c1) + t * SplashKit.RedOf(c2)); + int g = (int)((1 - t) * SplashKit.GreenOf(c1) + t * SplashKit.GreenOf(c2)); + int b = (int)((1 - t) * SplashKit.BlueOf(c1) + t * SplashKit.BlueOf(c2)); + return SplashKit.RGBColor(r, g, b); + } + + public void Run() + { + while (!SplashKit.QuitRequested()) + { + SplashKit.ProcessEvents(); + + // draw gradient background + for (int y = 0; y < _window.Height; y++) + { + double t = (double)y / _window.Height; + Color blended = BlendColors(_topColor, _bottomColor, t); + SplashKit.DrawLine(blended, 0, y, _window.Width, y); + } + + // display text + SplashKit.DrawText("Original string:", SplashKit.ColorDarkBlue(), "arial", 20, 60, 50); + SplashKit.DrawText(_text, SplashKit.ColorBlack(), "arial", 20, 280, 50); + + int yPos = 130; + foreach (string s in _parts) + { + SplashKit.DrawText("Token: " + s, SplashKit.ColorBlack(), "arial", 18, 60, yPos); + yPos += 40; + } + + SplashKit.DrawText("Press ESC to exit", SplashKit.ColorGray(), "arial", 14, 620, 360); + _window.Refresh(60); + + if (SplashKit.KeyTyped(KeyCode.EscapeKey)) + { + break; + } + } + + _window.Close(); + } + + public static void Main() + { + SplitText demo = new SplitText(); + demo.Run(); + } + } +} diff --git a/public/usage-examples/utilities/split_text-1-example-top-level.cs b/public/usage-examples/utilities/split_text-1-example-top-level.cs new file mode 100644 index 000000000..bf8c81b0b --- /dev/null +++ b/public/usage-examples/utilities/split_text-1-example-top-level.cs @@ -0,0 +1,53 @@ +using SplashKitSDK; +using static SplashKitSDK.SplashKit; + +Color BlendColors(Color c1, Color c2, double t) +{ + // linear interpolation for RGB channels + int r = (int)((1 - t) * RedOf(c1) + t * RedOf(c2)); + int g = (int)((1 - t) * GreenOf(c1) + t * GreenOf(c2)); + int b = (int)((1 - t) * BlueOf(c1) + t * BlueOf(c2)); + return RGBColor(r, g, b); +} + +OpenWindow("Split Text Example", 800, 400); + +// I am defining the text to split +string text = "apple,banana,carrot"; + +// I am splitting the string into parts using SplashKit's split() function +string[] parts = Split(text, ','); + +// I am drawing a gradient background manually +Color top = RGBColor(245, 251, 255); +Color bottom = RGBColor(200, 230, 255); + +for (int y = 0; y < 400; y++) +{ + double t = (double)y / 400.0; + Color blended = BlendColors(top, bottom, t); + DrawLine(blended, 0, y, 800, y); +} + +DrawText("Original string:", ColorDarkBlue(), "arial", 20, 60, 50); +DrawText(text, ColorBlack(), "arial", 20, 280, 50); + +// I am printing each token from the split result +int yPos = 130; +foreach (string s in parts) +{ + DrawText("Token: " + s, ColorBlack(), "arial", 18, 60, yPos); + yPos += 40; +} + +DrawText("Press ESC to exit", ColorGray(), "arial", 14, 620, 360); +RefreshScreen(); + +// wait for quit event +while (!QuitRequested()) +{ + ProcessEvents(); + if (KeyTyped(KeyCode.EscapeKey)) break; +} + +CloseAllWindows(); \ No newline at end of file diff --git a/public/usage-examples/utilities/split_text-1-example.cpp b/public/usage-examples/utilities/split_text-1-example.cpp new file mode 100644 index 000000000..4ecbcb380 --- /dev/null +++ b/public/usage-examples/utilities/split_text-1-example.cpp @@ -0,0 +1,56 @@ +#include "splashkit.h" +#include + +color blend_colors(color c1, color c2, double t) +{ + // linear interpolation for RGB channels + int r = (int)((1 - t) * red_of(c1) + t * red_of(c2)); + int g = (int)((1 - t) * green_of(c1) + t * green_of(c2)); + int b = (int)((1 - t) * blue_of(c1) + t * blue_of(c2)); + return rgb_color(r, g, b); +} + +int main() +{ + open_window("Split Text Example", 800, 400); + + // I am defining the text to split + string text = "apple,banana,carrot"; + + // I am splitting the string into parts using SplashKit's split() function + vector parts = split(text, ','); + + // I am drawing a gradient background manually + color top = rgb_color(245, 251, 255); + color bottom = rgb_color(200, 230, 255); + + for (int y = 0; y < 400; y++) + { + double t = static_cast(y) / 400.0; + color blended = blend_colors(top, bottom, t); + draw_line(blended, 0, y, 800, y); + } + + draw_text("Original string:", color_dark_blue(), "arial", 20, 60, 50); + draw_text(text, color_black(), "arial", 20, 280, 50); + + int y = 130; + for (string s : parts) + { + draw_text("Token: " + s, color_black(), "arial", 18, 60, y); + y += 40; + } + + draw_text("Press ESC to exit", color_gray(), "arial", 14, 620, 360); + + refresh_screen(); + + while (!quit_requested()) + { + process_events(); + if (key_typed(ESCAPE_KEY)) break; + } + + close_all_windows(); + return 0; +} diff --git a/public/usage-examples/utilities/split_text-1-example.png b/public/usage-examples/utilities/split_text-1-example.png new file mode 100644 index 000000000..3dbd39ff1 Binary files /dev/null and b/public/usage-examples/utilities/split_text-1-example.png differ diff --git a/public/usage-examples/utilities/split_text-1-example.py b/public/usage-examples/utilities/split_text-1-example.py new file mode 100644 index 000000000..92cd9ba03 --- /dev/null +++ b/public/usage-examples/utilities/split_text-1-example.py @@ -0,0 +1,45 @@ +from splashkit import * + +def blend_colors(c1, c2, t): + # linear interpolation for RGB channels + r = int((1 - t) * red_of(c1) + t * red_of(c2)) + g = int((1 - t) * green_of(c1) + t * green_of(c2)) + b = int((1 - t) * blue_of(c1) + t * blue_of(c2)) + return rgb_color(r, g, b) + +open_window("Split Text Example", 800, 400) + +# I am defining the text to split +text = "apple,banana,carrot" + +# I am splitting the string into parts using SplashKit's split() function +parts = split(text, ',') + +# I am drawing a gradient background manually +top = rgb_color(245, 251, 255) +bottom = rgb_color(200, 230, 255) + +for y in range(400): + t = y / 400 + blended = blend_colors(top, bottom, t) + draw_line(blended, 0, y, 800, y) + +draw_text("Original string:", color_dark_blue(), "arial", 20, 60, 50) +draw_text(text, color_black(), "arial", 20, 280, 50) + +# I am printing each token from the split result +y = 130 +for s in parts: + draw_text("Token: " + s, color_black(), "arial", 18, 60, y) + y += 40 + +draw_text("Press ESC to exit", color_gray(), "arial", 14, 620, 360) +refresh_screen() + +# wait for quit event +while not quit_requested(): + process_events() + if key_typed(KeyCode.escape_key): + break + +close_all_windows() \ No newline at end of file diff --git a/public/usage-examples/utilities/split_text-1-example.txt b/public/usage-examples/utilities/split_text-1-example.txt new file mode 100644 index 000000000..d99a40f8c --- /dev/null +++ b/public/usage-examples/utilities/split_text-1-example.txt @@ -0,0 +1 @@ +Split Text Example \ No newline at end of file