Skip to content

Commit

Permalink
Add the text font loading example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 23, 2023
1 parent 735f415 commit 3f413bb
Show file tree
Hide file tree
Showing 8 changed files with 282 additions and 0 deletions.
10 changes: 10 additions & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,13 @@ Examples using raylib audio functionality, including sound/music loading and pla
| 120 | [audio_sound_loading](audio/audio_sound_loading.rb) | <img src="audio/audio_sound_loading.png" alt="audio_sound_loading" width="80"> | ⭐️☆☆☆ | 1.1 | 3.5 | [Ray](https://github.com/raysan5) |

As always contributions are welcome, feel free to send new examples! Here it is an [examples template](examples_template.rb) to start with!

## Text

Examples using raylib text functionality, including sprite fonts loading/generation and text drawing.

| ## | example | image | difficulty<br>level | version<br>created | last version<br>updated | original<br>developer |
|----|------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 71 | [text_font_filters](text/text_font_filters.rb) | <img src="text/text_font_filters.png" alt="text_font_filters" width="80"> | ⭐️⭐️☆☆ | 1.3 | **4.2** | [Ray](https://github.com/raysan5) |
| 72 | [text_font_loading](text/text_font_loading.rb) | <img src="text/text_font_loading.png" alt="text_font_loading" width="80"> | ⭐️☆☆☆ | 1.4 | 3.0 | [Ray](https://github.com/raysan5) |

3 changes: 3 additions & 0 deletions examples/text/resources/LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
| resource | author | licence | notes |
| :----------------------------- | :---------: | :------ | :---- |
| fonts/pixantiqua.ttf | Gerhard Großmann | [Freeware](https://www.dafont.com/es/pixantiqua.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| KAISG.ttf | [Dieter Steffmann](http://www.steffmann.de/wordpress/) | [Freeware](https://www.1001fonts.com/users/steffmann/) | [Kaiserzeit Gotisch](https://www.dafont.com/es/kaiserzeit-gotisch.font) font |
| pixantiqua.fnt, pixantiqua.png | Gerhard Großmann | [Freeware](https://www.dafont.com/es/pixantiqua.font) | Atlas made with [BMFont](https://www.angelcode.com/products/bmfont/) by [@raysan5](https://github.com/raysan5) |
| pixantiqua.ttf | Gerhard Großmann | [Freeware](https://www.dafont.com/es/pixantiqua.font) | - |
Binary file added examples/text/resources/fonts/pixantiqua.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
188 changes: 188 additions & 0 deletions examples/text/resources/pixantiqua.fnt

Large diffs are not rendered by default.

Binary file added examples/text/resources/pixantiqua.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
Binary file added examples/text/resources/pixantiqua.ttf
Binary file not shown.
Binary file added examples/text/text_font_loading.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
81 changes: 81 additions & 0 deletions examples/text/text_font_loading.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
# ******************************************************************************************
#
# raylib [text] example - Font loading
#
# NOTE: raylib can load fonts from multiple input file formats:
#
# - TTF/OTF > Sprite font atlas is generated on loading, user can configure
# some of the generation parameters (size, characters to include)
# - BMFonts > Angel code font fileformat, sprite font image must be provided
# together with the .fnt file, font generation cannot be configured
# - XNA Spritefont > Sprite font image, following XNA Spritefont conventions,
# Characters in image must follow some spacing and order rules
#
# Example ported to Ruby by Wilson Silva (@wilsonsilva). Works with Raylib 4.5
#
# Example licensed under an unmodified zlib/libpng license, which is an OSI-certified,
# BSD-like license that allows static linking with closed source software
#
# Copyright (c) 2016-2023 Ramon Santamaria (@raysan5)
#
# ******************************************************************************************

require 'bundler/setup'
require 'raylib'

# Initialization
# --------------------------------------------------------------------------------------
SCREEN_WIDTH = 800
SCREEN_HEIGHT = 450

Raylib.init_window(SCREEN_WIDTH, SCREEN_HEIGHT, "raylib [text] example - font loading")

# Define characters to draw
# NOTE: raylib supports UTF-8 encoding, following list is actually codified as UTF8 internally
msg = "!\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHI\nJKLMNOPQRSTUVWXYZ[]^_`abcdefghijklmn\no" \
"pqrstuvwxyz{|}~¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓ\nÔÕÖ×ØÙÚÛÜÝÞßàáâãäåæçèéêëìíîïðñòóôõö÷\nøùúûüýþÿ"

# NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)

# BMFont (AngelCode) : Font data and image atlas have been generated using external program
font_bm = Raylib.load_font(File.join(__dir__, "resources/pixantiqua.fnt"))

# TTF font : Font data and atlas are generated directly from TTF
# NOTE: We define a font base size of 32 pixels tall and up-to 250 characters
font_ttf = Raylib.load_font_ex(File.join(__dir__, "resources/pixantiqua.ttf"), 32, nil, 250)

Raylib.set_target_fps(60) # Set our game to run at 60 frames-per-second
# --------------------------------------------------------------------------------------

# Main game loop
until Raylib.window_should_close # Detect window close button or ESC key
# Update
# ----------------------------------------------------------------------------------
use_ttf = Raylib.is_key_down(Raylib::KEY_SPACE)
# ----------------------------------------------------------------------------------

# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)
Raylib.draw_text("Hold SPACE to use TTF generated font", 20, 20, 20, Raylib::LIGHTGRAY)

if use_ttf
Raylib.draw_text_ex(font_ttf, msg, Raylib::Vector2.create(20.0, 100.0), font_ttf.base_size.to_f, 2, Raylib::LIME)
Raylib.draw_text("Using TTF font generated", 20, Raylib.get_screen_height - 30, 20, Raylib::GRAY)
else
Raylib.draw_text_ex(font_bm, msg, Raylib::Vector2.create(20.0, 100.0), font_bm.base_size.to_f, 2, Raylib::MAROON)
Raylib.draw_text("Using BMFont (Angelcode) imported", 20, Raylib.get_screen_height - 30, 20, Raylib::GRAY)
end

Raylib.end_drawing
# ----------------------------------------------------------------------------------
end

# De-Initialization
# --------------------------------------------------------------------------------------
Raylib.unload_font(font_bm) # AngelCode Font unloading
Raylib.unload_font(font_ttf) # TTF Font unloading

Raylib.close_window # Close window and OpenGL context
# --------------------------------------------------------------------------------------

0 comments on commit 3f413bb

Please sign in to comment.