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

Pages: 1 ... 12 13 [14] 15 16 ... 25
196
General / Re: Jumping and collision problem
« on: July 31, 2020, 02:36:43 am »
i don't understand what is the problem. isn't the plataform supposed to stop the player movement?

197
Graphics / Re: Render Texture with a View
« on: July 30, 2020, 10:37:18 pm »
can't you post some image of the problem and code? its hard to guess whats happening.

198
General / Re: Jumping and collision problem
« on: July 30, 2020, 10:34:11 pm »
if the problem is the player jumping higher when he is close to the plataform, i have a guess that it is because he is "stepping" in the plataform for a brief period of time (like 1ms), but it is enough to restart the jumping cycle. you can put some delay to the jumping method, avoiding the player to jump again in the first second, for example.

199
Graphics / Re: Render Texture with a View
« on: July 30, 2020, 06:09:13 pm »
it's hard to tell without any code, but my guess is that you are drawing this sprite to the same coordinates in the window view everytime.

200
General / Re: need a better collision idea
« on: July 25, 2020, 07:08:31 pm »
weird, in GCC (Linux, Debian Testing) +14 and +17 it seems not to be emplacing new itens in the Add method.
anyway, its working for me now, so i consider it solved. also need optimizations, but this test works:

#include <string>
#include <fstream>
#include <iostream>

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>
#include <SFML/OpenGL.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(512, 256), "Tests");
    sf::Vector2u tile_size(32, 32);
    sf::Vector2u map_size(16, 8);

    sf::Texture tileset;
    tileset.loadFromFile("terrain-6.png");

    sf::VertexArray vertices;
    vertices.setPrimitiveType(sf::Quads);
    vertices.resize(map_size.x*map_size.y*4);

    const int level[] ={
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
        1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
        0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
        0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
        0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
        2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
    };


    for (unsigned int i = 0; i < map_size.x; ++i){
        for (unsigned int j = 0; j < map_size.y; ++j){
            int tileNumber = level[i+j*map_size.x];
            int tu = tileNumber % (tileset.getSize().x / tile_size.x);
            int tv = tileNumber / (tileset.getSize().x / tile_size.x);
            sf::Vertex* quad = &vertices[(i+j*map_size.x)*4];

            quad[0].position = sf::Vector2f(i*tile_size.x, (j)*tile_size.y);
            quad[1].position = sf::Vector2f((i+1)*tile_size.x, (j)*tile_size.y);
            quad[2].position = sf::Vector2f((i+1)*tile_size.x, (j+1)*tile_size.y);
            quad[3].position = sf::Vector2f(i*tile_size.x, (j+1)*tile_size.y);

            quad[0].texCoords = sf::Vector2f(tu*tile_size.x, tv*tile_size.y);
            quad[1].texCoords = sf::Vector2f((tu+1)*tile_size.x, tv*tile_size.y);
            quad[2].texCoords = sf::Vector2f((tu+1)*tile_size.x, (tv+1)*tile_size.y);
            quad[3].texCoords = sf::Vector2f(tu*tile_size.x, (tv+1)*tile_size.y);
        }
    }

    sf::Shader shader;
    shader.loadFromFile("shaders/test.frag", sf::Shader::Fragment);
    shader.setUniform("texture", tileset);

    sf::RenderTexture buffer;
    buffer.create(window.getSize().x, window.getSize().y);
    sf::Sprite buffer_spr(tileset);

    sf::RenderStates rs;
    rs.texture = &tileset;
    rs.shader = &shader;

    while (window.isOpen()){
        buffer.clear();
        for (size_t y=0; y<map_size.y; y++){
            for (size_t x=0; x<map_size.x; x++){
                // Calc ID
                int ID = 1 + y * map_size.x + x;
                int r = (ID & 0x000000FF) >> 0;
                int g = (ID & 0x0000FF00) >> 8;
                int b = (ID & 0x00FF0000) >> 16;

                shader.setUniform("color_id", sf::Glsl::Vec4(r / 255.0f, g / 255.0f, b / 255.0f, 255));

                int tileNumber = level[y*map_size.x+x];
                int tu = tileNumber % (tileset.getSize().x / tile_size.x);
                int tv = tileNumber / (tileset.getSize().x / tile_size.x);

                buffer_spr.setTextureRect(sf::IntRect(tu*tile_size.x, tv*tile_size.y, tile_size.x, tile_size.y));
                buffer_spr.setPosition(x*tile_size.x, y*tile_size.y);
                buffer.draw(buffer_spr, rs);
            }
        }
        buffer.display();
        sf::Event event;
        while (window.pollEvent(event)){
            if(event.type == sf::Event::Closed) window.close();
            if(event.type == sf::Event::MouseButtonPressed && event.mouseButton.button == sf::Mouse::Left){
                unsigned char data[4];
                glReadPixels(sf::Mouse::getPosition(window).x, window.getSize().y-sf::Mouse::getPosition(window).y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &data);
                std::cout << " id:" << (data[0] + data[1] * 256 + data[2] * 256 * 256) - 1;
            }
        }
        window.clear(sf::Color::White);
        window.draw(vertices, &tileset);
        sf::Sprite test_sprite(buffer.getTexture());
        //window.draw(test_sprite);
        window.display();
        sf::sleep(sf::milliseconds(10));
    }
    return 0;
}
 

funny fact that maybe the developers could explain: I tought that glReadPixels would get pixels that are in the screen, not in textures or other render targets; but we are drawing the colorized sprites to a RenderTexture. still, the function reads it correctly (of course, if nothing else is drawn to the screen yet in that frame, in which case the id colors would be overlaid). maybe both RenderTargets are seen as the same thing for OpenGL?

anyway, thank you for the help! i'm going to focus on improving the performance, but the issue is completely solved.


EDIT: I forgot to mention, i think the shader isn't really optimized also, so it could be a bottleneck too? I simply made something that could work, but the newer versions of GLSL doesn't even accept things like gl_FragColor and gl_TexCoord.

201
General / Re: Idea for Collision
« on: July 24, 2020, 03:55:16 pm »

202
General / Re: Idea for Collision
« on: July 24, 2020, 03:04:34 pm »
well... the logic is simple:...
BEFORE the player move, test if he is going to collide with something. if true, the player don't move. if false, he moves.

so you don't move the player right from the start. you first test for collisions.

203
General / Re: need a better collision idea
« on: July 24, 2020, 02:58:47 pm »
great! i'll try to put it to my code
one question, tough: does the code from your last post works for you? I get a segmentation fault at line 88:
return lhs.pos.y < rhs.pos.y;

for some reason I can't debug a code that contains shader, so i don't know what could be the cause

204
Window / Re: Display window for a few seconds without event polling
« on: July 22, 2020, 02:17:38 pm »
hi
have you tried
sf::sleep(sf::seconds(2));
instead of
usleep(2000000);
?

205
I dont know if there is Visual Studio for MAC, but it would be something like that. you just take your code and compile it again in a OSX. you don't need to copy/paste all the code, just use the same files (.c, .cpp, .h, .hpp, etc). they are text files, so they work everywhere.
an easier way would be using crosscompilers. OR you can use a virtual machine to compile your program.

206
do you mean to convert your windows .exe to a mac aplication? I wouldn't agree to that. its better to simply compile it again in an OSX. same for Linux based distros...

207
General / Re: need a better collision idea
« on: July 21, 2020, 05:55:28 pm »
updating the journey:

shader to colorize:
uniform sampler2D texture;
uniform vec4 color_id;

void main(){
    vec4 pixel = texture2D(texture, gl_TexCoord[0].xy);
    if(pixel.a != 0.0){
        gl_FragColor = color_id;
    }
    else{
        discard;
    }
}
 
aparently gl_FragColor and gl_TexCoord are deprecated in GLSL, but I wasn't able to do it using "out vec4" and alikes

an aplication that can draw it directly to the screen, so we can see it is working:
#include <string>
#include <fstream>
#include <iostream>

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>


int main(){
    sf::RenderWindow window(sf::VideoMode(512, 256), "Tests");
    sf::Vector2u tile_size(32, 32);
    sf::Vector2u map_size(16, 8);

    sf::Texture tileset;
    tileset.loadFromFile("terrain-6.png");

    sf::VertexArray vertices;
    vertices.setPrimitiveType(sf::Quads);
    vertices.resize(map_size.x*map_size.y*4);

    const int level[] ={
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
        1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
        0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
        0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
        0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
        2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
    };


    for (unsigned int i = 0; i < map_size.x; ++i){
        for (unsigned int j = 0; j < map_size.y; ++j){
            int tileNumber = level[i+j*map_size.x];
            int tu = tileNumber % (tileset.getSize().x / tile_size.x);
            int tv = tileNumber / (tileset.getSize().x / tile_size.x);
            sf::Vertex* quad = &vertices[(i+j*map_size.x)*4];

            quad[0].position = sf::Vector2f(i*tile_size.x, (j)*tile_size.y);
            quad[1].position = sf::Vector2f((i+1)*tile_size.x, (j)*tile_size.y);
            quad[2].position = sf::Vector2f((i+1)*tile_size.x, (j+1)*tile_size.y);
            quad[3].position = sf::Vector2f(i*tile_size.x, (j+1)*tile_size.y);

            quad[0].texCoords = sf::Vector2f(tu*tile_size.x, tv*tile_size.y);
            quad[1].texCoords = sf::Vector2f((tu+1)*tile_size.x, tv*tile_size.y);
            quad[2].texCoords = sf::Vector2f((tu+1)*tile_size.x, (tv+1)*tile_size.y);
            quad[3].texCoords = sf::Vector2f(tu*tile_size.x, (tv+1)*tile_size.y);
        }
    }

    sf::Shader shader;
    shader.loadFromFile("shaders/test.frag", sf::Shader::Fragment);
    shader.setUniform("texture", tileset);
    shader.setUniform("color_id", sf::Glsl::Vec4(255, 0, 0, 255));

    sf::RenderStates rs;
    rs.texture = &tileset;
    rs.shader = &shader;

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

        window.clear(sf::Color::White);
        window.draw(vertices, rs);
        window.display();
        sf::sleep(sf::milliseconds(10));
    }
    return 0;
}

if we draw it without the shader:


and after enabling it:



next step would be to draw it to a buffer. problem is, as I said, the shader is applied to the whole vertexArray at once, so every tile would have the same id. so my idea was to draw sprites around the mouse (as you said, like 1/4 of the screen), each one with the shader applied with a different color value. so i tried this (my idea would be to iterate a single sprite  sized as a tile trough the screen, drawing it to a RenderTexture):

#include <string>
#include <fstream>
#include <iostream>

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>


int main(){
    sf::RenderWindow window(sf::VideoMode(512, 256), "Tests");
    sf::Vector2u tile_size(32, 32);
    sf::Vector2u map_size(16, 8);

    sf::Texture tileset;
    tileset.loadFromFile("terrain-6.png");

    sf::VertexArray vertices;
    vertices.setPrimitiveType(sf::Quads);
    vertices.resize(map_size.x*map_size.y*4);

    const int level[] ={
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
        1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
        0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
        0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
        0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
        2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
    };


    for (unsigned int i = 0; i < map_size.x; ++i){
        for (unsigned int j = 0; j < map_size.y; ++j){
            int tileNumber = level[i+j*map_size.x];
            int tu = tileNumber % (tileset.getSize().x / tile_size.x);
            int tv = tileNumber / (tileset.getSize().x / tile_size.x);
            sf::Vertex* quad = &vertices[(i+j*map_size.x)*4];

            quad[0].position = sf::Vector2f(i*tile_size.x, (j)*tile_size.y);
            quad[1].position = sf::Vector2f((i+1)*tile_size.x, (j)*tile_size.y);
            quad[2].position = sf::Vector2f((i+1)*tile_size.x, (j+1)*tile_size.y);
            quad[3].position = sf::Vector2f(i*tile_size.x, (j+1)*tile_size.y);

            quad[0].texCoords = sf::Vector2f(tu*tile_size.x, tv*tile_size.y);
            quad[1].texCoords = sf::Vector2f((tu+1)*tile_size.x, tv*tile_size.y);
            quad[2].texCoords = sf::Vector2f((tu+1)*tile_size.x, (tv+1)*tile_size.y);
            quad[3].texCoords = sf::Vector2f(tu*tile_size.x, (tv+1)*tile_size.y);
        }
    }

    sf::Shader shader;
    shader.loadFromFile("shaders/test.frag", sf::Shader::Fragment);
    shader.setUniform("texture", tileset);

    sf::RenderTexture buffer;
    buffer.create(window.getSize().x, window.getSize().y);
    sf::Texture buffer_tex;
    sf::Sprite buffer_spr(buffer_tex);

    sf::RenderStates rs;
    rs.texture = &tileset;
    rs.shader = &shader;

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if(event.type == sf::Event::Closed)
                window.close();
        }
        buffer.clear();
        for (size_t y=0; y<map_size.y; y++){
            for (size_t x=0; x<map_size.x; x++){
                shader.setUniform("color_id", sf::Glsl::Vec4(x, y, 0, 255));

                int tileNumber = level[x+y*map_size.x];
                int tu = tileNumber % (tileset.getSize().x / tile_size.x);
                int tv = tileNumber / (tileset.getSize().x / tile_size.x);

                buffer_spr.setTextureRect(sf::IntRect(tu*tile_size.x, tv*tile_size.y, tile_size.x, tile_size.y));
                buffer_spr.setPosition(x*tile_size.x, y*tile_size.y);
                buffer.draw(buffer_spr, rs);
            }
        }
        buffer.display();
        window.clear(sf::Color::White);
        window.draw(vertices, &tileset);
        sf::Sprite test_sprite(buffer.getTexture());
        window.draw(test_sprite);
        window.display();
        sf::sleep(sf::milliseconds(10));
    }
    return 0;
}

the test_sprite is just for testing purposes, obviously. I set it to use the render texture, so we could see what is being draw. but the results are weird:



maybe i'm not calculating the sprite and its texture positions correctly?  :P
or maybe a vertex shader would be better?


EDIT: watch out, my brain is probably melting after 10 days trying to solve this. i simply forgot to load the texture in the buffer_tex. and by the way, that was nonsense; I just had to use the original tileset texture in the buffer_spr sprite. fixing it:

#include <string>
#include <fstream>
#include <iostream>

#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>


int main(){
    sf::RenderWindow window(sf::VideoMode(512, 256), "Tests");
    sf::Vector2u tile_size(32, 32);
    sf::Vector2u map_size(16, 8);

    sf::Texture tileset;
    tileset.loadFromFile("terrain-6.png");

    sf::VertexArray vertices;
    vertices.setPrimitiveType(sf::Quads);
    vertices.resize(map_size.x*map_size.y*4);

    const int level[] ={
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
        1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
        0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
        0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
        0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
        2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
    };


    for (unsigned int i = 0; i < map_size.x; ++i){
        for (unsigned int j = 0; j < map_size.y; ++j){
            int tileNumber = level[i+j*map_size.x];
            int tu = tileNumber % (tileset.getSize().x / tile_size.x);
            int tv = tileNumber / (tileset.getSize().x / tile_size.x);
            sf::Vertex* quad = &vertices[(i+j*map_size.x)*4];

            quad[0].position = sf::Vector2f(i*tile_size.x, (j)*tile_size.y);
            quad[1].position = sf::Vector2f((i+1)*tile_size.x, (j)*tile_size.y);
            quad[2].position = sf::Vector2f((i+1)*tile_size.x, (j+1)*tile_size.y);
            quad[3].position = sf::Vector2f(i*tile_size.x, (j+1)*tile_size.y);

            quad[0].texCoords = sf::Vector2f(tu*tile_size.x, tv*tile_size.y);
            quad[1].texCoords = sf::Vector2f((tu+1)*tile_size.x, tv*tile_size.y);
            quad[2].texCoords = sf::Vector2f((tu+1)*tile_size.x, (tv+1)*tile_size.y);
            quad[3].texCoords = sf::Vector2f(tu*tile_size.x, (tv+1)*tile_size.y);
        }
    }

    sf::Shader shader;
    shader.loadFromFile("shaders/test.frag", sf::Shader::Fragment);
    shader.setUniform("texture", tileset);

    sf::RenderTexture buffer;
    buffer.create(window.getSize().x, window.getSize().y);
    sf::Sprite buffer_spr(tileset);

    sf::RenderStates rs;
    rs.texture = &tileset;
    rs.shader = &shader;

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if(event.type == sf::Event::Closed)
                window.close();
        }
        buffer.clear();
        for (size_t y=0; y<map_size.y; y++){
            for (size_t x=0; x<map_size.x; x++){
                shader.setUniform("color_id", sf::Glsl::Vec4(x, y, 0, 255));

                int tileNumber = level[y*map_size.x+x];
                int tu = tileNumber % (tileset.getSize().x / tile_size.x);
                int tv = tileNumber / (tileset.getSize().x / tile_size.x);

                buffer_spr.setTextureRect(sf::IntRect(tu*tile_size.x, tv*tile_size.y, tile_size.x, tile_size.y));
                buffer_spr.setPosition(x*tile_size.x, y*tile_size.y);
                buffer.draw(buffer_spr, rs);
            }
        }
        buffer.display();
        window.clear(sf::Color::White);
        window.draw(vertices, &tileset);
        sf::Sprite test_sprite(buffer.getTexture());
        window.draw(test_sprite);
        window.display();
        sf::sleep(sf::milliseconds(10));
    }
    return 0;
}

works now:


not bad for a prototype. last part is identify the color under the mouse cursor and return the id.

EDIT 2: tried this right before clearing the buffer, but it does not update the color (with or without the first line)
sf::Texture::bind(&buffer.getTexture(), sf::Texture::CoordinateType::Pixels);
        sf::Color color(10, 10, 10, 255);
        glReadPixels(sf::Mouse::getPosition().x, sf::Mouse::getPosition().y, 1, 1, GL_RGBA, GL_UNSIGNED_BYTE, &color);
        std::cout << size_t(color.r) << " ";

208
General / Re: need a better collision idea
« on: July 15, 2020, 03:40:19 pm »
yeah, I think that its just not possible at all to do it in a simple way using vertexarrays, since each tile should take one different color.
maybe for this color buffer a sequence of sprites rendered to a RenderTexture could work, since they would be rendered only in and around the screen (you can't click something you can't see). but still theres a problem with recognizing the color at given position. copying the render texture to a sf::Image every loop doesn't seem practical  ???

209
General / Re: Sprite rotation on tile map
« on: July 14, 2020, 06:59:25 pm »
it could be because you are constantly reseting the sprite origin when you update poll events.
have you tried like this?
void Player::initializePlayer(const sf::Texture& idle_T)
{      
//[...]
        this->player_Sprite.setOrigin(sf::Vector2f(
                this->player_Sprite.getLocalBounds().width / 2.f,
                this->player_Sprite.getLocalBounds().height / 2.f));
}

210
General / Re: need a better collision idea
« on: July 14, 2020, 05:49:25 pm »
hi! thanks for your response
I was trying to come up with some code based on what you said before replying again, but this may take some time. so, i just wanted to thank for the help. i'm going for the second approach. when i have somethign new, i'll post here.  ;D


EDIT: well, i'll need help for that. i have never writen a shader before, nor really used one with SFML.

so, i'll be using this code based on the tilemap tutorial:
#include <SFML/System.hpp>
#include <SFML/Window.hpp>
#include <SFML/Graphics.hpp>

int main(){
    sf::RenderWindow window(sf::VideoMode(512, 256), "Tilemap");
    sf::Vector2u tile_size(32, 32);
    sf::Vector2u map_size(32, 16);

    sf::Texture tileset;
    tileset.loadFromFile("terrain-6.png");

    sf::VertexArray vertices;
    vertices.setPrimitiveType(sf::Quads);
    vertices.resize(map_size.x*map_size.y*4);

    const int level[] ={
        0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1,
        0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 2, 0, 0, 0, 0,
        1, 1, 0, 0, 0, 0, 0, 0, 3, 3, 3, 3, 3, 3, 3, 3,
        0, 1, 0, 0, 2, 0, 3, 3, 3, 0, 1, 1, 1, 0, 0, 0,
        0, 1, 1, 0, 3, 3, 3, 0, 0, 0, 1, 1, 1, 2, 0, 0,
        0, 0, 1, 0, 3, 0, 2, 2, 0, 0, 1, 1, 1, 1, 2, 0,
        2, 0, 1, 0, 3, 0, 2, 2, 2, 0, 1, 1, 1, 1, 1, 1,
        0, 0, 1, 0, 3, 2, 2, 2, 0, 0, 0, 0, 1, 1, 1, 1,
    };

    for (unsigned int i = 0; i < map_size.x; ++i){
        for (unsigned int j = 0; j < map_size.y; ++j){
            int tileNumber = level[i+j*map_size.x];
            int tu = tileNumber % (tileset.getSize().x / tile_size.x);
            int tv = tileNumber / (tileset.getSize().x / tile_size.x);
            sf::Vertex* quad = &vertices[(i+j*map_size.x)*4];

            quad[0].position = sf::Vector2f(i*tile_size.x, (j-1)*tile_size.y);
            quad[1].position = sf::Vector2f((i+1)*tile_size.x, (j-1)*tile_size.y);
            quad[2].position = sf::Vector2f((i+1)*tile_size.x, (j+1)*tile_size.y);
            quad[3].position = sf::Vector2f(i*tile_size.x, (j+1)*tile_size.y);

            quad[0].texCoords = sf::Vector2f(tu*tile_size.x, tv*tile_size.y);
            quad[1].texCoords = sf::Vector2f((tu+1)*tile_size.x, tv*tile_size.y);
            quad[2].texCoords = sf::Vector2f((tu+1)*tile_size.x, (tv+1)*tile_size.y);
            quad[3].texCoords = sf::Vector2f(tu*tile_size.x, (tv+1)*tile_size.y);
        }
    }

    sf::Shader shader;
    shader.loadFromFile("shader.frag", sf::Shader::Fragment);
    shader.setUniform("mouse_position", window.mapPixelToCoords(sf::Mouse::getPosition(window)));
    shader.setUniform("tileset", tileset);

    while (window.isOpen()){
        sf::Event event;
        while (window.pollEvent(event)){
            if(event.type == sf::Event::Closed)
                window.close();
        }
        window.clear();
        window.draw(vertices, &tileset);
        window.display();
    }
    return 0;
}


and this not-yet shader "shader.frag" file
#version 330

uniform vec2 mouse_position;
uniform sampler2D tileset;

void main(){

}
 



what i think should be the next steps:
1- identify the current visible tiles on screen
2- for each one of these, get from the tileset the corresponding tile convert the non-transparent pixels to an unique color, respecting the draw order (left to right, up to down)
3- based on the mouse position, get the current color and convert it to a final tile id.

is it possible to return a number from a glsl shader back to the sfml program? in this case, the final tile id?

Pages: 1 ... 12 13 [14] 15 16 ... 25
anything