Yes, I was going for coding efficiency not performance, because my priority was to show ease of programming versus a pedantic (more cryptic) faster version.
# HelloWorldGraphical.cr
require "crsfml"
window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "Hello world/Graphical")
font = SF::Font.from_file("DejaVuSerif-Bold.ttf")
text = SF::Text.new
text.font = font
text.string = "Goodbye, world!"
text.character_size = 24
text.color = SF::Color::Black
while window.open?
while event = window.poll_event
if event.is_a? SF::Event::Closed
window.close
end
end
window.clear(SF::Color::White)
window.draw(text)
window.display
end
and installed the libraries on my KDE Linux laptop and ran it, and get this error when running it.
➜ Hello World Graphical crystal run HelloWorldGraphical.cr
Failed to load font "DejaVuSerif-Bold.ttf" (failed to create the font face)
Unhandled exception: Font.load_from_file failed (SF::InitError)
from lib/crsfml/src/graphics/obj.cr:3737:9 in 'from_file'
from HelloWorldGraphical.cr:4:1 in '__crystal_main'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:129:5 in 'main_user_code'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:115:7 in 'main'
from /home/jzakiya/crystal/share/crystal/src/crystal/main.cr:141:3 in 'main'
from /lib64/libc.so.6 in '??'
from /lib64/libc.so.6 in '__libc_start_main'
from ../sysdeps/x86_64/start.S:117 in '_start'
from ???
A small blank window quickly forms, then disappears, with the error message following.
It looks like you didn’t have a font file in the code directory itself. CrSFML doesn’t load fonts from root, it only loads font files that are within the project folder.
This is probably not a good Crystal example for Rosetta Code, because people should be able to run each example using the native implementation|tools of the given language.
Here, I had to install the libraries for crsfml from the repos of my Linux system (which luckily it had), but it’s still missing font libraries for it to run. None of these are native to Crystal, and people unfamiliar with Crystal may think something was wrong|bad with it when they can’t get this to run.
A better alternative is to implement it with standard shards that are (hopefully mostly) OS independent, so the procedure to install|run the code is the same|similar across platforms.
As an example, take a look at the Ruby example for this.
It gives multiple examples using different libraries to choose from, depending on your platform.
But the most important thing is to provide code that works, to show Crystal’s capabilities to others.
The only other possibility I can think of that’s actively developed is the GTK4 binding. If there are graphic output shards that are standard then I don’t know what they are.