8000 GitHub - dkosmari/gtk-sfml: A set of gtkmm3 widgets for SFML integration.
[go: up one dir, main page]
More Web Proxy on the site http://driver.im/
Skip to content

dkosmari/gtk-sfml

Repository files navigation

GTK-SFML - A set of gtkmm widgets for integration with SFML.

GTK-SFML is a C++ library that integrates SFML with gtkmm.

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 from the examples directory:

#include <SFML/Graphics.hpp>
#include <SFML/System/Vector2.hpp>
#include <gtkmm.h>

#include <gtk3-sfml3/Window.hpp>

#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:

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 (only gtkmm 3.x is supported at the moment)
  • SFML (version 3.0 or above)
  • A C++17 (or better) compiler.

Build instructions

  1. 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.

  2. ./configure --prefix=/usr

  3. make

  4. 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:

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:

AM_CXXFLAGS = $(GTKSFML_CFLAGS)
LDADD = $(GTKSFML_LIBS)
# if linking into a library, use _LIBADD instead of LDADD

With Cmake

The FindPkgConfig module can obtain the flags from a pkg-config module:

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 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)
0