Review example formatting

This commit is contained in:
Ray 2019-05-06 10:46:56 +02:00
parent 528e164ac5
commit eeef7fdb51
1 changed files with 123 additions and 123 deletions

View File

@ -9,9 +9,10 @@
*
********************************************************************************************/
#include "raylib.h"
#include <stdio.h>
#include <string.h>
#include "raylib.h"
#define SIZEOF(A) (sizeof(A)/sizeof(A[0]))
#define EMOJI_PER_WIDTH 8
@ -125,14 +126,12 @@ struct {
{"\xED\x95\x9C\xEA\xB5\xAD\xEB\xA7\x90\x20\xED\x95\x98\xEC\x8B\xA4\x20\xEC\xA4\x84\x20\xEC\x95\x84\xEC\x84\xB8\xEC\x9A\x94\x3F", "Korean"},
};
// Forward declaration of our function
//--------------------------------------------------------------------------------------
void Draw(); // Draws emojis and the text bubbles
static inline void RandomizeEmoji(); // Fills the emoji array with random emojis
// Module functions declaration
//--------------------------------------------------------------------------------------
static void RandomizeEmoji(void); // Fills the emoji array with random emojis
//--------------------------------------------------------------------------------------
// Global variables
//--------------------------------------------------------------------------------------
// Arrays that holds the random emojis
@ -142,30 +141,35 @@ struct {
Color color; // Emoji color
} emoji[EMOJI_PER_WIDTH*EMOJI_PER_HEIGHT] = { 0 };
const int screenWidth = 800, screenHeight = 450;
static int hovered = -1, selected = -1;
// Fonts that we use: `font2` is for asian languages, `font3` is the emoji font and `font1` is used for everything else
Font font1 = {0}, font2 = {0}, font3 = {0};
Vector2 hoveredPos = {0,0}, selectedPos = {0,0};
int hovered = -1, selected = -1;
//--------------------------------------------------------------------------------------
// Main entry point
//--------------------------------------------------------------------------------------
int main(int argc, char **argv)
{
// Initialization
//--------------------------------------------------------------------------------------
const int screenWidth = 800;
const int screenHeight = 450;
SetConfigFlags(FLAG_MSAA_4X_HINT | FLAG_VSYNC_HINT);
InitWindow(screenWidth, screenHeight, "raylib - unicode test");
SetTargetFPS(60);
// Load the font resources
font1 = LoadFont("resources/dejavu.fnt");
font2 = LoadFont("resources/notoCJK.fnt");
font3 = LoadFont("resources/emoji.fnt");
// NOTE: fontAsian is for asian languages,
// fontEmoji is the emojis and fontDefault is used for everything else
Font fontDefault = LoadFont("resources/dejavu.fnt");
Font fontAsian = LoadFont("resources/notoCJK.fnt");
Font fontEmoji = LoadFont("resources/emoji.fnt");
Vector2 hoveredPos = { 0.0f, 0.0f };
Vector2 selectedPos = { 0.0f, 0.0f };
// Set a random set of emojis when starting up
RandomizeEmoji();
SetTargetFPS(60);
//--------------------------------------------------------------------------------------
// Main loop
@ -174,12 +178,10 @@ int main(int argc, char **argv)
// Update
//----------------------------------------------------------------------------------
// Add a new set of emojis when SPACE is pressed
if(IsKeyPressed(KEY_SPACE))
{
RandomizeEmoji();
}
if (IsKeyPressed(KEY_SPACE)) RandomizeEmoji();
// Set the selected emoji and copy its text to clipboard
if(IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && hovered != -1 && hovered != selected)
if (IsMouseButtonPressed(MOUSE_LEFT_BUTTON) && (hovered != -1) && (hovered != selected))
{
selected = hovered;
selectedPos = hoveredPos;
@ -190,50 +192,11 @@ int main(int argc, char **argv)
// Draw
//----------------------------------------------------------------------------------
BeginDrawing();
ClearBackground(RAYWHITE);
// Draw emoji and the text bubbles
Draw();
// Draw the info text
DrawText("These emojis have something to tell you, click each to find out!", (screenWidth - 650)/2, screenHeight - 40, 20, GRAY);
DrawText("Each emoji is a unicode character from a font, not a texture... Press [SPACEBAR] to refresh", (screenWidth - 484)/2, screenHeight - 16, 10, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
// Unload all of the fonts
UnloadFont(font1);
UnloadFont(font2);
UnloadFont(font3);
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
// Fills the emoji array with random emoji (only those emojis present in font3)
static inline void RandomizeEmoji()
{
hovered = selected = -1;
int start = GetRandomValue(45, 360);
for(int i=0; i < SIZEOF(emoji); ++i)
{
emoji[i].index = GetRandomValue(0, 179)*5; // 0-179 emoji codepoints (from emoji char array) each 4bytes + null char
// Generate a random color for this emoji
Vector3 hsv = {(start*(i+1))%360, 0.6f, 0.85f};
emoji[i].color = Fade(ColorFromHSV(hsv), 0.8f);
// Set a random message for this emoji
emoji[i].message = GetRandomValue(0, SIZEOF(messages) - 1);
}
}
void Draw()
{
//------------------------------------------------------------------------------
// Draw random emoji in the background
Vector2 pos = {28.8f, 10.0f};
Vector2 mouse = GetMousePosition();
@ -241,20 +204,21 @@ void Draw()
for (int i = 0; i < SIZEOF(emoji); ++i)
{
const char *txt = &emojiCodepoints[emoji[i].index];
Rectangle emojiRect = { pos.x, pos.y, font3.baseSize, font3.baseSize };
Rectangle emojiRect = { pos.x, pos.y, fontEmoji.baseSize, fontEmoji.baseSize };
if (!CheckCollisionPointRec(mouse, emojiRect))
{
DrawTextEx(font3, txt, pos, font3.baseSize, 1.0, selected == i ? emoji[i].color : Fade(LIGHTGRAY, 0.4f));
DrawTextEx(fontEmoji, txt, pos, fontEmoji.baseSize, 1.0, selected == i ? emoji[i].color : Fade(LIGHTGRAY, 0.4f));
}
else
{
DrawTextEx(font3, txt, pos, font3.baseSize, 1.0, emoji[i].color );
DrawTextEx(fontEmoji, txt, pos, fontEmoji.baseSize, 1.0, emoji[i].color );
hovered = i;
hoveredPos = pos;
}
if(i != 0 && i % EMOJI_PER_WIDTH == 0 ) { pos.y += font3.baseSize + 24.25f; pos.x = 28.8f; } // this line is full go to next line
else pos.x += font3.baseSize + 28.8f;
if ((i != 0) && (i%EMOJI_PER_WIDTH == 0)) { pos.y += fontEmoji.baseSize + 24.25f; pos.x = 28.8f; } // this line is full go to next line
else pos.x += fontEmoji.baseSize + 28.8f;
}
// Draw the message when a emoji is selected
@ -262,25 +226,22 @@ void Draw()
{
const int message = emoji[selected].message;
const int horizontalPadding = 20, verticalPadding = 30;
Font* font = &font1;
Font* font = &fontDefault;
// Set correct font for asian languages
if(TextIsEqual(messages[message].language, "Chinese") || TextIsEqual(messages[message].language, "Korean") || TextIsEqual(messages[message].language, "Japanese"))
font = &font2;
if (TextIsEqual(messages[message].language, "Chinese") ||
TextIsEqual(messages[message].language, "Korean") ||
TextIsEqual(messages[message].language, "Japanese")) font = &fontAsian;
// Calculate size for the message box (approximate the height and width)
Vector2 sz = MeasureTextEx(*font, messages[message].text, font->baseSize, 1.0f);
if(sz.x > 300)
{
sz.y *= sz.x/300;
sz.x = 300;
}
else if(sz.x < 160)
sz.x = 160;
if (sz.x > 300) { sz.y *= sz.x/300; sz.x = 300; }
else if (sz.x < 160) sz.x = 160;
Rectangle msgRect = { selectedPos.x - 38.8f, selectedPos.y, 2 * horizontalPadding + sz.x, 2 * verticalPadding + sz.y };
msgRect.y -= msgRect.height;
Vector2 a = { selectedPos.x, msgRect.y + msgRect.height }, b = {a.x + 8, a.y + 10}, c= {a.x+10, a.y}; // coordinates for the chat bubble triangle
// Don't go outside the screen
if (msgRect.x < 10) msgRect.x += 28;
if (msgRect.y < 10)
@ -295,6 +256,7 @@ void Draw()
b = tmp;
}
if (msgRect.x + msgRect.width > screenWidth) msgRect.x -= (msgRect.x + msgRect.width) - screenWidth + 10;
// Draw chat bubble
DrawRectangleRec(msgRect, emoji[selected].color);
DrawTriangle(a, b, c, emoji[selected].color);
@ -311,5 +273,43 @@ void Draw()
Vector2 pos = { textRect.x + textRect.width - sz.x, msgRect.y + msgRect.height - sz.y - 2 };
DrawText(info, pos.x, pos.y, 10, RAYWHITE);
}
//------------------------------------------------------------------------------
// Draw the info text
DrawText("These emojis have something to tell you, click each to find out!", (screenWidth - 650)/2, screenHeight - 40, 20, GRAY);
DrawText("Each emoji is a unicode character from a font, not a texture... Press [SPACEBAR] to refresh", (screenWidth - 484)/2, screenHeight - 16, 10, GRAY);
EndDrawing();
//----------------------------------------------------------------------------------
}
// De-Initialization
//--------------------------------------------------------------------------------------
UnloadFont(fontDefault); // Unload font resource
UnloadFont(fontAsian); // Unload font resource
UnloadFont(fontEmoji); // Unload font resource
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
return 0;
}
// Fills the emoji array with random emoji (only those emojis present in fontEmoji)
static void RandomizeEmoji(void)
{
hovered = selected = -1;
int start = GetRandomValue(45, 360);
for (int i = 0; i < SIZEOF(emoji); ++i)
{
emoji[i].index = GetRandomValue(0, 179)*5; // 0-179 emoji codepoints (from emoji char array) each 4bytes + null char
// Generate a random color for this emoji
Vector3 hsv = {(start*(i + 1))%360, 0.6f, 0.85f};
emoji[i].color = Fade(ColorFromHSV(hsv), 0.8f);
// Set a random message for this emoji
emoji[i].message = GetRandomValue(0, SIZEOF(messages) - 1);
}
}