Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
82 changes: 82 additions & 0 deletions public/usage-examples/utilities/split_text-1-example-oop.cs
Original file line number Diff line number Diff line change
@@ -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();
}
}
}
53 changes: 53 additions & 0 deletions public/usage-examples/utilities/split_text-1-example-top-level.cs
Original file line number Diff line number Diff line change
@@ -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();
56 changes: 56 additions & 0 deletions public/usage-examples/utilities/split_text-1-example.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,56 @@
#include "splashkit.h"
#include <vector>

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<string> 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<double>(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;
}
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
45 changes: 45 additions & 0 deletions public/usage-examples/utilities/split_text-1-example.py
Original file line number Diff line number Diff line change
@@ -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()
1 change: 1 addition & 0 deletions public/usage-examples/utilities/split_text-1-example.txt

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The file has not description.

Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Split Text Example