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

Pages: 1 [2] 3
16
General / Re: Specializing the << operator for SFML types
« on: August 18, 2014, 09:58:57 pm »
I created an XCode project using SFML template project. This works, but not on my project. Precompiled headers might interfere, but I don't think so. On my project, even using specialized operator for each vector2f, vector2u and vector2i doesn't compile.

ttsty.h
#ifndef __testapp__ttsty__
#define __testapp__ttsty__
#include <SFML/Audio.hpp>
#include <SFML/Graphics.hpp>

#include <iostream>
using namespace std;
using namespace sf;
template<typename Type>
ostream& operator<<(ostream& os,const sf::Vector2<Type>& rhs)
//;
{
    os.precision(2);
    return os<<'['<<rhs.x<<", "<<rhs.y<<']';
}


#endif /* defined(__testapp__ttsty__) */
 

ttsty.cpp
// nothing
 
main.cpp

//
// Disclamer:
// ----------
//
// This code will work only if you selected window, graphics and audio.
//
// Note that the "Run Script" build phase will copy the required frameworks
// or dylibs to your application bundle so you can execute it on any OS X
// computer.
//
// Your resource files (images, sounds, fonts, ...) are also copied to your
// application bundle. To get the path to these resource, use the helper
// method resourcePath() from ResourcePath.hpp
//

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

// Here is a small helper for you ! Have a look.
#include "ResourcePath.hpp"
#include "ttsty.h"
int main(int, char const**)
{
    cout << Vector2f(432,3422);
    // Create the main window
    sf::RenderWindow window(sf::VideoMode(800, 600), "SFML window");

    // Set the Icon
    sf::Image icon;
    if (!icon.loadFromFile(resourcePath() + "icon.png")) {
        return EXIT_FAILURE;
    }
    window.setIcon(icon.getSize().x, icon.getSize().y, icon.getPixelsPtr());

    // Load a sprite to display
    sf::Texture texture;
    if (!texture.loadFromFile(resourcePath() + "cute_image.jpg")) {
        return EXIT_FAILURE;
    }
    sf::Sprite sprite(texture);

    // Create a graphical text to display
    sf::Font font;
    if (!font.loadFromFile(resourcePath() + "sansation.ttf")) {
        return EXIT_FAILURE;
    }
    sf::Text text("Hello SFML", font, 50);
    text.setColor(sf::Color::Black);

    // Load a music to play
//    sf::Music music;
//    if (!music.openFromFile(resourcePath() + "nice_music.ogg")) {
//        return EXIT_FAILURE;
//    }

    // Play the music
//    music.play();

    // Start the game loop
    while (window.isOpen())
    {
        // Process events
        sf::Event event;
        while (window.pollEvent(event))
        {
            // Close window : exit
            if (event.type == sf::Event::Closed) {
                window.close();
            }

            // Escape pressed : exit
            if (event.type == sf::Event::KeyPressed && event.key.code == sf::Keyboard::Escape) {
                window.close();
            }
        }

        // Clear screen
        window.clear();

        // Draw the sprite
        window.draw(sprite);

        // Draw the string
        window.draw(text);

        // Update the window
        window.display();
    }

    return EXIT_SUCCESS;
}

 

If I put the body of the function in the header, I get this linking error:
  "std::__1::basic_ostream<char, std::__1::char_traits<char> >& operator<<<float>(std::__1::basic_ostream<char, std::__1::char_traits<char> >&, sf::Vector2<float> const&)", referenced from:
 

In my project, if I put it in the header like it work inside my project, I get those linking errors:

duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/quadtree.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/main.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/pathfinding.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/ptdistr.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/bars.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/game.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/sandbox.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/misc.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/spacepart.o
duplicate symbol __ZlsRNSt3__113basic_ostreamIcNS_11char_traitsIcEEEERK6b2Vec2 in:
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/perlin.o
    /Users/eio/Library/Developer/Xcode/DerivedData/oikoneme-fitkqpenpxxapogrbeidhlriixyt/Build/Intermediates/oikoneme.build/Debug/oikoneme.build/Objects-normal/x86_64/pathfinding2.o
ld: 10 duplicate symbols for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

17
General / Re: Specializing the << operator for SFML types
« on: August 17, 2014, 09:55:11 pm »
But I also tried inside a cpp file.

Quote
Why inside namespace sf?

it previously fixed the problem on MSVC

18
General / Specializing the << operator for SFML types
« on: August 17, 2014, 08:31:36 pm »
I managed to specialize the operator<< for Vector2<> and Color on MSVC 2012, but my code doesn't work on xcode 5.

namespace sf {
    ostream & operator<< (ostream & os, const sf::Color &right)
    {
        os << right.r << " " << right.g << " " << right.b; return os;
    }
    template<typename Type>
    inline ostream& operator<<(ostream& os,const sf::Vector2<Type>& rhs)
    {
        os.precision(2);
        return os<<'['<<rhs.x<<", "<<rhs.y<<']';
    }
}
 

My code is inside a header and inside include guards. I tried with and without inline, with and without namespace sf{}.

I'm getting duplicate symbol error for each .o file that I'm linking.

I'm guessing I'm not the only one specializing this operator. clang and MSVC sure seem to not compile templates the same way. What is working for you ?

19
SFML projects / Re: Some simple "perlin" noise
« on: June 27, 2014, 01:43:29 am »
I fixed my code, it was not perlin noise as the dot product was incorrect.

Still I have issues with it, it looks blocky, I posted it on stackoverflow here http://stackoverflow.com/questions/24435064/wrote-some-perlin-noise-kind-of-code-it-looks-blocky

20
SFML projects / Re: Some simple "perlin" noise
« on: June 23, 2014, 04:32:31 pm »
I meant "simple" as in "short".

I made this code so it can be read, copied and modified. I encourage people to use libnoise if they don't want to understand how it works.

Using references or not it doesn't really change anything. If it compiles, it's valid code, style or not. I used a procedural style of programming, it's a little more simple if you want to understand the algorithm.

I'll add more comments !

21
SFML projects / Some simple "perlin" noise
« on: June 23, 2014, 12:20:18 pm »
There are many noise generators: perlin, brownian, simplex, value noise, etc.

I made this function, it's not a lot of code and the result seems somehow fine. I'm not sure if I can call this perlin noise, it just looks random. It requires C++11, especially for the random generator. I'm not using the standard rand() function since it's not implementation defined, while minstd_rand is.

Here is the result:



Here is the code, you can get different results by changing the seed.

#include <random>
using namespace std;
using namespace sf;
typedef Vector2f Vec2; // I'm lazy
VertexArray glines(Lines); // to show gradients
Sprite spr;
Texture tx;
// this allows the code to generate the same image depending on an integer, and it will still look random. I'm not using a permutation table.
int seed = 23;
// simple dot product
float        prod(Vec2 a, Vec2 b)       { return a.x*b.x + a.y*b.y; }
// linear interpolation between 2 vectors
Vec2         interp(Vec2 start,Vec2 end,float coef){return coef*(end-start)+start;}
// generates an array of float between 0 and 1, depending on a unsigned int seed
vector<float> seeded_rand_float(unsigned int seed, int many){
        vector<float> ret;
        minstd_rand rr; // C++11 template of std::linear_congruential_engine
   
    if(seed%2147483647==0) // those values won't work and will break the generator (look at the definition of minstd_rand)
    {
        seed/=2; // dirty fix
        msg("bad seed !");
    }
        rr.seed(seed);

        for(int j = 0 ; j < many; ++j)
        ret.push_back(float(rr())/0x7fffffff); // making sure values are [0,1]
        return ret;
}

// generates a vector2f, taking two consecutive random float from the function above
vector<Vec2>seeded_rand_vec2(unsigned int seed, int many){
    auto coeffs1 = seeded_rand_float(seed, many*2); // twice as many values
    vector<Vec2> pushere;
    for(int i = 0; i < many; ++i)
        pushere.push_back(Vec2(coeffs1[2*i],coeffs1[2*i+1])); // using consecutive values from the array
    return pushere;
}
void init()
{
        // this is a lambda
   // [this](){ // removed the lambda,
        glines.clear();
        int pixels = 500; // number of pixels in a row
        int divisions = 10; // number of cells in a row
        float cellsize = float(pixels)/divisions; // the size of one cell where interpolation will occur.

        //generates the gradients vectors, gradients vector are random vector place at each corner of the square cells, (they're lattice). I've added +1 because there is one more at the end
        auto randv = seeded_rand_vec2(seed,(divisions+1)*(divisions+1));
        float factor=2.f;// that's not a very relevant variable, but nothing is perfect
        for(auto&a:randv){ // rewrite vector so their range can go from [0,1] to [-1,1] instead
            a*=factor;
            a-=factor*.5f*Vec2(1.f,1.f);
        }
        Image img;
        img.create(pixels,pixels,Color(0,0,0)); // black image of size pixels*pixels
        for(int jj=0;jj<=divisions;++jj){ // this loop just help visualize the gradient vectors using simple lines
            for(int ii=0;ii<=divisions;++ii)
            {
                Vec2
                A = randv[divisions*jj      +ii],
                glines.append(Vertex(Vec2(10,10)+cellsize*Vec2(ii,jj)));
                glines.append(Vertex(Vec2(10,10)+cellsize*Vec2(ii,jj)+A*cellsize*.5f));
            }
        }
       
        for(int j=0;j<=pixels;++j)
        {
            for(int i=0;i<=pixels;++i)
            {
                int ii = int(i/cellsize);
                int jj = int(j/cellsize);
                // here we create 4 vectors, each one describing a corner of the square cell
                // the indexing use is a little painful to understand
                Vec2
                &A = randv[divisions*jj      +ii],
                &B = randv[divisions*jj      +ii+1],
                &C = randv[divisions*(jj+1)  +ii],
                &D = randv[divisions*(jj+1)  +ii+1];

                // those steps generate x,y float values in [0,1], indicating the relative position of the pixel in a square cell
                // for example if the cellsize is 50, the pixel at (142,351), (x,y) will be (float(42/50),float(1/50))
                float x = float(i-cellsize*(i/int(cellsize)))/cellsize; // converting to int is like a floor() call
                float y = float(j-cellsize*(j/int(cellsize)))/cellsize; // (I guess?)
                // interpolate the 4 gradients into one vector, depending on the relative position of the pixel in the cell
                // note that you can change the interp function for smoother results (those weird white artifacts)
                Vec2 grad = interp(interp(A,B,x),interp(C,D,x),y);
                // calculate the pixel value, it's the sum of the dot products between the interpolated gradient, and 4 vectors, which are made with the difference between the pixel relative value, and the corresponding corners.
                float bright =
                prod(grad ,Vec2(x     ,y))+
                prod(grad ,Vec2(1.f-x ,y))+
                prod(grad ,Vec2(x     ,1.f-y))+
                prod(grad ,Vec2(1.f-x ,1.f-y));
               
                bright=abs(bright);//making it positive
                bright*=.25f; // divides by four, since the value is in [0.0,4.0]

                img.setPixel(i,j,Color(255.f*bright,255.f*bright,255.f*bright)); // sets the pixel
            }
        }
        tx.loadFromImage(img);
        spr.setPosition(Vec2(10,10));// shifts the sprite
        spr.setTexture(tx);
       
    //}();
}

void draw(RenderWindow&window)
{
        window.draw(spr);
}

It's not a compile-and-work example, but I made it easier for you to test. It might require many other headers like vector.

The interpolation is linear hence the ghostly white line artifacts between some gradients.

It's not the best implementation you could find, I think there are many errors (for instance, the gradients seem to loop over, the one on the left comes back on the right), but it's short and simple, and sort of look like random noise, I commented so you can understand the steps, because I had quite a lot of trouble understanding online examples, code and explanations, especially how the dot products are calculated.

Tell me what you think and ask about !

I also intend to use this to get a contour as points to draw polygons, to generate forest/grass/lakes/mountain patterns. Noise is interesting, but it's costly.

22
System / Re: Mouse wheel problem on OS X.
« on: June 01, 2014, 12:26:45 am »
well I see two solutions.

by testing how my mac is behaving, any tiny scrolling with the touchpad triggers an event, so converting to either 1 or -1 will enable devs to come with some quick ratio solution, but it won't be optimal...

I guess you can't really fix this by changing the sf::Event::MouseWheelEvent::delta type from int to float since it might break existing applications...

another fair fix would be to add another float delta_float field to store a float data when possible...

I don't really know the difference between how linux/windows/mac handles the mousewheel/touchpad, but I guess some issues have as much problems as solutions.

23
General discussions / Re: About the documentation
« on: March 15, 2014, 12:22:43 pm »
yeah you're right, sorry !

24
General discussions / About the documentation
« on: March 15, 2014, 10:44:06 am »
I don't want to sound whiny, but why isn't there any downloadable API doc ? I found myself several times without internet, so no doxygen and going through the source code.

I know I could do it myself, but why not provide it in the zip packages ? Would save devs some time...


25
Window / Re: RenderWindow.SetSize blurs images
« on: November 25, 2013, 01:50:31 pm »
Sorry I did not find the results I wanted when I searched, found some after posting. Deleted my reply.

26
Graphics / Not sure if bug or not, getting division by zero...
« on: November 14, 2013, 03:10:49 pm »
Using SFML 2.1 with MSVC 2012 on windows 7 64 bits. Can't really remember if I'm using the 64 bits version of the SFML lib.

struct menu
{
    //map<string,Sprite> icons;
    vector<Sprite> icons;
    vector<Texture> tx;
    vector<Text> texts;
    Vec2 origin;
    vector<string> filenames;
    void init()
    {
        for(int i = 0; i < 50; ++i)
        {
            tx.push_back(Texture());
            if(!tx.back().loadFromFile("png/beans.png")) cout << "could not open breeagfdso" << endl;
            icons.push_back(Sprite(tx.back()));
        }
        cout << icons.size() << " elems" << endl;
        float
            c_x = icons.begin()->getTextureRect().width+2,
            c_y = icons.begin()->getTextureRect().height+2;
        cout << "cx cy = " << c_x << " " << c_y << endl;
        int i = 0;
        for(auto&a:icons)
        {
            a.setPosition(Vec2((i%4)*c_x, (i/4)*c_y)+origin);
            ++i;
        }

    }
    void draw(RenderWindow&window)
    {
        for(auto&a:icons)
            window.draw(a);
    }
};

I think the code is pretty straightforward. I instantiate a menu and initialize it like this:

struct game
{
menu m;
void init()
{
    m.origin=Vector2f(10,10);
    m.init();
}

This is the error: Integer division by zero.

This previous code crashes if I set i < 3, the third displays fine, and 2 others are white squares. If I use only one texture and many sprites by just putting those 2 lines out of the loop like this:
        tx.push_back(Texture());
        if(!tx.back().loadFromFile("png/beans.png")) cout << "could not open breeagfdso" << endl;
        for(int i = 0; i < 50; ++i)
        {
            icons.push_back(Sprite(tx.back()));
        }
It runs fine and I see those icons displayed many times in rows.

I had a discussion lately on IRC about this, I was told I should use unique_ptr, but I don't think that's a solution since I use plain vectors, not vectors of pointers.

Can somebody tell me if it's a bug, or if the Texture class should not be used plain in a vector ?

Here is the call stack : http://pastebin.com/47bJmH1c

I remember outputing the size of the sprite rectangle like it was advised in one thread, I got some very large int number, but I don't understand why.

27
General discussions / Re: Xcode templates updated
« on: March 16, 2013, 12:25:24 pm »
Could someone quickly post them on some download site, I'm in a public library and https is blocked (I asked and seems there's no other way).

EDIT: forget it I just got out of this library found some freewifi and downloaded it. Public service, what a mess.

28
General discussions / Re: Install for OSX Mountain Lion
« on: August 05, 2012, 07:49:14 pm »
adding correct search paths was enough, thanks !

29
General discussions / Re: Install for OSX Mountain Lion
« on: August 05, 2012, 06:39:45 pm »
Read the tutorial carefully, every questions you currently have are answered in it; especially the part where it say you should not use Xcode generator for CMake...

So you can confirm the SFML 2 rc installer doesn't work on mountain lion ? Or is there a fix ?

30
General discussions / Re: Install for OSX Mountain Lion
« on: August 05, 2012, 11:47:07 am »
But then downloaded the latest 2.0 snapshot and built it and used the templates from it rather than the RC.  I thought RC meant it was the latest, but now I get it that the snapshot has several things fixed that the RC doesn't have yet. I had to install XQuartz also to get the project to run.

Could you be more precise about the steps you achieved ? After generating the xcodeproj with cmake, how do you install it ?

How are the templates installed in xcode ? (I checked the tickbox in cmake)

Pages: 1 [2] 3