raylib/examples/text_font_select.c

158 lines
6.3 KiB
C
Raw Normal View History

2013-11-24 23:30:05 +04:00
/*******************************************************************************************
*
* raylib [text] example - Font selector
2013-11-24 23:30:05 +04:00
*
2015-08-30 18:46:37 +03:00
* This example has been created using raylib 1.3 (www.raylib.com)
2013-11-24 23:30:05 +04:00
* raylib is licensed under an unmodified zlib/libpng license (View raylib.h for details)
*
2015-08-30 18:46:37 +03:00
* Copyright (c) 2015 Ramon Santamaria (@raysan5)
2013-11-24 23:30:05 +04:00
*
********************************************************************************************/
#include "raylib.h"
int main()
{
// Initialization
//--------------------------------------------------------------------------------------
2013-11-24 23:30:05 +04:00
int screenWidth = 800;
2015-08-27 17:13:31 +03:00
int screenHeight = 450;
InitWindow(screenWidth, screenHeight, "raylib [text] example - font selector");
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
// NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
SpriteFont fonts[8]; // SpriteFont array
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
fonts[0] = LoadSpriteFont("resources/fonts/alagard.rbmf"); // SpriteFont loading
fonts[1] = LoadSpriteFont("resources/fonts/pixelplay.rbmf"); // SpriteFont loading
fonts[2] = LoadSpriteFont("resources/fonts/mecha.rbmf"); // SpriteFont loading
fonts[3] = LoadSpriteFont("resources/fonts/setback.rbmf"); // SpriteFont loading
fonts[4] = LoadSpriteFont("resources/fonts/romulus.rbmf"); // SpriteFont loading
fonts[5] = LoadSpriteFont("resources/fonts/pixantiqua.rbmf"); // SpriteFont loading
fonts[6] = LoadSpriteFont("resources/fonts/alpha_beta.rbmf"); // SpriteFont loading
fonts[7] = LoadSpriteFont("resources/fonts/jupiter_crash.rbmf"); // SpriteFont loading
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
int currentFont = 0; // Selected font
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
Color colors[8] = { MAROON, ORANGE, DARKGREEN, DARKBLUE, DARKPURPLE, LIME, GOLD, RED };
2014-09-30 01:41:05 +04:00
const char fontNames[8][20] = { "[0] Alagard", "[1] PixelPlay", "[2] MECHA", "[3] Setback",
2013-12-27 03:17:39 +04:00
"[4] Romulus", "[5] PixAntiqua", "[6] Alpha Beta", "[7] Jupiter Crash" };
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
const char text[50] = "THIS is THE FONT you SELECTED!"; // Main text
2014-09-30 01:41:05 +04:00
2015-08-30 18:46:37 +03:00
Vector2 textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].size*3, 1);
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
Vector2 mousePoint;
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
Color btnNextOutColor = DARKBLUE; // Button color (outside line)
Color btnNextInColor = SKYBLUE; // Button color (inside)
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
int framesCounter = 0; // Useful to count frames button is 'active' = clicked
2015-08-27 17:13:31 +03:00
int positionY = 180; // Text selector and button Y position
Rectangle btnNextRec = { 673, positionY, 109, 44 }; // Button rectangle (useful for collision)
2014-09-30 01:41:05 +04:00
SetTargetFPS(60); // Set our game to run at 60 frames-per-second
//--------------------------------------------------------------------------------------
2014-09-30 01:41:05 +04:00
2013-11-24 23:30:05 +04:00
// Main game loop
while (!WindowShouldClose()) // Detect window close button or ESC key
{
// Update
//----------------------------------------------------------------------------------
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
// Keyboard-based font selection (easy)
if (IsKeyPressed(KEY_RIGHT))
{
if (currentFont < 7) currentFont++;
}
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
if (IsKeyPressed(KEY_LEFT))
{
if (currentFont > 0) currentFont--;
}
2015-08-27 17:13:31 +03:00
if (IsKeyPressed('0')) currentFont = 0;
else if (IsKeyPressed('1')) currentFont = 1;
else if (IsKeyPressed('2')) currentFont = 2;
else if (IsKeyPressed('3')) currentFont = 3;
else if (IsKeyPressed('4')) currentFont = 4;
else if (IsKeyPressed('5')) currentFont = 5;
else if (IsKeyPressed('6')) currentFont = 6;
else if (IsKeyPressed('7')) currentFont = 7;
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
// Mouse-based font selection (NEXT button logic)
mousePoint = GetMousePosition();
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
if (CheckCollisionPointRec(mousePoint, btnNextRec))
{
// Mouse hover button logic
if (framesCounter == 0)
{
btnNextOutColor = DARKPURPLE;
btnNextInColor = PURPLE;
}
2014-09-30 01:41:05 +04:00
2014-12-31 21:17:50 +03:00
if (IsMouseButtonDown(MOUSE_LEFT_BUTTON))
2013-12-27 03:17:39 +04:00
{
framesCounter = 20; // Frames button is 'active'
btnNextOutColor = MAROON;
btnNextInColor = RED;
}
}
else
{
// Mouse not hover button
btnNextOutColor = DARKBLUE;
btnNextInColor = SKYBLUE;
}
2014-12-31 21:17:50 +03:00
if (framesCounter > 0) framesCounter--;
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
if (framesCounter == 1) // We change font on frame 1
2014-09-30 01:41:05 +04:00
{
2013-12-27 03:17:39 +04:00
currentFont++;
if (currentFont > 7) currentFont = 0;
}
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
// Text measurement for better positioning on screen
2015-08-30 18:46:37 +03:00
textSize = MeasureTextEx(fonts[currentFont], text, fonts[currentFont].size*3, 1);
//----------------------------------------------------------------------------------
2014-09-30 01:41:05 +04:00
2013-11-24 23:30:05 +04:00
// Draw
//----------------------------------------------------------------------------------
2013-11-24 23:30:05 +04:00
BeginDrawing();
2014-09-30 01:41:05 +04:00
2013-11-24 23:30:05 +04:00
ClearBackground(RAYWHITE);
2015-08-27 17:13:31 +03:00
DrawText("font selector - use arroys, button or numbers", 160, 80, 20, DARKGRAY);
DrawLine(120, 120, 680, 120, DARKGRAY);
2014-09-30 01:41:05 +04:00
2015-08-27 17:13:31 +03:00
DrawRectangle(18, positionY, 644, 44, DARKGRAY);
DrawRectangle(20, positionY + 2, 640, 40, LIGHTGRAY);
DrawText(fontNames[currentFont], 30, positionY + 13, 20, BLACK);
DrawText("< >", 610, positionY + 8, 30, BLACK);
2014-09-30 01:41:05 +04:00
2013-12-27 03:17:39 +04:00
DrawRectangleRec(btnNextRec, btnNextOutColor);
2015-08-27 17:13:31 +03:00
DrawRectangle(675, positionY + 2, 105, 40, btnNextInColor);
DrawText("NEXT", 700, positionY + 13, 20, btnNextOutColor);
2014-09-30 01:41:05 +04:00
DrawTextEx(fonts[currentFont], text, (Vector2){ screenWidth/2 - textSize.x/2,
2015-08-30 18:46:37 +03:00
260 + (70 - textSize.y)/2 }, fonts[currentFont].size*3,
2014-09-30 01:41:05 +04:00
1, colors[currentFont]);
2013-11-24 23:30:05 +04:00
EndDrawing();
//----------------------------------------------------------------------------------
2013-11-24 23:30:05 +04:00
}
// De-Initialization
//--------------------------------------------------------------------------------------
2013-12-27 03:17:39 +04:00
for (int i = 0; i < 8; i++) UnloadSpriteFont(fonts[i]); // SpriteFont(s) unloading
2014-09-30 01:41:05 +04:00
2013-11-24 23:30:05 +04:00
CloseWindow(); // Close window and OpenGL context
//--------------------------------------------------------------------------------------
2014-09-30 01:41:05 +04:00
2013-11-24 23:30:05 +04:00
return 0;
}