REVIEWED: text_draw_3d, code formatting

This commit is contained in:
raysan5 2021-03-28 21:09:34 +02:00
parent 6a24dd8945
commit 00e71faed6
2 changed files with 70 additions and 73 deletions

View file

@ -17,15 +17,18 @@
* This example has been created using raylib 3.5 (www.raylib.com)
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
* Example contributed by Vlad Adrian (@Demizdor) and reviewed by Ramon Santamaria (@raysan5)
*
* Copyright (C) 2021 Vlad Adrian (@Demizdor - https://github.com/Demizdor)
* Copyright (C) 2021 Ramon Santamaria (@raysan5)
*
********************************************************************************************/
#include <stddef.h> // for NULL
#include <math.h> // for sinf()
#include "raylib.h"
#include "rlgl.h"
#include <stddef.h> // Required for: NULL
#include <math.h> // Required for: sinf()
// To make it work with the older RLGL module just comment the line below
#define RAYLIB_NEW_RLGL
@ -34,13 +37,21 @@
//--------------------------------------------------------------------------------------
#define LETTER_BOUNDRY_SIZE 0.25f
#define TEXT_MAX_LAYERS 32
#define LETTER_BOUNDRY_COLOR VIOLET
bool SHOW_LETTER_BOUNDRY = false;
#define LETTER_BOUDRY_COLOR VIOLET
bool SHOW_TEXT_BOUNDRY = false;
//--------------------------------------------------------------------------------------
// Data Types definition
//--------------------------------------------------------------------------------------
// Configuration structure for waving the text
typedef struct {
Vector3 waveRange;
Vector3 waveSpeed;
Vector3 waveOffset;
} WaveTextConfig;
//--------------------------------------------------------------------------------------
// Module Functions Declaration
@ -51,12 +62,7 @@ void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, float fontS
void DrawText3D(Font font, const char *text, Vector3 position, float fontSize, float fontSpacing, float lineSpacing, bool backface, Color tint);
// Measure a text in 3D. For some reason `MeasureTextEx()` just doesn't seem to work so i had to use this instead.
Vector3 MeasureText3D(Font font, const char* text, float fontSize, float fontSpacing, float lineSpacing);
// Configuration structure for waving the text
typedef struct {
Vector3 waveRange;
Vector3 waveSpeed;
Vector3 waveOffset;
} WaveTextConfig;
// Draw a 2D text in 3D space and wave the parts that start with `~~` and end with `~~`.
// This is a modified version of the original code by @Nighten found here https://github.com/NightenDushi/Raylib_DrawTextStyle
void DrawTextWave3D(Font font, const char *text, Vector3 position, float fontSize, float fontSpacing, float lineSpacing, bool backface, WaveTextConfig* config, float time, Color tint);
@ -64,9 +70,6 @@ void DrawTextWave3D(Font font, const char *text, Vector3 position, float fontSiz
Vector3 MeasureTextWave3D(Font font, const char* text, float fontSize, float fontSpacing, float lineSpacing);
// Generates a nice color with a random hue
Color GenerateRandomColor(float s, float v);
//--------------------------------------------------------------------------------------
//------------------------------------------------------------------------------------
// Program main entry point
@ -105,7 +108,7 @@ int main(void)
float fontSpacing = 0.5f;
float lineSpacing = -1.0f;
// Set the text
// Set the text (using markdown!)
char text[64] = "Hello ~~World~~ in 3D!";
Vector3 tbox = {0};
int layers = 1;
@ -124,7 +127,7 @@ int main(void)
Color dark = RED;
// Load the alpha discard shader
Shader alphaDiscard = LoadShader(NULL, "resources/shaders/glsl330/alphaDiscard.fs");
Shader alphaDiscard = LoadShader(NULL, "resources/shaders/glsl330/alpha_discard.fs");
// Array filled with multiple random colors (when multicolor mode is set)
Color multi[TEXT_MAX_LAYERS] = {0};
@ -193,7 +196,7 @@ int main(void)
(Vector3){ cubePosition.x + cubeSize.x/2, cubePosition.y + cubeSize.y/2, cubePosition.z + cubeSize.z/2 }});
if (collision)
{
// generate new random colors
// Generate new random colors
light = GenerateRandomColor(0.5f, 0.78f);
dark = GenerateRandomColor(0.4f, 0.58f);
}
@ -214,8 +217,7 @@ int main(void)
else if (IsKeyDown(KEY_DELETE)) layerDistance += 0.001f;
else if (IsKeyPressed(KEY_TAB))
{
// enable /disable multicolor mode
multicolor = !multicolor;
multicolor = !multicolor; // Enable /disable multicolor mode
if (multicolor)
{
@ -232,7 +234,7 @@ int main(void)
int ch = GetCharPressed();
if (IsKeyPressed(KEY_BACKSPACE))
{
// remove last char
// Remove last char
int len = TextLength(text);
if (len > 0) text[len - 1] = '\0';
}
@ -240,7 +242,8 @@ int main(void)
{
// handle newline
int len = TextLength(text);
if(len < sizeof(text)-1) {
if (len < sizeof(text) - 1)
{
text[len] = '\n';
text[len+1] ='\0';
}
@ -249,7 +252,8 @@ int main(void)
{
// append only printable chars
int len = TextLength(text);
if(len < sizeof(text)-1) {
if (len < sizeof(text) - 1)
{
text[len] = ch;
text[len+1] ='\0';
}
@ -283,6 +287,7 @@ int main(void)
rlPushMatrix();
rlRotatef(90.0f, 1.0f, 0.0f, 0.0f);
rlRotatef(90.0f, 0.0f, 0.0f, -1.0f);
for (int i = 0; i < layers; ++i)
{
Color clr = light;
@ -290,8 +295,8 @@ int main(void)
DrawTextWave3D(font, text, (Vector3){ -tbox.x/2.0f, layerDistance*i, -4.5f }, fontSize, fontSpacing, lineSpacing, true, &wcfg, time, clr);
}
if(SHOW_TEXT_BOUNDRY) // draw the text boundry if set
DrawCubeWiresV((Vector3){0.0f, 0.0f, -4.5f+tbox.z/2}, tbox, dark);
// Draw the text boundry if set
if (SHOW_TEXT_BOUNDRY) DrawCubeWiresV((Vector3){ 0.0f, 0.0f, -4.5f + tbox.z/2 }, tbox, dark);
rlPopMatrix();
// Don't draw the letter boundries for the 3D text below
@ -436,12 +441,9 @@ int main(void)
return 0;
}
//--------------------------------------------------------------------------------------
// Module Functions Definitions
//--------------------------------------------------------------------------------------
// Draw codepoint at specified position in 3D space
void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, float fontSize, bool backface, Color tint)
{
@ -476,7 +478,7 @@ void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, float fontS
const float th = (srcRec.y+srcRec.height)/font.texture.height;
if(SHOW_LETTER_BOUNDRY)
DrawCubeWiresV((Vector3){ position.x+width/2, position.y, position.z+height/2}, (Vector3){width, LETTER_BOUNDRY_SIZE, height}, LETTER_BOUDRY_COLOR);
DrawCubeWiresV((Vector3){ position.x + width/2, position.y, position.z + height/2}, (Vector3){ width, LETTER_BOUNDRY_SIZE, height }, LETTER_BOUNDRY_COLOR);
#if defined(RAYLIB_NEW_RLGL)
rlCheckRenderBatchLimit(4 + 4*backface);
@ -515,11 +517,9 @@ void DrawTextCodepoint3D(Font font, int codepoint, Vector3 position, float fontS
#else
rlDisableTexture();
#endif
}
}
void DrawText3D(Font font, const char *text, Vector3 position, float fontSize, float fontSpacing, float lineSpacing, bool backface, Color tint)
{
int length = TextLength(text); // Total length in bytes of the text, scanned by codepoints in loop
@ -529,7 +529,6 @@ void DrawText3D(Font font, const char *text, Vector3 position, float fontSize, f
float scale = fontSize/(float)font.baseSize;
for (int i = 0; i < length;)
{
// Get next codepoint from byte string and glyph index in font
@ -569,7 +568,6 @@ Vector3 MeasureText3D(Font font, const char* text, float fontSize, float fontSpa
int tempLen = 0; // Used to count longer text line num chars
int lenCounter = 0;
float tempTextWidth = 0.0f; // Used to count longer text line width
float scale = fontSize/(float)font.baseSize;
@ -686,7 +684,6 @@ Vector3 MeasureTextWave3D(Font font, const char* text, float fontSize, float fon
int tempLen = 0; // Used to count longer text line num chars
int lenCounter = 0;
float tempTextWidth = 0.0f; // Used to count longer text line width
float scale = fontSize/(float)font.baseSize;
@ -744,7 +741,7 @@ Vector3 MeasureTextWave3D(Font font, const char* text, float fontSize, float fon
Color GenerateRandomColor(float s, float v)
{
const float Phi = 0.618033988749895; // golden ratio conjugate
const float Phi = 0.618033988749895f; // Golden ratio conjugate
float h = GetRandomValue(0, 360);
h = fmodf((h + h*Phi), 360.0f);
return ColorFromHSV(h, s, v);