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

Recent Posts

Pages: [1] 2 3 ... 10
1
General discussions / Shaders
« Last post by LedLoaf on Today at 08:19:12 am »
Hello,

So I'm checking out someone's Tron code from YouTube, which is basically like 2 player Snake. Towards the end he adds in a shader for the 2 players.

#include <SFML/Graphics.hpp>
#include <time.h>
#include <SFML/OpenGL.hpp>
#include <iostream>
using namespace sf;

const int W=600;
const int H=480;
int speed = 4;
bool field[W][H]={0};

struct player
{ int x,y,dir;
  Color color;
  player(Color c)
  {
    x=rand() % W;
    y=rand() % H;
    color=c;
    dir=rand() % 4;
  }
  void tick()
  {
    if (dir==0) y+=1;
    if (dir==1) x-=1;
    if (dir==2) x+=1;
    if (dir==3) y-=1;

    if (x>=W) x=0;  if (x<0) x=W-1;
    if (y>=H) y=0;  if (y<0) y=H-1;
  }

  Vector3f getColor()
  {return Vector3f(color.r,color.g,color.b);}
};

int main()
{
    srand(time(0));

    RenderWindow window(VideoMode(W, H), "The Tron Game!");
    window.setFramerateLimit(60);

    Texture texture;
    texture.loadFromFile("media/Tron/background.jpg");
    Sprite sBackground(texture);

    player p1(Color::Red), p2(Color::Green);

    Sprite sprite;
    RenderTexture t;
    t.create(W, H);
    t.setSmooth(true);
    sprite.setTexture(t.getTexture());
    t.clear();  t.draw(sBackground);

    Font font; font.loadFromFile("F:/Fonts/Sansation.ttf");
    sf::Text text("YOU WIN", font, 35);
    text.setPosition(W / 2 - 80, 20);

    bool Game=true;
    auto shader = new Shader;
    if (!shader->loadFromFile("shader.frag", sf::Shader::Fragment)) std::cout << "Failed to load Shader\n";
    shader->setUniform("frag_ScreenResolution", sf::Vector2f(W, H));
    shader->setUniform("frag_LightAttenuation",100);
    sf::RenderStates states; states.shader = shader;

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

        if (Keyboard::isKeyPressed(Keyboard::Left)) if (p1.dir!=2) p1.dir=1;
        if (Keyboard::isKeyPressed(Keyboard::Right)) if (p1.dir!=1)  p1.dir=2;
        if (Keyboard::isKeyPressed(Keyboard::Up)) if (p1.dir!=0) p1.dir=3;
        if (Keyboard::isKeyPressed(Keyboard::Down)) if (p1.dir!=3) p1.dir=0;

        if (Keyboard::isKeyPressed(Keyboard::A)) if (p2.dir!=2) p2.dir=1;
        if (Keyboard::isKeyPressed(Keyboard::D)) if (p2.dir!=1)  p2.dir=2;
        if (Keyboard::isKeyPressed(Keyboard::W)) if (p2.dir!=0) p2.dir=3;
        if (Keyboard::isKeyPressed(Keyboard::S)) if (p2.dir!=3) p2.dir=0;

        if (!Game) {
            window.draw(text);
            window.display();
                continue;
        }

       
        for (int i = 0; i < speed; i++) {
            p1.tick(); p2.tick();
            if (field[p1.x][p1.y] == 1) { Game = false; text.setFillColor(p2.color); }
            if (field[p2.x][p2.y] == 1) { Game = false; text.setFillColor(p1 .color);}
            field[p1.x][p1.y] = 1;
            field[p2.x][p2.y] = 1;

          /*  CircleShape c(3);
            c.setPosition(p1.x, p1.y); c.setFillColor(p1.color);    t.draw(c);
            c.setPosition(p2.x, p2.y); c.setFillColor(p2.color);    t.draw(c);  */

            t.display();
         
            shader->setUniform("frag_LightOrigin", sf::Vector2f{ (float)p1.x,(float)p1.y });
            shader->setUniform("frag_LightColor", p1.getColor());
            t.draw(sprite, states);
            shader->setUniform("frag_LightOrigin", sf::Vector2f{(float) p2.x,(float)p2.y });
            shader->setUniform("frag_LightColor", p2.getColor());
            t.draw(sprite, states);

        }

       ////// draw  ///////
        window.clear();
        window.draw(sprite);
       // window.draw(sf::Sprite{ t.getTexture() });
        window.display();
    }

    return 0;
}
 

And here is the shader itself
uniform vec2 frag_LightOrigin; 
uniform vec3 frag_LightColor;  
uniform float frag_LightAttenuation;   
uniform vec2 frag_ScreenResolution;
uniform sampler2D texture;
void main()
{              
    vec2 baseDistance =  gl_FragCoord.xy;
    baseDistance.y = frag_ScreenResolution.y - baseDistance.y;
    float d = length(frag_LightOrigin - baseDistance);
    float a = 1.0/(frag_LightAttenuation * d); 
    vec4 color = vec4(a,a,a,1.0) * vec4(frag_LightColor, 1.0);  
    vec4 t = texture2D(texture, gl_TexCoord[0].xy);
    if (t[0]>color[0]) color[0]=t[0];
    if (t[1]>color[1]) color[1]=t[1];
    if (t[2]>color[2]) color[2]=t[2];
    gl_FragColor=color;
}
 

He originally was using setParameter and I know that was deprecated so I'm thinking something here is no longer correct? What happens is the screen is just completely green, much like it would be if you used window.clear(sf::Color::Green).

Along with that this was in my console output:
An internal OpenGL call failed in shader.cpp(466).
Expression:
   GLEXT_glUniform1i(binder.location, x)
Error description:
   GL_INVALID_OPERATION
   The specified operation is not allowed in the current state.

One last thing, I changed the casted floats to sf::Vector2i and it was the same, except now it just continued to spam this error in the console box.

Thanks, everyone, hope the beginning of your year has been great.
2
General / Re: Cant Compile With Static libs.
« Last post by tom42 on January 22, 2021, 04:10:36 pm »
...I thought SFML needs GLFW, so I added it.

g++ -DSFML_STATIC -O2 -no-pie -fno-pie -I/home/uzzer/LIB/SFML-2.5.1/include -o main main.cpp -L/home/uzzer/LIB/SFML-2.5.1/lib -L/usr/lib/x86_64-linux-gnu -pthread -ludev -lX11 -lsfml-graphics-s -lsfml-system-s -lsfml-window-s -lGL -lGLU -lstdc++ -lc -lm -ldl
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-window-s.a(ClipboardImpl.cpp.o): undefined reference to symbol 'XConvertSelection'
/usr/bin/ld: /usr/lib/x86_64-linux-gnu/libX11.so: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status
... my head hurts.
3
General / Re: Cant Compile With Static libs.
« Last post by eXpl0it3r on January 22, 2021, 04:07:24 pm »
sfml-window depends on sfml-system so it should come after.

For other libraries you could potentially see it in the package manager somehow.
Or mostly just try things out.

What are you linking SFML and GLFW?
4
General / Re: Cant Compile With Static libs.
« Last post by tom42 on January 22, 2021, 03:42:38 pm »
Thank you. But how can I know what depends on what?
When I change the order, I get this:
g++ -DSFML_STATIC -O2 -no-pie -fno-pie -I/home/uzzer/LIB/SFML-2.5.1/include -o main main.cpp -L/home/uzzer/LIB/SFML-2.5.1/lib -L/usr/lib/x86_64-linux-gnu -lsfml-graphics-s -lsfml-system-s -lsfml-window-s -lstdc++ -lc -lm -ldl -ludev -lX11 -lglfw -lGL -lGLU
/usr/bin/ld: /home/uzzer/LIB/SFML-2.5.1/lib/libsfml-system-s.a(MutexImpl.cpp.o): undefined reference to symbol 'pthread_mutexattr_settype@@GLIBC_2.2.5'
/usr/bin/ld: /lib/x86_64-linux-gnu/libpthread.so.0: error adding symbols: DSO missing from command line
collect2: error: ld returned 1 exit status

So, I need to use this https://www.sfml-dev.org/tutorials/2.4/start-vc.php and put Dependencies before static modules that requires it?
5
General / Re: Cant Compile With Static libs.
« Last post by eXpl0it3r on January 22, 2021, 03:22:07 pm »
The order you link libraries in is important for ld.

My rule of thumb is "if X depends on Y, then X needs to come before Y"
Which would mean for example, that sfml-system needs to come after sfml-graphics and sfml-window and gl (OpenGL) needs to come after the sfml-window as well.
6
Graphics / Re: Overriding sf::RectangleShape::draw
« Last post by chudek123 on January 22, 2021, 01:00:02 pm »
Hello, I would like to share with my solution which allows for inheriting from sf::RectangleShape class. I met troubles with deriving from sf::Drawable and sf::Transformable, because then I couldn't use sf::Shape::setFillColor, so I tried to inherit from sf::Shape like our friend said above. Inheriting from sf::Shape was still problematic, because I had to define points like in eclipse example, I didn't want to do this in my case of drawing rectangle with grid. Finally, inheriting from sf::RectangleShape gave me a possibility for overriding draw function:
void CustomRectangleShape::draw(sf::RenderTarget &target, sf::RenderStates states) const
{
    states.transform *= this->getTransform();
    sf::VertexArray rect_shape(sf::Quads, 4);
    sf::Color rect_color = this->getFillColor();
    rect_shape[0].position = this->getPoint(0);
    rect_shape[1].position = this->getPoint(1);
    rect_shape[2].position = this->getPoint(2);
    rect_shape[3].position = this->getPoint(3);
    rect_shape[0].color = rect_color;
    rect_shape[1].color = rect_color;
    rect_shape[2].color = rect_color;
    rect_shape[3].color = rect_color;
    target.draw(rect_shape,states);
    if(this->grid_){
        this->drawGrid(target,states);
    }
}

void CustomRectangleShape::drawGrid(sf::RenderTarget &target, sf::RenderStates states) const
{
    for(int i = 0; i < grid_size_.x; i++){
        for(int j = 0; j < grid_size_.y; j++){
            grid_rec_->setPosition(this->getPosition().x + i * grid_rec_->getSize().x, this->getPosition().y + j * grid_rec_->getSize().y);
            target.draw(*grid_rec_, states);
        }
    }
}
 

I also created class fields for grid code, to be clear:
bool grid_;
sf::RectangleShape* grid_rec_;
sf::Vector2i grid_size_;
.
7
General / Cant Compile With Static libs.
« Last post by tom42 on January 22, 2021, 12:37:39 pm »
Hello everyone.
I'm trying to compile my silly hello-world SFML project for test. But getting strange error messages from linker. (When I compile with shared lib's - everything is OK.)

Also left this question & details here -> https://stackoverflow.com/questions/65830743/sfml-compilation-with-static-libs-error-c
Full error message is here. https://anotepad.com/notes/b34xbc2s

I don't  know what to do\read\learn about this, newer build anything with static libs before. I'm clearly doing something stupid here.
8
SFML projects / Re: Dispersio 2
« Last post by Nexus on January 21, 2021, 09:09:15 am »
Very nice, congratulations on the release! :)

The game looks cool, the style reminds me a lot of the original Mario/Donkey Kong game, with the wooden pillars and barrels.
9
SFML projects / Re: Dispersio 2
« Last post by achpile on January 21, 2021, 06:49:18 am »
10
Audio / Re: Music stops playing sometimes
« Last post by achpile on January 20, 2021, 03:36:48 pm »
Oh, sorry. My bad... I'm using old SFML version for windows build....
Pages: [1] 2 3 ... 10
anything