GTK-SFML is a C++ library that integrates SFML with gtkmm.
On some systems, this approach allows for higher frame rates than using OpenGL directly with GTK+ (e.g. GTK+ will cap the OpenGL rendering to 30 fps, while gtk-sfml will perform the exact same rendering at 60 fps)
This is done through widgets that offer an on_render()
pure virtual method for SFML
rendering.
Here's an example from the examples directory:
#include <SFML/Graphics.hpp>
#include <SFML/System/Vector2.hpp>
#include <gtkmm.h>
#include <gtk-sfml/Window.hpp>
#include "font.hpp"
struct MyWindow :
gtksfml::Window {
sf::Font font;
sf::Text text{"Window demo", font};
MyWindow()
{
set_default_size(500, 300);
font.loadFromMemory(FONT_DATA, FONT_SIZE);
text.setPosition(100, 100);
}
void on_render() override
{
clear({64, 0, 0});
draw(text);
display();
}
};
int main()
{
auto app = Gtk::Application::create();
MyWindow window;
return app->run(window);
}
The following widgets are provided:
gtksfml::ApplicationWindow
(from<gtk-sfml/ApplicationWindow.hpp>
)gtksfml::DrawingArea
(from<gtk-sfml/DrawingArea.hpp>
)gtksfml::Window
(from<gtk-sfml/Window.hpp>
)
To use these classes, the user must derive from them, and override one or more of the methods:
void on_event(const sf::Event& event)
: this is how GTK+ events can be handled as if they were SFML events. You cannot usepollEvent()
to retrieve events, since GTK+ is handling the events.void on_render()
: (mandatory) called by GTK+ during a widget's "draw" event.void on_update()
: called periodically if the "auto update" flag is set.
Each GTK-SFML widget has an "auto-update" flag, that can be controlled by the
set_auto_update(bool enable)
method. When it's enabled, the on_update()
method will be
called periodically, followed by a redraw request (that will invoke on_render()
). If
"auto-update" is disabled, the on_update()
method is not called, and the redraw only
occurs when GTK+ deems it necessary (e.g. the widget got resized.) This flag is enabled by
default.
For this library to work you need:
-
When cloning from the repository, you need to first run
./bootstrap
. This step is not needed when obtaining a source tarball, as the resulting files are already included in the tarball. -
./configure --prefix=/usr
-
make
-
sudo make install
For more installation options, use ./configure --help
. This is a standard Automake
package.
A pkg-config
script, gtk-sfml.pc
will be installed, which provides the compilation and
linker flags to use gtk-sfml.
In a Makefile, you would use:
CXXFLAGS := $(shell pkg-config --cflags gtk-sfml)
LIBS := $(shell pkg-config --libs gtk-sfml)
If using Autoconf/Automake, you can use the PKG_CHECK_MODULES
macro in configure.ac
:
PKG_CHECK_MODULES([GTKSFML], [gtk-sfml])
Then the Makefile.am
can use:
AM_CXXFLAGS = $(GTKSFML_CFLAGS)
LDADD = $(GTKSFML_LIBS)
# if linking into a library, use _LIBADD instead of LDADD
The FindPkgConfig module can obtain the flags from a pkg-config module:
find_package(PkgConfig)
pkg_check_modules(GTKSFML gtk-sfml)
# now use GTKSFML_CFLAGS and GTKSFML_LDFLAGS in your target
Simply use command
substitution
to invoke pkg-config
:
g++ -c my-code.cpp $(pkg-config gtk-sfml --cflags)
g++ my-code.o -o my-program $(pkg-config gtk-sfml --libs)