-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmain.cpp
More file actions
178 lines (145 loc) · 5.67 KB
/
Copy pathmain.cpp
File metadata and controls
178 lines (145 loc) · 5.67 KB
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
#include "ParticleSystem.h"
#include <SFML/Graphics.hpp>
#include <cmath>
#include <iostream>
using namespace sf;
int
main(int argc, char* argv[])
{
float angle = 0.f;
// Объект, который, собственно, является главным окном приложения
sf::ContextSettings settings;
// settings.antialiasingLevel = 8;
RenderWindow window(
VideoMode(1920, 1080), "SFML Works!", sf::Style::Fullscreen, settings);
sf::VertexArray lines(sf::LinesStrip);
sf::Vector2u permission = window.getSize();
int MAX_W = permission.x;
int MAX_H = permission.y;
// Задать кол-во частит
ParticleSystem particles(25000, window.getSize());
// create a clock to track the elapsed time
sf::Clock clock;
float CELL_SIZE = 70.0f;
lines.append(sf::Vertex(sf::Vector2f(0.0f, CELL_SIZE - 1), Color::White));
lines.append(
sf::Vertex(sf::Vector2f(CELL_SIZE - 1, CELL_SIZE - 1), Color::White));
lines.append(sf::Vertex(sf::Vector2f(CELL_SIZE - 1, 0.0f), Color::White));
float offset_W = (float)((MAX_W / 2) % (int)CELL_SIZE);
float offset_H = (float)((MAX_H / 2) % (int)CELL_SIZE);
std::cout << "offset_h = " << (int)offset_W << std::endl;
std::cout << "offset_w = " << (int)offset_H << std::endl;
sf::RenderTexture texture;
if (!texture.create((int)CELL_SIZE, (int)CELL_SIZE))
return -1;
texture.clear(Color::Blue);
texture.draw(lines);
texture.setRepeated(true);
texture.setSmooth(true);
texture.display();
sf::Sprite sprite(
texture.getTexture(),
IntRect(-(int)offset_W * 0,
-(int)offset_H * 0,
MAX_W - (MAX_W % (int)(2 * CELL_SIZE)) /*+ (int)offset_W*/,
MAX_H - (MAX_H % (int)(2 * CELL_SIZE)) /*+ (int)offset_H*/
));
// DEBUG Экспериментальный код
sf::RenderTexture texture2;
if (!texture2.create((int)CELL_SIZE, (int)CELL_SIZE))
return -1;
texture2.clear(Color::Red);
// Обновить текстуру
texture2.display();
sf::Sprite sprite2(texture2.getTexture(), IntRect(50, 50, 69, 69));
float position_x = MAX_W / 2 - 1;
float position_y = MAX_H / 2;
// Главный цикл приложения. Выполняется, пока открыто окно
while (window.isOpen()) {
// Обрабатываем очередь событий в цикле
Event event;
while (window.pollEvent(event)) {
// Пользователь нажал на «крестик» и хочет закрыть окно?
if (event.type == Event::Closed)
// тогда закрываем его
window.close();
// Была ли нажата клавиша на клавиатуре?
if (event.type == Event::KeyPressed) {
// Вверх
if (event.key.code == Keyboard::Up) {
std::cout << "Press Up" << std::endl;
if (position_y > CELL_SIZE)
position_y -= CELL_SIZE;
}
// Вниз
if (event.key.code == Keyboard::Down) {
std::cout << "Press Down" << std::endl;
if (position_y < MAX_H - 2 * CELL_SIZE)
position_y += CELL_SIZE;
}
// Влево
if (event.key.code == Keyboard::Left) {
std::cout << "Press Left" << std::endl;
if (position_x > CELL_SIZE)
position_x -= CELL_SIZE;
}
// Вправо
if (event.key.code == Keyboard::Right) {
std::cout << "Press Right" << std::endl;
if (position_x < MAX_W - 2 * CELL_SIZE)
position_x += CELL_SIZE;
}
}
}
particles.setEmitter(
window.mapPixelToCoords(sf::Vector2i{ MAX_W / 2, MAX_H / 2 }));
sf::Time elapsed = clock.restart();
particles.update(elapsed);
sprite2.setPosition(position_x, position_y);
// Зададим фон
window.clear(Color::Black);
Vertex line_horizontal[] = {
// Координата первой вершины
Vertex(Vector2f(0.0f, (float)(MAX_H / 2))),
// Координата второй вершины
Vertex(Vector2f((float)MAX_W, (float)(MAX_H / 2)))
};
Vertex line_vertical[] = {
// Координата первой вершины
Vertex(Vector2f((float)(MAX_W / 2), 0.0f)),
// Координата второй вершины
Vertex(Vector2f((float)(MAX_W / 2), (float)(MAX_H)))
};
// Устанавливаем цвет линии - красный
// line_horizontal->color = Color::Red;
// line_vertical->color = Color::Red;
float angleH = 15.f * 3.1419f / 180.f;
float angleV = -5.f * 3.1419f / 180.f;
sf::Transform tr = sf::Transform::Identity;
sf::Transform trOrigin = sf::Transform::Identity;
trOrigin.translate(-MAX_W / 2.f, -MAX_H / 2.f);
sf::Transform trSkew{ 1.f, sinf(angleH), 0.f, sinf(angleV), 1.f,
0.f, 0.f, 0.f, 1.f };
sf::Transform trRotate = sf::Transform::Identity;
trRotate.rotate(angle);
angle += 0.01;
sf::Transform trScale = sf::Transform::Identity;
trScale.scale(0.75f, 0.75f);
sf::Transform trCenter = sf::Transform::Identity;
trCenter.translate(MAX_W / 2.f, MAX_H / 2.f);
tr = trCenter * trScale * trSkew * trOrigin;
sf::Transform trSprite;
trSprite.translate((MAX_W - sprite.getLocalBounds().width) / 2,
(MAX_H - sprite.getLocalBounds().height) / 2);
trSprite = tr * trSprite;
window.draw(particles);
window.draw(sprite, trSprite);
window.draw(sprite2, tr);
// Отрисовка линии
// window.draw(line_horizontal, 2, Lines);
// window.draw(line_vertical, 2, Lines);
// Отрисовка окна
window.display();
}
return 0;
}