Getting started with SFML 2.0 and C++

Hi guys , in this article my purpose is to show you what you can do with a library like SFML , of course out there are a lot of other libraries. But I chose to make this article about SFML ( Simple and Fast Multimedia Library ) , because is one of my favorite , is very simple to use and you can understand it even if you are not an advanced programmer.

First of all, what is a library?

–  Well, a library is a collection of classes , interfaces , functions and other stuff that helps you ( in this case ) : to open a window , draw a rectangle there or any kind of geometric figure that you want , you can move that rectangle , you can draw a circle and then make them dance , and a lot of other crazy things.
The best thing of all , is that you don’t need to know how that rectangle is drawn , you don’t need to know all the logical stuff that is behind . Of course if you like it , you can learn all that “core” things .But today, we will limit ourselves to the simple things.

Step One

You need go at this link : http://www.sfml-dev.org/tutorials/2.0 , and depending on what IDE ( Integrated development environment ) you use, integrate SFML 2.0 into your project.

Step Two : Code

How to open/close a window.

#include <SFML\Graphics.hpp>


int main()
{
	sf::RenderWindow lv_Window(sf::VideoMode(800,600), "Hello");


	while(lv_Window.isOpen())
	{
		sf::Event event;
        while (lv_Window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
			{
                lv_Window.close();
			}
			if (event.key.code == sf::Keyboard::Escape)
			{
				lv_Window.close();
			}
        }
		lv_Window.display();
	}

    return 0;
}

As you can see is a very simple job to open and close a window. Maybe only that “event” is a little confusing . I will try to explain it , an event is like a trigger, when an “event” condition is true we do something . In this case when we press ESC we want to close the window , same when we press Close. Let’s move to the next example.

How to draw something on the screen.

#include <SFML\Graphics.hpp>


int main()
{
	sf::RenderWindow lv_Window(sf::VideoMode(800,600), "Hello");
	sf::RectangleShape lv_Rect;
	sf::CircleShape lv_Circle;

	// Rectangle 
	lv_Rect.setFillColor(sf::Color::Blue);
	lv_Rect.setSize(sf::Vector2f(100,100));
	lv_Rect.setPosition(50,50);

	//Circle
	lv_Circle.setFillColor(sf::Color::Yellow);
	lv_Circle.setRadius(50);
	lv_Circle.setPosition(650,450);

	while(lv_Window.isOpen())
	{
		sf::Event event;
        while (lv_Window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
			{
                lv_Window.close();
			}
			if (event.key.code == sf::Keyboard::Escape)
			{
				lv_Window.close();
			}
        }

		lv_Window.draw(lv_Rect);
		lv_Window.draw(lv_Circle);

		lv_Window.display();
	}

    return 0;
}

Look, is so simple to add a rectangle or a circle to the screen , is just logic to set position , width , height or radius.

How to move objects and check for triggers.

#include <SFML\Graphics.hpp>
#include <SFML\System.hpp>
#include <stdlib.h>
#include <iostream>

using std::cout;
using std::endl;

bool lf_bCheckForMouseTrigger(sf::RectangleShape &av_Sprite, sf::RenderWindow &av_Window)
{
	
	int mouseX = sf::Mouse::getPosition().x;
	int mouseY = sf::Mouse::getPosition().y;
	
	sf::Vector2i windowPosition = av_Window.getPosition();

	if(mouseX > av_Sprite.getPosition().x + windowPosition.x && mouseX < ( av_Sprite.getPosition().x + av_Sprite.getGlobalBounds().width + windowPosition.x)
		&& mouseY > av_Sprite.getPosition().y + windowPosition.y + 30  && mouseY < ( av_Sprite.getPosition().y + av_Sprite.getGlobalBounds().height + windowPosition.y + 35) )
	{
		if(sf::Mouse::isButtonPressed(sf::Mouse::Left))
		{
			return true;
		}
		return false;
	}
	
	return false;

}



int main()
{
	sf::RenderWindow lv_Window(sf::VideoMode(800,600), "Hello");
	sf::RectangleShape lv_Rect;
	sf::CircleShape lv_Circle;

	// Rectangle 
	lv_Rect.setFillColor(sf::Color::Blue);
	lv_Rect.setSize(sf::Vector2f(100,100));
	lv_Rect.setPosition(50,50);

	//Circle
	lv_Circle.setFillColor(sf::Color::Yellow);
	lv_Circle.setRadius(50);
	lv_Circle.setPosition(650,450);

	while(lv_Window.isOpen())
	{
		sf::Event event;
        while (lv_Window.pollEvent(event))
        {
            if (event.type == sf::Event::Closed)
			{
                lv_Window.close();
			}
			if (event.key.code == sf::Keyboard::Escape)
			{
				lv_Window.close();
			}

			if(event.key.code == sf::Keyboard::Space)
			{
				lv_Rect.move(0,5);
				unsigned long lv_ldTime = 50;
				_sleep(lv_ldTime);
				cout << "You press Space!!" << endl;
			}
		}


		if(lf_bCheckForMouseTrigger(lv_Rect, lv_Window))
		{
			lv_Circle.move(-5,0);
			unsigned long lv_ldTime = 50;
			_sleep(lv_ldTime);
			cout << "Rectangle has been clicked" << endl;
		}


		lv_Window.draw(lv_Rect);
		lv_Window.draw(lv_Circle);
		lv_Window.display();
		lv_Window.clear();
	}

    return 0;
}

I really hope that this article has been useful for you . And of course if you have any questions I will answer you as quick as I can.
Enjoy.

Author: Horațiu Condrea

My name is Horațiu Condrea, and I work as a Software Developer Manager at Siemens PLM Software. I hope that my experiments will prove to be useful for many of you guys/girls out there. Don’t forget to leave a comment whenever you run over a bug or something that is not working as it should be. For any kind of information contact me.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.