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

Recent Posts

Pages: 1 ... 3 4 [5] 6 7 ... 10
41
Graphics / Re: Text and shaders
« Last post by Hapax on April 30, 2025, 07:04:09 pm »
I realised that gradients in texts are so common that I really should have a shader prepared for that sort of thing already but alas, I did not.

That was then (up to yesterday) and this is now!

I present to you my new shader: multiGradient!

I've added it to be a part of my Lens collection. All shaders are in the "Lens" folder of that directory.
For information on how to use it, you can read the short wiki entry:
https://github.com/Hapaxia/Lens/wiki#multigradient

It allows to provide multiple heights (y positions) and multiple colours to the shader. The shader then interpolates those colours based on the current y position, creating a gradient.
This can be used on textured or non-textured objects and there can be up to 16 different heights and colours!

Here are a couple of quick test examples...

3 steps (3 different colours - yellow, magenta, cyan):


10 steps (10 different colours, yellow, magenta, cyan, white, blue, yellow, magenta, cyan, red, black):

42
On the fence about BubbleByte?

A free demo is now available on Steam!
https://store.steampowered.com/app/3499760/BubbleByte/

Jump in and play the entire first hour of the game. If you enjoy it and decide to buy the full version, all your progress and achievements carry right over.

Give it a try – we hope you love it!
43
Graphics / Re: Text and shaders
« Last post by eXpl0it3r on April 30, 2025, 08:15:30 am »
A fourth option, although I've never done this and wouldn't know how, is to use stencil testing. So the rendered text becomes a stencil to use with another texture that has the gradient.
44
General / Re: qualified-id in declaration before ‘event’
« Last post by kimci86 on April 29, 2025, 10:16:38 pm »
What version of SFML did you install?
It looks like you are trying to use SFML 3 while having SFML 2.6 or earlier installed.

Once you install SFML 3, then you would also need to tell the compiler to use C++17 or higher, adding -std=c++17 to your compilation command line.
Eventually, you can setup a CMake project to take care of this detail.
45
Graphics / Re: sf::View responsive zooming (solved)
« Last post by Realishak on April 29, 2025, 09:47:24 pm »
Oh wow I didn't know the name of this technique, thanks a lot!
46
General / qualified-id in declaration before ‘event’
« Last post by branrjab on April 29, 2025, 09:27:19 pm »
I'm new to SFML but I installed on Windows 11 and tried a sample program and it worked first try.
Impressive.

I installed on new minimal install of  Ubuntu 20.04 with only gcc and sfml added and it failed. I suspect it's some trivial problem but I cannot find it after extensive searches.

I also tried it on Ubuntu 24.04 with the same result.

Here's the compile command and the error I get:

------------------------------

jimbrown@Ub20:~$ g++  -O0 -g3 -Wall  "sfmltest.cpp"
sfmltest.cpp: In function ‘int main(int, char**)’:
sfmltest.cpp:14:36: error: qualified-id in declaration before ‘event’
   14 |         while (const std::optional event = window.pollEvent())
      |                                    ^~~~~
sfmltest.cpp:16:17: error: ‘event’ was not declared in this scope
   16 |             if (event->is<sf::Event::Closed>())
      |                 ^~~~~
sfmltest.cpp:16:46: error: expected primary-expression before ‘)’ token
   16 |             if (event->is<sf::Event::Closed>())
      |

------------------------------

Here's the program - mostly from a sample on the web with some added undef's because of a suggetion I found while searching:

#undef Status
#undef None
#undef BadRequest
#include <SFML/Graphics.hpp>
#include <SFML/Window.hpp>

int main(int argc, char** argv) {
    sf::RenderWindow window(sf::VideoMode({ 200, 200 }), "SFML works!");
    sf::CircleShape shape(100.f);
    shape.setFillColor(sf::Color::Green);

    while (window.isOpen())
    {
        while (const std::optional event = window.pollEvent())
        {
            if (event->is<sf::Event::Closed>())
                window.close();
        }

        window.clear();
        window.draw(shape);
        window.display();
    }
} //end main
47
Graphics / Re: sf::View responsive zooming (solved)
« Last post by Hapax on April 29, 2025, 07:37:35 pm »
Hi and welcome :)

It's good that you managed to already fix this issue by yourself but it should be noted that there is already code available to do this for you (or to show you how to do it).
It's on the SFML wiki and, although it's currently targetted at SFML 2, it should be easily adjusted for SFML 3.

The effect that you are describing is often called the Letterbox effect and here is the code that automatically does that for you:
https://github.com/SFML/SFML/wiki/Source%3A-Letterbox-effect-using-a-view
48
Graphics / Re: Text and shaders
« Last post by Hapax on April 29, 2025, 07:26:33 pm »
Presumably, the vertical gradient is to what you refer?

This effect can be done in SFML simply with two very different methods.

The first would be to use a bitmap text instead of a scalable text (like TTF, for example).
One example of this is Selba Ward's Bitmap Text:


The second would be to use a fragment shader. Fragment shaders adjust the pixel dependant on a multitude of things but included is the pixel position so you can, for example, just adjust the colour based on the y position of the fragment (pixel). Note, though, that OpenGL fragment shaders use non-inverted y axis; that is, the opposite to SFML - the y value increase as it goes up instead of down.
These can seem to be a bit complicated at first but once you get used to them, they are very powerful and flexible, and - I expect - would be the ideal approach for your situation.


EDIT:
In addition, there is another rather simple solution but adds additional overhead (cost of the simplicity):
Render the text to a render texture and the draw that render texture with a quad (vertex array). This vertex array can have different colours per vertex so each corner can have a different colour. This allows both the top corners to be the same and both the bottom ones to be the same. However, the top and bottom can be different, causing linear interpolation - vertically - causing a gradient. If you want to try this method, remember to render the text as while (on a transparent render texture) and note that you can only pick two colours (the top and bottom) so you cannot have it go from one colour to another and back again without splitting the quad into pieces (although that is relatively simple to do too!).
49
Graphics / sf::View responsive zooming (solved)
« Last post by Realishak on April 29, 2025, 04:26:32 pm »
Hi,
it's the first time I make a post here for help I hope to give all the necessary info about my issue.

I'm building a game engine using SFML and I got stuck in a very "dumb" feature that I want for my cameras: when the window is resized I want the "view" to be fully fit inside the viewport. I will attach a clip of what a Unity game does (I want to replicate that, even if I prefer to use the resize event for now instead of constant checking like Unity).
As you can see, you can resize on both axis and maintaining the game's aspect ratio the engine zooms in or out to fit as wide as possible the viewport. I'm focusing on the down-sizing for now, my implementation works if I resize only width or only Y. but as soon as I first resize on one axis and then on the other the zoom starts being a little off, never really fitting correctly the view in the viewport.
This is the code I'm using, it's not relying on any engine specific feature (a part for being attached to my custom event), just SFML3.

m_engine->OnResize += [this](const sf::Vector2u& oldSize, const sf::Vector2u& newSize)
                {
                    constexpr float targetAspectRatio = 16.f / 9.f; // testing on 1920x1080

                    float newAspectRatio = (float)newSize.x / (float)newSize.y;

                    auto oldViewSize = m_view->getSize(); //! this changes every time because of the zoom

                    m_view->setSize({ (float)newSize.x, (float)newSize.y });
                    float zoomX = oldViewSize.x / (float)newSize.x;
                    float zoomY = oldViewSize.y / (float)newSize.y;

                    float zoom = (newAspectRatio > targetAspectRatio) ? zoomY : zoomX;

                    m_view->zoom(zoom);

                    std::cout << zoomX << " " << zoomY << " " << zoom << std::endl;

                };
 


*Edit
of course I make a post after 4 hours of banging my head against a wall and find my answer right after. I should have used the original width and height instead of oldViewSize (which, as stated in my own commend, changes with zoom..)

The following now works as intended:
m_engine->OnResize += [this](const sf::Vector2u& oldSize, const sf::Vector2u& newSize)
                {
                    constexpr float targetAspectRatio = 16.f / 9.f; // testing on 1920x1080
                    constexpr float initialX = 1920.f, initialY = 1080.f;

                    float newAspectRatio = (float)newSize.x / (float)newSize.y;

                    m_view->setSize({ (float)newSize.x, (float)newSize.y });

                    float zoomX = initialX / (float)newSize.x;
                    float zoomY = initialY / (float)newSize.y;
                    float zoom = (newAspectRatio > targetAspectRatio) ? zoomY : zoomX;

                    m_view->zoom(zoom);

                };
50
General discussions / Re: SFML 3.0.1 released
« Last post by kellanphil on April 29, 2025, 04:49:51 am »
Thanks for all the updates SFML version is getting better day by day!
Pages: 1 ... 3 4 [5] 6 7 ... 10