Welcome, Guest. Please login or register. Did you miss your activation email?

Show Posts

This section allows you to view all posts made by this member. Note that you can only see posts made in areas you currently have access to.

Topics - texus

Pages: [1] 2
SFML development / DPI awareness on Windows
« on: November 03, 2022, 11:22:58 pm »
I've written an implementation to provide minimal High-DPI support on Windows. The code can be found at https://github.com/texus/SFML/tree/feature/high-dpi-windows (view diff).
I can make a PR for this once the design has been discussed. I don't expect the code to be merged as-is, the design can be discussed and altered, but the code itself is already functional (although not extensively tested).

I've looked at how DPI scaling is handled on Windows in SDL and GLFW and did some research on my own. When I saw GLFW's implementation I started questioning whether we really need to have a framebuffer and window that have seperate sizes like in SDL: while GLFW has a separate framebuffer size, it keeps the same size as the window by default. Based on this I designed the DPI support to work without much changes to SFML's API.

Current SFML behavior

Before creating the window, System Aware scaling is enabled, but the window size is exactly as requested (i.e. no scaling is happening).

Unfortunately when dragging the window to a monitor with different scaling, Windows stretches the window and draws it at a different size without SFML knowing about it.

New default behavior

Per Monitor aware scaling is requested when creating the window. This changes nothing when only one monitor is involved or if they all have the same scaling, the window will still be exactly the size as requested. Moving the window to a monitor with different scaling (or changing the DPI on the monitor where the window is open) keeps the exact same window size though.

I've created a PR on github to make this the default behavior even in SFML 2.6, as this should be better behavior than System Aware.

New behavior with HighDpi

When manually choosing to have a high-DPI window (in my code this is done through VideoMode) the window may be created at a larger size than requested. If the main monitor has a scaling of 150% and a window of 800x600 is created, SFML would create a window of 1200x900. Everything has a 1:1 relation afterwards: rendering and mouse events all happen with the 1200x900 rectangle, only the initial size of the window differs.
When dragging the window to a monitor with 200% scaling (or when changing the scaling of the display that contains the window), the window will automatically be resized to 1600x1200. When moved back to the first monitor, it will become 1200x900 again.

On Windows 10 version 1703 or higher, the title bar of the window will also be scaled with the window (Per Monitor V2 awareness). On older Windows versions, the title bar will keep the size which it has when creating the window.

I did notice a rare bug on my system where the window sometimes gets a few pixels smaller or larger when moving monitor, but this occurs because a Windows event (WM_GETDPISCALEDSIZE) isn't being send to the app. I reproduced this in both SDL and GLFW so I'm assuming this is a bug in Windows, so I didn't bother trying to work around it.

There is currently no special event when the window changes monitor, but you would get an event about the window changing size and you could call window.getDpiScale() to figure out whether the resize was the result of the scaling changing or not.

Example code

#include <SFML/Graphics.hpp>
#include <iostream>

const bool highDpi = true;

int main()
    sf::RenderWindow window{ sf::VideoMode{{800, 600}, 32, highDpi}, "SFML - HighDpi" };

    // The green rectangle is centered in the window at 100% scaling
    sf::RectangleShape shape1;
    shape1.setPosition({ 100, 100 });
    shape1.setSize({ 600, 400 });

    // The red rectangle is placed just outside the view at 100% scaling
    sf::RectangleShape shape2;
    shape2.setPosition({ 800, 600 });
    shape2.setSize({ 800, 600 });

    while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)

            else if (event.type == sf::Event::Resized)
                window.setView(sf::View{ sf::FloatRect{{0.f, 0.f},
                    {static_cast<float>(event.size.width), static_cast<float>(event.size.height)}} });

                std::cerr << "Resized: "
                          << event.size.width << "x" << event.size.height
                          << "  dpi-scale=" << window.getDpiScale()
                          << "\n";


How to test

Go to Display Settings in Windows and change the Scale to a value other than 100%.
If you have 2 monitors, set the same or a different scale and see what happens when the window is dragged to the other monitors.
You can also change the scale while the window is open.

The code contains different code paths depending on the system. While I did test different code paths by manually editing the code, I only tested on Windows 10 21H2.
- Windows 10 version 1703 or newer (Per Monitor V2 awareness)
- Windows 10 version 1607 (Per Monitor V2 introduced, documentation is unclear about whether you need 1607 or 1703 to use V2)
- Windows 8.1 or Windows 10 before version 1607 (Per Monitor V1 awareness)
- Windows Vista, Windows 7 and Windows 8.0 (Only System Aware, no per monitor settings)
- Windows XP or any other version older than Vista (no DPI scaling at all)

Feature requests / Diagonal resize cursors on Linux
« on: July 12, 2020, 05:51:22 pm »
I would like to change the mouse cursor when the mouse is at the corner of a child window in my gui, but SFML doesn't support diagonal arrows on Linux (Cursor.hpp#L65-L66).

I know that this is because X11 doesn't support those double arrows, but could directional arrows (e.g. XC_bottom_left_corner) perhaps be added?
The following would be useful, but only the last 4 are required to add support for diagonal resizing on linux.
- SizeLeft
- SizeRight
- SizeTop
- SizeBottom
- SizeTopLeft
- SizeTopRight
- SizeBottomLeft
- SizeBottomRight

On linux, all of those exist.

On windows, none of those exist. It should be easy to just display the double arrow if one of those directional sizes are requested though.

On macOS, all of those should also exist, but are undocumented just like the diagonal double arrows. I'm not sure if they are ever used on mac though, maybe it would be better to fall back to double arrows here as well (for my gui I would only try to set those directional cursors in Linux anyway and use double arrows on Windows and macOS).

Is there any chance that those cursors could be added to SFML, even though they are specific for Linux?
I've already changed my code to just load those cursors directly and only use SFML when loading other cursors, but it would be nice if I could set those cursors via SFML as well in future SFML versions (as I now have to link with Xlib myself).

When linking static SFML libraries on linux, cmake doesn't automatically link to GLX (while it links fine to all other SFML dependencies).

The following simple cmake script is enough to reproduce it.
cmake_minimum_required(VERSION 3.5)

find_package(SFML 2 COMPONENTS graphics window system REQUIRED)

add_executable(Test main.cpp)
target_link_libraries(Test PRIVATE sfml-graphics)

The main.cpp file is practically empty:
#include <SFML/Graphics.hpp>
int main()
    sf::RenderTexture target;

Running make will give the following output:
(click to show/hide)

Changing SFML_STATIC_LIBRARIES to FALSE fixes it, so it only occurs when linking statically.
Adding "GLX" to the target_link_libraries line also fixes the issue.

Edit: Linking to "GL" (instead of "GLX") also fixes it (but linking to "OpenGL" doesn't).

General / [iOS] SFMLConfigDependencies.cmake looks for OpenGL
« on: April 08, 2018, 09:08:29 am »
When using the latest SFML version from github, I can't use SFMLConfig.cmake to find SFML when building for iOS.
On iOS the sfml-window target has to link to OpenGL ES but the SFMLConfigDependencies.cmake always looks for OpenGL, so CMake gives the following error on find_package(SFML ...)
CMake Error at /Users/texus/Desktop/SFML/build-iOS/SFMLConfigDependencies.cmake:34 (set_property):
  set_property could not find TARGET OpenGL.  Perhaps it has not yet been
Call Stack (most recent call first):
  /Users/texus/Desktop/SFML/build-iOS/SFMLConfigDependencies.cmake:55 (sfml_bind_dependency)
  /Users/texus/Desktop/SFML/build-iOS/SFMLConfig.cmake:117 (include)
  /Users/texus/Desktop/SFML/cmake/toolchains/iOS.toolchain.cmake:211 (find_package)
  CMakeLists.txt:175 (find_host_package)

Graphics / RenderTexture texture flipped after copying
« on: November 13, 2017, 07:26:56 pm »
Since commit 6b71456, copying a texture from a render texture causes it to be flipped.

Code to reproduce:
#include <SFML/Graphics.hpp>

int main()
    sf::RenderWindow window(sf::VideoMode(400, 200), "SFML Window");

    sf::RectangleShape shape{{100, 100}};

    sf::RenderTexture rt;
    rt.create(200, 200);

    sf::Sprite s1(rt.getTexture());

    sf::Texture t = rt.getTexture(); // Copy texture before passing it to sprite
    sf::Sprite s2(t);
    s2.setPosition({200, 0});

    while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)


The attached images show how it looked before and after commit 6b71456.

Window / Android sends Delete event instead of Backspace
« on: October 22, 2017, 11:37:21 am »
SFML maps the AKEYCODE_DEL event to Keyboard::Delete (WindowImplAndroid.cpp#L623) while this is actually the backspace key. The real delete key is AKEYCODE_FORWARD_DEL.

Is this a bug or was there a reason to do this? I find it strange that I just worked around this in my code when I encountered it a few years ago. Maybe I saw this post back then and though that it would be fixed, but it looks like it was forgotten.

I found a case in which text rendering is broken. After spending most of my day minimizing the code, I came up with the following minimal example.
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

int main()
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    sf::Font font;

    sf::Text text("Hello", font, 17);

    font.getGlyph('g', 130, false); // A character that does not occur in the text

    for (const auto c : text.getString())
        font.getGlyph(c, 130, false);


    sf::Text anotherText("Hello", font, 17); // Same string

    while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)


        GLint m_scissor[4] = {};
        glGetIntegerv(GL_SCISSOR_BOX, m_scissor);
        glScissor(0, 150, 400, 450);

        // Clear again with different color just to show clipping area


        glScissor(m_scissor[0], m_scissor[1], m_scissor[2], m_scissor[3]);


    return EXIT_SUCCESS;

Instead of showing "Hello", it shows only the top part of some letters (the "L"s are ccompletely gone). I've attached a screenshot showing the issue.

I traced back at which SFML version this problem was introduced and I found that it only started since 6b71456. SFML versions before this commit show the text correctly.

It looks like glScissor has an effect on the internal font rendering, calling glDisable(GL_SCISSOR_TEST) at the start of Font::loadGlyph and calling glEnable(GL_SCISSOR_TEST) at the end of the function seems to fix the issue.

DotNet / Create Font and Texture from cPointer
« on: September 24, 2016, 05:02:11 pm »
I'm writing a .Net binding for TGUI and I can't find a way to receive fonts and textures from my c++ code.

The Font and Texture class provide a constructor to create them from a cPointer, however their access specifiers don't allow using them. The one in Font is 'private' because it isn't used outside the class while the one from Texture is 'internal' because it is used in the RenderTexture constructor to get the texture from C.

I basically have to do something similar as that line in the RenderTexture constructor but with both fonts and textures, but these font and texture constructors can't be used outside the SFML.Net code.

Is there any chance that these constructors could be specified as 'protected internal'? That way they still can't be used under normal circumstances but if someone needs them they can inherit from the class and pass the cPointer from there.
Or is there another solution that I am overlooking?

General / [Android] Using c++11 and gnustl
« on: July 22, 2016, 12:37:14 pm »
The code that I have requires c++11 and always worked without issues because libc++ could be used (and was used by default by SFML).
The last sfml commit however removed support for c++_shared and c++_static. (Could someone perhaps link to a discussion on why libc++ support was dropped?)

I can't use stlport because it lacks c++11 support (it e.g. does not have std::shared_ptr).

My code could compile using gnustl with minor modifications (gnustl e.g. misses std::round), but SFML seems to crash when using this STL. Below is the adb logcat output of the crash when running the sfml android example with "APP_STL := gnustl_static" and "APP_ABI := x86" set in jni/Application.mk.
Code: [Select]
07-22 11:32:13.163  2998  3011 F libc    : Fatal signal 11 (SIGSEGV), code 1, fault addr 0xc730245c in tid 3011 (om.example.sfml)
07-22 11:32:13.280   950   950 F DEBUG   : *** *** *** *** *** *** *** *** *** *** *** *** *** *** *** ***
07-22 11:32:13.280   950   950 F DEBUG   : Build fingerprint: 'Android/sdk_google_phone_x86/generic_x86:6.0/MASTER/2524533:userdebug/test-keys'
07-22 11:32:13.280   950   950 F DEBUG   : Revision: '0'
07-22 11:32:13.280   950   950 F DEBUG   : ABI: 'x86'
07-22 11:32:13.280   950   950 F DEBUG   : pid: 2998, tid: 3011, name: om.example.sfml  >>> com.example.sfml <<<
07-22 11:32:13.280   950   950 F DEBUG   : signal 11 (SIGSEGV), code 1 (SEGV_MAPERR), fault addr 0xc730245c
07-22 11:32:13.283   950   950 F DEBUG   :     eax c730245c  ebx b734a71c  ecx c730245c  edx b7357f1c
07-22 11:32:13.283   950   950 F DEBUG   :     esi c7302454  edi ab200000
07-22 11:32:13.283   950   950 F DEBUG   :     xcs 00000073  xds 0000007b  xes 0000007b  xfs 00000007  xss 0000007b
07-22 11:32:13.283   950   950 F DEBUG   :     eip b72d3ba3  ebp ab2381f0  esp a40ff210  flags 00210286
07-22 11:32:13.285   950   950 F DEBUG   :
07-22 11:32:13.285   950   950 F DEBUG   : backtrace:
07-22 11:32:13.285   950   950 F DEBUG   :     #00 pc 00081ba3  /system/lib/libc.so (pthread_mutex_lock+11)
07-22 11:32:13.285   950   950 F DEBUG   :     #01 pc 000a281a  /system/lib/libc.so (je_arena_dalloc_large+40)
07-22 11:32:13.285   950   950 F DEBUG   :     #02 pc 000be74e  /system/lib/libc.so (je_free+830)
07-22 11:32:13.285   950   950 F DEBUG   :     #03 pc 00016c4b  /system/lib/libc.so (free+30)
07-22 11:32:13.285   950   950 F DEBUG   :     #04 pc 00009e6b  /data/app/com.example.sfml-1/lib/x86/libsfml-example.so (operator delete(void*)+27)
07-22 11:32:13.285   950   950 F DEBUG   :     #05 pc 000081f6  /data/app/com.example.sfml-1/lib/x86/libsfml-example.so (main+1494)
07-22 11:32:13.285   950   950 F DEBUG   :     #06 pc 000082d8  /data/app/com.example.sfml-1/lib/x86/libsfml-example.so (sf::priv::main(sf::priv::ActivityStates*)+152)
07-22 11:32:13.285   950   950 F DEBUG   :     #07 pc 000089b0  /data/app/com.example.sfml-1/lib/x86/libsfml-example.so (sf::priv::ThreadFunctorWithArg<void* (*)(sf::priv::ActivityStates*), sf::priv::ActivityStates*>::run()+16)
07-22 11:32:13.285   950   950 F DEBUG   :     #08 pc 0000e082  /data/app/com.example.sfml-1/lib/x86/libsfml-system.so (sf::Thread::run()+18)
07-22 11:32:13.285   950   950 F DEBUG   :     #09 pc 0000f24b  /data/app/com.example.sfml-1/lib/x86/libsfml-system.so
07-22 11:32:13.285   950   950 F DEBUG   :     #10 pc 00080a93  /system/lib/libc.so (__pthread_start(void*)+56)
07-22 11:32:13.285   950   950 F DEBUG   :     #11 pc 00021952  /system/lib/libc.so (__start_thread+25)
07-22 11:32:13.285   950   950 F DEBUG   :     #12 pc 000170b6  /system/lib/libc.so (__bionic_clone+70)

This was the cmake command used for this sfml version:
Code: [Select]
cmake -DANDROID_ABI=x86 -DCMAKE_TOOLCHAIN_FILE=../cmake/toolchains/android.toolchain.cmake -DANDROID_STL=gnustl_static ..
The same crash seems to occur in older SFML versions as well, but at least in these version I was able to use c++_static (or c++_shared).

Is there a way to use c++11 on android with the latest sfml version?

General / sf::Keyboard::setVirtualKeyboardVisible crashes on android
« on: February 15, 2016, 05:40:40 pm »
The following code crashes on android since this commit.
#include <SFML/Window.hpp>
int main()

Backtrace from adb logcat:
Code: [Select]
02-15 17:27:44.603   949   949 F DEBUG   :     #00 pc 00083606  /system/lib/libc.so (tgkill+22)
02-15 17:27:44.603   949   949 F DEBUG   :     #01 pc 000815e8  /system/lib/libc.so (pthread_kill+70)
02-15 17:27:44.603   949   949 F DEBUG   :     #02 pc 00027205  /system/lib/libc.so (raise+36)
02-15 17:27:44.603   949   949 F DEBUG   :     #03 pc 000209e4  /system/lib/libc.so (abort+80)
02-15 17:27:44.603   949   949 F DEBUG   :     #04 pc 005173cb  /system/lib/libart.so (art::Runtime::Abort()+377)
02-15 17:27:44.603   949   949 F DEBUG   :     #05 pc 0014d9f3  /system/lib/libart.so (art::LogMessage::~LogMessage()+1343)
02-15 17:27:44.603   949   949 F DEBUG   :     #06 pc 003a5252  /system/lib/libart.so (art::JavaVMExt::JniAbort(char const*, char const*)+3842)
02-15 17:27:44.603   949   949 F DEBUG   :     #07 pc 003a5eac  /system/lib/libart.so (art::JavaVMExt::JniAbortV(char const*, char const*, char*)+116)
02-15 17:27:44.603   949   949 F DEBUG   :     #08 pc 00163a10  /system/lib/libart.so (art::ScopedCheck::AbortF(char const*, ...)+62)
02-15 17:27:44.603   949   949 F DEBUG   :     #09 pc 00176f4d  /system/lib/libart.so (art::CheckJNI::DeleteRef(char const*, _JNIEnv*, _jobject*, art::IndirectRefKind)+829)
02-15 17:27:44.603   949   949 F DEBUG   :     #10 pc 001772d3  /system/lib/libart.so (art::CheckJNI::DeleteLocalRef(_JNIEnv*, _jobject*)+52)
02-15 17:27:44.603   949   949 F DEBUG   :     #11 pc 000127b4  /data/app/com.example.sfml-1/lib/x86/libsfml-window.so
02-15 17:27:44.603   949   949 F DEBUG   :     #12 pc 000092ac  /data/app/com.example.sfml-1/lib/x86/libsfml-window.so (sf::Keyboard::setVirtualKeyboardVisible(bool)+28)
02-15 17:27:44.603   949   949 F DEBUG   :     #13 pc 000020d1  /data/app/com.example.sfml-1/lib/x86/libsfml-example.so (main+33)
02-15 17:27:44.603   949   949 F DEBUG   :     #14 pc 00002178  /data/app/com.example.sfml-1/lib/x86/libsfml-example.so (sf::priv::main(sf::priv::ActivityStates*)+152)
02-15 17:27:44.603   949   949 F DEBUG   :     #15 pc 00002330  /data/app/com.example.sfml-1/lib/x86/libsfml-example.so (sf::priv::ThreadFunctorWithArg<void* (*)(sf::priv::ActivityStates*), sf::priv::ActivityStates*>::run()+16)
02-15 17:27:44.603   949   949 F DEBUG   :     #16 pc 0000e392  /data/app/com.example.sfml-1/lib/x86/libsfml-system.so (sf::Thread::run()+18)
02-15 17:27:44.603   949   949 F DEBUG   :     #17 pc 0000f54b  /data/app/com.example.sfml-1/lib/x86/libsfml-system.so
02-15 17:27:44.603   949   949 F DEBUG   :     #18 pc 00080a93  /system/lib/libc.so (__pthread_start(void*)+56)
02-15 17:27:44.603   949   949 F DEBUG   :     #19 pc 00021952  /system/lib/libc.so (__start_thread+25)
02-15 17:27:44.603   949   949 F DEBUG   :     #20 pc 000170b6  /system/lib/libc.so (__bionic_clone+70)

The issue seems to be related to this line. Removing that line and thus not deleting the lNativeActivity seems to solve the crash.

General / Crash on android due to font destruction
« on: February 09, 2016, 06:41:22 pm »
The following code crashes on android with the latest SFML version. The bug was introduced in this commit, earlier sfml versions don't crash on this code.
#include <SFML/Graphics.hpp>
#include <memory>

int main()
    std::shared_ptr<sf::Font> font3;

    sf::Font font1;

    font3 = std::make_shared<sf::Font>(font1);

    sf::Font font2;

    font3 = std::make_shared<sf::Font>(font2);

The backtrace that is printed in logcat:
Code: [Select]
02-09 18:10:35.218   949   949 F DEBUG   : backtrace:
02-09 18:10:35.218   949   949 F DEBUG   :     #00 pc 00058eef  /data/app/com.example.tgui-1/lib/x86/libsfml-graphics.so (FT_Stroker_Done+31)
02-09 18:10:35.218   949   949 F DEBUG   :     #01 pc 00019ea6  /data/app/com.example.tgui-1/lib/x86/libsfml-graphics.so (sf::Font::cleanup()+310)
02-09 18:10:35.218   949   949 F DEBUG   :     #02 pc 0001b91e  /data/app/com.example.tgui-1/lib/x86/libsfml-graphics.so (sf::Font::~Font()+30)
02-09 18:10:35.218   949   949 F DEBUG   :     #03 pc 00002dee  /data/app/com.example.tgui-1/lib/x86/libtgui-example.so (std::__1::__shared_ptr_emplace<sf::Font, std::__1::allocator<sf::Font> >::__on_zero_shared()+30)
02-09 18:10:35.218   949   949 F DEBUG   :     #04 pc 00089315  /data/app/com.example.tgui-1/lib/x86/libc++_shared.so (std::__1::__shared_weak_count::__release_shared()+53)
02-09 18:10:35.218   949   949 F DEBUG   :     #05 pc 00002b26  /data/app/com.example.tgui-1/lib/x86/libtgui-example.so (main+422)
02-09 18:10:35.218   949   949 F DEBUG   :     #06 pc 00002c98  /data/app/com.example.tgui-1/lib/x86/libtgui-example.so (sf::priv::main(sf::priv::ActivityStates*)+152)
02-09 18:10:35.218   949   949 F DEBUG   :     #07 pc 00002fd0  /data/app/com.example.tgui-1/lib/x86/libtgui-example.so (sf::priv::ThreadFunctorWithArg<void* (*)(sf::priv::ActivityStates*), sf::priv::ActivityStates*>::run()+16)
02-09 18:10:35.218   949   949 F DEBUG   :     #08 pc 0000e392  /data/app/com.example.tgui-1/lib/x86/libsfml-system.so (sf::Thread::run()+18)
02-09 18:10:35.218   949   949 F DEBUG   :     #09 pc 0000f54b  /data/app/com.example.tgui-1/lib/x86/libsfml-system.so
02-09 18:10:35.218   949   949 F DEBUG   :     #10 pc 00080a93  /system/lib/libc.so (__pthread_start(void*)+56)
02-09 18:10:35.218   949   949 F DEBUG   :     #11 pc 00021952  /system/lib/libc.so (__start_thread+25)
02-09 18:10:35.218   949   949 F DEBUG   :     #12 pc 000170b6  /system/lib/libc.so (__bionic_clone+70)

SFML projects / TGUI: GUI library for SFML
« on: September 01, 2015, 02:47:25 am »
TGUI: Texus' Graphical User Interface

Easy to use
TGUI aims to be easy to use, with only a few lines you have a fully functioning TextBox on your screen.
tgui::TextBox::Ptr textbox = tgui::TextBox::create();
textbox->setPosition(100, 50);
textbox->setSize(300, 200);

The widgets can be created by just using colors or by using images, making the look very customizable.
When widgets are created without a theme then they will use the build-in white theme (top left image). TGUI also ships with ready to use Black and BabyBlue themes (top right and bottom right images).

(click images for larger version)

TGUI tries to support the same operating systems as SFML, so it supports Windows, Linux, Mac OS X and FreeBSD and provides experimental support for Raspberry Pi, Android and iOS.

Installation tutorials are available for each of these systems.

Gui Builder
A Gui Builder is included to allow easily creating and editing forms.

Relative sizes and positions
Next to passing absolute values, you can specify positions and sizes relative to the parent.
checkbox->setPosition("30%", "20%");
editBox->setSize(400, "5%");

The widget position and size will automatically update when its parent resizes.

More information
For more information about the project, check the website which has tutorials, documentation and a few example codes. Source code can be found on GitHub.

If you have problems with getting tgui to work or if you find bugs then please open a topic on the TGUI forum.

Some images


SFML projects / Non-rectangular semi-transparent window
« on: June 07, 2015, 01:12:35 am »
This is just some code that I wanted to share.
It loads an image from a file and will give the window the shape of that image. You can also set the opacity of the window.

I only wrote code for Windows and Linux. I will try to have a look at OS X next month. In case I update the code you can find it on github.

Edit: The code now works on Windows, Linux and Mac.
On mac you have to compile both files, on windows and linux just Transparent.cpp.
On windows you only need the sfml libraries, on linux you also have to add "-lX11 -lXext" to the linker flags and on mac you should add "-framework Cocoa".

(click to show/hide)

Transparent.mm (only needed on mac)
(click to show/hide)


Graphics / font.getLineSpacing not correct for first line
« on: May 10, 2015, 04:58:21 pm »
The getLineSpacing apparently can't be used to determine the height of several lines of text. Suppose I have a TextBox widget which is given the size of 5*lineSpacing so that it could fit exactly 5 lines of text. The bottom part of the last line will be outside the text box because there is too much space on top. I'm not talking about the text.getLocalBounds().top here because the offset is slightly different.

The last line of the text looks like this:

The background rectangle (of which the height is calculated with getLineSpacing) is missing 9 pixels for a text size of 100.

It is a bit hard to explain so here is the full example code (I took the font from the pong example).
#include <SFML/Graphics.hpp>
#include <iostream>

int main()
    const unsigned int TEXT_SIZE = 100;

    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML test");

    sf::Font font;

    sf::Text text;

    sf::FloatRect bounds = text.getLocalBounds();

    sf::RectangleShape bg(sf::Vector2f(bounds.left + bounds.width, 5 * font.getLineSpacing(TEXT_SIZE)));

    std::cout << "bounds top = " << bounds.top << std::endl;
    std::cout << "bounds height = " << bounds.height << std::endl;
    std::cout << "full heigh = " << bounds.top + bounds.height << std::endl << std::endl;

    std::cout << "line spacing = " << font.getLineSpacing(TEXT_SIZE) << std::endl;
    std::cout << "5*lineSpacing = " << 5*font.getLineSpacing(TEXT_SIZE) << std::endl << std::endl;

    // lineHeight is character size + part of letter 'g' that lies below the baseline
    // When using this as height for the first line of text, the total height of the background rectangle is correct
    float lineHeight = text.getCharacterSize()
                       + font.getGlyph('g', TEXT_SIZE, false).bounds.height
                       + font.getGlyph('g', TEXT_SIZE, false).bounds.top;

    std::cout << "calculated line height = " << lineHeight << std::endl;
    std::cout << "lineHeight + 4*lineSpacing = " << lineHeight + 4*(font.getLineSpacing(TEXT_SIZE)) << std::endl;

    while (window.isOpen())
        sf::Event event;
        while (window.pollEvent(event))
            if (event.type == sf::Event::Closed)

        window.clear(sf::Color(200, 200, 200));

    return EXIT_SUCCESS;

The following is being printed in the terminal:
Code: [Select]
bounds top = 11
bounds height = 543
full heigh = 554

line spacing = 109
5 * lines spacing = 545

calculated line height = 118
lineHeight + 4*lineSpacing = 554

Is this a bug in SFML?
If it isn't a bug, is there any way that I can know the height of several lines of text upfront? Because I either seem to need the bounds.top or the part of the letters beneath the baseline (for which I had to hardcode the 'g' character in this example to get it).

DotNet / TGUI for C#
« on: June 21, 2013, 04:11:33 pm »

TGUI.Net is now released and can be found on tgui.net.

Original post:

Some time ago someone requested a c# port for TGUI.
I wanted to ask if anyone is still interested in this.

Code would look like the following (can still be changed if needed).
using System;
using SFML.Window;
using SFML.Graphics;
using TGUI;

namespace window
    static class Program
        static public void LoadObjects ( Gui gui )
            // Create the background image
            Picture picture = gui.Add (new Picture("xubuntu_bg_aluminium.jpg"));
            picture.Size = new Vector2f(800, 600);

            // Create the username label
            Label labelUsername = gui.Add (new Label());
            labelUsername.Text = "Username:";
            labelUsername.Position = new Vector2f(200, 100);

            // Create the password label
            Label labelPassword = gui.Add (new Label());
            labelPassword.Text = "Password:";
            labelPassword.Position = new Vector2f(200, 250);

            // Create the username edit box
            EditBox editBoxUsername = gui.Add (new EditBox("objects/Black.conf"), "Username");
            editBoxUsername.Size = new Vector2f(400, 40);
            editBoxUsername.Position = new Vector2f(200, 140);

            // Create the password edit box (we will copy the previously created edit box)
            EditBox editBoxPassword = gui.Add(new EditBox(editBoxUsername), "Password");
            editBoxPassword.Position = new Vector2f(200, 290);
            editBoxPassword.PasswordCharacter = "*";

            // Create the login button
            Button button = gui.Add (new Button("objects/Black.conf"));
            button.Size = new Vector2f(260, 60);
            button.Position = new Vector2f(270, 440);
            button.Text = "Login";
            button.CallbackId = 1;
            button.LeftMouseClickedCallback += OnButtonClick;

        static void Main ()
            // Create the window
            RenderWindow window = new RenderWindow (new VideoMode(800, 600), "TGUI.Net Login Screen Example");
            Gui gui = new Gui (window);

            window.Closed += OnClosed;

            // Load the font
            gui.GlobalFont = new Font("Fonts/DejaVuSans.ttf");

            // Load the objects
            LoadObjects (gui);

            // Main loop
            while (window.IsOpen())
                // Process events
                window.DispatchEvents ();


        static void OnClosed(object sender, EventArgs e)
            ((Window)sender).Close ();

        static void OnButtonClick(object sender, MouseCallbackArgs e)
            // Get the username and password
            EditBox editBoxUsername = ((Button)sender).Parent.Get<EditBox>("Username");
            EditBox editBoxPassword = ((Button)sender).Parent.Get<EditBox>("Password");

            System.Console.WriteLine("Username: " + editBoxUsername.Text);
            System.Console.WriteLine("Password: " + editBoxPassword.Text);

Which would give the following result:

Pages: [1] 2