-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathDrawTextStyle.h
172 lines (135 loc) · 5.43 KB
/
DrawTextStyle.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
// Alternative DrawText() function to use with raylib
// Created by Nighten, using the code from the Raylib source code written by Ramon Santamaria (@raysan5)
/*
Features:
*italic*
**Bold**
~wave animation~
~~crossed~~
__underline__
+ user defined line spacing
*/
// okistuff/butter dog - because we are now including a few things. lets pragma once
#pragma once
// okistuff/butter dog - Include Raylib and math.h
#include "raylib.h"
#include <math.h>
// add Timer here
float timer;
// implemented in the bottom
void UpdateTimer();
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);
//It is really recommended to use a wrapper function like this :
void DrawTextStyle(const char* text, float posx, float posy, int fontsize, Color color)
{
//Example:
DrawTextStyleEx(my_main_font, my_italic_font, my_bold_font, my_bolditalic_font,
text, (Vector2){posx, posy}, fontsize,
1, 1.1, timer, color);
}
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)
{
// DrawTextEx(Text_font, text, (Vector2){posX, posY},fontSize, 1,color);
Font font = main_font;
int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop
int textOffsetY = 0; // Offset between lines (on line break '\n')
float textOffsetX = 0.0f; // Offset X to next character to draw
float scaleFactor = fontSize/font.baseSize; // Character quad scaling factor
//Style flags
bool flag_bold = false;
bool flag_italic = false;
bool flag_wave = false;
bool flag_crossed = false;
bool flag_underline = false;
//Parameters for the waves effect
float wave_x_range = 1;
float wave_y_range = 2;
int wave_x_speed = 4;
int wave_y_speed = 4;
float wave_x_offset = 0.5;
float wave_y_offset = 0.5;
for (int i = 0; i < length;)
{
// Get next codepoint from byte string and glyph index in font
int codepointByteCount = 0;
int codepoint = GetNextCodepoint(&text[i], &codepointByteCount);
int index = GetGlyphIndex(font, codepoint);
if (codepoint == 0x3f) codepointByteCount = 1;
if (codepoint == '\n')
{
textOffsetY += (int)((font.baseSize * linespacing)*scaleFactor);
textOffsetX = 0.0f;
}
else if (codepoint == '*')
{
if (GetNextCodepoint(&text[i+1], &codepointByteCount) == '*') // -> **
{
flag_bold = !flag_bold;
codepointByteCount += 1;
}
else
{
flag_italic = !flag_italic;
}
//Font Weight switching
if (flag_bold && flag_italic) font = bolditalic_font;
else if (flag_bold)font = bold_font;
else if (flag_italic) font = italic_font;
else font = main_font;
}
else if (codepoint == '~') // Maybe use a different symbol for wave effect (or use a escape character ?)
{
if (GetNextCodepoint(&text[i+1], &codepointByteCount) == '~') // -> ~~
{
flag_crossed = !flag_crossed;
codepointByteCount += 1;
}
else
{
flag_wave = !flag_wave;
}
}
else if (codepoint == '_' && GetNextCodepoint(&text[i+1], &codepointByteCount) == '_') // -> __
{
flag_underline = !flag_underline;
codepointByteCount += 1;
}
else
{
if ((codepoint != ' ') && (codepoint != '\t'))
{
float position_x;
float position_y;
position_x = position.x + textOffsetX;
position_y = position.y + textOffsetY;
if (flag_wave) //Apply the wave effect
{
position_x += sin(time*wave_x_speed-i*wave_x_offset)*wave_x_range;
position_y += sin(time*wave_y_speed-i*wave_y_offset)*wave_y_range;
}
DrawTextCodepoint(font, codepoint, (Vector2){ position_x, position_y }, fontSize, color);
//Draw the crossed and underline
//TODO: Draw these lines over spaces when needed
if (flag_crossed)
{
DrawLine(position_x, position_y+fontSize/2,
position_x + ((float)font.chars[index].advanceX*scaleFactor + spacing), position_y+fontSize/2,
color);
}
if (flag_underline)
{
DrawLine(position_x, position_y+fontSize,
position_x + ((float)font.chars[index].advanceX*scaleFactor + spacing), position_y+fontSize,
color);
}
}
if (font.chars[index].advanceX == 0) textOffsetX += ((float)font.recs[index].width*scaleFactor + spacing);
else textOffsetX += ((float)font.chars[index].advanceX*scaleFactor + spacing);
}
i += codepointByteCount; // Move text bytes counter to next codepoint
}
}
void UpdateTimer()
{
timer += GetFrameTime();
}