https://github.com/dkosmari/gtk-sfml
A set of gtkmm3 widgets for SFML integration.
https://github.com/dkosmari/gtk-sfml
c-plus-plus cpp cross-platform-library graphics gtkmm gtkmm3 library multimedia opengl sfml
Last synced: 2 months ago
JSON representation
A set of gtkmm3 widgets for SFML integration.
- Host: GitHub
- URL: https://github.com/dkosmari/gtk-sfml
- Owner: dkosmari
- License: gpl-3.0
- Created: 2023-10-30T05:02:34.000Z (over 2 years ago)
- Default Branch: master
- Last Pushed: 2023-11-09T19:28:24.000Z (over 2 years ago)
- Last Synced: 2025-02-22T12:26:19.346Z (over 1 year ago)
- Topics: c-plus-plus, cpp, cross-platform-library, graphics, gtkmm, gtkmm3, library, multimedia, opengl, sfml
- Language: C++
- Homepage:
- Size: 364 KB
- Stars: 3
- Watchers: 1
- Forks: 0
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- Changelog: ChangeLog
- License: COPYING
- Authors: AUTHORS
Awesome Lists containing this project
README
# GTK-SFML - A set of gtkmm widgets for integration with SFML.
GTK-SFML is a C++ library that integrates [SFML](https://www.sfml-dev.org/) with
[gtkmm](https://gtkmm.org).
This version supports gtkmm 3 and SFML 3.
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.
## Example
Here's [an example](examples/window.cpp) from the examples directory:
```cpp
#include
#include
#include
#include
#include "font.hpp"
struct MyWindow :
gtksfml::Window {
sf::Font font{FONT_DATA, FONT_SIZE};
sf::Text text{font, "Window demo"};
MyWindow()
{
set_default_size(500, 300);
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 [``](include/gtk3-sfml3/ApplicationWindow.hpp))
- `gtksfml::DrawingArea` (from [``](include/gtk3-sfml3/DrawingArea.hpp))
- `gtksfml::Window` (from [``](include/gtk3-sfml3/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 use `pollEvent()` 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.
## Prerequisites
For this library to work you need:
- [gtkmm](https://gtkmm.org) (only gtkmm 3.x is supported at the moment)
- [SFML](https://www.sfml-dev.org/) (version 3.0 or above)
- A C++17 (or better) compiler.
## Build instructions
0. 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.
1. `./configure --prefix=/usr`
2. `make`
3. `sudo make install`
For more installation options, use `./configure --help`. This is a standard Automake
package.
## Usage
A `pkg-config` script, `gtk3-sfml3.pc` will be installed, which provides the compilation
and linker flags to use GTK-SFML.
### With Makefiles
In a Makefile, you would use:
```Makefile
CXXFLAGS := $(shell pkg-config --cflags gtk3-sfml3)
LIBS := $(shell pkg-config --libs gtk3-sfml3)
```
### With Autoconf/Automake
If using Autoconf/Automake, you can use the `PKG_CHECK_MODULES` macro in `configure.ac`:
```
PKG_CHECK_MODULES([GTKSFML], [gtk3-sfml3])
```
Then the `Makefile.am` can use:
```Makefile
AM_CXXFLAGS = $(GTKSFML_CFLAGS)
LDADD = $(GTKSFML_LIBS)
# if linking into a library, use _LIBADD instead of LDADD
```
### With Cmake
The [FindPkgConfig](https://cmake.org/cmake/help/latest/module/FindPkgConfig.html) module can obtain the flags from a pkg-config module:
```cmake
find_package(PkgConfig)
pkg_check_modules(GTKSFML gtk3-sfml3)
# now use GTKSFML_CFLAGS and GTKSFML_LDFLAGS in your target
```
### Invoking the compiler directly in a shell
Simply use [command
substitution](https://www.gnu.org/software/bash/manual/html_node/Command-Substitution.html)
to invoke `pkg-config`:
```
g++ -c my-code.cpp $(pkg-config gtk3-sfml3 --cflags)
g++ my-code.o -o my-program $(pkg-config gtk3-sfml3 --libs)
```