Содержание блога
Скачать проект для Visual Studio: DrawText_SfmlCpp.zip (5.21 Мб). Библиотека SFML подключена и проект должен запуститься без каких-либо дополнительных действий, то есть вы скачиваете проект, открываете его в VS и сразу запускаете, без самостоятельного подключения библиотек. Можете посмотреть в настройках, как я подключал библиотеки по относительным путям.
main.cpp
C++ | 1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
| #include <iostream>
#include <SFML/Graphics.hpp>
int main()
{
sf::RenderWindow window(sf::VideoMode(280, 280), "SFML works!");
sf::CircleShape circle0(20.f);
circle0.setFillColor(sf::Color::Red);
sf::CircleShape circle1(30.f);
circle1.setFillColor(sf::Color::Green);
sf::CircleShape circle2(40.f);
circle2.setFillColor(sf::Color::Blue);
sf::RectangleShape rectangle;
rectangle.setSize(sf::Vector2f(200, 50));
rectangle.setFillColor(sf::Color::Yellow);
rectangle.setOutlineColor(sf::Color::Magenta);
rectangle.setOutlineThickness(5);
sf::Font font;
if (!font.loadFromFile("C:/Windows/Fonts/arial.ttf"))
{
std::cout << "Failed to load the font file.";
return EXIT_FAILURE;
}
sf::Text text;
text.setFont(font);
text.setString("Hello, World!");
text.setCharacterSize(24);
text.setFillColor(sf::Color::Red);
text.setStyle(sf::Text::Bold);
circle0.setPosition(100, 100);
circle0.setPosition(50, 150);
circle1.setPosition(150, 150);
rectangle.setPosition(50, 40);
text.setPosition(80, 50);
// Run the program as long as the window is open
while (window.isOpen())
{
// check all the window's events that were triggered since the last iteration of the loop
sf::Event event;
while (window.pollEvent(event))
{
// "close requested" event: we close the window
if (event.type == sf::Event::Closed)
window.close();
}
window.clear(sf::Color::White);
window.draw(circle0);
window.draw(circle1);
window.draw(circle2);
window.draw(rectangle);
window.draw(text);
window.display();
}
return EXIT_SUCCESS;
} |
|
Если вы используете в качестве редактора кода, например, VSCode и собираете из консоли с помощью MinGW, то это ниже представлен Makefile. Компиляция в EXE осуществляется командой:
Makefile
# -mwindows - without a console window
CC = g++
INC = -I"E:\Libs\SFML-2.5.1-windows-gcc-7.3.0-mingw-32-bit\include"
LIB = -L"E:\Libs\SFML-2.5.1-windows-gcc-7.3.0-mingw-32-bit\lib"
all: main.o
$(CC) -mwindows main.o $(LIB) -lsfml-system -lsfml-window -lsfml-graphics -o app
main.o: main.cpp
$(CC) -c $(INC) main.cpp
|