Skip to content

Commit

Permalink
Add text font spritefont example
Browse files Browse the repository at this point in the history
  • Loading branch information
wilsonsilva committed Oct 23, 2023
1 parent 44adc9e commit 188857c
Show file tree
Hide file tree
Showing 7 changed files with 88 additions and 1 deletion.
3 changes: 2 additions & 1 deletion examples/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,5 +60,6 @@ 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 |
|----|------------------------------------------------------|--------|:-------------------:|:------------------:|:------------------:|:----------|
| 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) |
| 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,6 +1,9 @@
| 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) |
| 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) |
| custom_mecha.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) |
| 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/custom_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/custom_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/custom_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/text_font_spritefont.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
83 changes: 83 additions & 0 deletions examples/text/text_font_spritefont.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
# ******************************************************************************************
#
# raylib [text] example - Sprite font loading
#
# NOTE: Sprite fonts should be generated following these conventions:
#
# - Characters must be ordered starting with character 32 (Space)
# - Every character must be contained within the same Rectangle height
# - Every character and every line must be separated by the same distance (margin/padding)
# - Rectangles must be defined by a MAGENTA color background
#
# Following those constraints, a font can be provided just by an image,
# this is quite handy to avoid additional font descriptor files (like BMFonts use).
#
# 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) 2014-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 - sprite font loading")

msg1 = "THIS IS A custom SPRITE FONT..."
msg2 = "...and this is ANOTHER CUSTOM font..."
msg3 = "...and a THIRD one! GREAT! :D"

# NOTE: Textures/Fonts MUST be loaded after Window initialization (OpenGL context is required)
font1 = Raylib.load_font(File.join(__dir__, "resources/custom_mecha.png")) # Font loading
font2 = Raylib.load_font(File.join(__dir__, "resources/custom_alagard.png")) # Font loading
font3 = Raylib.load_font(File.join(__dir__, "resources/custom_jupiter_crash.png")) # Font loading

font_position1 = Raylib::Vector2.create(
SCREEN_WIDTH / 2.0 - Raylib.measure_text_ex(font1, msg1, font1.base_size, -3).x / 2,
SCREEN_HEIGHT / 2.0 - font1.base_size / 2.0 - 80.0
)

font_position2 = Raylib::Vector2.create(
SCREEN_WIDTH / 2.0 - Raylib.measure_text_ex(font2, msg2, font2.base_size, -2).x / 2.0,
SCREEN_HEIGHT / 2.0 - font2.base_size / 2.0 - 10.0
)

font_position3 = Raylib::Vector2.create(
SCREEN_WIDTH / 2.0 - Raylib.measure_text_ex(font3, msg3, font3.base_size, 2).x / 2.0,
SCREEN_HEIGHT / 2.0 - font3.base_size / 2.0 + 50.0
)

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
# Draw
# ----------------------------------------------------------------------------------
Raylib.begin_drawing
Raylib.clear_background(Raylib::RAYWHITE)

Raylib.draw_text_ex(font1, msg1, font_position1, font1.base_size, -3, Raylib::WHITE)
Raylib.draw_text_ex(font2, msg2, font_position2, font2.base_size, -2, Raylib::WHITE)
Raylib.draw_text_ex(font3, msg3, font_position3, font3.base_size, 2, Raylib::WHITE)

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

# De-Initialization
# --------------------------------------------------------------------------------------
Raylib.unload_font(font1) # Font unloading
Raylib.unload_font(font2) # Font unloading
Raylib.unload_font(font3) # Font unloading

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

0 comments on commit 188857c

Please sign in to comment.