- SFML Game Development By Example
- Raimondas Pupius
- 536字
- 2021-07-23 14:55:04
Basics of SFML drawing
Much like in kindergarten, we will start with basic shapes and make our way up to more complex types. Let's work on rendering a rectangle shape by first declaring it and setting it up:
sf::RectangleShape rectangle(sf::Vector2f(128.0f,128.0f)); rectangle.setFillColor(sf::Color::Red); rectangle.setPosition(320,240);
sf::RectangleShape
is a derived class of sf::Shape
that inherits from sf::Drawable
, which is an abstract base class that all entities must inherit from and implement its virtual methods in order to be able to be drawn on screen. It also inherits from sf::Transformable
, which provides all the necessary functionality in order to move, scale, and rotate an entity. This relationship allows our rectangle to be transformed, as well as rendered to the screen. In its constructor, we've introduced a new data type: sf::Vector2f
. It's essentially just a struct of two floats, x and y, that represent a point in a two-dimensional universe, not to be confused with the std::vector
, which is a data container.
Tip
SFML provides a few other vector types for integers and unsigned integers: sf::Vector2i
and sf::Vector2u
. The actual sf::Vector2
class is templated, so any primitive data type can be used with it like so:
sf::Vector2<long> m_vector;
The rectangle constructor takes a single argument of sf::Vector2f
which represents the size of the rectangle in pixels and is optional. On the second line, we set the fill color of the rectangle by providing one of SFML's predefined colors this time. Lastly, we set the position of our shape by calling the setPosition
method and passing its position in pixels alongside the x and y axis, which in this case is the centre of our window. There is only one more thing missing until we can draw the rectangle:
window.draw(rectangle); // Render our shape.
This line goes right before we call window.display();
and is responsible for bringing our shape to the screen. Let's run our revised application and take a look at the result:

Now we have a red square drawn on the screen, but it's not quite centered. This is because the default origin of any sf::Transformable
, which is just a 2D point that represents the global position of the object, is at the local coordinates (0,0), which is the top left corner. In this case, it means that the top left corner of this rectangle is set to the position of the screen centre. That can easily be resolved by calling the setOrigin
method and passing in the desired local coordinates of our shape that will represent the new origin, which we want to be right in the middle:
rectangle.setOrigin(64.0f,64.0f);
If the size of a shape is unknown for whatever reason, the rectangle class provides a nice method getSize
, which returns a float vector containing the size:
rectangle.setOrigin(rectangle.getSize().x / 2, rectangle.getSize().y / 2);
Now our shape is sitting happily in the very middle of the black screen. The entire segment of code that makes this possible looks a little something like this:
#include <SFML/Graphics.hpp> void main(int argc, char** argv[]){ sf::RenderWindow window(sf::VideoMode(640,480), "Rendering the rectangle."); // Creating our shape. sf::RectangleShape rectangle(sf::Vector2f(128.0f,128.0f)); rectangle.setFillColor(sf::Color::Red); rectangle.setPosition(320,240); rectangle.setOrigin(rectangle.getSize().x / 2, rectangle.getSize().y / 2); while(window.isOpen()){ sf::Event event; while(window.pollEvent(event)){ if(event.type == sf::Event::Closed){ // Close window button clicked. window.close(); } } window.clear(sf::Color::Black); window.draw(rectangle); // Drawing our shape. window.display(); } }
- Data Visualization with D3 4.x Cookbook(Second Edition)
- Vue.js 3.x快速入門
- ASP.NET程序設(shè)計教程
- Python Web數(shù)據(jù)分析可視化:基于Django框架的開發(fā)實戰(zhàn)
- Python深度學(xué)習:模型、方法與實現(xiàn)
- Oracle GoldenGate 12c Implementer's Guide
- Scratch趣味編程:陪孩子像搭積木一樣學(xué)編程
- HTML+CSS+JavaScript網(wǎng)頁設(shè)計從入門到精通 (清華社"視頻大講堂"大系·網(wǎng)絡(luò)開發(fā)視頻大講堂)
- HTML5移動前端開發(fā)基礎(chǔ)與實戰(zhàn)(微課版)
- Learning Concurrency in Python
- Drupal Search Engine Optimization
- 寫給青少年的人工智能(Python版·微課視頻版)
- Enterprise Application Architecture with .NET Core
- Roslyn Cookbook
- Selenium Essentials