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

Author Topic: [Solved] Drawing a 2D Pixel matrix  (Read 5197 times)

0 Members and 2 Guests are viewing this topic.

Littlebaby

  • Newbie
  • *
  • Posts: 4
    • View Profile
[Solved] Drawing a 2D Pixel matrix
« on: January 27, 2013, 01:31:44 am »
(Im so bad at English, I will try my best)

A little bit of context:
I'm new to using graphic libraries. I have never used any libraries appart from the STL ones before. I started with SFML a couple of days ago, I managed to have a Tic-Tac-Toe game and a Four-in-a-line working. Now I'm planning to program something similar to the classic "Tanks" game. When I was thinking on a way to set up the collision detection functions between the tank and the terrain I realized I need a way to treat and handle the pixels of an image or a texture one by one. I red the tutorials and the documentation and I found the sf::Vertex and the possibility of drawing an array of these vertex.

I didn't consider the option of using the .setPixel and .getPixel from the sf::Image because I find them a little bit weird. What if my data is stored on a texture or on a sprite?

So in order to get used to this structure I made this minimal program, which basicly creates an array of pixels, draws a blue rectangle on it and then draws it to the window.

And here comes the problem. The program works fine but on the output, appart from the blue rectangle and the white blackground I get some weird black lines that cross the image from top to bottom, like when your computer crashes or your monitor falls of the table.

Can someone tell me what did I do wrong or why I'm getting this black lines?

Thank you.
« Last Edit: January 28, 2013, 09:44:16 pm by Littlebaby »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: Drawing a 2D Pixel matrix
« Reply #1 on: January 27, 2013, 05:26:32 pm »
You're encountering a problem that stumped me early on too.

The crux of the problem is that OpenGL coordinates are not really pixel coordinates as one might expect.  0,0 refers to a corner of a pixel at the origin.  0.5, 0.5 refers to the center of that pixel.  In order to make sure a vertex is displayed at a pixel coordinate, you draw it at the center of the pixel.

           if (i <= Alt/2+200 && i >= Alt/2-200 && j <= Amp/2+200 && j >= Amp/2-200)
                mapaBits[i][j] = sf::Vertex(sf::Vector2f(j+.5f, i+.5f), sf::Color::Cyan);
            else
                mapaBits[i][j] = sf::Vertex(sf::Vector2f(j+.5f, i+.5f), sf::Color::White);

Littlebaby

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Drawing a 2D Pixel matrix
« Reply #2 on: January 27, 2013, 06:38:50 pm »
Oh waw, I was expecting everything but this to be the problem. Thank you very much man, i guess it's time to finish my game!

Littlebaby

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: Drawing a 2D Pixel matrix
« Reply #3 on: January 28, 2013, 09:04:56 pm »
Hi, it's me again.

My previous problem was solved, but I continued my project, I found another problem and I can't find any solution.

The question is, I want to draw my pixel matrix and then over this drawing I want to draw some objects (sprites). But that doens't seem to work.
I made another minimal example to show what I mean:

int main() {
        const int H = 500;
        const int W = 1000;

        sf::RenderTexture RT;
        RT.create(W, H);

        sf::RenderWindow window(sf::VideoMode(W, H, 32), "Test");

        //Creates the sf::Vertex matrix
        sf::Vertex **pixMat = new sf::Vertex*[H];
        for (int i=0;i<H;++i)
                pixMat[i] = new sf::Vertex[W];

        //Fills the matrix with blue pixels
        for (int i=0;i<H;++i) {
                for (int j=0;j<W;++j)
                        pixMat[i][j] = sf::Vertex(sf::Vector2f(j+.5f, i+.5f), sf::Color::Blue);
        }

        //Loads the texture and creates the sf::Sprite we want to draw
        sf::Texture tex;
        tex.loadFromFile("pleasedosomequickdrawonpaint.png");
        sf::Sprite whatever(tex);
        whatever.setPosition(W/2, H/2);

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

                //Draws the pixel matrix
                for (int i=0;i<H;++i)
                        RT.draw(pixMat[i], W, sf::Points);

                //THIS IS WHAT DOESNT WORK
                RT.draw(whatever);

                //And finally draws the RenderTexture to the RenderWindow
                RT.display();
                window.draw(sf::Sprite(RT.getTexture()));
                window.display();
        }
}

The drawn sprite is not shown correctly. But I tried to change the sf::Sprite for something else, like a sf::RectangleShape, and it works. The problem seems to be the Sprite.

Thank you very much.
« Last Edit: January 28, 2013, 09:08:59 pm by Littlebaby »

cire

  • Full Member
  • ***
  • Posts: 138
    • View Profile
Re: [StillNotSolved] Drawing a 2D Pixel matrix
« Reply #4 on: January 28, 2013, 09:43:42 pm »
loadFromFile has a return value.  Have you checked it and verified the load is actually working?

Quote
The drawn sprite is not shown correctly

Can you be a little more descriptive?  Works fine for me.

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 11034
    • View Profile
    • development blog
    • Email
Re: [Solved] Drawing a 2D Pixel matrix
« Reply #5 on: January 28, 2013, 09:47:08 pm »
Works fine on my end too... ;)

Here's a bit a nicer code (as long as your compiler supports std::vector::data()):

#include <SFML/Graphics.hpp>

int main() {
    const int H = 500;
    const int W = 1000;

    sf::RenderTexture RT;
    RT.create(W, H);

    sf::RenderWindow window(sf::VideoMode(W, H, 32), "Test");
    window.setFramerateLimit(60);

    //Creates the sf::Vertex matrix
    std::vector<sf::Vertex> pixMat(H*W);
        for (int i=0;i<H;++i)
                for (int j=0;j<W;++j)
                        pixMat[(i*W)+j] = sf::Vertex(sf::Vector2f(j+.5f, i+.5f), sf::Color(255, i, j));

    //Loads the texture and creates the sf::Sprite we want to draw
    sf::Texture tex;
    tex.loadFromFile("pickone.png");
    sf::Sprite whatever(tex);
    whatever.setPosition(W/2, H/2);

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

        //Draws the pixel matrix
                RT.draw(pixMat.data(), H*W, sf::Points);

        //THIS IS WHAT DOESNT WORK
        RT.draw(whatever);

        //And finally draws the RenderTexture to the RenderWindow
        RT.display();
        window.draw(sf::Sprite(RT.getTexture()));
        window.display();
    }
}
 
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Littlebaby

  • Newbie
  • *
  • Posts: 4
    • View Profile
Re: [Solved] Drawing a 2D Pixel matrix
« Reply #6 on: January 28, 2013, 09:51:40 pm »
Quote
Can you be a little more descriptive?  Works fine for me.

Before posting this I checked the forum looking for posts with problems like mine, but I couldn't find any. Just after posting it I found some people that were having issues drawing sprites combined with other objects on computers using ATI cards. They solved it updating the Graphic drivers.
I updated my drivers and tachaaaaan it works now.

I'm sorry.

Quote
Here's a bit a nicer code (as long as your compiler supports std::vector::data()):

Oh, thanks! I didn't think about it.

And once again, sorry. I think updating your graphic drivers is the first think you do when something doesn't work.
« Last Edit: January 28, 2013, 09:58:00 pm by Littlebaby »

 

anything