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

Author Topic: Drawing pixel  (Read 23364 times)

0 Members and 1 Guest are viewing this topic.

sim642

  • Newbie
  • *
  • Posts: 2
    • View Profile
Drawing pixel
« on: August 03, 2010, 10:10:52 am »
Hi!

Is there some way to draw individual pixels or points? I can't seem to find anything for it in the docs.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing pixel
« Reply #1 on: August 03, 2010, 10:13:34 am »
It depends. If you don't care about performances, you can draw a 1x1 sprite or rectangle. If you want optimal performances, you must use an off-screen buffer (sf::Image) to write pixels, and then display it with a screen-wide sprite.
Laurent Gomila - SFML developer

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Drawing pixel
« Reply #2 on: December 19, 2010, 12:15:26 am »
Laurent,

I was going back through some Allegro files I had written from a tutorial book a long time ago and looked at their putPixel function. Is it simply a matter of taste that you decided not to include a similar function in SFML that didn't require a buffer, or is the same amount of work most likely being done by Allegro as SFML, just with more of a straightforward wrapper? Thank you for your time.

Colton

EDIT:

Also, I noticed that using the buffer is actually much slower than drawing Rectangles that are 1 x 1 pixel in size. Perhaps I'm doing it wrong? Here's my functional code:

Code: [Select]

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

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(640, 480), "SFML Graphics");

// Buffer
sf::Image buffer(640, 480, sf::Color(0, 0, 0));

// Sprite buffer
sf::Sprite bufferSprite(buffer);

// Random seed
srand(10);

int randX, randY;
int r, g, b;

App.Clear();

    // Start game loop
    while (App.IsOpened())
    {
        // Process events
        sf::Event Event;
        while (App.GetEvent(Event))
        {
            // Close window : exit
            if (Event.Type == sf::Event::Closed)
                App.Close();
        }

        randX = rand() % 640;
randY = rand() % 480;

r = rand() % 255;
g = rand() % 255;
b = rand() % 255;

buffer.SetPixel(randX, randY, sf::Color(r, g, b));

App.Draw(bufferSprite);

        // Finally, display the rendered frame on screen
        App.Display();
    }

    return EXIT_SUCCESS;
}


The Allegro putPixel function draws the pixels at lightning speed, so I'd like to know if this would be possible somehow with SFML :]
Whether you think you can or you can't, you're right.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Drawing pixel
« Reply #3 on: December 19, 2010, 08:35:15 am »
I don't know how Allegro renders internally, but I guess that it's pure software rendering like SDL, except maybe for the most recent version. So, writing a pixel is as simple as changing the value of a variable in an array.

SFML is different, it uses hardware acceleration and thus every modification has to make its way from system RAM to video memory first. Your example is not relevant, you're drawing only one rectangle so of course using a sf::Shape will be faster than re-uploading an entire image to the graphics card. But with thousands of points (and using a raw array instead of Image::SetPixel) it would be equally fast.

By the way, the best technique is to use OpenGL to render points directly. There should be an API for doing this in SFML 2.
Laurent Gomila - SFML developer

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Drawing pixel
« Reply #4 on: December 19, 2010, 08:49:43 am »
I think it is software rendering, yes. I'm trying to do a cross comparison of SDL and SFML because there are a bunch of tutorials available for SDL that I wanted to try with SFML.

Quote

By the way, the best technique is to use OpenGL to render points directly. There should be an API for doing this in SFML 2.


Sweet. I'm going to be embarking on the quest of learning OpenGL sometime in the future :] Should be lotsa fun.
Whether you think you can or you can't, you're right.