shooter/engine/Screen.cpp

134 lines
3.3 KiB
C++
Raw Normal View History

2021-09-13 15:53:43 +03:00
//
// Created by Иван Ильин on 14.01.2021.
//
#include "Screen.h"
#include "utils/Time.h"
#include <utility>
#include "utils/Log.h"
#include "ResourceManager.h"
#include <cstdio>
void Screen::open(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background, sf::Uint32 style) {
_name = name;
_w = screenWidth;
_h = screenHeight;
_background = background;
2021-09-13 15:53:43 +03:00
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
window.create(sf::VideoMode(_w, _h), name, style, settings);
2021-09-13 15:53:43 +03:00
window.setVerticalSyncEnabled(verticalSync);
}
void Screen::display() {
sf::Event event{};
while (window.pollEvent(event)) {
if (event.type == sf::Event::Closed) {
window.close();
}
}
std::string title = _name + " (" + std::to_string(Time::fps()) + " fps)";
2021-09-13 15:53:43 +03:00
window.setTitle(title);
window.display();
}
void Screen::clear() {
window.clear(_background);
2021-09-13 15:53:43 +03:00
}
void Screen::drawTriangle(const Triangle& triangle)
2021-09-13 15:53:43 +03:00
{
sf::Vertex tris[3] =
{
sf::Vertex(sf::Vector2f(triangle[0].x(), triangle[0].y()), triangle.color()),
sf::Vertex(sf::Vector2f(triangle[1].x(), triangle[1].y()), triangle.color()),
sf::Vertex(sf::Vector2f(triangle[2].x(), triangle[2].y()), triangle.color())
2021-09-13 15:53:43 +03:00
};
window.draw(tris, 3, sf::Triangles);
}
void Screen::setName(const std::string& title) {
_name = title;
2021-09-13 15:53:43 +03:00
}
bool Screen::isOpen() {
return window.isOpen();
}
void Screen::close() {
window.close();
}
bool Screen::isKeyPressed(sf::Keyboard::Key key) {
return sf::Keyboard::isKeyPressed(key);
}
Point4D Screen::getMousePosition() const {
sf::Vector2<int> pos = sf::Mouse::getPosition(window);
return Point4D(pos.x, pos.y, 0, 0);
}
Point4D Screen::getMouseDisplacement() const {
sf::Vector2<int> disp = sf::Mouse::getPosition(window) - sf::Vector2<int>(_w/2, _h/2);
2021-09-13 15:53:43 +03:00
setMouseInCenter();
return Point4D(disp.x, disp.y, 0, 0);
}
void Screen::setMouseInCenter() const {
sf::Mouse::setPosition({ _w / 2, _h / 2 }, window);
2021-09-13 15:53:43 +03:00
}
void Screen::setMouseCursorVisible(bool visible) {
window.setMouseCursorVisible(visible);
}
bool Screen::isKeyTapped(sf::Keyboard::Key key) {
if (!Screen::isKeyPressed(key))
return false;
if(_tappedKeys.count(key) == 0) {
_tappedKeys.emplace(key, Time::time());
2021-09-13 15:53:43 +03:00
return true;
} else if((Time::time() - _tappedKeys[key]) > 0.2) {
_tappedKeys[key] = Time::time();
2021-09-13 15:53:43 +03:00
return true;
}
return false;
}
bool Screen::isButtonPressed(sf::Mouse::Button button) {
return sf::Mouse::isButtonPressed(button);
}
bool Screen::isButtonTapped(sf::Mouse::Button button) {
if (!Screen::isButtonPressed(button))
return false;
if(_tappedButtons.count(button) == 0) {
_tappedButtons.emplace(button, Time::time());
2021-09-13 15:53:43 +03:00
return true;
} else if((Time::time() - _tappedButtons[button]) > 0.2) {
_tappedButtons[button] = Time::time();
2021-09-13 15:53:43 +03:00
return true;
}
return false;
}
void Screen::debugText(const std::string& text) {
sf::Text t;
t.setFont(*ResourceManager::loadFont(_font));
2021-09-13 15:53:43 +03:00
t.setString(text);
t.setCharacterSize(30);
t.setFillColor(sf::Color::Black);
t.setPosition(10, 10);
window.draw(t);
}