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

Author Topic: how to draw like this?  (Read 2042 times)

0 Members and 1 Guest are viewing this topic.

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
how to draw like this?
« on: November 19, 2012, 07:38:48 pm »
How can I draw like the picture?
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>
#include <ctime>

static inline std::string int2Str(int x);

int main()
{

    int x,y;
    int red,green,blue;
    std::srand(time(NULL));
    int SCREEN_W = 640;
    int SCREEN_H = 480;
    std::string width = int2Str(SCREEN_W);
    std::string height = int2Str(SCREEN_H);
    std::string printonscr = "Pixels Program - " + width + " x " + height + " - " + "Press ESC to Quit";
    sf::RenderWindow window(sf::VideoMode(SCREEN_W,SCREEN_H), "Test");


    sf::Font font;
    if(!font.loadFromFile("arial.ttf"))
        return EXIT_FAILURE;

    sf::Text text(printonscr, font, 15);
    text.setStyle(sf::Text::Bold);
    while(window.isOpen())
    {
    x = 10 + rand() % (SCREEN_W);
    y = 10 + rand() % (SCREEN_H);
    red =  rand() % 255;
    blue = rand() % 255;
    green = rand() % 255;
    sf::Image dot;
    dot.create(10,10,sf::Color(red,blue,green));
    sf::Texture texture;
    texture.loadFromImage(dot);
    sf::Texture background;
    sf::Sprite sprite(texture);
    sprite.setPosition(x,y);

        sf::Event event;
        while(window.pollEvent(event))
        {

            if(event.type == sf::Event::Closed)
            window.close();
            if(sf::Keyboard::isKeyPressed(sf::Keyboard::Escape))
            window.close();

        }
        window.clear(sf::Color::Black);
        window.draw(sprite);
        window.draw(text);
        window.display();
    }
    return EXIT_SUCCESS;
}
static inline std::string int2Str(int x)
        {
                std::stringstream type;
                type << x;
                return type.str();
        }
 

[attachment deleted by admin]
« Last Edit: November 19, 2012, 08:49:03 pm by Laurent »

Perde

  • Jr. Member
  • **
  • Posts: 63
    • View Profile
Re: how to draw like this?
« Reply #1 on: November 19, 2012, 07:59:04 pm »
The way you're trying to achieve this would take away a lot of resources.
Better create (or load) 2-3 (I don't think you need more than three) "random noise" textures and cycle through them. Randomly creating screen-filling noise in every frame would be waaay too heavy.

So what you can do is create those frames at program start or when you need them (still, only once, and keep them in memory if you need it more often), by filling each pixel of each frame with a random value. You could also just create those textures in some image manipulation software that offers a noise-filter and load them as textures.

Then you cycle through those few frames, it's unlikely that anyone will notice that you're using the same non-random textures over and over again.

At least that's how I would do it.

P.S.
I only took a quick glance at your code, but when you call sf::Texture::loadFromImage(), you're not adding something to your Texture, but resetting it. That's why you only draw one of your dots each frame. (Also there are other ways to properly do this, but you should rather do something like mentioned above.)

[edit]
Also you should read about the difference of Events and Input using sf::Keyboard and alike, it's not supposed to be inside the event loop. I suppose the tutorials would be a good start.
« Last Edit: November 19, 2012, 08:34:19 pm by Perde »

eXpl0it3r

  • SFML Team
  • Hero Member
  • *****
  • Posts: 10815
    • View Profile
    • development blog
    • Email
Re: how to draw like this?
« Reply #2 on: November 19, 2012, 08:37:37 pm »
You can use a sf::Image and call setPixel to set a specific color to that pixel and then call loadFromImage on a texture, reference the texture in a sprite and draw it to the screen.
Repeat the first two steps every frame iteration.

But keep in mind that loadFromImage is a heavy operation, thus you'd be probably better off with some shader, but this needs some more knowledge and I think you're not ready for it atm. ;)
Official FAQ: https://www.sfml-dev.org/faq.php
Official Discord Server: https://discord.gg/nr4X7Fh
——————————————————————
Dev Blog: https://duerrenberger.dev/blog/

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Re: how to draw like this?
« Reply #3 on: November 19, 2012, 08:50:42 pm »
A raw array of pixels (sf::Uint8) + Texture::update is fast enough for real-time, as an alternative to shaders.
Laurent Gomila - SFML developer

mnajrg

  • Newbie
  • *
  • Posts: 26
    • View Profile
Re: how to draw like this?
« Reply #4 on: November 20, 2012, 04:13:34 am »
thankyou so much for your replies..
Im just trying to translate the code in game programming all in one book by james harbour and I've got trouble in here.. maybe I will just skip this step..