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

Author Topic: pixel color  (Read 10959 times)

0 Members and 1 Guest are viewing this topic.

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
pixel color
« on: February 18, 2011, 05:15:05 pm »
Code: [Select]
#include <SFML/Graphics.hpp>

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

    // 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();
        }

        // Clear screen
        App.Clear(sf::Color(128,128,255));

        // Draw apredefined shape
        App.Draw(sf::Shape::Circle(200, 200, 100, sf::Color::Yellow, 10, sf::Color::Blue));

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

    return EXIT_SUCCESS;
}

Will someone please add the lines of code required to get the color of the pixel at x = 100, y = 100?
I can not find it in the tutorials.
Thanks a bunch.
Warren

Lupinius

  • Jr. Member
  • **
  • Posts: 85
    • View Profile
pixel color
« Reply #1 on: February 18, 2011, 08:54:48 pm »

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Pixel color
« Reply #2 on: February 18, 2011, 09:48:51 pm »
Thank you for pointing me to the documentation.
With my limited experience I have no idea how to write the code I need?
Warren

Nexus

  • SFML Team
  • Hero Member
  • *****
  • Posts: 6286
  • Thor Developer
    • View Profile
    • Bromeon
pixel color
« Reply #3 on: February 18, 2011, 10:19:26 pm »
If you don't have the experience to call a simple function, it's maybe a better choice to spend some time learning C++ and to return to SFML later. Because like this, it's only a matter of time until you have the next problem. You won't make progress in your SFML applications, when you are wasting your whole time finding out how to achieve simple tasks ;)
Zloxx II: action platformer
Thor Library: particle systems, animations, dot products, ...
SFML Game Development:

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
pixel color
« Reply #4 on: February 19, 2011, 12:09:18 am »
If it is so simple, how about showing me the code?
Thanks
Warren

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
pixel color
« Reply #5 on: February 19, 2011, 12:46:53 am »
Code: [Select]

Color pixel = image.GetPixel(x, y);


The problem isn't showing you the code, what Nexus picked up on is that you don't seem to have enough knowledge to construct something yourself from the bare-scrap. Calling a function is fundamental in any programming and you have already done it several times in the code you gave us at the first post. But if you are unable to identify that then it's very clear that you haven't studied the programming field at all.

Programming is a craftwork, you have to keep working with it practically like a carpenter. But even a carpenter must have learned somewhere how to use a hammer or saw.

If we just give you the solution like I did, you won't be able to progress at all as a programmer. Because in the end you will come with the exact same problem but it's another function instead of GetPixel.

I think that summed it all up :)
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
pixel color
« Reply #6 on: February 19, 2011, 03:40:28 pm »
I really appreciate what you fellows are trying to do for me but, This October I will be eighty eight years young and I doubt if I will ever be a programmer in any language.  I am able to sit at the keyboard and type in simple code and enjoy watching it execute.  If someone would just add the lines of code to the above so I can compile and execute it I would be extremely grateful.
Thanks for your time.
Warren Trammell

Groogy

  • Hero Member
  • *****
  • Posts: 1469
    • MSN Messenger - groogy@groogy.se
    • View Profile
    • http://www.groogy.se
    • Email
pixel color
« Reply #7 on: February 19, 2011, 04:25:43 pm »
Just because you are old doesn't prohibit you from learning or still evolve in a field. You still got your brain don't you? I would never allow myself to give up on something just because of the society telling me I'm too old. Wouldn't you enjoy it more if you knew that you came up with this all by yourself? Fundamentals of being good at something is simply understanding it. As soon as you understand the core of programming it's easy to create things yourself from nothing.

Anyway here it is:
Code: [Select]

#include <SFML/Graphics.hpp>

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

    // 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();
        }

        // Clear screen
        App.Clear(sf::Color(128,128,255));

        // Draw apredefined shape
        App.Draw(sf::Shape::Circle(200, 200, 100, sf::Color::Yellow, 10, sf::Color::Blue));

        // Finally, display the rendered frame on screen
        App.Display();
     
        ///////////////////////////////////////////
        // Here it starts
        ///////////////////////////////////////////
        sf::Image Image = App.Capture();
        sf::Color Pixel = Image.GetPixel(100, 100);
    }

    return EXIT_SUCCESS;
}


To explain my addition:
I first tell the window to save what it is displaying as an sf::Image. From that sf::Image I then request the color at the position 100,100.
Hope that helped.

PS: My pension plan is to lock myself in a room and just play games till the moment I die. ^^
The room will probably be a bathroom =/
Aaah sweet sweet dreams.
Developer and Maker of rbSFML and Programmer at Paradox Development Studio

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
pixel color
« Reply #8 on: February 19, 2011, 08:25:47 pm »
You are right.  I need to learn Classes before I can hope to do any programming in SFML.
I assumed the word Color in the expression "sf::Color Pixel = "
was the name of an integer variable containing the value of the color in the designated pixel.
Boy was I ever wrong.
Warreni

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
pixel color
« Reply #9 on: February 20, 2011, 07:54:21 pm »
With your help I was able to come up with code that works.  Thanks.

Code: [Select]
#include <SFML/Graphics.hpp>
#include <iostream>
#include <sstream>

int main()
{
int Color;
char buffer[10];


    // Create main window
    sf::RenderWindow App(sf::VideoMode(1200, 900), "SFML Graphics");


// Load a font from a file
    sf::Font MyFont;
    if (!MyFont.LoadFromFile("/users/warren/projects/impact.ttf", 50))
        return EXIT_FAILURE;


// Clear screen
App.Clear(sf::Color(128,128,255));

// Draw apredefined shape
App.Draw(sf::Shape::Rectangle(50,50,200, 200, sf::Color(64,128,190)));

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

sf::Image Image = App.Capture();
sf::Color color = Image.GetPixel(100, 100);

// Create a graphical string
    sf::String prntr;

      sprintf(buffer,"%i",(int)color.r);
       prntr.SetText(buffer);
            prntr.SetFont(MyFont);
            prntr.SetColor(sf::Color(0, 0, 0));
            prntr.SetPosition(150, 350);
            prntr.SetSize(150);

       // Draw our strings
       App.Draw(prntr);


// Create a graphical string
    sf::String prntg;

sprintf(buffer,"%i",(int)color.g);
prntg.SetText(buffer);
prntg.SetFont(MyFont);
prntg.SetColor(sf::Color(0, 0, 0));
prntg.SetPosition(450, 350);
prntg.SetSize(150);

// Draw our strings
App.Draw(prntg);


// Create a graphical string
    sf::String prntb;

sprintf(buffer,"%i",(int)color.b);
prntb.SetText(buffer);
prntb.SetFont(MyFont);
prntb.SetColor(sf::Color(0, 0, 0));
prntb.SetPosition(750, 350);
prntb.SetSize(150);

// Draw our strings
App.Draw(prntb);

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


    // 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();
        }

 

    }

    return EXIT_SUCCESS;
}