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

Pages: [1]
1
General / Re: Install SFML via NuGet
« on: April 22, 2015, 04:30:37 pm »
So it's not maintained by the SFML team, but should be ok for lazy people like me :)
That's all I need to know :D

Thanks for the clarification :)


2
General / Re: Install SFML via NuGet
« on: April 22, 2015, 03:08:50 pm »
Just that it is not mentioned in the tutorials.
And a lot of beginners have trouble setting up SFML with VS2013.

I just wanted to know if this method is ok and if this will be maintained in the future.


3
General / Install SFML via NuGet
« on: April 22, 2015, 02:15:35 pm »
Hi guys,

What do you think about installing SFML into a VS2013 C++ project via NuGet?

https://www.nuget.org/packages/sfml-graphics/

Most tutorials do not mention this possibility but I've got used to this because I'm lazy to do the manual setup and it works for me.

4
SFML projects / Re: SFML simple shader experiment
« on: April 03, 2015, 02:17:05 pm »
Newest version on github:
https://github.com/Miretz/FollowersOfLight

- refactored into multiple classes
- created RandomGenerator class
- created ShaderLoader class

5
SFML projects / Re: SFML simple shader experiment
« on: April 02, 2015, 10:43:07 am »
Yes. I found out it doesn't even compile now in VS2013. In Code::Blocks (GCC) on Linux it works fine.

I will try the workaround for rng you are suggesting.

Thanks :)

6
SFML projects / Re: SFML simple shader experiment
« on: April 02, 2015, 12:53:14 am »
Updated version. I hope I'm on the right path and did not introduce any more errors :-\

Also used the Code::Block source formatter  8)

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

#ifndef _NOEXCEPT
#define _NOEXCEPT noexcept
#endif

typedef float FrameTime;
const float ftStep
{
    1.f
};
const float ftSlice
{
    1.f
};

static const std::string shaderCode = \
                                      "uniform vec2 frag_LightOrigin;"\
                                      "uniform vec3 frag_LightColor;"\
                                      "uniform float frag_LightAttenuation;"\
                                      "uniform vec2 frag_ScreenResolution;"\
                                      "void main(){"\
                                      " vec2 baseDistance =  gl_FragCoord.xy;"\
                                      " baseDistance.y = frag_ScreenResolution.y-baseDistance.y;"\
                                      " vec2 distance=frag_LightOrigin - baseDistance;"\
                                      " float linear_distance = length(distance);"\
                                      " float attenuation=1.0/( frag_LightAttenuation*linear_distance + frag_LightAttenuation*linear_distance);"\
                                      " vec4 lightColor = vec4(frag_LightColor, 1.0);"\
                                      " vec4 color = vec4(attenuation, attenuation, attenuation, 1.0) * lightColor; gl_FragColor=color;}";

class Walker
{
private:
    const float walkerWidth
    {
        8.f
    };
    const float walkerHeight
    {
        8.f
    };
    const float walkerVelocity
    {
        1.f
    };
    const float strenghts[2] = { 0.008f, 0.002f };

    sf::RectangleShape shape;
    sf::Vector2f velocity;
    sf::Vector3f color;

    std::mt19937 gen {std::random_device{}()};
    std::uniform_int_distribution<int> randomColor {0, 255};
    std::bernoulli_distribution randomChance {0.02};
    std::uniform_real_distribution<float> randomVelocity {0, walkerVelocity * 2};

    void generateRandomColor()
    {
        color = sf::Vector3f(randomColor(gen), randomColor(gen), randomColor(gen));
    }

    int generateRandomChance()
    {
        return randomChance(gen);
    }

    float generateVelocityFloat()
    {
        return randomVelocity(gen) - walkerVelocity;
    }

    void normalize(sf::Vector2f& source)
    {
        float length = sqrt((source.x * source.x) + (source.y * source.y));
        if (length != 0)
        {
            source.x = source.x / length;
            source.y = source.y / length;
        }
    }

public:

    static int sSelected;

    Walker(float mX, float mY)
    {
        generateRandomColor();

        shape.setPosition(mX, mY);
        shape.setSize({ walkerWidth, walkerHeight });
        shape.setFillColor(sf::Color(color.x, color.y, color.z));
        shape.setOrigin(walkerWidth / 2.0f, walkerHeight / 2.0f);
    }

    virtual ~Walker() {}

    static void changeStrenght()
    {
        sSelected = (sSelected == 0) ? 1 : 0;
    }

    void update(FrameTime ft, sf::RenderWindow& window)
    {

        sf::Vector2f mousePosition = (sf::Vector2f) sf::Mouse::getPosition(window);
        sf::Vector2u winSize = window.getSize();

        //follow mouse if it is inside of the window
        if (mousePosition.x > 0.f && mousePosition.y > 0.f &&
                mousePosition.x < winSize.x && mousePosition.y < winSize.y)
        {

            sf::Vector2f direction = mousePosition - shape.getPosition();
            normalize(direction);

            direction *= Walker::strenghts[sSelected];
            direction.x += generateVelocityFloat() * 0.05;
            direction.y += generateVelocityFloat() * 0.05;

            velocity += direction;

            //limit velocity
            velocity.x = std::min(walkerVelocity, velocity.x);
            velocity.y = std::min(walkerVelocity, velocity.y);
            velocity.x = std::max(-walkerVelocity, velocity.x);
            velocity.y = std::max(-walkerVelocity, velocity.y);

        }
        else
        {
            if (generateRandomChance())
            {
                velocity.x = generateVelocityFloat();
                velocity.y = generateVelocityFloat();
            }
        }

        //guard edges of screen
        if ((right() >= winSize.x && velocity.x > 0) || (left() <= 0 && velocity.x < 0))
        {
            velocity.x = 0;
        }
        if ((top() <= 0 && velocity.y < 0) || (bottom() >= winSize.y && velocity.y > 0))
        {
            velocity.y = 0;
        }

        shape.move(velocity * ft);
    }

    void draw(sf::RenderTarget& target)
    {
        target.draw(shape);
    }

    sf::Vector2f getPosition() const _NOEXCEPT
    {
        return shape.getPosition();
    }
    sf::Vector3f getColor() const _NOEXCEPT
    {
        return color;
    }
    float x()      const _NOEXCEPT
    {
        return shape.getPosition().x;
    }
    float y()      const _NOEXCEPT
    {
        return shape.getPosition().y;
    }
    float left()   const _NOEXCEPT
    {
        return x() - shape.getSize().x / 2.0f;
    }
    float right()  const _NOEXCEPT
    {
        return x() + shape.getSize().x / 2.0f;
    }
    float top()    const _NOEXCEPT
    {
        return y() - shape.getSize().y / 2.0f;
    }
    float bottom() const _NOEXCEPT
    {
        return y() + shape.getSize().y / 2.0f;
    }

};

int Walker::sSelected = 0;

class Game
{
private:
    unsigned int windowWidth = 800;
    unsigned int windowHeight = 600;
    int walkerCount = 15;

    sf::RenderWindow window { sf::VideoMode(windowWidth, windowHeight), "Followers!" };
    FrameTime lastFt { 0.f }, currentSlice { 0.f };
    bool running { false };

    sf::RenderTexture myRenderTexture;
    sf::Sprite spriteWorld;
    sf::Shader shader;

    std::vector<Walker> walkers;

    void initializeWalkers()
    {
        walkers.clear();
        for (int a { 0 }; a < walkerCount; ++a)
        {
            walkers.emplace_back(window.getSize().x / 2.f,
                                 window.getSize().y / 2.f);
        }
    }

    void checkInput()
    {
        sf::Event event;
        while (window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
            {
                running = false;
            }
            else if ((event.type == sf::Event::KeyPressed) && (event.key.code == sf::Keyboard::Escape))
            {
                running = false;
            }
            else if (event.type == sf::Event::MouseButtonPressed)
            {
                Walker::changeStrenght();
            }
        }

    }

    void update()
    {
        currentSlice += lastFt;

        //update multiple times based on fps
        for (; currentSlice >= ftSlice; currentSlice -= ftSlice)
        {
            for (auto& walker : walkers)
            {
                walker.update(ftStep, window);
            }
        }
    }

    void draw()
    {
        for (auto& walker : walkers)
        {

            walker.draw(myRenderTexture);

            shader.setParameter("frag_LightOrigin", walker.getPosition());
            shader.setParameter("frag_LightColor", walker.getColor());
            shader.setParameter("frag_LightAttenuation", 50);

            sf::RenderStates states;
            states.shader = &shader;
            states.blendMode = sf::BlendAdd;

            myRenderTexture.draw(spriteWorld, states);

        }

        myRenderTexture.display();
        window.draw(spriteWorld);
        window.display();
    }

public:

    void run()
    {
        myRenderTexture.create(windowWidth, windowHeight);
        spriteWorld.setTexture(myRenderTexture.getTexture());
        spriteWorld.setOrigin(spriteWorld.getTextureRect().width / 2, spriteWorld.getTextureRect().height / 2);
        spriteWorld.setPosition(windowWidth / 2.f, windowHeight / 2.f);

        shader.loadFromMemory(shaderCode, sf::Shader::Fragment);
        shader.setParameter("frag_ScreenResolution", sf::Vector2f(windowWidth, windowHeight));

        running = true;

        while (running)
        {

            auto time1(std::chrono::high_resolution_clock::now());

            window.clear(sf::Color::Black);
            myRenderTexture.clear();

            checkInput();
            update();
            draw();

            auto time2(std::chrono::high_resolution_clock::now());
            auto elapsedTime(time2 - time1);
            FrameTime ft { std::chrono::duration_cast<std::chrono::duration<float, std::milli>>(elapsedTime).count() };

            lastFt = ft;

            auto ftSeconds(ft / 1000.f);
            if (ftSeconds > 0.f)
            {
                auto fps(1.f / ftSeconds);
                window.setTitle("FT: " + std::to_string(ft) + "\tFPS: " + std::to_string(fps));
            }
        }
    }


    Game()
    {
        window.setFramerateLimit(500);
        initializeWalkers();
    }

    virtual ~Game() {}
};

int main()
{
    Game().run();
    return EXIT_SUCCESS;
}
 

7
SFML projects / Re: SFML simple shader experiment
« on: April 01, 2015, 07:44:09 pm »
What I would like to know are some specific errors for C++ & SFML.
For example if the program is not leaking memory or if I'm calling the SFML methods correctly & effectively.

If you want specific feedback, I will go ahead - otherwise ignore this post.

using namespace std;
using namespace sf;

Bad idea, always use full qualifying namespaces - don't believe me? Just google around and you will find plenty of reasons.

if(velocity.x > walkerVelocity) velocity.x = walkerVelocity;
            if(velocity.y > walkerVelocity) velocity.y = walkerVelocity;
            if(velocity.x < -walkerVelocity) velocity.x = -walkerVelocity;
            if(velocity.y < -walkerVelocity) velocity.y = -walkerVelocity;

Use std::min and std::max functions instead, it will be easier to read.

if (( 1 + rand() % 100) == 1)

You are already using c++11, so why not make use of the <random> header?

https://channel9.msdn.com/Events/GoingNative/2013/rand-Considered-Harmful

Game{}.run();

This just looks weird using curly braces instead of parenthesis.

struct Rectangle {
    RectangleShape shape;
    float x()      const _NOEXCEPT{ return shape.getPosition().x; }
    float y()      const _NOEXCEPT{ return shape.getPosition().y; }
    float left()   const _NOEXCEPT{ return x() - shape.getSize().x / 2.0f; }
    float right()  const _NOEXCEPT{ return x() + shape.getSize().x / 2.0f; }
    float top()    const _NOEXCEPT{ return y() - shape.getSize().y / 2.0f; }
    float bottom() const _NOEXCEPT{ return y() + shape.getSize().y / 2.0f; }
};

I really don't see how this utility class helps at all, to me it seems like more typing and another layer for no real benefit.

did not know about the <random> and std::min / std::max.
Also the Game{}.run() was probably copied from a tutorial or my mistake. I'm not sure.
You are right about the utility class. There is no reason to keep it there.
This is really good advice.
Thanks :)

#ifndef _NOEXCEPT
#define _NOEXCEPT noexcept
#endif

What exactly is this supposed to accomplish?

I have read somewhere that it is effective to mark methods that never throw an exception with the 'noexcept' C++11 operator for optimization.
I work in Visual Studio on my Windows PC and with Code::Blocks on my Ubuntu laptop and to get the same code working on both computers I'm using the #define.
VS2013 does not support noexcept but it has _NOEXCEPT which should be the same.

8
SFML projects / Re: SFML simple shader experiment
« on: April 01, 2015, 04:27:38 pm »
Why not all wrong? Who think that it has several minor mistakes - try to expand this app by adding several classes in their own files. It won't even compile. =)

I know that the design is wrong. Looks more like a script not a correct OOP solution.
But It can be refactored into a clean OOP solution.

What I would like to know are some specific errors for C++ & SFML.
For example if the program is not leaking memory or if I'm calling the SFML methods correctly & effectively.








9
SFML projects / Re: SFML simple shader experiment
« on: April 01, 2015, 02:37:52 pm »
Sorry, but almost all is wrong. You have to read C++ books.

Doesn't matter, there are lights, lights everywhere, just look the lights,... whoooooo!

 ;D ;D ;D

10
SFML projects / Re: SFML simple shader experiment
« on: April 01, 2015, 02:07:27 pm »
Sorry, but almost all is wrong. You have to read C++ books.
???
Sure it's not the best code design etc. but "almost all is wrong" is quite an overstatement.

Anyways here's a video for the curious people:



The lag is due to my bad GPU!

Thanks for the upload :)

11
SFML projects / Re: SFML simple shader experiment
« on: April 01, 2015, 02:05:58 pm »
Are you just from Java? =)

Yes =) The curly brace position gives it away. I do Java for work. But I like C++ for it's power.   

Sorry, but almost all is wrong. You have to read C++ books.

I agree. I'm currently reading C++ Succinctly but not sure if this is the best book to start with.

12
SFML projects / SFML simple shader experiment
« on: April 01, 2015, 01:03:04 pm »
Hey guys,

This is my first post here.

I'm fairly new to SFML, C++ and graphics programming in general.
I would like to share my latest experiment with you. It's based on the various tutorials I found here and also on youtube.

What it does:
- Generate small squares which follow the mouse
- If the mouse is out of the window they move randomly
- On click change the strenght of the attraction towards the mouse
- Each square shines light (using inline GLSL shader code)

The effect looks kinda like colorful fireflies :-)

Please let me know what you think about the code.
I'm new to C++ so if you see something that is not good practice / wrong / buggy, please let me know.

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

#ifndef _NOEXCEPT
#define _NOEXCEPT noexcept
#endif

using namespace std;
using namespace sf;

using FrameTime = float;

static const string shaderCode = \
"uniform vec2 frag_LightOrigin;"\
"uniform vec3 frag_LightColor;"\
"uniform float frag_LightAttenuation;"\
"uniform vec2 frag_ScreenResolution;"\
"void main(){"\
"       vec2 baseDistance =  gl_FragCoord.xy;"\
"       baseDistance.y = frag_ScreenResolution.y-baseDistance.y;"\
"       vec2 distance=frag_LightOrigin - baseDistance;"\
"       float linear_distance = length(distance);"\
"       float attenuation=1.0/( frag_LightAttenuation*linear_distance + frag_LightAttenuation*linear_distance);"\
"       vec4 lightColor = vec4(frag_LightColor, 1.0);"\
"       vec4 color = vec4(attenuation, attenuation, attenuation, 1.0) * lightColor; gl_FragColor=color;}";


static const unsigned int windowWidth{ 800 }, windowHeight{ 600 };

static const float walkerWidth{ 8.f }, walkerHeight{ 8.f };
static const float walkerVelocity{ 1.f };

static const float ftStep{ 1.f }, ftSlice{ 1.f };

static const int walkerCount{ 15 };

static const float directionMultiplier1{ 0.008f }, directionMultiplier2{ 0.002f };

static float directionMultiplier{ 0.008f };

/* UTILITY FUNCTIONS */

void normalize(Vector2f& source){
        float length = sqrt((source.x * source.x) + (source.y * source.y));
        if (length != 0){
                source.x = source.x / length;
                source.y = source.y / length;
        }
}

/* GAME CLASSES */

struct Rectangle {
        RectangleShape shape;
        float x()      const _NOEXCEPT{ return shape.getPosition().x; }
        float y()      const _NOEXCEPT{ return shape.getPosition().y; }
        float left()   const _NOEXCEPT{ return x() - shape.getSize().x / 2.0f; }
        float right()  const _NOEXCEPT{ return x() + shape.getSize().x / 2.0f; }
        float top()    const _NOEXCEPT{ return y() - shape.getSize().y / 2.0f; }
        float bottom() const _NOEXCEPT{ return y() + shape.getSize().y / 2.0f; }
};


struct Walker : public Rectangle{

        Vector2f velocity;
        Vector3f color;

        Walker(float mX, float mY){
                shape.setPosition(mX, mY);
                shape.setSize({ walkerWidth, walkerHeight });
                color = Vector3f(rand() % 255, rand() % 255, rand() % 255);
                shape.setFillColor(Color(color.x, color.y, color.z));
                shape.setOrigin(walkerWidth / 2.0f, walkerHeight / 2.0f);
        }

        void update(FrameTime ft, RenderWindow& window){
               
                Vector2f mousePosition = (Vector2f)Mouse::getPosition(window);

                //follow mouse if it is inside of the window           
                if (mousePosition.x > 0.f && mousePosition.y > 0.f &&
                        mousePosition.x < windowWidth && mousePosition.y < windowHeight){

                        Vector2f direction = mousePosition - shape.getPosition();

                        normalize(direction);

                        direction *= directionMultiplier;
                        direction.x += generateVelocityFloat() * 0.05;
                        direction.y += generateVelocityFloat() * 0.05;

                        velocity += direction;

                        //limit velocity
                        if(velocity.x > walkerVelocity) velocity.x = walkerVelocity;
                        if(velocity.y > walkerVelocity) velocity.y = walkerVelocity;
                        if(velocity.x < -walkerVelocity) velocity.x = -walkerVelocity;
                        if(velocity.y < -walkerVelocity) velocity.y = -walkerVelocity;

                } else {
                        if (( 1 + rand() % 100) == 1){
                                velocity.x = generateVelocityFloat();
                                velocity.y = generateVelocityFloat();
                        }
                }
                       
                //guard edges of screen
                if ((right() >= windowWidth && velocity.x > 0) || (left() <= 0 && velocity.x < 0)){
                        velocity.x = 0;
                }
                if ((top() <= 0 && velocity.y < 0) || (bottom() >= windowHeight && velocity.y > 0)){
                        velocity.y = 0;
                }

                shape.move(velocity * ft);
        }
       
        float generateVelocityFloat(){
                return (-walkerVelocity) +static_cast <float> (rand()) / (static_cast <float> (RAND_MAX / (walkerVelocity - (-walkerVelocity))));
        }
};

struct Game {

        RenderWindow window{ VideoMode(windowWidth, windowHeight) , "Followers!" };
        FrameTime lastFt{ 0.f }, currentSlice{ 0.f };
        bool running{ false };

        RenderTexture myRenderTexture;
    Sprite spriteWorld;
        Shader shader;    

        vector<Walker> walkers;

        void initializeWalkers(){
                walkers.clear();
                for (int a{ 0 }; a < walkerCount; ++a){
                        walkers.emplace_back(windowWidth / 2.f,
                                windowHeight/2.f);
                }
        }

        void checkInput(){
                Event event;
                while (window.pollEvent(event)) {
                        if (event.type == Event::Closed){
                                window.close();
                                break;
                        }
                        if (event.type == Event::MouseButtonPressed){
                                if (directionMultiplier == directionMultiplier1){
                                        directionMultiplier = directionMultiplier2;
                                }
                                else {
                                        directionMultiplier = directionMultiplier1;
                                }
                        }
                }

                if (Keyboard::isKeyPressed(Keyboard::Escape)) running = false;
               
        }
       
        void update(){
                        currentSlice += lastFt;

                        //update multiple times based on fps
                        for (; currentSlice >= ftSlice; currentSlice -= ftSlice){

                                for (auto& walker : walkers){
                                        walker.update(ftStep, window);
                                }

                        }
        }

        void draw(){
                for (auto& walker : walkers){

                        myRenderTexture.draw(walker.shape);

                        shader.setParameter("frag_LightOrigin", walker.shape.getPosition());
                shader.setParameter("frag_LightColor", walker.color);
                shader.setParameter("frag_LightAttenuation", 50);

                sf::RenderStates states;
                states.shader = &shader;
                states.blendMode = sf::BlendAdd;

                myRenderTexture.draw(spriteWorld, states);

                }

                myRenderTexture.display();
        window.draw(spriteWorld);
                window.display();
        }


        void run(){

                myRenderTexture.create(windowWidth, windowHeight);
            spriteWorld.setTexture(myRenderTexture.getTexture());
        spriteWorld.setOrigin(spriteWorld.getTextureRect().width/2, spriteWorld.getTextureRect().height/2);
        spriteWorld.setPosition(windowWidth / 2, windowHeight / 2);

        shader.loadFromMemory(shaderCode, sf::Shader::Fragment);
        shader.setParameter("frag_ScreenResolution",sf::Vector2f(windowWidth, windowHeight));

                running = true;

                while (running) {

                        auto time1(chrono::high_resolution_clock::now());

                        window.clear(Color::Black);
                        myRenderTexture.clear();

                        checkInput();
                        update();
                        draw();

                        auto time2(chrono::high_resolution_clock::now());
                        auto elapsedTime(time2 - time1);
                        FrameTime ft{ chrono::duration_cast<chrono::duration<float, milli>>(elapsedTime).count() };

                        lastFt = ft;

                        auto ftSeconds(ft / 1000.f);
                        if (ftSeconds > 0.f){
                                auto fps(1.f / ftSeconds);
                                window.setTitle("FT: " + to_string(ft) + "\tFPS: " + to_string(fps));
                        }
                }
        }


        Game(){

                //framerate now handled by chrono library
                //to test with low framerate:
                //window.setFramerateLimit(15);
                window.setFramerateLimit(500);
                initializeWalkers();

        }
};


int main() {
        Game{}.run();
        return 0;
}
 


Pages: [1]