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

Author Topic: Pointer to Rectangle  (Read 5501 times)

0 Members and 1 Guest are viewing this topic.

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Pointer to Rectangle
« on: December 20, 2010, 01:31:57 pm »
Hello, everybody :]

Is it possible to make a pointer to an sf::Shape::Rectangle object? I know it's a static function, but I'm not altogether familiar with static functions to know what can and cannot be done with them.... I just know that you can access their data without one being instantiated.

My idea was to create a pointer to a single Rectangle (which is to be 1 x 1 in size so as to emulate drawing a pixel) and then point to it using a pointer in the Draw function in my program to randomly redraw it a bunch of times on the screen. What I have as my original idea is this, without pointers:

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(640, 480), "SFML Graphics",
                    sf::Style::Fullscreen);
int pointCounter = 0;
int randX, randY;

sf::Clock pointTimer;

App.Clear();
pointTimer.Reset();

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

randX = sf::Randomizer::Random(0, 640);
randY = sf::Randomizer::Random(0, 480);

App.Draw(sf::Shape::Rectangle(randX, randY, randX + 1, randY + 1,
        sf::Color(sf::Randomizer::Random(0, 255),
          sf::Randomizer::Random(0, 255),
  sf::Randomizer::Random(0, 255))));

pointCounter += 1;

if (pointTimer.GetElapsedTime() >= 10)
{
  cout << "Points Drawn 10 Sec: " << pointCounter << endl;
  return EXIT_SUCCESS;
   }

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

    return EXIT_SUCCESS;
}


It lets you see how many of the Rectangles were drawn through the console in 10 seconds, and I'm at about 23,000 while in fullscreen and 12,000 in windowed mode. It was my hope that I could increase this number using a pointer to a single Rectangle and then refer to it and adjust its properties every iteration instead of drawing a brand new one. Any thoughts or possible suggestions, or any information as to whether this idea is even plausible to begin with? :] Thank you all very much!

Colton
Whether you think you can or you can't, you're right.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pointer to Rectangle
« Reply #1 on: December 20, 2010, 01:43:24 pm »
Very simple
Code: [Select]
sf::Shape rect = sf::Shape::Rectangle(...);
...
window.Draw(rectangle);
Laurent Gomila - SFML developer

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Pointer to Rectangle
« Reply #2 on: December 20, 2010, 01:56:43 pm »
Ahhh, okay, thank you :] That much makes sense, but that's an object and not a pointer, isn't it? I'm not even sure if what I want to do will work anymore, but I'm working on it right now and trying to figure it out. :p

Once I've initialized the Rectangle object, how can I alter its dimensions again? I know I can use SetPosition to affect its coordinates, but I don't know how to access its width and height parameters.
Whether you think you can or you can't, you're right.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pointer to Rectangle
« Reply #3 on: December 20, 2010, 02:28:40 pm »
Quote
That much makes sense, but that's an object and not a pointer, isn't it? I'm not even sure if what I want to do will work anymore, but I'm working on it right now and trying to figure it out. :p

Why do you want a pointer? You just want to store the object in a variable so that it is not reconstructed every time.

Quote
Once I've initialized the Rectangle object, how can I alter its dimensions again? I know I can use SetPosition to affect its coordinates, but I don't know how to access its width and height parameters.

You can't. sf::Shape::Rectangle is just a helper function that builds a rectangle, but after that it is treated like a convex polygon -- like all other shapes. So you have no choice but reconstructing it every time you want to change the way the shape is built.
But to change a rectangle's dimension, you can rather use the scale (build the rectangle as 1x1 and set its width/height in scale).
Laurent Gomila - SFML developer

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Pointer to Rectangle
« Reply #4 on: December 20, 2010, 02:38:27 pm »
Quote

Why do you want a pointer? You just want to store the object in a variable so that it is not reconstructed every time.


You're right, haha. I was confusing myself, but I understand now.

And as for the Rectangle itself and trying to adjust its dimensions, I discovered that doing all of this and trying to save time and make the program better is actually making it run a tiny bit slower :p I have it nearly finished with how I would like for it to be, but it's not drawing them visibly. It's enough to let me know how many it's drawing in 10 seconds though, and it's almost 1000 lower than the original. What a shame :[

Thanks for your help! :][/quote]
Whether you think you can or you can't, you're right.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pointer to Rectangle
« Reply #5 on: December 20, 2010, 02:42:48 pm »
If all you did is storing the shape in a variable, and get a lower framerate, you certainly did something wrong ;)
Laurent Gomila - SFML developer

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Pointer to Rectangle
« Reply #6 on: December 20, 2010, 10:10:36 pm »
Quote from: "Laurent"
If all you did is storing the shape in a variable, and get a lower framerate, you certainly did something wrong ;)


Perhaps I did. Would you mind taking a look at my code and telling me what I could do to get my desired results? :]

Code: [Select]

#include <SFML/Graphics.hpp>
#include <iostream>

using namespace std;

int main()
{
    // Create main window
    sf::RenderWindow App(sf::VideoMode(640, 480), "SFML Graphics",
                    sf::Style::Fullscreen);
int pointCounter = 0;
int randX, randY, r, g, b;

sf::Clock pointTimer;
sf::Shape rect = sf::Shape::Rectangle(0, 0, 1, 1, sf::Color(0, 0, 0));

App.Clear();
pointTimer.Reset();

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

randX = sf::Randomizer::Random(0, 640);
randY = sf::Randomizer::Random(0, 480);
r = sf::Randomizer::Random(0, 255);
g = sf::Randomizer::Random(0, 255);
b = sf::Randomizer::Random(0, 255);

rect.SetPosition(randX, randY);
rect.SetColor(sf::Color(r, g, b));

App.Draw(rect);

pointCounter += 1;

if (pointTimer.GetElapsedTime() >= 10)
{
  cout << "Points Drawn 10 Sec: " << pointCounter << endl;
  return EXIT_SUCCESS;
   }

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

    return EXIT_SUCCESS;
}


Thank ya, sir. :]
Whether you think you can or you can't, you're right.

Terrydil

  • Jr. Member
  • **
  • Posts: 51
    • View Profile
Pointer to Rectangle
« Reply #7 on: December 20, 2010, 10:40:05 pm »
I'm actually getting more points drawn in 10 sec with the changed code you posted than with the original.  Perhaps your cpu was just doing something else when you tested the changed version.  Since its basically trying to draw as many points as possible, if your cpu is distracted you'll get a lower result.

Either way, seems like the performance is fine and the code looks ok to me.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pointer to Rectangle
« Reply #8 on: December 20, 2010, 10:40:32 pm »
This code looks ok. But you shouldn't focus on such details, write a real application an optimize real problems -- sample codes are often far from real life ;)

By the way, sf::Shape is clearly overkill for drawing points. There's no easy way to draw points efficiently in SFML 1; using an image would probably be faster beyond a certain amount of points.
Laurent Gomila - SFML developer

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Pointer to Rectangle
« Reply #9 on: December 20, 2010, 11:07:33 pm »
Quote

This code looks ok. But you shouldn't focus on such details, write a real application an optimize real problems -- sample codes are often far from real life Wink


Of course... and I intend to :] But I was just reading through a book and going back to Allegro programs and trying to do some comparisons. How fast is rendering points with OpenGL?

Quote

By the way, sf::Shape is clearly overkill for drawing points. There's no easy way to draw points efficiently in SFML 1; using an image would probably be faster beyond a certain amount of points.


It ain't too big a deal.... just experimenting and having fun while trying to learn is all :] I probably realistically wouldn't use this in a real program, anyways. Thank you for the assistance :]
Whether you think you can or you can't, you're right.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pointer to Rectangle
« Reply #10 on: December 20, 2010, 11:11:36 pm »
Quote
How fast is rendering points with OpenGL?

You can basically fill the entire screen with individual points at decent framerates.
Laurent Gomila - SFML developer

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Pointer to Rectangle
« Reply #11 on: December 20, 2010, 11:21:23 pm »
Quote

You can basically fill the entire screen with individual points at decent framerates.


Do you have an estimate as to what that would equate to points per second?
Whether you think you can or you can't, you're right.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pointer to Rectangle
« Reply #12 on: December 21, 2010, 07:35:30 am »
Quote
Do you have an estimate as to what that would equate to points per second?

No, really... that depends on your graphics card, GPU, window resolution, etc. Don't expect global statements such as "OpenGL can render N million points per second".
Laurent Gomila - SFML developer

Noegddgeon

  • Jr. Member
  • **
  • Posts: 53
    • View Profile
    • Soundcloud
Pointer to Rectangle
« Reply #13 on: December 21, 2010, 08:42:54 am »
Quote

No, really... that depends on your graphics card, GPU, window resolution, etc. Don't expect global statements such as "OpenGL can render N million points per second".


That's very true. I should have known better :p Would you recommend I work on learning OpenGL now or keep working with SFML and developing graphical applications? My goal is to become adept at programming in 2D and 3D, but I understand that you must learn to walk before you can run, unlike many people who come to the table believing there's an easy way to start making MMORPG's right off the bat :p And I do very much desire understanding how graphical applications work on as low a level as OpenGL at some point.
Whether you think you can or you can't, you're right.

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32504
    • View Profile
    • SFML's website
    • Email
Pointer to Rectangle
« Reply #14 on: December 21, 2010, 08:46:36 am »
That sounds wise :)

I'd say, get familiar with graphics programming first (either 2D or 3D), and once you're confident enough you can start diving in the low-level of OpenGL. Should be easier than starting directly with it, I think.
Laurent Gomila - SFML developer

 

anything