├── .gitattributes ├── raylib_is_coool.gif ├── font ├── NotoSans-Bold.ttf ├── NotoSans-Italic.ttf ├── NotoSans-Regular.ttf └── NotoSans-BoldItalic.ttf ├── LICENSE ├── README.md ├── main.c └── DrawTextStyle.h /.gitattributes: -------------------------------------------------------------------------------- 1 | # Auto detect text files and perform LF normalization 2 | * text=auto 3 | -------------------------------------------------------------------------------- /raylib_is_coool.gif: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NathanGuilhot/Raylib_DrawTextStyle/HEAD/raylib_is_coool.gif -------------------------------------------------------------------------------- /font/NotoSans-Bold.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NathanGuilhot/Raylib_DrawTextStyle/HEAD/font/NotoSans-Bold.ttf -------------------------------------------------------------------------------- /font/NotoSans-Italic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NathanGuilhot/Raylib_DrawTextStyle/HEAD/font/NotoSans-Italic.ttf -------------------------------------------------------------------------------- /font/NotoSans-Regular.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NathanGuilhot/Raylib_DrawTextStyle/HEAD/font/NotoSans-Regular.ttf -------------------------------------------------------------------------------- /font/NotoSans-BoldItalic.ttf: -------------------------------------------------------------------------------- https://raw.githubusercontent.com/NathanGuilhot/Raylib_DrawTextStyle/HEAD/font/NotoSans-BoldItalic.ttf -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | MIT License 2 | 3 | Copyright (c) 2021 Nighten 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # DrawTextStyle( ) 2 | 3 | > a stylish markdown-like DrawText function for Raylib! 4 | 5 | ![Example](raylib_is_coool.gif) 6 | 7 | ## Usage 8 | 9 | You can use basic markdown syntax to style your text : 10 | 11 | ````markdown 12 | *italic* 13 | **Bold** 14 | ~wave animation~ 15 | ~~crossed~~ 16 | __underline__ 17 | ```` 18 | 19 | Of course, you can easily combine these effects with each other; maybe even all at once! 20 | 21 | ``` 22 | DrawTextStyle("~Hellooo~ :D", 100,100, DARKGRAY) 23 | ``` 24 | 25 | You can also use this function to specify the line spacing to use in your multi-line text (something you can't do by default in Raylib) 26 | 27 | I really recommend tweaking this function to your liking! I hope I wrote something clear enough. Have fun! 28 | 29 | ## How to setup 30 | 31 | - You need to load your font, with variations (normal, italic, bold, bold+italic) 32 | - Create a timer variable to animate the ~waaave~ (and add the delta-time each frame) 33 | - Include the [header](DrawTextStyle.h) file 34 | - Customize the *DrawTextStyle()* wrapper to use your favorite settings 35 | - **You're good to go!** 36 | 37 | Thank Raysan for making such a lovely framework <3 -------------------------------------------------------------------------------- /main.c: -------------------------------------------------------------------------------- 1 | /******************************************************************************************* 2 | * 3 | * DrawTextStyle() Example by Nighten 4 | * (Base on the raylib [core] example) 5 | * 6 | ********************************************************************************************/ 7 | 8 | #include "raylib.h" 9 | #include //So we can use sin() to animate the ~wave~ 10 | 11 | Font my_main_font; 12 | Font my_italic_font; 13 | Font my_bold_font; 14 | Font my_bolditalic_font; 15 | 16 | float timer; //timer is used to animate the text 17 | 18 | //Include the actual header file 19 | #include "DrawTextStyle.h" 20 | 21 | int main(void) 22 | { 23 | // Initialization 24 | //-------------------------------------------------------------------------------------- 25 | const int screenWidth = 800; 26 | const int screenHeight = 450; 27 | InitWindow(screenWidth, screenHeight, "DrawTextStyle() Example"); 28 | SetTargetFPS(60); 29 | 30 | //Loading our fonts 31 | my_main_font = LoadFont("font/NotoSans-Regular.ttf"); 32 | my_italic_font = LoadFont("font/NotoSans-Italic.ttf"); 33 | my_bold_font = LoadFont("font/NotoSans-Bold.ttf"); 34 | my_bolditalic_font = LoadFont("font/NotoSans-BoldItalic.ttf"); 35 | 36 | char* text_to_display = "**Congrats!** ~You just discovered the *DrawTextStyle*!~\nYou ~~should~~ can include this *function* in **__any project__**,\nand start ~having fun!!!~ "; 37 | 38 | //-------------------------------------------------------------------------------------- 39 | 40 | // Main game loop 41 | while (!WindowShouldClose()) 42 | { 43 | //Update the timer 44 | UpdateTimer(); 45 | 46 | // Draw 47 | //---------------------------------------------------------------------------------- 48 | BeginDrawing(); 49 | ClearBackground(RAYWHITE); 50 | 51 | //Style :) 52 | DrawTextStyle(text_to_display, 20, 100, 32, DARKGRAY); 53 | 54 | //No style :( 55 | DrawTextEx(my_main_font ,text_to_display, (Vector2){20, 275}, 32, 1, DARKGRAY); 56 | 57 | 58 | EndDrawing(); 59 | //---------------------------------------------------------------------------------- 60 | } 61 | 62 | // De-Initialization 63 | //-------------------------------------------------------------------------------------- 64 | CloseWindow(); 65 | 66 | UnloadFont(my_main_font); 67 | UnloadFont(my_italic_font); 68 | UnloadFont(my_bold_font); 69 | UnloadFont(my_bolditalic_font); 70 | 71 | //-------------------------------------------------------------------------------------- 72 | 73 | return 0; 74 | } 75 | -------------------------------------------------------------------------------- /DrawTextStyle.h: -------------------------------------------------------------------------------- 1 | 2 | // Alternative DrawText() function to use with raylib 3 | // Created by Nighten, using the code from the Raylib source code written by Ramon Santamaria (@raysan5) 4 | 5 | /* 6 | Features: 7 | 8 | *italic* 9 | **Bold** 10 | ~wave animation~ 11 | ~~crossed~~ 12 | __underline__ 13 | 14 | + user defined line spacing 15 | */ 16 | 17 | // okistuff/butter dog - because we are now including a few things. lets pragma once 18 | #pragma once 19 | 20 | // okistuff/butter dog - Include Raylib and math.h 21 | #include "raylib.h" 22 | #include 23 | 24 | 25 | // add Timer here 26 | float timer; 27 | 28 | // implemented in the bottom 29 | void UpdateTimer(); 30 | 31 | void DrawTextStyleEx(Font main_font, Font italic_font, Font bold_font, Font bolditalic_font, const char *text, Vector2 position, float fontSize, float spacing, float linespacing, float time, Color color); 32 | 33 | 34 | //It is really recommended to use a wrapper function like this : 35 | void DrawTextStyle(const char* text, float posx, float posy, int fontsize, Color color) 36 | { 37 | //Example: 38 | DrawTextStyleEx(my_main_font, my_italic_font, my_bold_font, my_bolditalic_font, 39 | text, (Vector2){posx, posy}, fontsize, 40 | 1, 1.1, timer, color); 41 | } 42 | 43 | 44 | void DrawTextStyleEx(Font main_font, Font italic_font, Font bold_font, Font bolditalic_font, const char *text, Vector2 position, float fontSize, float spacing, float linespacing, float time,Color color) 45 | { 46 | // DrawTextEx(Text_font, text, (Vector2){posX, posY},fontSize, 1,color); 47 | 48 | Font font = main_font; 49 | 50 | int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop 51 | 52 | int textOffsetY = 0; // Offset between lines (on line break '\n') 53 | float textOffsetX = 0.0f; // Offset X to next character to draw 54 | 55 | 56 | float scaleFactor = fontSize/font.baseSize; // Character quad scaling factor 57 | 58 | //Style flags 59 | bool flag_bold = false; 60 | bool flag_italic = false; 61 | bool flag_wave = false; 62 | bool flag_crossed = false; 63 | bool flag_underline = false; 64 | 65 | //Parameters for the waves effect 66 | float wave_x_range = 1; 67 | float wave_y_range = 2; 68 | int wave_x_speed = 4; 69 | int wave_y_speed = 4; 70 | float wave_x_offset = 0.5; 71 | float wave_y_offset = 0.5; 72 | 73 | 74 | for (int i = 0; i < length;) 75 | { 76 | // Get next codepoint from byte string and glyph index in font 77 | int codepointByteCount = 0; 78 | int codepoint = GetNextCodepoint(&text[i], &codepointByteCount); 79 | int index = GetGlyphIndex(font, codepoint); 80 | 81 | 82 | if (codepoint == 0x3f) codepointByteCount = 1; 83 | 84 | if (codepoint == '\n') 85 | { 86 | textOffsetY += (int)((font.baseSize * linespacing)*scaleFactor); 87 | textOffsetX = 0.0f; 88 | } 89 | else if (codepoint == '*') 90 | { 91 | if (GetNextCodepoint(&text[i+1], &codepointByteCount) == '*') // -> ** 92 | { 93 | flag_bold = !flag_bold; 94 | codepointByteCount += 1; 95 | } 96 | else 97 | { 98 | flag_italic = !flag_italic; 99 | } 100 | 101 | //Font Weight switching 102 | if (flag_bold && flag_italic) font = bolditalic_font; 103 | else if (flag_bold)font = bold_font; 104 | else if (flag_italic) font = italic_font; 105 | else font = main_font; 106 | } 107 | else if (codepoint == '~') // Maybe use a different symbol for wave effect (or use a escape character ?) 108 | { 109 | if (GetNextCodepoint(&text[i+1], &codepointByteCount) == '~') // -> ~~ 110 | { 111 | flag_crossed = !flag_crossed; 112 | codepointByteCount += 1; 113 | } 114 | else 115 | { 116 | flag_wave = !flag_wave; 117 | } 118 | } 119 | else if (codepoint == '_' && GetNextCodepoint(&text[i+1], &codepointByteCount) == '_') // -> __ 120 | { 121 | flag_underline = !flag_underline; 122 | codepointByteCount += 1; 123 | 124 | } 125 | else 126 | { 127 | if ((codepoint != ' ') && (codepoint != '\t')) 128 | { 129 | float position_x; 130 | float position_y; 131 | position_x = position.x + textOffsetX; 132 | position_y = position.y + textOffsetY; 133 | 134 | if (flag_wave) //Apply the wave effect 135 | { 136 | position_x += sin(time*wave_x_speed-i*wave_x_offset)*wave_x_range; 137 | position_y += sin(time*wave_y_speed-i*wave_y_offset)*wave_y_range; 138 | } 139 | 140 | DrawTextCodepoint(font, codepoint, (Vector2){ position_x, position_y }, fontSize, color); 141 | 142 | //Draw the crossed and underline 143 | //TODO: Draw these lines over spaces when needed 144 | if (flag_crossed) 145 | { 146 | DrawLine(position_x, position_y+fontSize/2, 147 | position_x + ((float)font.chars[index].advanceX*scaleFactor + spacing), position_y+fontSize/2, 148 | color); 149 | } 150 | 151 | if (flag_underline) 152 | { 153 | DrawLine(position_x, position_y+fontSize, 154 | position_x + ((float)font.chars[index].advanceX*scaleFactor + spacing), position_y+fontSize, 155 | color); 156 | } 157 | 158 | } 159 | 160 | if (font.chars[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing); 161 | else textOffsetX += ((float)font.chars[index].advanceX*scaleFactor + spacing); 162 | } 163 | 164 | i += codepointByteCount; // Move text bytes counter to next codepoint 165 | 166 | } 167 | } 168 | 169 | void UpdateTimer() 170 | { 171 | timer += GetFrameTime(); 172 | } 173 | --------------------------------------------------------------------------------