mirror of
				https://github.com/raysan5/raylib.git
				synced 2025-10-26 12:27:01 +00:00 
			
		
		
		
	ADDED: SplitText() function
This commit is contained in:
		| @@ -1088,9 +1088,12 @@ RLAPI void DrawTextEx(Font font, const char *text, Vector2 position, float fontS | |||||||
| // Text misc. functions | // Text misc. functions | ||||||
| RLAPI int MeasureText(const char *text, int fontSize);                                      // Measure string width for default font | RLAPI int MeasureText(const char *text, int fontSize);                                      // Measure string width for default font | ||||||
| RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing);    // Measure string size for Font | RLAPI Vector2 MeasureTextEx(Font font, const char *text, float fontSize, float spacing);    // Measure string size for Font | ||||||
|  | RLAPI int GetGlyphIndex(Font font, int character);                                          // Get index position for a unicode character on font | ||||||
|  |  | ||||||
|  | // Text string edition functions | ||||||
| RLAPI const char *FormatText(const char *text, ...);                    // Formatting of text with variables to 'embed' | RLAPI const char *FormatText(const char *text, ...);                    // Formatting of text with variables to 'embed' | ||||||
| RLAPI const char *SubText(const char *text, int position, int length);  // Get a piece of a text string | RLAPI const char *SubText(const char *text, int position, int length);  // Get a piece of a text string | ||||||
| RLAPI int GetGlyphIndex(Font font, int character);                                          // Get index position for a unicode character on font | RLAPI char **SplitText(char *text, char delimiter, int *strCount);      // Split text string into multiple strings (memory should be freed manually!) | ||||||
|  |  | ||||||
| //------------------------------------------------------------------------------------ | //------------------------------------------------------------------------------------ | ||||||
| // Basic 3d Shapes Drawing Functions (Module: models) | // Basic 3d Shapes Drawing Functions (Module: models) | ||||||
|   | |||||||
							
								
								
									
										142
									
								
								src/text.c
									
									
									
									
									
								
							
							
						
						
									
										142
									
								
								src/text.c
									
									
									
									
									
								
							| @@ -566,6 +566,28 @@ void UnloadFont(Font font) | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
|  | // Shows current FPS on top-left corner | ||||||
|  | // NOTE: Uses default font | ||||||
|  | void DrawFPS(int posX, int posY) | ||||||
|  | { | ||||||
|  |     // NOTE: We are rendering fps every second for better viewing on high framerates | ||||||
|  |  | ||||||
|  |     static int fps = 0; | ||||||
|  |     static int counter = 0; | ||||||
|  |     static int refreshRate = 20; | ||||||
|  |  | ||||||
|  |     if (counter < refreshRate) counter++; | ||||||
|  |     else | ||||||
|  |     { | ||||||
|  |         fps = GetFPS(); | ||||||
|  |         refreshRate = fps; | ||||||
|  |         counter = 0; | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     // NOTE: We have rounding errors every frame, so it oscillates a lot | ||||||
|  |     DrawText(FormatText("%2i FPS", fps), posX, posY, 20, LIME); | ||||||
|  | } | ||||||
|  |  | ||||||
| // Draw text (using default font) | // Draw text (using default font) | ||||||
| // NOTE: fontSize work like in any drawing program but if fontSize is lower than font-base-size, then font-base-size is used | // NOTE: fontSize work like in any drawing program but if fontSize is lower than font-base-size, then font-base-size is used | ||||||
| // NOTE: chars spacing is proportional to fontSize | // NOTE: chars spacing is proportional to fontSize | ||||||
| @@ -642,44 +664,6 @@ void DrawTextEx(Font font, const char *text, Vector2 position, float fontSize, f | |||||||
|     } |     } | ||||||
| } | } | ||||||
|  |  | ||||||
| // Formatting of text with variables to 'embed' |  | ||||||
| const char *FormatText(const char *text, ...) |  | ||||||
| { |  | ||||||
|     static char buffer[MAX_FORMATTEXT_LENGTH]; |  | ||||||
|  |  | ||||||
|     va_list args; |  | ||||||
|     va_start(args, text); |  | ||||||
|     vsprintf(buffer, text, args); |  | ||||||
|     va_end(args); |  | ||||||
|  |  | ||||||
|     return buffer; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Get a piece of a text string |  | ||||||
| const char *SubText(const char *text, int position, int length) |  | ||||||
| { |  | ||||||
|     static char buffer[MAX_SUBTEXT_LENGTH] = { 0 }; |  | ||||||
|     int textLength = strlen(text); |  | ||||||
|  |  | ||||||
|     if (position >= textLength) |  | ||||||
|     { |  | ||||||
|         position = textLength - 1; |  | ||||||
|         length = 0; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     if (length >= textLength) length = textLength; |  | ||||||
|  |  | ||||||
|     for (int c = 0 ; c < length ; c++) |  | ||||||
|     { |  | ||||||
|         *(buffer + c) = *(text + position); |  | ||||||
|         text++; |  | ||||||
|     } |  | ||||||
|  |  | ||||||
|     *(buffer + length) = '\0'; |  | ||||||
|  |  | ||||||
|     return buffer; |  | ||||||
| } |  | ||||||
|  |  | ||||||
| // Measure string width for default font | // Measure string width for default font | ||||||
| int MeasureText(const char *text, int fontSize) | int MeasureText(const char *text, int fontSize) | ||||||
| { | { | ||||||
| @@ -764,26 +748,78 @@ int GetGlyphIndex(Font font, int character) | |||||||
| #endif | #endif | ||||||
| } | } | ||||||
|  |  | ||||||
| // Shows current FPS on top-left corner | // Formatting of text with variables to 'embed' | ||||||
| // NOTE: Uses default font | const char *FormatText(const char *text, ...) | ||||||
| void DrawFPS(int posX, int posY) |  | ||||||
| { | { | ||||||
|     // NOTE: We are rendering fps every second for better viewing on high framerates |     static char buffer[MAX_FORMATTEXT_LENGTH]; | ||||||
|  |  | ||||||
|     static int fps = 0; |     va_list args; | ||||||
|     static int counter = 0; |     va_start(args, text); | ||||||
|     static int refreshRate = 20; |     vsprintf(buffer, text, args); | ||||||
|  |     va_end(args); | ||||||
|  |  | ||||||
|     if (counter < refreshRate) counter++; |     return buffer; | ||||||
|     else |  | ||||||
|     { |  | ||||||
|         fps = GetFPS(); |  | ||||||
|         refreshRate = fps; |  | ||||||
|         counter = 0; |  | ||||||
| } | } | ||||||
|  |  | ||||||
|     // NOTE: We have rounding errors every frame, so it oscillates a lot | // Get a piece of a text string | ||||||
|     DrawText(FormatText("%2i FPS", fps), posX, posY, 20, LIME); | const char *SubText(const char *text, int position, int length) | ||||||
|  | { | ||||||
|  |     static char buffer[MAX_SUBTEXT_LENGTH] = { 0 }; | ||||||
|  |     int textLength = strlen(text); | ||||||
|  |  | ||||||
|  |     if (position >= textLength) | ||||||
|  |     { | ||||||
|  |         position = textLength - 1; | ||||||
|  |         length = 0; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     if (length >= textLength) length = textLength; | ||||||
|  |  | ||||||
|  |     for (int c = 0 ; c < length ; c++) | ||||||
|  |     { | ||||||
|  |         *(buffer + c) = *(text + position); | ||||||
|  |         text++; | ||||||
|  |     } | ||||||
|  |  | ||||||
|  |     *(buffer + length) = '\0'; | ||||||
|  |  | ||||||
|  |     return buffer; | ||||||
|  | } | ||||||
|  |  | ||||||
|  | // Split string into multiple strings | ||||||
|  | // NOTE: Files count is returned by parameters pointer | ||||||
|  | // NOTE: Allocated memory should be manually freed | ||||||
|  | char **SplitText(char *text, char delimiter, int *strCount) | ||||||
|  | { | ||||||
|  |     #define MAX_SUBSTRING_LENGTH 128 | ||||||
|  |  | ||||||
|  |     char **strings = NULL; | ||||||
|  |     int len = strlen(text); | ||||||
|  |     char *strDup = (char *)malloc(len + 1); | ||||||
|  |     strcpy(strDup, text); | ||||||
|  |     int counter = 1; | ||||||
|  |      | ||||||
|  |     // Count how many substrings we have on string | ||||||
|  |     for (int i = 0; i < len; i++) if (text[i] == delimiter) counter++; | ||||||
|  |      | ||||||
|  |     // Memory allocation for substrings | ||||||
|  |     strings = (char **)malloc(sizeof(char *)*counter); | ||||||
|  |     for (int i = 0; i < counter; i++) strings[i] = (char *)malloc(sizeof(char)*MAX_SUBSTRING_LENGTH); | ||||||
|  |      | ||||||
|  |     char *substrPtr = NULL; | ||||||
|  |     char delimiters[1] = { delimiter };         // Only caring for one delimiter | ||||||
|  |     substrPtr = strtok(strDup, delimiters); | ||||||
|  |      | ||||||
|  |     for (int i = 0; (i < counter) && (substrPtr != NULL); i++) | ||||||
|  |     { | ||||||
|  |         strcpy(strings[i], substrPtr); | ||||||
|  |         substrPtr = strtok(NULL, delimiters); | ||||||
|  |     } | ||||||
|  |      | ||||||
|  |     *strCount = counter; | ||||||
|  |     free(strDup); | ||||||
|  |      | ||||||
|  |     return strings; | ||||||
| } | } | ||||||
|  |  | ||||||
| //---------------------------------------------------------------------------------- | //---------------------------------------------------------------------------------- | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 Ray
					Ray