Skip to content

Instantly share code, notes, and snippets.

@davydmaker
Created July 16, 2025 01:54
Show Gist options
  • Save davydmaker/5e4c42b9b231ab98286116cceeb8c4b8 to your computer and use it in GitHub Desktop.
Save davydmaker/5e4c42b9b231ab98286116cceeb8c4b8 to your computer and use it in GitHub Desktop.
GameMaker GML: Optimized Text Outline Functions
#macro THICKNESS 1
/**
* Draws text with an outline effect by rendering the text multiple times
* with slight offsets before drawing the main text on top.
*
* @param {real} _x - X position to draw the text
* @param {real} _y - Y position to draw the text
* @param {string} _text - The text string to draw
* @param {color} _textColor - Color of the main text (default: white)
* @param {real} _outlineThickness - Thickness of the outline in pixels (default: THICKNESS macro)
* @param {color} _outlineColor - Color of the outline (default: black)
*
* @example
* // Draw white text with black outline
* draw_text_with_outline(100, 100, "Hello World!", c_white, 2, c_black);
*
* // Draw red text with yellow outline using default thickness
* draw_text_with_outline(200, 200, "Score: 1000", c_red, THICKNESS, c_yellow);
*/
function draw_text_with_outline(_x, _y, _text, _textColor = c_white, _outlineThickness = THICKNESS, _outlineColor = c_black) {
// Convert text to handle newlines properly
var processed_text = string_hash_to_newline(_text);
// Set outline color once before drawing all outline instances
draw_set_color(_outlineColor);
// Draw outline by rendering text at offset positions around the main position
// This creates a uniform outline effect in all directions
for (var offset_x = -_outlineThickness; offset_x <= _outlineThickness; offset_x++) {
for (var offset_y = -_outlineThickness; offset_y <= _outlineThickness; offset_y++) {
// Skip the center position (0,0) as that's where the main text will be drawn
if (offset_x != 0 || offset_y != 0) {
draw_text(_x + offset_x, _y + offset_y, processed_text);
}
}
}
// Draw the main text on top of the outline
draw_set_color(_textColor);
draw_text(_x, _y, processed_text);
}
/**
* Alternative optimized version that draws outline in 8 directions only
* More performance-friendly for thicker outlines
*
* @param {real} _x - X position to draw the text
* @param {real} _y - Y position to draw the text
* @param {string} _text - The text string to draw
* @param {color} _textColor - Color of the main text (default: white)
* @param {real} _outlineThickness - Thickness of the outline in pixels (default: THICKNESS macro)
* @param {color} _outlineColor - Color of the outline (default: black)
*/
function draw_text_with_outline_optimized(_x, _y, _text, _textColor = c_white, _outlineThickness = THICKNESS, _outlineColor = c_black) {
var processed_text = string_hash_to_newline(_text);
// Set outline color
draw_set_color(_outlineColor);
// Draw outline in 8 directions (cardinal + diagonal)
// Top row
draw_text(_x - _outlineThickness, _y - _outlineThickness, processed_text); // Top-left
draw_text(_x, _y - _outlineThickness, processed_text); // Top
draw_text(_x + _outlineThickness, _y - _outlineThickness, processed_text); // Top-right
// Middle row (skip center)
draw_text(_x - _outlineThickness, _y, processed_text); // Left
draw_text(_x + _outlineThickness, _y, processed_text); // Right
// Bottom row
draw_text(_x - _outlineThickness, _y + _outlineThickness, processed_text); // Bottom-left
draw_text(_x, _y + _outlineThickness, processed_text); // Bottom
draw_text(_x + _outlineThickness, _y + _outlineThickness, processed_text); // Bottom-right
// Draw main text
draw_set_color(_textColor);
draw_text(_x, _y, processed_text);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment