Git Product home page Git Product logo

crsfml's Introduction

CrSFML

Crystal bindings to Simple and Fast Multimedia Library.

Documentation

Introduction

CrSFML is a library that allows SFML to be used with the Crystal programming language. SFML is a library written in C++, so CrSFML also needs to ship thin C bindings to SFML.

To quote the official site of SFML,

SFML provides a simple interface to the various components of your PC, to ease the development of games and multimedia applications.

Indeed, SFML is most often used to make video games. It provides features such as hardware-accelerated 2D graphics, handling keyboard, mouse and gamepad input, vector and matrix manipulation, managing windows (can also be used as a base for OpenGL drawing), working with multiple image formats, audio playback and recording, basic networking... Check out some demos of CrSFML to see what it can do.

CrSFML consists almost entirely of automatically generated code, based on SFML's header files. Read more about generated code and API differences between SFML and CrSFML.

Installation

First, install SFML.

Then create a shard.yml file in your project's folder (or add to it) with the following contents:

name: awesome-game
version: 0.1.0

dependencies:
  crsfml:
    github: oprypin/crsfml
    version: ~> 2.5.2

Resolve dependencies with Shards:

shards install

During installation this will invoke make to build the C++ wrappers as object files. If that fails, see Custom SFML location.

Try running an example:

cp lib/crsfml/examples/snakes.cr .
crystal snakes.cr

Now you're ready for the tutorials!

Windows

Crystal does not officially support Windows, but CrSFML supports it and is perfectly usable already. See a video detailing the full setup.

Prerequisites

The C++ wrappers require a C++ compiler (C++03 will do).

Install SFML

SFML must be installed, with the version that matches SFML_VERSION in src/version.cr (usually latest). If it doesn't, no need to look for an older release of CrSFML, just re-generate the sources for your version. SFML versions 2.3.x through 2.5.x are supported by CrSFML.

There are detailed official instructions on how to install SFML manually, but on many systems there are easier ways.

If SFML is not installed in a global/default location, see Custom SFML location.

Linux

Many Linux-based systems provide SFML through their package manager. Make sure to install the -dev packages if there is such a separation in your Linux distribution of choice.

Note that most often the packages provided by Linux distributions are outdated. If you're installing an older version of SFML (not recommended), make sure that it's still supported by CrSFML. You will need to re-generate the sources.

Mac

The easiest way to install SFML on macOS is through the Homebrew package manager:

brew update
brew install sfml

It can also be installed by copying binaries, as described in official instructions.

Windows

Downloading the official binaries ("Visual C++ 15 (2017) - 64-bit") will do. Check out the video on how to set things up on Windows.

From source

Building SFML from source is as simple as downloading the source code and running:

cmake .
cmake --build .
sudo make install  # optional!

In some cases the dependencies are bundled with SFML, otherwise see the official build instructions.

Custom SFML location

If SFML's headers and libraries are not in a path where the compiler would look by default (and the defaults usually work only on Linux), additional steps are needed.

First, before building the extensions (make) or generating sources, you need to configure the include path:

export SFML_INCLUDE_DIR=/full/path/to/sfml/include

Windows equivalent:

set INCLUDE=C:\path\to\sfml\include;%INCLUDE%

Setting these variables beforehand can also fix shards install.

Then, whenever using CrSFML, you need to configure the path to SFML libraries so the linker can find them. To apply these for the current shell session, run:

export LIBRARY_PATH=/full/path/to/sfml/lib     # Used during linking
export LD_LIBRARY_PATH=/full/path/to/sfml/lib  # Used when running an executable

Windows equivalent:

set LIB=c:\path\to\sfml\lib;%LIB%
set PATH=c:\path\to\sfml\bin;%PATH%

CrSFML's top-level scripts also need the include path to work. E.g. crystal generate.cr -- /full/path/to/sfml/include.

Generating sources

CrSFML's sources come almost entirely from a generator program. They are based on a particular version of SFML. But as sources for the latest version are already bundled, usually you don't need to do this. More details.

As this is out of scope for Shards, let's download the repository separately (then use CrSFML without Shards).

git clone https://github.com/oprypin/crsfml
cd crsfml

Then we can generate the sources, either directly with crystal generate.cr or as part of the build process:

touch generate.cr
make

If run successfully, this generates all the source files and also compiles the C++ wrapper.

CrSFML without Shards

It's also possible to use CrSFML outside of Shards, as with any library. One option is to directly create a symbolic link to CrSFML in your project's lib folder.

mkdir lib
ln -s /full/path/to/crsfml lib/crsfml

Another option is to export CRYSTAL_PATH=/full/path/to a directory that contains the crsfml directory.

CrSFML as bindings to SFML

API differences between SFML and CrSFML

The API of CrSFML (a library for Crystal) attempts to be similar to SFML (a C++ library), but some general changes are present:

  • Methods are renamed to snake_case.
  • Getter, setter methods are changed:
    • x.getSomeProperty() becomes x.some_property.
    • x.isSomeProperty(), x.hasSomeProperty() become x.some_property?.
    • x.setSomeProperty(v) becomes x.some_property = v.
  • Structs in Crystal are always passed by copy, so modifying them can be problematic. For example, my_struct.x = 7 is fine but array_of_structs[2].x = 5 will not work. To work around this, copy the whole struct, modify it, then write it back. Better yet, avoid the need to modify structs (work with them like with immutable objects).
  • Member functions, such as loadFromFile, that are used for initialization, each have a corresponding shorthand class method (from_file) that raises SF::InitError on failure.
  • SFML sometimes uses enum values as bitmasks. You can combine them using the | operator.
  • enum members are exposed at class level, so instead of SF::Keyboard::Code::Slash you can use SF::Keyboard::Slash.
  • SFML sometimes requires that an instance must remain alive as long as it is attached to the object. For example, a textured shape will cause errors if the texture object is destroyed. CrSFML prevents this problem by keeping a reference to the object.
  • The Event union and EventType enum are represented as a class hierarchy. Instead of ev.type == SF::Event::Resized use ev.is_a?(SF::Event::Resized); instead of ev.size.width use ev.width.
  • Instead of subclassing Drawable, include the Drawable module with an abstract draw method.
  • Most of the API documentation is taken directly from SFML, so don't be surprised if it talks in C++ terms.

The C++ wrapper

The interface of the C++ → C wrapper (which Crystal ultimately binds to) consists entirely of simple functions that accept only native types (such as float, uint32_t, char*) and untyped pointers (void*). The untyped pointers are never exposed to the user, only to other auto-generated parts of the code. The function names consist of the original SFML class name, the function name itself, and a base62 hash of the parameter types. Return types are never used; instead, the output is done into a pointer (which is usually the last argument of the function), but, as usual, the memory allocation is the caller's job. The first argument of each function is a pointer to the receiver object (if applicable).

Abstract classes are implemented by exposing a collection of global callback variables, which must be set by the user if they want to use the corresponding class. The callback's first argument is the object, and some arguments are pointers that need to be assigned to inside the callback implementation (because return values are not used).

Compilation of the C++ extensions is based only on SFML's header files, these are made into object files, and all the linking is deferred to the final linker invocation done by Crystal.

Why not CSFML?

CSFML is a great library that allows SFML to be used with C. It goes to great lengths to be human-friendly and does a good job of converting C++ idioms to C idioms. In the past CrSFML used to be based on it, but after a while it became apparent that the advantages of CSFML's nice interface are also disadvantages when constructing (especially auto-generated) bindings that attempt to look as close to the real SFML as possible.

Many details about functions' signatures are lost, as well as function overloads. Names of data types had to be simplified (not namespaced). And many other such small things that bring the frustration of having to reconstruct the details of the original SFML interface based on the simplified CSFML interface.

There are many aspects that prevent an efficient implementation from the standpoint of bindings, most importantly, CSFML takes memory allocation into its own hands, so any object creation in CrSFML involved allocation of two objects on the heap by two different libraries, and every interaction with it had to go through at least two pointers. Structs in CSFML are actually completely separate data types and they have to be constantly be converted between a "SFML-struct" and a "CSFML-struct".

Instead of that, the C++ → C wrapper passes the bare SFML data types directly through untyped pointers, and relies on the higher-level binding to deal safely with them. In case of structs the data layout is mirrored, in case of classes the pointers remain completely opaque.

Not to forget that the wrapper is made automatically, so it can be quickly updated to any SFML release and prevents human error that could happen when implementing CSFML.

Credits

CrSFML was made by Oleh Prypin.

CrSFML is licensed under the terms and conditions of the zlib/libpng license.

This library uses and is based on SFML.

Thanks to Alan Willms for translating tutorials to Crystal.

crsfml's People

Contributors

alanwillms avatar heaven31415 avatar nicck avatar oprypin avatar petoem avatar respitesage avatar sergio-pi avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

crsfml's Issues

disable generation after shards install for Windows

Can we disable the generation via the Makefile for Windows, or maybe not run make for windows after shards install?

I have been successful at running a crsfml game on Windows using crystal 1.5.1 by mainly following your linked video in the README, thanks! However, some of the artifacts from crystal GH actions are no longer available, but regardless I think the make would still fail after shards install. make failing means that none of the shards are included in the lib folder, of any dependent libraries using crsfml. (I'm only including crsfml, or a shard that includes only crsfml, so not sure if it affects any other added shards).

Here's the error, but it's clearly because the generation/Makefile was meant for non-Windows (no complaints there, not asking for work towards that effort!)

D:\code\cr\shoot>shards install
Resolving dependencies
Fetching https://github.com/mswieboda/game_sf.git
Fetching https://github.com/oprypin/crsfml.git
Installing crsfml (2.5.2)
Postinstall of crsfml: make
Failed postinstall of crsfml on make:
g++ -Wno-deprecated-declarations -I '/usr/include'  -o src/system/ext.o -c src/system/ext.cpp
process_begin: CreateProcess(NULL, g++ -Wno-deprecated-declarations -I /usr/include -o src/system/ext.o -c src/system/ext.cpp, ...) failed.
make (e=2): The system cannot find the file specified.
make: *** [Makefile:21: src/system/ext.o] Error 2

Here's the workaround I do:

{% if flag?(:win32) %}
  require "../../crsfml/src/crsfml"
  require "../../crsfml/src/audio"
{% else %}
  require "crsfml"
  require "crsfml/audio"
{% end %}

Instead of the normal require "crsfml" I require the source from the checked out crsfml repo, side by side with the project including crsfml. This works, but ideally I'd like to not to have to do that, because I can't use a wrapper shard dependent on crsfml as it won't install in lib when on Windows. I realize the generation won't work on Windows, and I'm not expecting changes, or work towards that manner, I just would like to be able to successfully shards install, so I don't need the workaround, and could successfully include a wrapper shard using crsfml internally.

I'm going to try to fork the repo, and try to disable the make for Windows on shards install, or worst-case do-so from within the Makefile. Not sure if that's the best approach, but figured it's in the right direction. Just thought I'd post here for history, and if others have similar interests of doing this on Windows too (it's been working great on a mac, and linux). I realize I'm probably in the minority here, but I love crystal, and want to use it for game jams, and unfortunately that requires something runnable on Windows.

CMAKE_MODULE_PATH on Ubuntu

Just for your information:

  • Ubuntu 16.04.1 LTS
  • SFML 2.3.2

The path to FindSFML.cmake is /usr/share/SFML/cmake/Modules/FindSFML.cmake.

I had to add list(APPEND CMAKE_MODULE_PATH "/usr/share/SFML/cmake/Modules") to CMakeLists.txt to compile all the things.

Keep the good work 😄

Error loading fonts

I tried a few fonts, I keep getting the same erorr:

Warning: CrSFML was built for SFML 2.4.1, found SFML 2.4.2
Warning: CrSFML was built for SFML 2.4.1, found SFML 2.4.2
Warning: CrSFML was built for SFML 2.4.1, found SFML 2.4.2
Failed to load font "/home/unshadow/Desktop/git-projects/TestGame/assests/Digirtu.ttf" (failed to create the font face)
Font.load_from_file failed (SF::InitError)
0x5555555ddf65: from_file at /home/unshadow/Desktop/git-projects/TestGame/lib/crsfml/src/graphics/obj.cr 3682:9
0x55555558f5ef: __crystal_main at /home/unshadow/Desktop/git-projects/TestGame/src/TestGame.cr 14:1
0x55555559edd9: main at /usr/lib/crystal/main.cr 12:15
0x7ffff649b511: __libc_start_main at ??
0x55555558e70a: _start at ??
0x0: ??? at ??

double free

I think this problem might be related to crsfml, so I post here as well.
Here is the original page: https://forum.crystal-lang.org/t/double-free-or-corruption-prev/4811


Recently I want to write a program to read script files like this: https://pastebin.com/nKMS14fX


I know little about C, so as the 'double free'. And I am confused.
Here is my code: (it truly takes time for me to locate the problematic function, since this error msg has no traceback...)

      
  def ArcaeaInspector.update_txt(new_text : String, line_num : Int32)
    case line_num
    when 0
      tx = @@txta
    when 1
      tx = @@txtb
    when 2
      tx = @@txtc
    end
    tx = tx.not_nil!
    tx.string = new_text
    lb = tx.local_bounds
    tx.origin = SF.vector2f lb.left + lb.width / 2, lb.top + lb.height / 2
    tx.position = SF.vector2f(
      @@window.size.x / 2,
      @@window.size.y - @@window.size.y / 5 + line_num * 20)
  end

Here is where this func is called:

  def say(content : Array(String))
      ArcaeaInspector.clear_txt
      i = -1
      content.each do |t|
        SF.sleep SF.seconds 1.5
        i += 1
        ArcaeaInspector.update_txt t, i
      end
      SF.sleep SF.seconds 1
      ArcaeaInspector.show_arrow
    end

The code is at my github repository

By the way, I've also tried run the program in release mode, and the error exists still.
What's more, this occurs "sometimes". If I'm luck enough

VertexArray#[](index) should not return a copy of the Vertex

It seems VertexArray#[](index) returns a copy of Vertex at index instead of the original instance.

Look:

vertex_array = SF::VertexArray.new(SF::Quads, 4)

puts vertex_array[0].position.inspect
# SF::Vector2(Float32)(@x=0, @y=0)

vertex_array[0].position = SF.vector2(64, 128)

puts vertex_array[0].position.inspect
# SF::Vector2(Float32)(@x=0, @y=0)

The workaround is creating a new vertex and overwriting it:

vertex_array = SF::VertexArray.new(SF::Quads, 4)

puts vertex_array[0].position.inspect
# SF::Vector2(Float32)(@x=0, @y=0)

vertex = SF::Vertex.new
vertex.position = SF.vector2(64, 128)
vertex_array[0] = vertex

puts vertex_array[0].position.inspect
# SF::Vector2(Float32)(@x=64, @y=128)

Error compiling crsfml (read before assignment to local variable)

When trying to compile crsfml (Approach 1) I keep getting the following error:

-- Found SFML 2.5.0 in /usr/lib64/cmake/SFML
-- Found SFML 2.5.0 in /usr/lib64/cmake/SFML
-- Configuring done
-- Generating done
-- Build files have been written to: /home/reiswindy/Documents/proyectos/crystal/crsfml
[  8%] Generating voidcsfml/include/voidcsfml/system.h, voidcsfml/src/voidcsfml/system.cpp, voidcsfml/include/voidcsfml/window.h, voidcsfml/src/voidcsfml/window.cpp, voidcsfml/include/voidcsfml/graphics.h, voidcsfml/src/voidcsfml/graphics.cpp, voidcsfml/include/voidcsfml/audio.h, voidcsfml/src/voidcsfml/audio.cpp, voidcsfml/include/voidcsfml/network.h, voidcsfml/src/voidcsfml/network.cpp, src/system/lib.cr, src/system/obj.cr, src/window/lib.cr, src/window/obj.cr, src/graphics/lib.cr, src/graphics/obj.cr, src/audio/lib.cr, src/audio/obj.cr, src/network/lib.cr, src/network/obj.cr



Error in generate.cr:1895: instantiating 'CModule.class#new(String)'

modules = %w[System Window Graphics Audio Network].map { |m| CModule.new(m) }
                                                                     ^~~

in generate.cr:1579: instantiating 'process_file(String)'

    process_file "#{name}.hpp"
    ^~~~~~~~~~~~

in generate.cr:1668: instantiating 'File.class#each_line(String)'

    File.each_line("#{SFML_PATH}/#{file_name}") do |line|
         ^~~~~~~~~

in /usr/lib/crystal/file.cr:659: instantiating 'open(String, String)'

    open(filename, "r", encoding: encoding, invalid: invalid) do |file|
    ^~~~

in /usr/lib/crystal/file.cr:659: instantiating 'open(String, String)'

    open(filename, "r", encoding: encoding, invalid: invalid) do |file|
    ^~~~

in /usr/lib/crystal/file.cr:660: instantiating 'File#each_line()'

      file.each_line(chomp: chomp) do |line|
           ^~~~~~~~~

in /usr/lib/crystal/file.cr:660: instantiating 'File#each_line()'

      file.each_line(chomp: chomp) do |line|
           ^~~~~~~~~

in generate.cr:1668: instantiating 'File.class#each_line(String)'

    File.each_line("#{SFML_PATH}/#{file_name}") do |line|
         ^~~~~~~~~

in generate.cr:1784: read before assignment to local variable '__temp_496'

        func = CFunction.new(**common_info,
                         ^~~

make[2]: *** [CMakeFiles/c-sources.dir/build.make:75: voidcsfml/include/voidcsfml/system.h] Error 1
make[1]: *** [CMakeFiles/Makefile2:100: CMakeFiles/c-sources.dir/all] Error 2
make: *** [Makefile:130: all] Error 2

Tried to compile on Manjaro Linux
Crystal 0.26
SFML version 2.5

Not really sure what other details I should provide 😨

error on cmake . using Approach 2

cmake --version => 2.8.12.2
sfml 2.4.2 installed in /usr/local/

after running
$ cmake .

get the error:
CMake Error: The source directory "/home/dheitzman/code/crystal/crsfml/voidcsfml" does not appear to contain CMakeLists.txt.

after this, I tried copying CMakeLists.txt.in => CMakeLists.txt, then
$ cmake .

Then I get

-- The C compiler identification is GNU 4.8.4
-- The CXX compiler identification is GNU 4.8.4
-- Check for working C compiler: /usr/bin/cc
-- Check for working C compiler: /usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Check for working CXX compiler: /usr/bin/c++
-- Check for working CXX compiler: /usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
CMake Error at CMakeLists.txt:242 (find_package):
find_package called with invalid argument
"@SFML_VERSION_MAJOR@.@SFML_VERSION_MINOR@"

CMake Warning at CMakeLists.txt:246 (message):
Expecting SFML
@SFML_VERSION_MAJOR@.@SFML_VERSION_MINOR@.@SFML_VERSION_PATCH@, found SFML
..

-- Configuring incomplete, errors occurred!

Wrong example in SF::VertexArray

Currently when you run this example in SF::VertexArray

lines = SF::VertexArray.new(SF::LineStrip, 4)
lines[0] = SF::Vertex.new(SF.vector2f(10, 0))
lines[1] = SF::Vertex.new(SF.vector2f(20, 0))
lines[2] = SF::Vertex.new(SF.vector2f(30, 5))
lines[3] = SF::Vertex.new(SF.vector2f(40, 2))

window.draw(lines)

then it gives you this:

in myfile.cr:44: undefined constant SF::LineStrip (did you mean 'SF::LinesStrip')

lines = SF::VertexArray.new(SF::LineStrip, 4)
                            ^~~~~~~~~~~~~

So using SF::LinesStrip instead of SF::LineStrip fixes it but in the list of primitive types it says that LinesStrip is deprecated and then it says you should use LineStrip instead. That is a bit confusing.

Option for centred text?

I'm currently making a clone of the game Rullo, and I've had to make extensive use of centred text (i.e. text that had a given coordinate, and the centre of the text block was at the coordinate).

Could you provide some way for this to happen?

Execution of command failed in Gentoo

Hello, today I tried to install crsfml for a project I have, and I just followed your Wiki to install and tried with the example provided. I have the same SFML version as the version file, and it's the same ( the last version: 2.5.1)

When I tried to run the snake example, I had this error (I didn't have an error when shard builded sfml for crystal):


~/Documents/Programmation/Crystal SFML ❯❯❯ eix sfml
[I] media-libs/libsfml
     Available versions:  2.5.1(0/2.5) {debug doc examples}
     Installed versions:  2.5.1(0/2.5)(20:10:28 04/12/2021)(-debug -doc -examples)
     Homepage:            https://www.sfml-dev.org/ https://github.com/SFML/SFML
     Description:         Simple and Fast Multimedia Library (SFML)

~/Documents/Programmation/Crystal SFML ❯❯❯ cp lib/crsfml/examples/snakes.cr .
~/Documents/Programmation/Crystal SFML ❯❯❯ crystal snakes.cr 
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/ld : ne peut pas trouver /home/zohran/Documents/Programmation/Crystal : Aucun fichier ou dossier de ce type
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/ld : ne peut pas trouver SFML/lib/crsfml/src/graphics/ext.o : Aucun fichier ou dossier de ce type
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/ld : ne peut pas trouver /home/zohran/Documents/Programmation/Crystal : Aucun fichier ou dossier de ce type
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/ld : ne peut pas trouver SFML/lib/crsfml/src/window/ext.o : Aucun fichier ou dossier de ce type
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/ld : ne peut pas trouver /home/zohran/Documents/Programmation/Crystal : Aucun fichier ou dossier de ce type
/usr/lib/gcc/x86_64-pc-linux-gnu/11.2.1/../../../../x86_64-pc-linux-gnu/bin/ld : ne peut pas trouver SFML/lib/crsfml/src/system/ext.o : Aucun fichier ou dossier de ce type
collect2: erreur: ld a retourné le statut de sortie 1
Error: execution of command failed with code: 1: `cc "${@}" -o /home/zohran/.cache/crystal/crystal-run-snakes.tmp  -rdynamic -L/usr/bin/../lib/crystal /home/zohran/Documents/Programmation/Crystal SFML/lib/crsfml/src/graphics/ext.o -lsfml-graphics -lsfml-window -lsfml-system  /home/zohran/Documents/Programmation/Crystal SFML/lib/crsfml/src/window/ext.o -lsfml-window -lsfml-system  /home/zohran/Documents/Programmation/Crystal SFML/lib/crsfml/src/system/ext.o -lsfml-system  -lstdc++ -lpcre -lm -lgc -lpthread -levent  -lrt -ldl`

Memory leak (maybe directly in sfml)

Hi Oleh,

if I run below code on my machine it starts swapping pretty fast.
Of course I understand the code is badly wrong; I construct a texture in the event loop and run it with 50 fps.
Nevertheless I assume constructing textures in the loop is allowed and needed; and having it done less frequently doesn't change the problem.

Thanks for having a look!

require "crsfml"

window = SF::RenderWindow.new(SF::VideoMode.new(800, 600), "MyTest")
window.framerate_limit = 50

while window.open?
  while event = window.poll_event()
    if (
      event.is_a?(SF::Event::Closed) ||
      (event.is_a?(SF::Event::KeyPressed) && event.code.escape?)
    )
      window.close()
    end
  end

  window.clear SF::Color::Black
  texture = SF::RenderTexture.new(200,100) # define size (in texture)
  #~ texture.clear(SF::Color::Red)
  texture.draw(SF::Sprite.new(SF::Texture.from_file("resources/background.jpg"))) # leaks!
  texture.display()
  sprite = SF::Sprite.new(texture.texture) # make sprite out of texture
  sprite.position = {200, 100} # define position (in sprite)
  window.draw(sprite) # now draw to window

  window.display() # also consumes time until framerate is reached
end

Exporting to IOS?

Hi,

SFML can compile for IOS I was wondering if there is any way to build in Crystal and export to IOS?

Building with VoidCSFML on OSX

cmake . fails with

CMake Error at CMakeLists.txt:15 (find_package):
  By not providing "FindSFML.cmake" in CMAKE_MODULE_PATH this project has
  asked CMake to find a package configuration file provided by "SFML", but
  CMake did not find one.

  Could not find a package configuration file provided by "SFML" (requested
  version 2) with any of the following names:

    SFMLConfig.cmake
    sfml-config.cmake

  Add the installation prefix of "SFML" to CMAKE_PREFIX_PATH or set
  "SFML_DIR" to a directory containing one of the above files.  If "SFML"
  provides a separate development package or SDK, be sure it has been
  installed.

I had to add list(APPEND CMAKE_MODULE_PATH "/usr/local/Cellar/sfml/2.3.2/share/SFML/cmake/Modules") to CmakeLists.txt to make the build work. Is there any way to automate this finding step?

Also, having to compile from source now is a bit of a turn down. Do you have plans to publish voidcsfml through homebrew?

Position seems to double when += 1

My code to move the sprite up and down (Y) is this:

      when SF::Keyboard::Down
        position = sprite.position
        sprite.move(SF.vector2(position.x, position.y - 1))
        window.clear
        window.draw(sprite)
        window.display
      when SF::Keyboard::Up
        position = sprite.position
        sprite.move(SF.vector2(position.x, position.y + 1))
        window.clear
        window.draw(sprite)
        window.display

Now, it seems that the - part is not affecting the sprite position at all, and the sprite position will only increase, and not by 1 at a time but by x2, here is print of the position:

SF::Vector2(Float32)(@x=0, @y=2)
SF::Vector2(Float32)(@x=0, @y=4)
SF::Vector2(Float32)(@x=0, @y=8)
SF::Vector2(Float32)(@x=0, @y=16)
SF::Vector2(Float32)(@x=0, @y=32)
SF::Vector2(Float32)(@x=0, @y=64)
SF::Vector2(Float32)(@x=0, @y=128)
SF::Vector2(Float32)(@x=0, @y=256)
SF::Vector2(Float32)(@x=0, @y=512)
SF::Vector2(Float32)(@x=0, @y=1022)
SF::Vector2(Float32)(@x=0, @y=2044)
SF::Vector2(Float32)(@x=0, @y=4088)
SF::Vector2(Float32)(@x=0, @y=8176)
SF::Vector2(Float32)(@x=0, @y=16352)
SF::Vector2(Float32)(@x=0, @y=32704)
SF::Vector2(Float32)(@x=0, @y=65408)
SF::Vector2(Float32)(@x=0, @y=130816)
SF::Vector2(Float32)(@x=0, @y=261632)
SF::Vector2(Float32)(@x=0, @y=523264)

What am I missing ? :)

Simpler setup on windows

I was wondering i there is a simpler way to setup crsfml on windows. The current process is fairly complex and is likely to be a huge hurdle to people thinking of trying it out.

IMO, a great solution will be to have it as a scoop install where users can just do scoop install crsfml and that fetches the binaries, adds the env vars and creates all the folders needed.

SF::Sound#buffer broken

This code

buffer = SF::SoundBuffer.from_file "foo.wav"
sound = SF::Sound.new
sound.buffer = buffer

buffer_get_result = sound.buffer # Error

produces following error:

in lib/crsfml/src/audio/obj.cr:1180: argument 'result' of 'VoidCSFML#sfml_sound_getbuffer' must be Pointer(Pointer(Void)), not SF::SoundBuffer (nor Pointer(Void) returned by 'SF::SoundBuffer#to_unsafe')

      VoidCSFML.sfml_sound_getbuffer(to_unsafe, result)
                                                ^~~~~~

Issue seems to lie in
https://github.com/oprypin/crsfml/blob/v2.5.0/src/audio/obj.cr#L1178

def buffer() : SoundBuffer?
      result = SoundBuffer.allocate
      VoidCSFML.sfml_sound_getbuffer(to_unsafe, result)
      return result
    end

and
https://github.com/oprypin/crsfml/blob/v2.5.0/voidcsfml/src/voidcsfml/audio.cpp#L383

void sfml_sound_getbuffer(void* self, void** result) {
    *(SoundBuffer**)result = const_cast<SoundBuffer*>(((Sound*)self)->getBuffer());
}

Strange `free(): invalid pointer` on program exit after parsing XML.

In my project, I'm using code to load a .tmx file (Tiled Map). Everything runs fine, but every time the program finishes running, I get free(): invalid pointer. I've finally tracked down exactly where it's happening and was able to make a minimal example, but I am stumped as to why.

require "crsfml"
require "xml"

File.write("test.xml", "<xml />") # just for the test case

XML.parse(File.read("test.xml"))

window = SF::RenderWindow.new
window.create(SF::VideoMode.new(400, 400), "Things")

window.close

If you remove the window bits, there is no error.

If you remove the XML.parse(File.read("test.xml")) line, there is no error.

Anyone know what's going on?

Weird warning with SFML 2.5.1 on arch linux

I installed sfml 2.5.1 via pacman and built crsfml from source.

When I run anything, my program prints
Warning: CrSFML was built for SFML 2.5.1, found SFML 2.5.0 to STDERR 5 times.

I rebuilt crsfml and reinstalled sfml several times.

I do not think that I have another sfml installation anywhere...

Where does it come from?

Can't generate on OS X

When I run ./generate/generate.sh or ./generate.sh I get the following errors:

Traceback (most recent call last):
  File "generate.py", line 605, in <module>
    Visitor().visit(ast)
  File "/usr/local/lib/python3.4/site-packages/pycparser/c_ast.py", line 120, in visit
    return visitor(node)
  File "/usr/local/lib/python3.4/site-packages/pycparser/c_ast.py", line 127, in generic_visit
    self.visit(c)
  File "/usr/local/lib/python3.4/site-packages/pycparser/c_ast.py", line 120, in visit
    return visitor(node)
  File "generate.py", line 518, in visit_Typedef
    node.type.type.my_name = node.type.declname
AttributeError: 'Struct' object has no attribute 'my_name'
cp: generate/*.cr: No such file or directory
rm: generate/*.cr: No such file or directory

This is my environment:

Crystal 0.7.4
Python 3.4.3
CSFML 2.3
pycparser 2.14

SF::Time::Zero doesn't work

When I try to use SF::Time::Zero I get:

in ./src/crsfml/system.cr:132: undefined method 'milliseconds'

    Zero = milliseconds(0)

Workaround: calling SF.milliseconds(0) instead of the above.

SF::View#dup broken

Hi, it seems like SF::View#dup is broken, because it tries to call initialize of either SF::View or SF::View::Reference (which appears twice in src/graphics/obj.cr btw), both of which do not accept a SF::View instance as a single parameter.

Periodic lags due to (possibly) GC

First of all, thanks for the great work.
When i've tried even simplest examples (e.g. a flippy_bird, though it is more visible on a less dynamic example, so i commented line that move bird and it only rotates), i see about half-second lags every several seconds.
I wonder if anyone experience these lags too or problem is just in my PC. In the latter case the issue perhaps should be closed as my linux distro have some compatibility issues irrelevant to the library.

Bunch of `undefined reference` errors

I cloned crsfml and built it:

[  8%] Generating voidcsfml/include/voidcsfml/system.h, voidcsfml/src/voidcsfml/system.cpp, voidcsfml/include/voidcsfml/window.h, voidcsfml/src/voidcsfml/window.cpp, voidcsfml/include/voidcsfml/graphics.h, voidcsfml/src/voidcsfml/graphics.cpp, voidcsfml/include/voidcsfml/audio.h, voidcsfml/src/voidcsfml/audio.cpp, voidcsfml/include/voidcsfml/network.h, voidcsfml/src/voidcsfml/network.cpp, src/system/lib.cr, src/system/obj.cr, src/window/lib.cr, src/window/obj.cr, src/graphics/lib.cr, src/graphics/obj.cr, src/audio/lib.cr, src/audio/obj.cr, src/network/lib.cr, src/network/obj.cr
[  8%] Built target c-sources
Scanning dependencies of target voidcsfml-window
[ 16%] Building CXX object voidcsfml/CMakeFiles/voidcsfml-window.dir/src/voidcsfml/window.cpp.o
[ 25%] Linking CXX shared library libvoidcsfml-window.so
[ 25%] Built target voidcsfml-window
Scanning dependencies of target voidcsfml-audio
[ 33%] Building CXX object voidcsfml/CMakeFiles/voidcsfml-audio.dir/src/voidcsfml/audio.cpp.o
[ 41%] Linking CXX shared library libvoidcsfml-audio.so
[ 41%] Built target voidcsfml-audio
Scanning dependencies of target voidcsfml-graphics
[ 50%] Building CXX object voidcsfml/CMakeFiles/voidcsfml-graphics.dir/src/voidcsfml/graphics.cpp.o
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1269:46: warning: 'create' is deprecated [-Wdeprecated-declarations]
    *(bool*)result = ((RenderTexture*)self)->create((unsigned int)width, (unsigned int)height, depth_buffer != 0);
                                             ^
/usr/include/SFML/Graphics/RenderTexture.hpp:89:5: note: 'create' has been explicitly marked deprecated here
    SFML_DEPRECATED bool create(unsigned int width, unsigned int height, bool depthBuffer);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1374:46: warning: 'capture' is deprecated [-Wdeprecated-declarations]
    *(Image*)result = ((RenderWindow*)self)->capture();
                                             ^
/usr/include/SFML/Graphics/RenderWindow.hpp:158:5: note: 'capture' has been explicitly marked deprecated here
    SFML_DEPRECATED Image capture() const;
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1533:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), (float)x);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:554:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, float x);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1536:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), (float)x, (float)y);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:562:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, float x, float y);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1539:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), (float)x, (float)y, (float)z);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:570:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, float x, float y, float z);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1542:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), (float)x, (float)y, (float)z, (float)w);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:578:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, float x, float y, float z, float w);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1545:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), *(Vector2f*)vector);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:586:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, const Vector2f& vector);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1548:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), *(Vector3f*)vector);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:594:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, const Vector3f& vector);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1551:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), *(Color*)color);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:602:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, const Color& color);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1554:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), *(Transform*)transform);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:610:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, const Transform& transform);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1557:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), *(Texture*)texture);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:618:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, const Texture& texture);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1560:22: warning: 'setParameter' is deprecated [-Wdeprecated-declarations]
    ((Shader*)self)->setParameter(std::string(name, name_size), Shader::CurrentTexture);
                     ^
/usr/include/SFML/Graphics/Shader.hpp:626:5: note: 'setParameter' has been explicitly marked deprecated here
    SFML_DEPRECATED void setParameter(const std::string& name, CurrentTextureType);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1716:20: warning: 'setColor' is deprecated [-Wdeprecated-declarations]
    ((Text*)self)->setColor(*(Color*)color);
                   ^
/usr/include/SFML/Graphics/Text.hpp:210:5: note: 'setColor' has been explicitly marked deprecated here
    SFML_DEPRECATED void setColor(const Color& color);
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
/home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/src/voidcsfml/graphics.cpp:1748:38: warning: 'getColor' is deprecated [-Wdeprecated-declarations]
    *(Color*)result = ((Text*)self)->getColor();
                                     ^
/usr/include/SFML/Graphics/Text.hpp:338:5: note: 'getColor' has been explicitly marked deprecated here
    SFML_DEPRECATED const Color& getColor() const;
    ^
/usr/include/SFML/Config.hpp:191:45: note: expanded from macro 'SFML_DEPRECATED'
    #define SFML_DEPRECATED __attribute__ ((deprecated))
                                            ^
14 warnings generated.
[ 58%] Linking CXX shared library libvoidcsfml-graphics.so
[ 58%] Built target voidcsfml-graphics
Scanning dependencies of target voidcsfml-system
[ 66%] Building CXX object voidcsfml/CMakeFiles/voidcsfml-system.dir/src/voidcsfml/system.cpp.o
[ 75%] Linking CXX shared library libvoidcsfml-system.so
[ 75%] Built target voidcsfml-system
Scanning dependencies of target voidcsfml-network
[ 83%] Building CXX object voidcsfml/CMakeFiles/voidcsfml-network.dir/src/voidcsfml/network.cpp.o
[ 91%] Linking CXX shared library libvoidcsfml-network.so
[ 91%] Built target voidcsfml-network
[ 91%] Built target VoidCSFML
[100%] Built target crystal-sources
[100%] Built target CrSFML

And when I try to run my binary using it, I get:

(many more undefined reference errors)
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Texture::isSmooth() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Texture::Texture(sf::Texture const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Shape::getLocalBounds() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Transformable::getScale() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-audio.so: undefined reference to `sf::SoundStream::getLoop() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::VertexBuffer::update(sf::VertexBuffer const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Text::setStyle(unsigned int)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-audio.so: undefined reference to `sf::Sound::setBuffer(sf::SoundBuffer const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-window.so: undefined reference to `sf::Joystick::update()'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Shape::Shape()'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-system.so: undefined reference to `sf::Mutex::~Mutex()'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::CircleShape::getRadius() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Window::setMouseCursorVisible(bool)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-audio.so: undefined reference to `sf::SoundBuffer::loadFromSamples(short const*, unsigned long long, unsigned int, unsigned int)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-window.so: undefined reference to `sf::operator<(sf::VideoMode const&, sf::VideoMode const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Text::getLetterSpacing() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-system.so: undefined reference to `sf::operator-(sf::Time)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::BlendMode::BlendMode()'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Shader::loadFromMemory(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Shader::loadFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&, std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Text::getLineSpacing() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Image::loadFromFile(std::__cxx11::basic_string<char, std::char_traits<char>, std::allocator<char> > const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Transformable::getPosition() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Text::setLetterSpacing(float)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Shape::setFillColor(sf::Color const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Sprite::Sprite()'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-system.so: undefined reference to `sf::operator*(sf::Time, long long)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Text::setFillColor(sf::Color const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::ConvexShape::setPoint(unsigned long, sf::Vector2<float> const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-audio.so: undefined reference to `sf::SoundSource::setRelativeToListener(bool)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-window.so: undefined reference to `sf::Context::Context()'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Window::close()'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::VertexBuffer::setUsage(sf::VertexBuffer::Usage)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-window.so: undefined reference to `sf::Keyboard::setVirtualKeyboardVisible(bool)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::RenderTexture::create(unsigned int, unsigned int, bool)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Transform::translate(sf::Vector2<float> const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-window.so: undefined reference to `sf::Joystick::Identification::Identification()'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::RectangleShape::getSize() const'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Transform::Transform(float, float, float, float, float, float, float, float, float)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Window::setSize(sf::Vector2<unsigned int> const&)'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `vtable for sf::RectangleShape'
/usr/bin/ld: /home/sardaukar/Code/FOREIGN/crsfml/voidcsfml/libvoidcsfml-graphics.so: undefined reference to `sf::Texture::update(sf::Window const&)'
collect2: error: ld returned 1 exit status
Error: execution of command failed with code: 1: `cc "${@}" -o '/home/sardaukar/Code/OWN/crystal/adlez/adlez_release'  -rdynamic  -lxml2 -lvoidcsfml-audio -lvoidcsfml-graphics -lvoidcsfml-window -lvoidcsfml-system -lpcre -lm -lgc -lpthread /home/sardaukar/.asdf/installs/crystal/0.23.1/src/ext/libcrystal.a -levent -lrt -ldl -L/usr/lib -L/usr/local/lib`
make: *** [Makefile:20: adlez] Error 1

I'm on Manjaro Linux, with SFML 2.5.1 installed, on Crystal 0.23.1 (for legacy reasons, want to convert this old project I have) and GCC 9.2.0.

Please let me know what I'm doing wrong.

Build fails with SFML 2.5.1 on Mac OS Mojave 10.14.3

I tried to install CrSFML on a MacBook Pro 2017 running Mac OS Mojave 10.14.3

I followed the installation instructions for Approach 1 described in https://github.com/oprypin/crsfml .

I started with the installation of SFML using homebrew. A week ago that installed SFML 2.4.2 and most recently (a day or two ago) that changed to installing SFML 2.5.1. This change broke everything!!!

It should be noted that the SFML 2.5.1 installation changed significantly, especially regarding the location and content of the cmake Modules.

Homebrew installs SFML 2.4.2 in the following directories:

  • /usr/local/Cellar/sfml/2.4.2_1

with the following subdirectories:

  • lib (containing all libsfml-*.dylib)
  • include/SFML (containing all top-level C++ header files plus subdirectories for Audio etc.)
  • share/SFML/doc
  • share/SFML/cmake/Modules (containing the FindSFML.cmake module for locating the SFML library)

Moreover, soft links are created in the following directories:

  • /usr/local/lib (with soft links to all libsfml-*.dylib stored in the lib directory of the homebrew cellar)
  • /usr/local/include (with a SFML subdirectory soft link to the include/SFML directory of the homebrew cellar)
  • /usr/local/share (with a SFML subdirectory soft link to the share/SFML directory of the homebrew cellar)

NOTE: with the SFML 2.4.2 directory setup described above, the CrSFML installation (using Approach 1, i.e. running the command: cmake . && make from the crsfml directory) works just fine :-).

In contrast, when SFML 2.5.1 is installed via homebrew, it seems to break the CrSFML cmake installation procedure. Homebrew install the newest SFML version in the following directories:

  • /usr/local/Cellar/sfml/2.5.1

with the following subdirectories:

  • lib (containing all libsfml-*.dylib)
  • lib/cmake/SFML (containing a SFMLconfig.cmake plus four additional cmake files)
  • include/SFML (containing all top-level C++ header files plus subdirectories for Audio etc.)
  • share/SFML
  • NOTE: the previously existing share/SFML/cmake/Modules subdirectory has been removed for SFML 2.5.1.

Moreover, soft links are created in the following directories:

  • /usr/local/lib (with soft links to all libsfml-*.dylib stored in the lib directory of the homebrew cellar)
  • /usr/local/lib/cmake (with a SFML subdirectory soft link to the lib/cmake/SFML directory of the homebrew cellar)
  • /usr/local/include (with a SFML subdirectory soft link to the include/SFML directory of the homebrew cellar)
  • /usr/local/share (with a SFML subdirectory soft link to the share/SFML directory of the homebrew cellar)

In summary, the main differences are:

  1. all cmake files are now stored in the lib/cmake/SFML subdirectory of the homebrew cellar.
  2. these cmake files are now accessible via soft links via /usr/local/lib/cmake/SFML.

As a consequence, the installation of CrSFML (using Approach 1) fails as follows:

$ cmake . && make
-- The C compiler identification is AppleClang 10.0.0.10001044
-- The CXX compiler identification is AppleClang 10.0.0.10001044
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc
-- Check for working C compiler: /Library/Developer/CommandLineTools/usr/bin/cc -- works
-- Detecting C compiler ABI info
-- Detecting C compiler ABI info - done
-- Detecting C compile features
-- Detecting C compile features - done
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++
-- Check for working CXX compiler: /Library/Developer/CommandLineTools/usr/bin/c++ -- works
-- Detecting CXX compiler ABI info
-- Detecting CXX compiler ABI info - done
-- Detecting CXX compile features
-- Detecting CXX compile features - done
-- Found SFML 2.5.1 in /usr/local/lib/cmake/SFML
-- Found SFML 2.5.1 in /usr/local/lib/cmake/SFML
-- Configuring done
CMake Warning (dev):
  Policy CMP0068 is not set: RPATH settings on macOS do not affect
  install_name.  Run "cmake --help-policy CMP0068" for policy details.  Use
  the cmake_policy command to set the policy and suppress this warning.

  For compatibility with older versions of CMake, the install_name fields for
  the following targets are still affected by RPATH settings:

   voidcsfml-audio
   voidcsfml-graphics
   voidcsfml-network
   voidcsfml-system
   voidcsfml-window

This warning is for project developers.  Use -Wno-dev to suppress it.

-- Generating done
-- Build files have been written to: /Users/cjaensch/Dev/Crystal/3rdparty/crsfml
Scanning dependencies of target c-sources
[  8%] Generating voidcsfml/include/voidcsfml/system.h, voidcsfml/src/voidcsfml/system.cpp, voidcsfml/include/voidcsfml/window.h, voidcsfml/src/voidcsfml/window.cpp, voidcsfml/include/voidcsfml/graphics.h, voidcsfml/src/voidcsfml/graphics.cpp, voidcsfml/include/voidcsfml/audio.h, voidcsfml/src/voidcsfml/audio.cpp, voidcsfml/include/voidcsfml/network.h, voidcsfml/src/voidcsfml/network.cpp, src/system/lib.cr, src/system/obj.cr, src/window/lib.cr, src/window/obj.cr, src/graphics/lib.cr, src/graphics/obj.cr, src/audio/lib.cr, src/audio/obj.cr, src/network/lib.cr, src/network/obj.cr
Unhandled exception: Error opening file '/usr/include/SFML/System.hpp' with mode 'r': No such file or directory (Errno)
  from /usr/local/Cellar/crystal/0.27.2/src/crystal/system/unix/file.cr:10:7 in 'open'
  from /usr/local/Cellar/crystal/0.27.2/src/file.cr:104:10 in 'new'
  from generate.cr:683:5 in 'process_file'
  from generate.cr:1580:5 in 'initialize'
  from generate.cr:1565:3 in 'new'
  from /usr/local/Cellar/crystal/0.27.2/src/array.cr:130:9 in '__crystal_main'
  from /usr/local/Cellar/crystal/0.27.2/src/crystal/main.cr:97:5 in 'main_user_code'
  from /usr/local/Cellar/crystal/0.27.2/src/crystal/main.cr:86:7 in 'main'
  from /usr/local/Cellar/crystal/0.27.2/src/crystal/main.cr:106:3 in 'main'
make[2]: *** [voidcsfml/include/voidcsfml/system.h] Error 1
make[1]: *** [CMakeFiles/c-sources.dir/all] Error 2
make: *** [all] Error 2

The above error message clearly indicates that a SFML include header is not found and in fact looked up in the wrong directory. In order to fix this, I tried the cmake options explanation provided in: https://github.com/oprypin/crsfml/blob/master/voidcsfml/README.md#cmake-options .

When I ran the following command:

cmake -DSFML_INCLUDE_DIR="/usr/local/include" . && make

the installation managed to find the SFML header files. But now the installation fails to resolve the location of the SFML dynamic libraries (libsfml-*.dylib). The associated cmd output is shown below:

...
-- Generating done
-- Build files have been written to: /Users/cjaensch/Dev/Crystal/3rdparty/crsfml
[  8%] Generating voidcsfml/include/voidcsfml/system.h, voidcsfml/src/voidcsfml/system.cpp, voidcsfml/include/voidcsfml/window.h, voidcsfml/src/voidcsfml/window.cpp, voidcsfml/include/voidcsfml/graphics.h, voidcsfml/src/voidcsfml/graphics.cpp, voidcsfml/include/voidcsfml/audio.h, voidcsfml/src/voidcsfml/audio.cpp, voidcsfml/include/voidcsfml/network.h, voidcsfml/src/voidcsfml/network.cpp, src/system/lib.cr, src/system/obj.cr, src/window/lib.cr, src/window/obj.cr, src/graphics/lib.cr, src/graphics/obj.cr, src/audio/lib.cr, src/audio/obj.cr, src/network/lib.cr, src/network/obj.cr
[  8%] Built target c-sources
Scanning dependencies of target voidcsfml-window
[ 16%] Building CXX object voidcsfml/CMakeFiles/voidcsfml-window.dir/src/voidcsfml/window.cpp.o
[ 25%] Linking CXX shared library libvoidcsfml-window.dylib
Undefined symbols for architecture x86_64:
  "sf::Mouse::getPosition(sf::Window const&)", referenced from:
      _sfml_mouse_getposition_JRh in window.cpp.o
  "sf::Mouse::getPosition()", referenced from:
      _sfml_mouse_getposition in window.cpp.o 

I finally tried the following command, hoping that explicitly setting the cmake module path might help. But I still get the same message as shown above, i.e. it fails to resolve the SFML library path during linkage.

sfml=(/usr/local/Cellar/sfml/2.5.1)
cmake -DSFML_INCLUDE_DIR="/usr/local/include" -DCMAKE_MODULE_PATH="$sfml/lib/cmake/SFML" . && make

NOTE: I have no experience with cmake, otherwise I would have fixed this. Obviously, the linkage step fails to resolve the SFML library path and this despite the fact that all libraries are accessible from /usr/local/lib through soft links.

For now, the only way I managed to install CrSFML is using SFML 2.4.2. This wouldn't be half as bad, if homebrew still offered to install a previous version of SFML out-of-the-box. I took me a few hours to finally figure out how to trick homebrew to do so.

I hope you can offer some help on getting CrSFML installed with SFML 2.5.1. Any help is highly appreciated!!!

undefined method 'cast' for UInt64:Class

Is this a Crystal issue or crsfml issue?
Seems to be a type casting problem.

I can reproduce it by running a basic window and trying to draw a triangle using the CircleShape class. Seems to produce the same error message each time. http://d.pr/i/1exJ5

window = SF::RenderWindow.new(SF.video_mode(600, 600), "Triangle Test")
while window.open?
  while event = window.poll_event
    if event.type == SF::Event::Closed
      window.close
    elsif event.type == SF::Event::Resized
      # adjust the viewport when the window is resized.
      GL.viewport(0, 0, event.size.width, event.size.height)
    end
  end

  window.clear SF::Color::Black

  triangle = SF::CircleShape.new(80, 3)
  triangle.fill_color = SF::Color::Red
  window.draw(triangle)

  window.display
end

Can't run examples :(

I've installed SFML and CSFML and VoidCSFML (I think, new Fedora setup - sorry). This is what I get when I run sudo ldconfig -v | grep sfml:

	libvoidcsfml-network.so.2.4 -> libvoidcsfml-network.so.2.4
	libvoidcsfml-audio.so.2.4 -> libvoidcsfml-audio.so.2.4
	libvoidcsfml-graphics.so.2.4 -> libvoidcsfml-graphics.so.2.4
	libvoidcsfml-window.so.2.4 -> libvoidcsfml-window.so.2.4
	libvoidcsfml-system.so.2.4 -> libvoidcsfml-system.so.2.4
	libcsfml-audio.so.2.4 -> libcsfml-audio.so.2.4.0
	libcsfml-graphics.so.2.4 -> libcsfml-graphics.so.2.4.0
	libcsfml-network.so.2.4 -> libcsfml-network.so.2.4.0
	libcsfml-window.so.2.4 -> libcsfml-window.so.2.4.0
	libcsfml-system.so.2.4 -> libcsfml-system.so.2.4.0
	libsfml-window.so.2.4 -> libsfml-window.so.2.4.2
	libsfml-system.so.2.4 -> libsfml-system.so.2.4.2
	libsfml-network.so.2.4 -> libsfml-network.so.2.4.2
	libsfml-graphics.so.2.4 -> libsfml-graphics.so.2.4.2
	libsfml-audio.so.2.4 -> libsfml-audio.so.2.4.2

I had to add /usr/local/lib to /etc/ld.so.conf for it to work. But now when I run an example, I get:

❯ crystal examples/diagnostics.cr                                                                                          
Error in examples/diagnostics.cr:2: while requiring "crsfml": can't find file 'crsfml' relative to '/home/sardaukar/Code/FOREIGN/crsfml/examples'

require "crsfml"

This is odd, since lib/crsfml relative to examples points to ../../src.

What am I doing wrong? Thanks.

The require and files should be "crsfml", not "csmfl"

Because in a Projectfile to include this as a library you do:

deps do
  github "BlaXpirit/crsfml"
end

But then you do:

require "crsfml"

and it doesn't work. You have to rewrite the Projectfile like this:

deps do
  github "BlaXpirit/crsfml", name: "csfml"
end

and then do:

require "csfml"

Which is more typing and unintuitive.

You are basically not following the rules for libraries, rules that are written nowhere 😊

But luckily the next version of the compiler will include crystal init which will simplify libraries creation and set them in the right track.

FindSFML.cmake required to build VoidCSFML SFML v2.5.x

OS: OSX 10.13.6 High Sierra

FindSFML.cmake is no longer included with SFML v2.5.x (see here).
I had to copy FindSFML.cmake from my old install for VoidCSFML to work.

I wanted to try SF::Cursor, a SFML v2.5.x feature but realized brew only installs 2.4.2

I tried upgrading to v2.5.1 using a formula that has not been merged yet:

brew install https://raw.githubusercontent.com/Homebrew/homebrew-core/e057f60014c3f95710542915df6ba763ee57923b/Formula/sfml.rb

Next I tried building VoidCSFML:

crystal run generate.cr -- /usr/local/opt/sfml/include
cmake . && make

And I got linking errors. The new v2.5.x directory structure is a little different. Modules are now found in:

/usr/local/opt/sfml/lib/cmake/SFML

I had to create the cmake dir in /usr/local/opt/sfml/share/sfml and create a symlink to the correct dir:

cd /usr/local/opt/sfml/share/sfml
mkdir cmake
cd cmake
ln -s /usr/local/opt/sfml/lib/cmake/SFML Modules

So I tried copying it from my old SFML install to the new one. And it worked:

cp /old/path/FindSFML.cmake /usr/local/opt/sfml/lib/cmake/SFML

Then I could build VoidCSFML without issues and got to enjoy my fancy new cursor.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.