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

Author Topic: Please evaluate my code for correctness + Visual C++ 2010  (Read 1739 times)

0 Members and 1 Guest are viewing this topic.

Pixel_Outlaw

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Please evaluate my code for correctness + Visual C++ 2010
« on: December 18, 2010, 08:47:56 am »
I've been hard at work trying to learn different functions and such. For some odd reason my computer is running this demonstration at around 45 frames per second according to FRAPs. Now, I'm using Visual C++ 2010 in debug mode. I watched the installation video on Youtube to recompile the 2008 files.

I have several beginners questions.
1. How do I create a release version of a program with SFML? Currently I just use the -s-d extensions as shown in the video.

2. Any ideas why this runs at 45 frames per second when I have locked it to 60? (Windows XP Service pack 3, 2 GB RAM, AMD Athlon 64 processor.)

3. Please let me know any bad habits I have started here while trying to teach myself this library.

Code: [Select]


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



class Player
{
public:
//positions
int x;
int y;
// for grabbing parent window events
sf::RenderWindow * parent_window;

Player(int start_x, int start_y, sf::RenderWindow * window)
{
x = start_x;
y = start_y;
parent_window = window;
}

void update()
{
// move the player in the window
const sf::Input& input = parent_window->GetInput();


// use the sums of the key directions to figure out movement
x -= (input.IsKeyDown(sf::Key::Left)-input.IsKeyDown(sf::Key::Right)) * 8;
y -= (input.IsKeyDown(sf::Key::Up)-input.IsKeyDown(sf::Key::Down)) * 8;

sf::Shape my_shape = sf::Shape::Circle(x,y, 16, sf::Color(255,0,255));
parent_window->Draw(my_shape);

}

~Player(){};
};

int main()
{

sf::RenderWindow main_window(sf::VideoMode(800, 600, 32), "Demo");
main_window.SetFramerateLimit(60);
main_window.UseVerticalSync(true);

Player the_player(400,300,&main_window);

while(main_window.IsOpened())
{
main_window.Clear();
sf::Event main_window_event;

// Process events
        while (main_window.GetEvent(main_window_event))
        {
            // Close window : exit
            if (main_window_event.Type == sf::Event::Closed)
{
                main_window.Close();
}
        }

// update the player
the_player.update();

        // Display window contents on screen
        main_window.Display();
}

    return 0;
}


Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Please evaluate my code for correctness + Visual C++ 2010
« Reply #1 on: December 18, 2010, 09:05:16 am »
Quote
1. How do I create a release version of a program with SFML? Currently I just use the -s-d extensions as shown in the video.

Select "Release" in the configuration drop-down list, and link to -s SFML libraries (not -s-d).

Quote
2. Any ideas why this runs at 45 frames per second when I have locked it to 60? (Windows XP Service pack 3, 2 GB RAM, AMD Athlon 64 processor.)

Running in debug mode doesn't help. Then, never mix framerate limit and vertical sync, it don't like each other.
Also make sure that your graphics drivers are up-to-date. You could also create your shape in the constructor, and then change its position with SetPosition, instead of re-creating it everytime.

Quote
3. Please let me know any bad habits I have started here while trying to teach myself this library.

Looks good so far. You should separate update() and draw() in Player, and make the latter const.
Laurent Gomila - SFML developer

Pixel_Outlaw

  • Jr. Member
  • **
  • Posts: 50
    • View Profile
Please evaluate my code for correctness + Visual C++ 2010
« Reply #2 on: December 18, 2010, 09:43:07 am »
Thanks again for your dedication.

I'm still trying to get a handle on how the events are bound to the window. If an object needs to check for an event they must first go through the window correct?

Does this mean that every game element that checks for events needs to have a pointer or reference to an event generating window?  :idea:

Laurent

  • Administrator
  • Hero Member
  • *****
  • Posts: 32498
    • View Profile
    • SFML's website
    • Email
Please evaluate my code for correctness + Visual C++ 2010
« Reply #3 on: December 18, 2010, 09:51:06 am »
Usually, evant handling is done the same way as update() and draw(): it is called from the main loop.
Code: [Select]
class Player
{
    void handle_event(const sf::Event& event)
    {
        ...
    }

    ...
};

// main loop...
{
    sf::Event main_window_event;

    // Process events
    while (main_window.GetEvent(main_window_event))
    {
        // Close window : exit
        if (main_window_event.Type == sf::Event::Closed)
            main_window.Close();
        else
            player.handle_event(main_window_event);
    }

If objects of different types need to handle events, you can write a EventHandler base class that has handle_event as a pure virtual.
Laurent Gomila - SFML developer

 

anything