In Raylib, how do I create a translucent outline around translucent text, without the…
- User
In Raylib, how do I create a translucent outline around translucent text, without the text stopping to be translucent?
- Assistant
In Raylib, how do I create a translucent outline around translucent text, without the text stopping to be translucent?
Creating a translucent outline around translucent text in Raylib requires careful handling of blending modes to ensure that both the text and its outline maintain their desired transparency levels. Here's a step-by-step guide to achieve this effect:
When you draw an outline by rendering the text multiple times with slight offsets, overlapping these renders can cause the alpha (transparency) values to accumulate. This can unintentionally make the outline more opaque than intended, affecting the translucency of the main text.
To prevent the outline from making the text opaque:
Here's a function that encapsulates this approach:
#include "raylib.h"
void DrawTextWithTranslucentOutline(Vector2 position, const char *text, float fontSize, float spacing, Color textColor, Color outlineColor, float outlineThickness, Font font)
{
// Save the current blend mode
BlendMode previousBlend = GetBlendMode();
// Set blend mode to additive for the outline
SetBlendMode(BLEND_ADDITIVE);
// Draw the outline by offsetting the text in multiple directions
for (int dx = -outlineThickness; dx <= outlineThickness; dx++) {
for (int dy = -outlineThickness; dy <= outlineThickness; dy++) {
if (dx != 0 || dy != 0) { // Skip the center position
Vector2 offsetPosition = { position.x + dx, position.y + dy };
DrawTextEx(font, text, offsetPosition, fontSize, spacing, outlineColor);
}
}
}
// Restore the previous blend mode
SetBlendMode(previousBlend);
// Draw the main text with normal alpha blending
DrawTextEx(font, text, position, fontSize, spacing, textColor);
}
Save Current Blend Mode: It's essential to remember the current blend mode so you can restore it after drawing the outline.
Set Additive Blending for the Outline:
BLEND_ADDITIVE): This mode adds the color values of the source and destination pixels. It works well for outlines because it prevents the alpha from accumulating excessively, maintaining the translucency.Draw the Outline:
dx == 0 && dy == 0) is skipped to avoid overdrawing the main text area.Restore the Previous Blend Mode: After drawing the outline, it's crucial to set the blend mode back to its original state to ensure that subsequent drawing operations behave as expected.
Draw the Main Text:
textColor).Here's how you can use the DrawTextWithTranslucentOutline function within your Raylib application:
int main(void)
{
// Initialization
const int screenWidth = 800;
const int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "Raylib - Translucent Text Outline Example");
Font font = LoadFont("resources/your_font.ttf"); // Replace with your font path
Vector2 position = { 200, 200 };
const char *text = "Hello, Raylib!";
float fontSize = 40.0f;
float spacing = 2.0f;
Color textColor = (Color){ 255, 255, 255, 200 }; // White text with 200/255 alpha
Color outlineColor = (Color){ 0, 0, 0, 100 }; // Black outline with 100/255 alpha
float outlineThickness = 2.0f;
SetTargetFPS(60);
// Main game loop
while (!WindowShouldClose())
{
// Draw
BeginDrawing();
ClearBackground(RAYWHITE);
DrawTextWithTranslucentOutline(position, text, fontSize, spacing, textColor, outlineColor, outlineThickness, font);
EndDrawing();
}
// De-Initialization
UnloadFont(font);
CloseWindow();
return 0;
}
Font Rendering: Ensure that the font you're using (your_font.ttf in the example) is properly loaded and supports the characters you intend to render.
Performance: Drawing the outline involves multiple draw calls (typically 8 around the main text). While this is generally fine for text rendering, be mindful if you're rendering a large amount of text or doing this every frame.
Adjusting Thickness: The outlineThickness parameter controls how thick the outline appears. Increasing this value will make the outline more pronounced but will also increase the number of draw calls.
If you require more advanced text outlining techniques or better performance, consider:
By strategically using blending modes and rendering the text multiple times with slight offsets, you can effectively create a translucent outline around translucent text in Raylib. This method ensures that both the text and its outline maintain their intended transparency, resulting in a visually appealing effect without unintended opacity issues.
Note: Always ensure that the blending modes and color alpha values are adjusted according to your specific needs and the overall aesthetics of your application.
Not yet included in a published release.