Skip to content

Commit

Permalink
Add text raylib fonts example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 23, 2023
1 parent 7e865c2 commit 3bba982
Show file tree
Hide file tree
Showing 11 changed files with 111 additions and 0 deletions.
1 change: 1 addition & 0 deletions examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ Examples using raylib text functionality, including sprite fonts loading/generat

| ## | example | image | difficulty<br>level | version<br>created | last version<br>updated | original<br>developer |
|----|------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 69 | [text_raylib_fonts](text/text_raylib_fonts.rb) | <img src="text/text_raylib_fonts.png" alt="text_raylib_fonts" width="80"> | ⭐️☆☆☆ | 1.7 | 3.7 | [Ray](https://github.com/raysan5) |
| 70 | [text_font_spritefont](text/text_font_spritefont.rb) | <img src="text/text_font_spritefont.png" alt="text_font_spritefont" width="80"> | ⭐️☆☆☆ | 1.0 | 1.0 | [Ray](https://github.com/raysan5) |
| 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) |
Expand Down
7 changes: 7 additions & 0 deletions examples/text/resources/LICENSE.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,12 @@
| resource | author | licence | notes |
| :----------------------------- | :---------: | :------ | :---- |
| fonts/alagard.png | Hewett Tsoi | [Freeware](https://www.dafont.com/es/alagard.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| fonts/romulus.png | Hewett Tsoi | [Freeware](https://www.dafont.com/es/romulus.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| fonts/alpha_beta.png | [Brian Kent (AEnigma)](https://www.dafont.com/es/aenigma.d188) | [Freeware](https://www.dafont.com/es/alpha-beta.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| fonts/setback.png | [Brian Kent (AEnigma)](https://www.dafont.com/es/aenigma.d188) | [Freeware](https://www.dafont.com/es/setback.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| fonts/jupiter_crash.png | [Brian Kent (AEnigma)](https://www.dafont.com/es/aenigma.d188) | [Freeware](https://www.dafont.com/es/jupiter-crash.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| fonts/mecha.png | Captain Falcon | [Freeware](https://www.dafont.com/es/mecha-cf.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| fonts/pixelplay.png | Aleksander Shevchuk | [Freeware](https://www.dafont.com/es/pixelplay.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| fonts/pixantiqua.ttf | Gerhard Großmann | [Freeware](https://www.dafont.com/es/pixantiqua.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| custom_alagard.png | [Brian Kent (AEnigma)](https://www.dafont.com/es/aenigma.d188) | [Freeware](https://www.dafont.com/es/jupiter-crash.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
| custom_jupiter_crash.png | [Brian Kent (AEnigma)](https://www.dafont.com/es/aenigma.d188) | [Freeware](https://www.dafont.com/es/jupiter-crash.font) | Atlas created by [@raysan5](https://github.com/raysan5) |
Expand Down
Binary file added examples/text/resources/fonts/alagard.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/fonts/alpha_beta.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/fonts/jupiter_crash.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/fonts/mecha.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/fonts/pixelplay.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/fonts/romulus.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/fonts/setback.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/text_raylib_fonts.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
103 changes: 103 additions & 0 deletions examples/text/text_raylib_fonts.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
# ******************************************************************************************
#
# raylib [text] example - raylib fonts loading
#
# NOTE: raylib is distributed with some free to use fonts (even for commercial purposes!)
# To view details and credits for those fonts, check raylib license file
#
# Example originally created with raylib 1.7, last time updated with raylib 3.7
#
# 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) 2017-2023 Ramon Santamaria (@raysan5)
#
# ******************************************************************************************

require 'bundler/setup'
require 'raylib'

MAX_FONTS = 8

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

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

# NOTE: Textures MUST be loaded after Window initialization (OpenGL context is required)
fonts = Array.new(MAX_FONTS)
fonts[0] = Raylib.load_font(File.join(__dir__, "resources/fonts/alagard.png"))
fonts[1] = Raylib.load_font(File.join(__dir__, "resources/fonts/pixelplay.png"))
fonts[2] = Raylib.load_font(File.join(__dir__, "resources/fonts/mecha.png"))
fonts[3] = Raylib.load_font(File.join(__dir__, "resources/fonts/setback.png"))
fonts[4] = Raylib.load_font(File.join(__dir__, "resources/fonts/romulus.png"))
fonts[5] = Raylib.load_font(File.join(__dir__, "resources/fonts/pixantiqua.png"))
fonts[6] = Raylib.load_font(File.join(__dir__, "resources/fonts/alpha_beta.png"))
fonts[7] = Raylib.load_font(File.join(__dir__, "resources/fonts/jupiter_crash.png"))

messages = [
"ALAGARD FONT designed by Hewett Tsoi",
"PIXELPLAY FONT designed by Aleksander Shevchuk",
"MECHA FONT designed by Captain Falcon",
"SETBACK FONT designed by Brian Kent (AEnigma)",
"ROMULUS FONT designed by Hewett Tsoi",
"PIXANTIQUA FONT designed by Gerhard Grossmann",
"ALPHA_BETA FONT designed by Brian Kent (AEnigma)",
"JUPITER_CRASH FONT designed by Brian Kent (AEnigma)"
]

spacings = [2, 4, 8, 4, 3, 4, 4, 1]
positions = Array.new(MAX_FONTS) { Raylib::Vector2.create(0.0, 0.0) }

MAX_FONTS.times do |i|
positions[i].x = SCREEN_WIDTH / 2.0 - Raylib.measure_text_ex(fonts[i], messages[i], fonts[i].base_size * 2.0, spacings[i].to_f).x / 2.0
positions[i].y = 60.0 + fonts[i].base_size + 45.0 * i
end

# Small Y position corrections
positions[3].y += 8
positions[4].y += 2
positions[7].y -= 8

colors = [
Raylib::MAROON,
Raylib::ORANGE,
Raylib::DARKGREEN,
Raylib::DARKBLUE,
Raylib::DARKPURPLE,
Raylib::LIME,
Raylib::GOLD,
Raylib::RED
]

Raylib.set_target_fps(60)
# --------------------------------------------------------------------------------------

# Main game loop
until Raylib.window_should_close
# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)

Raylib.draw_text("free fonts included with raylib", 250, 20, 20, Raylib::DARKGRAY)
Raylib.draw_line(220, 50, 590, 50, Raylib::DARKGRAY)

MAX_FONTS.times do |i|
Raylib.draw_text_ex(fonts[i], messages[i], positions[i], fonts[i].base_size * 2.0, spacings[i].to_f, colors[i])
end

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

# De-Initialization
# --------------------------------------------------------------------------------------
fonts.each { |font| Raylib.unload_font(font) } # Fonts unloading

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

0 comments on commit 3bba982

Please sign in to comment.