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

Author Topic: How to draw a buffer as a line  (Read 5444 times)

0 Members and 1 Guest are viewing this topic.

jonnybonbon

  • Newbie
  • *
  • Posts: 2
    • View Profile
How to draw a buffer as a line
« on: October 10, 2021, 01:20:08 am »
I am going through the Lodev Raycaster tutorials, and I am up to texturing the walls, and it goes through a process of making a buffer, although from what I have found there isn't any way for me to draw a buffer. Is there any way that I can? Or can I convert it to something drawable?

jonnybonbon

  • Newbie
  • *
  • Posts: 2
    • View Profile
Re: How to draw a buffer as a line
« Reply #1 on: October 10, 2021, 01:28:24 am »
Oh yeh, here is the code:

Drawing Line:
sf::Color color(255, 255, 0);

            sf::Vertex line[2] =
            {
                sf::Vertex(sf::Vector2f(x, drawStart), color),
                sf::Vertex(sf::Vector2f(x, drawEnd), color)
            };
            window.draw(line , 2, sf::Lines);
Colouring Buffer:
//texturing calculations
            int texNum = map[mapX][mapY] - 1;

            //calculate value of wallX
            double wallX; //where exactly the wall was hit
            if(side==0) wallX = posY + perpWallDist * rayDirY;
            else        wallX = posX + perpWallDist * rayDirX;
            wallX -= floor((wallX));

            //x coordinate on the texture
            int texX = int(wallX * double(texWidth));
            if(side == 0 && rayDirX > 0) texX = texWidth - texX - 1;
            if(side == 1 && rayDirY < 0) texX = texWidth - texX - 1;

            double step = 1.0 * texHeight / lineHeight;
            //starting texture coordinate
            double texPos = (drawStart - h / 2 + lineHeight / 2) * step;
            for(int y=drawStart; y<drawEnd; y++){
                int texY = (int)texPos & (texHeight - 1);
                texPos += step;
                uint32_t color = texture[texNum][texHeight * texY + texX];

                if(side==1) color = (color >> 1) & 8255711;
                buffer[y][x] = color;

            }
Setting Textures:
for(int x=0; x<texWidth; x++)
    {
        for(int y=0; y<texHeight; y++)
        {
            int xorcolor = (x * 256 / texWidth) ^ (y * 256 / texHeight);
            int ycolor = y * 256 / texHeight;
            int xycolor = y * 128 + x * 128/ texWidth;
            texture[0][texWidth * y + x] = 65536 * 254 * (x!= y && x != texWidth - y);
            texture[1][texWidth * y + x] = xycolor + 256 * xycolor + 65536 * xycolor;
            texture[2][texWidth * y + x] = 256 * xycolor + 65536 * xycolor; //sloped yellow gradient
            texture[3][texWidth * y + x] = xorcolor + 256 * xorcolor + 65536 * xorcolor; //xor greyscale
            texture[4][texWidth * y + x] = 256 * xorcolor; //xor green
            texture[5][texWidth * y + x] = 65536 * 192 * (x % 16 && y % 16); //red bricks
            texture[6][texWidth * y + x] = 65536 * ycolor; //red gradient
            texture[7][texWidth * y + x] = 128 + 256 * 128 + 65536 * 128; //flat grey texture
        }
    }

fallahn

  • Hero Member
  • *****
  • Posts: 507
  • Buns.
    • View Profile
    • Trederia
Re: How to draw a buffer as a line
« Reply #2 on: October 10, 2021, 04:56:44 pm »
Assuming your buffer is a vector or an array of bytes you can use sf::Texture::update()

https://www.sfml-dev.org/documentation/2.5.1/classsf_1_1Texture.php#ae4eab5c6781316840b0c50ad08370963

to upload it to a texture.