strings management
There is no browser build of this example yet.
Is it the same as C?
- Desktop:verified The Zig version renders a final frame that is byte-identical to the C example.
- Browser:not checked yet The browser comparison does not cover this example yet.
//! raylib [text] example - strings management
//!
//! Zig-API translation of examples/text/text_strings_management.c (raylib 6.2).
//! Example contributed by David Buzatto (@davidbuzatto) and reviewed by Ramon Santamaria (@raysan5)
//! Example licensed under an unmodified zlib/libpng license.
//! Copyright (c) 2025 David Buzatto (@davidbuzatto)
const std = @import("std");
const rl = @import("raylib");
const max_text_length = 100;
const max_text_particles = 100;
const font_size = 30;
const screen_width = 800;
const screen_height = 450;
const TextParticle = struct {
buffer: [max_text_length]u8,
len: usize,
rect: rl.Rectangle, // Boundary
vel: rl.Vector2, // Velocity
ppos: rl.Vector2, // Previous position
padding: f32,
border_width: f32,
friction: f32,
elasticity: f32,
color: rl.Color,
grabbed: bool,
fn init(content: []const u8, x: f32, y: f32, color: rl.Color) TextParticle {
var tp: TextParticle = .{
.buffer = undefined,
.len = @min(content.len, max_text_length - 1),
.rect = .{ .x = x, .y = y, .width = 30, .height = 30 },
.vel = .{
.x = @floatFromInt(rl.getRandomValue(-200, 200)),
.y = @floatFromInt(rl.getRandomValue(-200, 200)),
},
.ppos = .{ .x = 0, .y = 0 },
.padding = 5.0,
.border_width = 5.0,
.friction = 0.99,
.elasticity = 0.9,
.color = color,
.grabbed = false,
};
@memcpy(tp.buffer[0..tp.len], content[0..tp.len]);
tp.buffer[tp.len] = 0;
tp.rect.width = @as(f32, @floatFromInt(rl.measureText(tp.text(), font_size))) + tp.padding * 2;
tp.rect.height = font_size + tp.padding * 2;
return tp;
}
fn text(tp: *const TextParticle) [:0]const u8 {
return tp.buffer[0..tp.len :0];
}
};
/// A fixed-capacity list of text particles
const Particles = struct {
buffer: [max_text_particles]TextParticle = undefined,
len: usize = 0,
fn items(particles: *Particles) []TextParticle {
return particles.buffer[0..particles.len];
}
fn append(particles: *Particles, tp: TextParticle) void {
particles.buffer[particles.len] = tp;
particles.len += 1;
}
/// Remove the particle at `index`, shifting the following ones down.
fn orderedRemove(particles: *Particles, index: usize) void {
std.mem.copyForwards(TextParticle, particles.buffer[index .. particles.len - 1], particles.buffer[index + 1 .. particles.len]);
particles.len -= 1;
}
fn reset(particles: *Particles, content: []const u8) void {
const x = @as(f32, @floatFromInt(rl.getScreenWidth())) / 2.0;
const y = @as(f32, @floatFromInt(rl.getScreenHeight())) / 2.0;
particles.buffer[0] = .init(content, x, y, .ray_white);
particles.len = 1;
}
};
fn randomColor() rl.Color {
return .{
.r = @intCast(rl.getRandomValue(0, 255)),
.g = @intCast(rl.getRandomValue(0, 255)),
.b = @intCast(rl.getRandomValue(0, 255)),
.a = 255,
};
}
pub fn main() void {
// Initialization
rl.initWindow(screen_width, screen_height, "raylib [text] example - strings management");
defer rl.closeWindow(); // Close window and OpenGL context
var particles: Particles = .{};
var grabbed: ?usize = null; // Index of the grabbed text particle
var press_offset: rl.Vector2 = .{ .x = 0, .y = 0 };
particles.reset("raylib => fun videogames programming!");
rl.setTargetFPS(60); // Set our game to run at 60 frames-per-second
// Main game loop
while (!rl.windowShouldClose()) { // Detect window close button or ESC key
// Update
const delta = rl.getFrameTime();
const mouse_pos = rl.getMousePosition();
// Checks if a text particle was grabbed
if (rl.isMouseButtonPressed(.left)) {
var i = particles.len;
while (i > 0) {
i -= 1;
const tp = &particles.buffer[i];
press_offset.x = mouse_pos.x - tp.rect.x;
press_offset.y = mouse_pos.y - tp.rect.y;
if (rl.checkCollisionPointRec(mouse_pos, tp.rect)) {
tp.grabbed = true;
grabbed = i;
break;
}
}
}
// Releases any text particle the was grabbed
if (rl.isMouseButtonReleased(.left)) {
if (grabbed) |index| {
particles.buffer[index].grabbed = false;
grabbed = null;
}
}
// Slice os shatter a text particle
if (rl.isMouseButtonPressed(.right)) {
var i = particles.len;
while (i > 0) {
i -= 1;
const tp = particles.buffer[i];
if (rl.checkCollisionPointRec(mouse_pos, tp.rect)) {
if (rl.isKeyDown(.left_shift)) {
shatterTextParticle(&particles, i);
} else {
sliceTextParticle(&particles, i, tp.len / 2);
}
break;
}
}
}
// Shake text particles
if (rl.isMouseButtonPressed(.middle)) {
for (particles.items()) |*tp| {
if (!tp.grabbed) tp.vel = .{
.x = @floatFromInt(rl.getRandomValue(-2000, 2000)),
.y = @floatFromInt(rl.getRandomValue(-2000, 2000)),
};
}
}
// Reset using TextTo* functions
if (rl.isKeyPressed(.one)) particles.reset("raylib => fun videogames programming!");
if (rl.isKeyPressed(.two)) particles.reset(rl.textToUpper("raylib => fun videogames programming!"));
if (rl.isKeyPressed(.three)) particles.reset(rl.textToLower("raylib => fun videogames programming!"));
if (rl.isKeyPressed(.four)) particles.reset(rl.textToPascal("raylib_fun_videogames_programming"));
if (rl.isKeyPressed(.five)) particles.reset(rl.textToSnake("RaylibFunVideogamesProgramming"));
if (rl.isKeyPressed(.six)) particles.reset(rl.textToCamel("raylib_fun_videogames_programming"));
// Slice by char pressed only when we have one text particle
const char_pressed: u8 = @truncate(@as(u32, @bitCast(rl.getCharPressed())));
if (char_pressed >= 'A' and char_pressed <= 'z' and particles.len == 1) {
sliceTextParticleByChar(&particles, char_pressed);
}
// Updates each text particle state
var i: usize = 0;
while (i < particles.len) : (i += 1) {
const tp = &particles.buffer[i];
// The text particle is not grabbed
if (!tp.grabbed) {
// text particle repositioning using the velocity
tp.rect.x += tp.vel.x * delta;
tp.rect.y += tp.vel.y * delta;
// Does the text particle hit the screen right boundary?
if ((tp.rect.x + tp.rect.width) >= screen_width) {
tp.rect.x = screen_width - tp.rect.width; // Text particle repositioning
tp.vel.x = -tp.vel.x * tp.elasticity; // Elasticity makes the text particle lose 10% of its velocity on hit
}
// Does the text particle hit the screen left boundary?
else if (tp.rect.x <= 0) {
tp.rect.x = 0.0;
tp.vel.x = -tp.vel.x * tp.elasticity;
}
// The same for y axis
if ((tp.rect.y + tp.rect.height) >= screen_height) {
tp.rect.y = screen_height - tp.rect.height;
tp.vel.y = -tp.vel.y * tp.elasticity;
} else if (tp.rect.y <= 0) {
tp.rect.y = 0.0;
tp.vel.y = -tp.vel.y * tp.elasticity;
}
// Friction makes the text particle lose 1% of its velocity each frame
tp.vel.x = tp.vel.x * tp.friction;
tp.vel.y = tp.vel.y * tp.friction;
} else {
// Text particle repositioning using the mouse position
tp.rect.x = mouse_pos.x - press_offset.x;
tp.rect.y = mouse_pos.y - press_offset.y;
// While the text particle is grabbed, recalculates its velocity
tp.vel.x = (tp.rect.x - tp.ppos.x) / delta;
tp.vel.y = (tp.rect.y - tp.ppos.y) / delta;
tp.ppos.x = tp.rect.x;
tp.ppos.y = tp.rect.y;
// Glue text particles when dragging and pressing left ctrl
if (rl.isKeyDown(.left_control)) {
var j: usize = 0;
while (j < particles.len) : (j += 1) {
const grabbed_index = grabbed.?;
const grabbed_tp = particles.buffer[grabbed_index];
if (j != grabbed_index and grabbed_tp.grabbed) {
if (rl.checkCollisionRecs(grabbed_tp.rect, particles.buffer[j].rect)) {
glueTextParticles(&particles, grabbed_index, j);
grabbed = particles.len - 1;
}
}
}
}
}
}
// Draw
rl.beginDrawing();
defer rl.endDrawing();
rl.clearBackground(.ray_white);
for (particles.items()) |*tp| {
rl.drawRectangleRec(.{
.x = tp.rect.x - tp.border_width,
.y = tp.rect.y - tp.border_width,
.width = tp.rect.width + tp.border_width * 2,
.height = tp.rect.height + tp.border_width * 2,
}, .black);
rl.drawRectangleRec(tp.rect, tp.color);
rl.drawText(tp.text(), @intFromFloat(tp.rect.x + tp.padding), @intFromFloat(tp.rect.y + tp.padding), font_size, .black);
}
rl.drawText("grab a text particle by pressing with the mouse and throw it by releasing", 10, 10, 10, .dark_gray);
rl.drawText("slice a text particle by pressing it with the mouse right button", 10, 30, 10, .dark_gray);
rl.drawText("shatter a text particle keeping left shift pressed and pressing it with the mouse right button", 10, 50, 10, .dark_gray);
rl.drawText("glue text particles by grabbing than and keeping left control pressed", 10, 70, 10, .dark_gray);
rl.drawText("1 to 6 to reset", 10, 90, 10, .dark_gray);
rl.drawText("when you have only one text particle, you can slice it by pressing a char", 10, 110, 10, .dark_gray);
rl.drawText(rl.textFormat("TEXT PARTICLE COUNT: {d}", .{particles.len}), 10, rl.getScreenHeight() - 30, 20, .black);
}
}
/// Replace the particle at `index` by slices of `slice_length` chars of its text
fn sliceTextParticle(particles: *Particles, index: usize, slice_length: usize) void {
const tp = particles.buffer[index];
const length = tp.len;
if (length > 1 and (particles.len + length) < max_text_particles) {
var i: usize = 0;
while (i < length) : (i += slice_length) {
const content = if (slice_length == 1)
rl.textFormat("{c}", .{tp.buffer[i]})
else
rl.textSubtext(tp.text(), @intCast(i), @intCast(slice_length));
const color = randomColor();
const x = tp.rect.x + @as(f32, @floatFromInt(i)) * tp.rect.width / @as(f32, @floatFromInt(length));
particles.append(.init(content, x, tp.rect.y, color));
}
particles.orderedRemove(index);
}
}
/// Replace the single particle by the pieces of its text split by `char_to_slice`
fn sliceTextParticleByChar(particles: *Particles, char_to_slice: u8) void {
const tp = particles.buffer[0];
const tokens = rl.textSplit(tp.text(), char_to_slice);
if (tokens.len > 1) {
for (tp.buffer[0..tp.len]) |char| {
if (char == char_to_slice) {
const color = randomColor();
particles.append(.init(rl.textFormat("{c}", .{char_to_slice}), tp.rect.x, tp.rect.y, color));
}
}
for (tokens, 0..) |token_ptr, i| {
const token = std.mem.span(token_ptr);
const color = randomColor();
const x = tp.rect.x + @as(f32, @floatFromInt(i)) * tp.rect.width / @as(f32, @floatFromInt(token.len));
particles.append(.init(token, x, tp.rect.y, color));
}
particles.orderedRemove(0);
}
}
/// Slice the particle at `index` into single chars
fn shatterTextParticle(particles: *Particles, index: usize) void {
sliceTextParticle(particles, index, 1);
}
/// Replace the grabbed and target particles by one joining their texts
fn glueTextParticles(particles: *Particles, grabbed: usize, target: usize) void {
const grabbed_tp = &particles.buffer[grabbed];
const content = rl.textFormat("{s}{s}", .{ grabbed_tp.text(), particles.buffer[target].text() });
var tp: TextParticle = .init(content, grabbed_tp.rect.x, grabbed_tp.rect.y, .ray_white);
tp.grabbed = true;
particles.append(tp);
grabbed_tp.grabbed = false;
// Remove the higher index first so the lower one stays valid
particles.orderedRemove(@max(grabbed, target));
particles.orderedRemove(@min(grabbed, target));
}/*******************************************************************************************
*
* raylib [text] example - strings management
*
* Example complexity rating: [★★★☆] 3/4
*
* Example originally created with raylib 6.0, last time updated with raylib 6.0
*
* Example contributed by David Buzatto (@davidbuzatto) and reviewed by Ramon Santamaria (@raysan5)
*
* Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
* BSD-like license that allows static linking with closed source software
*
* Copyright (c) 2025 David Buzatto (@davidbuzatto)
*
********************************************************************************************/
#include "raylib.h"
#include <stdlib.h>
#define MAX_TEXT_LENGTH 100
#define MAX_TEXT_PARTICLES 100
#define FONT_SIZE 30
//----------------------------------------------------------------------------------
// Types and Structures Definition
//----------------------------------------------------------------------------------
typedef struct TextParticle {
char text[MAX_TEXT_LENGTH];
Rectangle rect; // Boundary
Vector2 vel; // Velocity
Vector2 ppos; // Previous position
float padding;
float borderWidth;
float friction;
float elasticity;
Color color;
bool grabbed;
} TextParticle;
//----------------------------------------------------------------------------------
// Module Functions Declaration
//----------------------------------------------------------------------------------
void PrepareFirstTextParticle(const char* text, TextParticle *tps, int *particleCount);
TextParticle CreateTextParticle(const char *text, float x, float y, Color color);
void SliceTextParticle(TextParticle *tp, int particlePos, int sliceLength, TextParticle *tps, int *particleCount);
void SliceTextParticleByChar(TextParticle *tp, char charToSlice, TextParticle *tps, int *particleCount);
void ShatterTextParticle(TextParticle *tp, int particlePos, TextParticle *tps, int *particleCount);
void GlueTextParticles(TextParticle *grabbed, TextParticle *target, TextParticle *tps, int *particleCount);
void RealocateTextParticles(TextParticle *tps, int particlePos, int *particleCount);
//------------------------------------------------------------------------------------
// Program main entry point
//------------------------------------------------------------------------------------
int main(void)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [text] example - strings management");
TextParticle textParticles[MAX_TEXT_PARTICLES] = { 0 };
int particleCount = 0;
TextParticle *grabbedTextParticle = NULL;
Vector2 pressOffset = { 0 };
PrepareFirstTextParticle("raylib => fun videogames programming!", textParticles, &particleCount);
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//---------------------------------------------------------------------------------------
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
float delta = GetFrameTime();
Vector2 mousePos = GetMousePosition();
// Checks if a text particle was grabbed
if (IsMouseButtonPressed(MOUSE_BUTTON_LEFT))
{
for (int i = particleCount - 1; i >= 0; i--)
{
TextParticle *tp = &textParticles[i];
pressOffset.x = mousePos.x - tp->rect.x;
pressOffset.y = mousePos.y - tp->rect.y;
if (CheckCollisionPointRec(mousePos, tp->rect))
{
tp->grabbed = true;
grabbedTextParticle = tp;
break;
}
}
}
// Releases any text particle the was grabbed
if (IsMouseButtonReleased(MOUSE_BUTTON_LEFT))
{
if (grabbedTextParticle != NULL)
{
grabbedTextParticle->grabbed = false;
grabbedTextParticle = NULL;
}
}
// Slice os shatter a text particle
if (IsMouseButtonPressed(MOUSE_BUTTON_RIGHT))
{
for (int i = particleCount - 1; i >= 0; i--)
{
TextParticle *tp = &textParticles[i];
if (CheckCollisionPointRec(mousePos, tp->rect))
{
if (IsKeyDown(KEY_LEFT_SHIFT))
{
ShatterTextParticle(tp, i, textParticles, &particleCount);
}
else
{
SliceTextParticle(tp, i, TextLength(tp->text)/2, textParticles, &particleCount);
}
break;
}
}
}
// Shake text particles
if (IsMouseButtonPressed(MOUSE_BUTTON_MIDDLE))
{
for (int i = 0; i < particleCount; i++)
{
if (!textParticles[i].grabbed) textParticles[i].vel = (Vector2){ (float)GetRandomValue(-2000, 2000), (float)GetRandomValue(-2000, 2000) };
}
}
// Reset using TextTo* functions
if (IsKeyPressed(KEY_ONE)) PrepareFirstTextParticle("raylib => fun videogames programming!", textParticles, &particleCount);
if (IsKeyPressed(KEY_TWO)) PrepareFirstTextParticle(TextToUpper("raylib => fun videogames programming!"), textParticles, &particleCount);
if (IsKeyPressed(KEY_THREE)) PrepareFirstTextParticle(TextToLower("raylib => fun videogames programming!"), textParticles, &particleCount);
if (IsKeyPressed(KEY_FOUR)) PrepareFirstTextParticle(TextToPascal("raylib_fun_videogames_programming"), textParticles, &particleCount);
if (IsKeyPressed(KEY_FIVE)) PrepareFirstTextParticle(TextToSnake("RaylibFunVideogamesProgramming"), textParticles, &particleCount);
if (IsKeyPressed(KEY_SIX)) PrepareFirstTextParticle(TextToCamel("raylib_fun_videogames_programming"), textParticles, &particleCount);
// Slice by char pressed only when we have one text particle
char charPressed = GetCharPressed();
if ((charPressed >= 'A') && (charPressed <= 'z') && (particleCount == 1))
{
SliceTextParticleByChar(&textParticles[0], charPressed, textParticles, &particleCount);
}
// Updates each text particle state
for (int i = 0; i < particleCount; i++)
{
TextParticle *tp = &textParticles[i];
// The text particle is not grabbed
if (!tp->grabbed)
{
// text particle repositioning using the velocity
tp->rect.x += tp->vel.x * delta;
tp->rect.y += tp->vel.y * delta;
// Does the text particle hit the screen right boundary?
if ((tp->rect.x + tp->rect.width) >= screenWidth)
{
tp->rect.x = screenWidth - tp->rect.width; // Text particle repositioning
tp->vel.x = -tp->vel.x*tp->elasticity; // Elasticity makes the text particle lose 10% of its velocity on hit
}
// Does the text particle hit the screen left boundary?
else if (tp->rect.x <= 0)
{
tp->rect.x = 0.0f;
tp->vel.x = -tp->vel.x*tp->elasticity;
}
// The same for y axis
if ((tp->rect.y + tp->rect.height) >= screenHeight)
{
tp->rect.y = screenHeight - tp->rect.height;
tp->vel.y = -tp->vel.y*tp->elasticity;
}
else if (tp->rect.y <= 0)
{
tp->rect.y = 0.0f;
tp->vel.y = -tp->vel.y*tp->elasticity;
}
// Friction makes the text particle lose 1% of its velocity each frame
tp->vel.x = tp->vel.x*tp->friction;
tp->vel.y = tp->vel.y*tp->friction;
}
else
{
// Text particle repositioning using the mouse position
tp->rect.x = mousePos.x - pressOffset.x;
tp->rect.y = mousePos.y - pressOffset.y;
// While the text particle is grabbed, recalculates its velocity
tp->vel.x = (tp->rect.x - tp->ppos.x)/delta;
tp->vel.y = (tp->rect.y - tp->ppos.y)/delta;
tp->ppos.x = tp->rect.x;
tp->ppos.y = tp->rect.y;
// Glue text particles when dragging and pressing left ctrl
if (IsKeyDown(KEY_LEFT_CONTROL))
{
for (int i = 0; i < particleCount; i++)
{
if (&textParticles[i] != grabbedTextParticle && grabbedTextParticle->grabbed)
{
if (CheckCollisionRecs(grabbedTextParticle->rect, textParticles[i].rect))
{
GlueTextParticles(grabbedTextParticle, &textParticles[i], textParticles, &particleCount);
grabbedTextParticle = &textParticles[particleCount-1];
}
}
}
}
}
}
//----------------------------------------------------------------------------------
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
for (int i = 0; i < particleCount; i++)
{
TextParticle *tp = &textParticles[i];
DrawRectangleRec((Rectangle) { tp->rect.x - tp->borderWidth, tp->rect.y - tp->borderWidth, tp->rect.width + tp->borderWidth * 2, tp->rect.height + tp->borderWidth * 2 }, BLACK);
DrawRectangleRec(tp->rect, tp->color);
DrawText(tp->text, (int)(tp->rect.x+tp->padding), (int)(tp->rect.y+tp->padding), FONT_SIZE, BLACK);
}
DrawText("grab a text particle by pressing with the mouse and throw it by releasing", 10, 10, 10, DARKGRAY);
DrawText("slice a text particle by pressing it with the mouse right button", 10, 30, 10, DARKGRAY);
DrawText("shatter a text particle keeping left shift pressed and pressing it with the mouse right button", 10, 50, 10, DARKGRAY);
DrawText("glue text particles by grabbing than and keeping left control pressed", 10, 70, 10, DARKGRAY);
DrawText("1 to 6 to reset", 10, 90, 10, DARKGRAY);
DrawText("when you have only one text particle, you can slice it by pressing a char", 10, 110, 10, DARKGRAY);
DrawText(TextFormat("TEXT PARTICLE COUNT: %d", particleCount), 10, GetScreenHeight() - 30, 20, BLACK);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
//----------------------------------------------------------------------------------
// Module Functions Definition
//----------------------------------------------------------------------------------
void PrepareFirstTextParticle(const char* text, TextParticle *tps, int *particleCount)
{
tps[0] = CreateTextParticle(
text,
GetScreenWidth()/2.0f,
GetScreenHeight()/2.0f,
RAYWHITE
);
*particleCount = 1;
}
TextParticle CreateTextParticle(const char *text, float x, float y, Color color)
{
TextParticle tp = {
.text = "",
.rect = { x, y, 30, 30 },
.vel = { (float)GetRandomValue(-200, 200), (float)GetRandomValue(-200, 200) },
.ppos = { 0 },
.padding = 5.0f,
.borderWidth = 5.0f,
.friction = 0.99f,
.elasticity = 0.9f,
.color = color,
.grabbed = false
};
TextCopy(tp.text, text);
tp.rect.width = MeasureText(tp.text, FONT_SIZE)+tp.padding*2;
tp.rect.height = FONT_SIZE+tp.padding*2;
return tp;
}
void SliceTextParticle(TextParticle *tp, int particlePos, int sliceLength, TextParticle *tps, int *particleCount)
{
int length = TextLength(tp->text);
if((length > 1) && ((*particleCount+length) < MAX_TEXT_PARTICLES))
{
for (int i = 0; i < length; i += sliceLength)
{
const char *text = sliceLength == 1 ? TextFormat("%c", tp->text[i]) : TextSubtext(tp->text, i, sliceLength);
tps[(*particleCount)++] = CreateTextParticle(
text,
tp->rect.x + i * tp->rect.width/length,
tp->rect.y,
(Color) { GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255 }
);
}
RealocateTextParticles(tps, particlePos, particleCount);
}
}
void SliceTextParticleByChar(TextParticle *tp, char charToSlice, TextParticle *tps, int *particleCount)
{
int tokenCount = 0;
char **tokens = TextSplit(tp->text, charToSlice, &tokenCount);
if (tokenCount > 1)
{
int textLength = TextLength(tp->text);
for (int i = 0; i < textLength; i++)
{
if (tp->text[i] == charToSlice)
{
tps[(*particleCount)++] = CreateTextParticle(
TextFormat("%c", charToSlice),
tp->rect.x,
tp->rect.y,
(Color) { GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255 }
);
}
}
for (int i = 0; i < tokenCount; i++)
{
int tokenLength = TextLength(tokens[i]);
tps[(*particleCount)++] = CreateTextParticle(
TextFormat("%s", tokens[i]),
tp->rect.x + i * tp->rect.width/tokenLength,
tp->rect.y,
(Color) { GetRandomValue(0, 255), GetRandomValue(0, 255), GetRandomValue(0, 255), 255 }
);
}
if (tokenCount)
{
RealocateTextParticles(tps, 0, particleCount);
}
}
}
void ShatterTextParticle(TextParticle *tp, int particlePos, TextParticle *tps, int *particleCount)
{
SliceTextParticle(tp, particlePos, 1, tps, particleCount);
}
void GlueTextParticles(TextParticle *grabbed, TextParticle *target, TextParticle *tps, int *particleCount)
{
int p1 = -1;
int p2 = -1;
for (int i = 0; i < *particleCount; i++)
{
if (&tps[i] == grabbed) p1 = i;
if (&tps[i] == target) p2 = i;
}
if ((p1 != -1) && (p2 != -1))
{
TextParticle tp = CreateTextParticle(
TextFormat( "%s%s", grabbed->text, target->text),
grabbed->rect.x,
grabbed->rect.y,
RAYWHITE
);
tp.grabbed = true;
tps[(*particleCount)++] = tp;
grabbed->grabbed = false;
if (p1 < p2)
{
RealocateTextParticles(tps, p2, particleCount);
RealocateTextParticles(tps, p1, particleCount);
}
else
{
RealocateTextParticles(tps, p1, particleCount);
RealocateTextParticles(tps, p2, particleCount);
}
}
}
void RealocateTextParticles(TextParticle *tps, int particlePos, int *particleCount)
{
for (int i = particlePos+1; i < *particleCount; i++)
{
tps[i-1] = tps[i];
}
(*particleCount)--;
}From raylib 6.2. The original on GitHub.