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.


Messages - GetterSetter

Pages: [1] 2
1
Got it, thank you!

2
General / Re: How can I use stbi writing functions?
« on: April 29, 2024, 10:51:20 am »
Thank you, kojack!

3
Thank you, texus and eXpl0it3r! The only question I have now is whether we should use dynamic_pointer_cast instead of static_pointer_cast because we are performing a downcast.

4
I have tgui::Textures. Would you mind explaining to me, please, how I can use the bind function, because it's not clear from the documentation?

5
General / Re: How can I use stbi writing functions?
« on: April 28, 2024, 11:31:23 pm »
Yeah, I tried it before starting the topic, and I got the error about multiple definitions of these functions because, I assume, they are already implemented in libsfml-graphics.a.
Oh, I think, I've got your idea. Do you mean that in my cpp file I should only include headers without using defines?

6
General / How can I use stbi writing functions?
« on: April 28, 2024, 08:20:55 pm »
Hello, if I try to include stbi headers, I receive an error from the linker about multiple definition of stbi_write_.... If I try to use
extern int stbi_write_png(char const *filename, int w, int h, int comp, const void *data, int stride_in_bytes);
//and so on
 
I receive the following error: undefined reference to stbi_... and it doesn't matter which link order I use. So, how can I use these functions? I need to do it because I have the set of pixels itself, so I think that creating a sf::Texture, updating it with this set, then copying it to an sf::Image and finally calling saveToFile() is a bit inefficient and stupid.
RMK: I'm using static sfml libraries

7
Hm, what if put rt.display() inside the loop and don't clear rt? Does it have any side effects? From my perspective, it looks like all the rendered rectangles will just stack in the texture, and that's definitely what I want, but I'm not sure if it works as I expect. No, it doesn't work like this.
The problem is that I don't want to create a whole set of texture copies just to render them, because it requires a lot of memory.

8
The problem: I have a set of textures, but they are not of the sf::Texture type. However, sometimes I need to set them to sf::RectangleShape and render them. The idea is as follows:
sf::RenderTexture rt;
rt.create(...);
rt.clear();
for(auto &texture:non_sfml_textures)
{
  sf::Texture t=convertion_function(texture);
  sf::RectangleShape rect;
  rect.setTexture(&t);

  //Setting position of rect, etc...

  rt.draw(rect);
}
rt.display();
 
Is it safe to do so? By 'safe' I mean that all the textures will be rendered correctly after calling rt.disaplay().

9
Quote
Do you want to reuse the textures or just save it to disk?

I want to reuse them: show the user the render result and, if it is satisfactory, the user can then save it. Thank you for your advice about asynchronous saving. I have taken note of it.
Thank you very much for your help  ;) !

10
Your explanation was quite comprehensive. Thank you for that.
Some thoughts on using std::vector: I was considering returning it as a std::shared_ptr<std::vector> and now I am sure that I should do so because I see that copying is expensive.
The problem with using only one RenderTexture is that the resolution of each individual texture is 2480 x 3507. Therefore, creating a large texture may not be technically possible due to limitations of VRAM (?).
The reason for wanting to separate rendering is that when I start the rendering function, my window freezes and it appears that my program has stopped working, but in fact it hasn't. Additionally, some operating systems may suggest the user to close the inactive window, which is unfortunate.
Of course, I can render the textures in the following way: one per loop tick.
Do you have any suggestions on how to prevent the window from freezing?

11
Hello, what is the safe way to use RenderTexture in asynchronous code? I need to render some objects to the texture in the background without suspending the main loop. Now I have the following code (simplified).

async_func(...)
{
        std::vector<sf::Texture> textures;
        RenderTexture t;
        for(...)
       {
          //...
          t.clear();
          t.draw();
          t.display();
          textures.emplace_back(t.getTexture());
       }
        return textures;
}

main()
{
        std::future<...>async_render;

       //inside the loop
       if(event) async_render=std::async(async_func, ...);
       if (async_render.wait_for(0s) == std::future_status::ready)
            {
                auto c = async_render.get();
                //....
            }

        //window.clear/draw/display();
}

And it works with varrying success because sometimes my app terminates abnormally with different errors:
X Error of failed request:  GLXBadPbuffer
  Major opcode of failed request:  152 (GLX)
  Minor opcode of failed request:  28 (X_GLXDestroyPbuffer)
  Serial number of failed request:  7663
  Current serial number in output stream:  7666

or

(according to the callstack it raises from window.pollEvent(event))
GUI: ../../src/xcb_conn.c:215: write_vec: Assertion `!c->out.queue_len&#39; failed.
 

I've also noticed that the first error occurs when textures.size()>1.

The callstack for the second error:
libc.so.6!__pthread_kill_implementation(int no_tid, int signo, pthread_t threadid) (pthread_kill.c:44)
libc.so.6!__pthread_kill_internal(int signo, pthread_t threadid) (pthread_kill.c:78)
libc.so.6!__GI___pthread_kill(pthread_t threadid, int signo) (pthread_kill.c:89)
libc.so.6!__GI_raise(int sig) (raise.c:26)
libc.so.6!__GI_abort() (abort.c:79)
libc.so.6!__assert_fail_base(const char * fmt, const char * assertion, const char * file, unsigned int line, const char * function) (assert.c:92)
libc.so.6!__GI___assert_fail(const char * assertion, const char * file, unsigned int line, const char * function) (assert.c:101)
libxcb.so.1![Unknown/Just-In-Time compiled code] (Unknown:0)
libxcb.so.1!xcb_writev (Unknown:0)
libX11.so.6!_XSend (Unknown:0)
libX11.so.6!_XFlush (Unknown:0)
libX11.so.6!XCheckIfEvent (Unknown:0)
libsfml-window-d.so.2.6!sf::priv::WindowImplX11::processEvents(sf::priv::WindowImplX11 * const this) (/LIBS/SFML-2.6.1/src/SFML/Window/Unix/WindowImplX11.cpp:723)
libsfml-window-d.so.2.6!sf::priv::WindowImpl::popEvent(sf::priv::WindowImpl * const this, sf::Event & event, bool block) (/LIBS/SFML-2.6.1/src/SFML/Window/WindowImpl.cpp:147)
libsfml-window-d.so.2.6!sf::WindowBase::pollEvent(sf::WindowBase * const this, sf::Event & event) (/LIBS/SFML-2.6.1/src/SFML/Window/WindowBase.cpp:165)
main() (GUI.cpp:123)

12
Audio / Re: In implementation of MyStream sound sometimes doesn't play
« on: January 26, 2022, 06:42:31 pm »
Ok, I found the similiar problem on SO (i guess): https://stackoverflow.com/a/62172120
I've compiled new SFML release and the output is:
An internal OpenAL call failed in SoundStream.cpp(448).
Expression:
   alBufferData(buffer, m_format, data.samples, size, static_cast<ALsizei>(m_sampleRate))
Error description:
   AL_INVALID_VALUE
   A numeric argument is out of range.
 


And the solution was:
samplesToStream -= samplesToStream % 4;

13
Audio / In implementation of MyStream sound sometimes doesn't play
« on: January 25, 2022, 11:38:24 am »
I got the code from https://www.sfml-dev.org/tutorials/2.5/audio-streams.php and replaced
const int samplesToStream = 50000;
to
const int samplesToStream = getSampleRate()/factor;
It worked fine until factor was 4 (or 4^k: 4,16,64...). In case if factor == 4^k, sound doesn't play and the app closes immediately with no errors
#include <SFML/Audio.hpp>
#include <vector>

// custom audio stream that plays a loaded buffer
class MyStream : public sf::SoundStream
{
public:

    void load(const sf::SoundBuffer& buffer)
    {
        // extract the audio samples from the sound buffer to our own container
        m_samples.assign(buffer.getSamples(), buffer.getSamples() + buffer.getSampleCount());

        // reset the current playing position
        m_currentSample = 0;

        // initialize the base class
        initialize(buffer.getChannelCount(), buffer.getSampleRate());
    }

private:

    virtual bool onGetData(Chunk& data)
    {
        // number of samples to stream every time the function is called;
        // in a more robust implementation, it should be a fixed
        // amount of time rather than an arbitrary number of samples
        const std::size_t samplesToStream = getSampleRate()/4;

        // set the pointer to the next audio samples to be played
        data.samples = &m_samples[m_currentSample];

        // have we reached the end of the sound?
        if (m_currentSample + samplesToStream <= m_samples.size())
        {
            // end not reached: stream the samples and continue
            data.sampleCount = samplesToStream;
            m_currentSample += samplesToStream;
            return true;
        }
        else
        {
            // end of stream reached: stream the remaining samples and stop playback
            data.sampleCount = m_samples.size() - m_currentSample;
            m_currentSample = m_samples.size();
            return false;
        }
    }

    virtual void onSeek(sf::Time timeOffset)
    {
        // compute the corresponding sample index according to the sample rate and channel count
        m_currentSample = static_cast<std::size_t>(timeOffset.asSeconds() * getSampleRate() * getChannelCount());
    }

    std::vector<sf::Int16> m_samples;
    std::size_t m_currentSample;
};

int main()
{
    // load an audio buffer from a sound file
    sf::SoundBuffer buffer;
    buffer.loadFromFile("sound.ogg");

    // initialize and play our custom stream
    MyStream stream;
    stream.load(buffer);
    stream.play();

    // let it play until it is finished
    while (stream.getStatus() == MyStream::Playing)
        sf::sleep(sf::seconds(0.1f));

    return 0;
}
 
OS: Ubuntu 20.04.3 LTS
SFML version: 2.5.1

14
Graphics / Re: Problem with sf::RenderTexture & std::future
« on: March 19, 2020, 02:56:21 pm »
Thanks a lot, I thought that rendering is a long and difficult task.

But why does my code work well with RenderWindow?

That's not the same thing ;)
I meant that I don't like working with mutexes when I use std::thread


15
Graphics / Re: Problem with sf::RenderTexture & std::future
« on: March 19, 2020, 09:05:45 am »
Hi

You should read this if not already done: https://www.sfml-dev.org/tutorials/2.5/window-opengl.php#rendering-from-threads

And I don't know what you're trying to achieve, but it looks like a bad idea ;)

I have already done. I rewrote render(...), but it didn't help:
void render(sf::RenderTarget &target, const std::vector<sf::Drawable *> &objects, sf::RenderStates states = sf::RenderStates{})
{
    #ifdef __linux__
    XInitThreads();
    #endif
    target.setActive(true);
    target.clear();
    for(auto &el : objects)
    {
        target.draw(*el, states);
    }
    target.setActive(false);
}
 
Why does it look like a bad idea? I just want to render objects in another thread while doing something else, so that I decided to use std::future, because I don't like using mutexes.

Pages: [1] 2