SFML community forums
Help => Graphics => Topic started by: wvtrammell on February 18, 2011, 05:15:05 pm
-
#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
-
The documentation is your friend: http://www.sfml-dev.org/documentation/1.6/classsf_1_1Image.htm#b7590e372a153ace848cf8a6081c8bc4
-
Thank you for pointing me to the documentation.
With my limited experience I have no idea how to write the code I need?
Warren
-
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 ;)
-
If it is so simple, how about showing me the code?
Thanks
Warren
-
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 :)
-
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
-
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:
#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.
-
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
-
With your help I was able to come up with code that works. Thanks.
#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;
}