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

Author Topic: Variables in App.Draw  (Read 2938 times)

0 Members and 1 Guest are viewing this topic.

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Variables in App.Draw
« on: June 05, 2010, 05:30:16 pm »
May I use a variablet o define the color instead of the actual Color in the App.Draw Circle command?
Warren

TheBoff

  • Newbie
  • *
  • Posts: 10
    • View Profile
Variables in App.Draw
« Reply #1 on: June 05, 2010, 09:07:59 pm »
If I am reading this correctly, then yes it is perfectly possible to say (for example)

Code: [Select]

sf::Color red(255, 0, 0);
sf::Shape circle = sf::Shape::Circle(0, 0, 20, red);

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Variables in App.Draw
« Reply #2 on: June 06, 2010, 10:22:36 pm »
Thanks for your advice. I guess I did not program correctly for I get an error saying "red is not a recognized color.  
Apparently I am not defining the variable name I am using correctly?
Waarren

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Variables in App.Draw
« Reply #3 on: June 10, 2010, 05:49:49 pm »
When I finally defined the variable properly it works great,
Thanks for your help.

wvtrammell

  • Jr. Member
  • **
  • Posts: 70
    • View Profile
Variables in App.Draw
« Reply #4 on: June 29, 2010, 09:53:30 pm »
This worked great so I decided to use it in an IF statement (enclosed).
It compiles but is ignored in execution.
What am I doing wrong please?
Warren

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

int main()
{
int x = 150, y = 450, random = 5;

    // Create main window
    sf::RenderWindow App(sf::VideoMode(1400, 900), "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();

    //Escape key exit
    if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::Escape))
    App.Close();
   
//F1 key pressed
    if((Event.Type == sf::Event::KeyPressed) && (Event.Key.Code == sf::Key::F1))
    App.Close();
}

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

sf::Color grn(0,255,0);
if(random == 5)
{
sf::Color grn(255,215,0);
}

App.Draw(sf::Shape::Circle(x,y,50,sf::Color(grn)));

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

    return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Variables in App.Draw
« Reply #5 on: June 29, 2010, 10:51:53 pm »
Here you define a new variable with the same name, but which lives only in the scope of your if {}.

Code: [Select]
     sf::Color grn(0,255,0);  
      if(random == 5)
      {
         grn = sf::Color(255,215,0);
      }
   
      App.Draw(sf::Shape::Circle(x,y,50,grn));
Laurent Gomila - SFML developer

 

anything