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

Author Topic: Drawing with shape and color objects.  (Read 1315 times)

0 Members and 1 Guest are viewing this topic.

Pixel_Outlaw

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Drawing with shape and color objects.
« on: December 15, 2010, 09:35:23 am »
First let me say that SFML will be my first venture into multimedia with C++. I'm a college student and I have toyed with C++ off and on for some time. SMFL presents an exciting way to actually DO something with a programming language.  :D

Now I have looked through the tutorials a bit and have been adding and playing with the different functions. My biggest question is the use of the graphics objects and the color objects. Am I supposed to make a new graphics object and color object every time I want to draw something? Isn't this quite a lot of overhead? I guess I'm used to the state machine approach where the set color affects all following routines untill changed.

Here is my code, please tell me if I am abusing something here.

Code: [Select]


#include <SFML\System.hpp>
#include <SFML\Window.hpp>
#include <SFML\Graphics.hpp>


int main()
{
// Create the main rendering window
sf::RenderWindow App(sf::VideoMode(800, 600, 32), "SFML Graphics");
// use a set framerate
App.UseVerticalSync(true);
App.SetFramerateLimit(60);

// incrimenter
float inc = 0;

// 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 the screen (fill it with blue color)
App.Clear(sf::Color(0,0,64));
inc += .25; // move the counter
if(inc >= 360)
{
inc -= 360;
}

// wavy warp effect
float step = 0;
for(float i = 0; i<360; i += 3)
{
step = i/32;
sf::Color my_color(0,255 - i/360 * 255, i/360 * 255);
sf::Shape my_line = sf::Shape::Line(cos(inc+step)*32+32, i, cos(inc+step)*32+480, i, 1, my_color, false, my_color);
App.Draw(my_line);
}
// Display window contents on screen

App.Display();
}

return EXIT_SUCCESS;
}

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Drawing with shape and color objects.
« Reply #1 on: December 15, 2010, 09:45:01 am »
Usually you don't have to create every graphical object from scratch at every iteration, you can initialize them at startup and then just change their properties when needed.

However your code is fine, because what you draw is entirely dynamic and really needs to be re-created at every iteration.
Laurent Gomila - SFML developer

Pixel_Outlaw

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Drawing with shape and color objects.
« Reply #2 on: December 15, 2010, 10:18:44 am »
OK thanks for the great library and help.  Would much be gained from using a single line object and reassigning the x and y values inside each for loop step? I'm just curious...

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Drawing with shape and color objects.
« Reply #3 on: December 15, 2010, 10:34:20 am »
Quote
Would much be gained from using a single line object and reassigning the x and y values inside each for loop step? I'm just curious...

You can't do that.
sf::Shape::Line is just a helper function, internally you don't have an actual line but rather a convex polygon. So you have to re-build it if you change its coordinates.
Laurent Gomila - SFML developer