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 - janos_

Pages: [1]
1
Graphics / Error opening window with Intel HD Graphics 3000
« on: July 27, 2023, 05:24:14 pm »
I have a laptop with a Intel HD Graphics 3000. When I try to run this code:
...
int main()
{
   sf::RenderWindow window(sf::VideoMode(1366, 768, "Window");

}
 

2
Audio / Creating audio by pulling single samples
« on: July 26, 2021, 03:09:29 pm »
Hello,

I am trying to create an audio on the fly. I want a sound to be playing and while that happens new audio samples should be pulled from a function.
I have looked at the sf::SoundStream class. But this requires you to push back a few thousand new samples each time onGetData(Chunk& data) gets called. I want to provide only 1 sample. The function onGetData should look something like this:

sf::Int16 pullSample()
{
    return 1000 * std::sin(currentTime * ...);
}

I know that this requires with a sample rate of 44100, 44100 calls to that function each second. Is this still somehow possible or are there other work-arounds?

3
Graphics / LoadFromFile fails after too many Text objects
« on: March 28, 2021, 08:02:54 pm »
I am trying to create 1000 sf::Text objects. But loadFromFile returns 0 after 508 initialized objects. Why is that the case and is there a way to by pass this to create all 1000 objects? The code example demonstrates this.

Thank you for your time.

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

class Text
{
private:
    sf::Text text;
    sf::Font font;

    sf::RenderTarget& renderTarget;
public:
    Text(const sf::String& fontPath, sf::RenderTarget& p_renderTarget) : renderTarget(p_renderTarget)
    {
        std::cout << "Font loaded: " << font.loadFromFile(fontPath) << std::endl;
        text.setFont(font);
        text.setString("Hallo Welt!");
        text.setCharacterSize(25);
    }

    void Update() { renderTarget.draw(text); }
};

int main()
{
    sf::RenderWindow window(sf::VideoMode(900, 600), "SFML works!");
    std::vector<Text*> texts;
    int textAmount = 1000;
    for (int i = 0; i < textAmount; i++)
    {
        std::cout << "Nr." << i << ": ";
        Text* t = new Text("arial.ttf", window);
        texts.push_back(t);
    }

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

        window.clear();
        for (int i = 0; i < textAmount; i++)
        {
            texts[i]->Update();
        }
        window.display();
    }

    for (int i = 0; i < textAmount; i++)
    {
        delete texts[i];
    }
}

4
I want to check if my mouse is over a shape inside a renderTexture. The problem: shape.GetGlobalBounds is relative to the renderTexture, whereas the mouse positon is relative to the window. I don't know how to make both relative to the same origin. So how would I check a collision between mouse and shape?

Thanks for any help 8)

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

int main()
{
        sf::RenderWindow window(sf::VideoMode(900, 600), "Title");

        sf::CircleShape circle(50.f);
        circle.setPosition(0.f, 0.f);
        circle.setFillColor(sf::Color::Red);

        sf::RenderTexture rTexture;
        rTexture.create(400.f, 400.f);
        sf::Sprite sprite;
        sf::Texture texture;

        sprite.move(100.f, 100.f);

        sf::Event myEvent;
        while (window.isOpen())
        {
                while (window.pollEvent(myEvent))
                {
                        if (myEvent.type == sf::Event::Closed)
                                window.close();
                }

                rTexture.clear(sf::Color::Blue);
                rTexture.draw(circle);
                rTexture.display();

                texture = rTexture.getTexture();
                sprite.setTexture(texture);
               
                window.clear();
                window.draw(sprite);

                if (circle.getGlobalBounds().contains(window.mapPixelToCoords(sf::Mouse::getPosition(window)))) /*Mouse positon is relative to window -> returns (100,100) when getGlobalBounds returns (0,0)*/
                        circle.setFillColor(sf::Color::White);
                else circle.setFillColor(sf::Color::Red);

                std::cout << circle.getGlobalBounds().left << " " << circle.getGlobalBounds().top << std::endl;

                window.display();
        }
}
 

Pages: [1]
anything