vectozavr-shooter/engine/Screen.cpp

117 lines
3.1 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) {
_title = name;
_w = screenWidth;
_h = screenHeight;
_background = background;
2021-09-13 15:53:43 +03:00
sf::ContextSettings settings;
settings.antialiasingLevel = 8;
_window = std::make_shared<sf::RenderWindow>();
_window->create(sf::VideoMode(_w, _h), name, style, settings);
_window->setVerticalSyncEnabled(verticalSync);
2021-09-13 15:53:43 +03:00
}
void Screen::display() {
sf::Event event{};
while (_window->pollEvent(event)) {
2021-09-13 15:53:43 +03:00
if (event.type == sf::Event::Closed) {
_window->close();
2021-09-13 15:53:43 +03:00
}
}
std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)";
_window->setTitle(title);
2021-09-13 15:53:43 +03:00
_window->display();
2021-09-13 15:53:43 +03:00
}
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] =
{
2021-10-09 13:41:12 +03:00
sf::Vertex(sf::Vector2f((float)triangle[0].x(), (float)triangle[0].y()), triangle.color()),
sf::Vertex(sf::Vector2f((float)triangle[1].x(), (float)triangle[1].y()), triangle.color()),
sf::Vertex(sf::Vector2f((float)triangle[2].x(), (float)triangle[2].y()), triangle.color())
2021-09-13 15:53:43 +03:00
};
_window->draw(tris, 3, sf::Triangles);
2021-09-13 15:53:43 +03:00
}
void Screen::setTitle(const std::string& title) {
_title = title;
2021-09-13 15:53:43 +03:00
}
bool Screen::isOpen() {
return _window->isOpen();
2021-09-13 15:53:43 +03:00
}
void Screen::close() {
_window->close();
2021-09-13 15:53:43 +03:00
}
void Screen::debugText(const std::string& text) {
sf::Text t;
2021-09-13 15:53:43 +03:00
t.setFont(*ResourceManager::loadFont(_font));
t.setString(text);
t.setCharacterSize(30);
t.setFillColor(sf::Color::Black);
t.setPosition(10, 10);
2021-09-13 15:53:43 +03:00
_window->draw(t);
2021-09-13 15:53:43 +03:00
}
void Screen::drawTetragon(const Point4D &p1, const Point4D &p2, const Point4D &p3, const Point4D &p4, sf::Color color) {
sf::ConvexShape polygon;
polygon.setPointCount(4);
polygon.setPoint(0, sf::Vector2f((float)p1.x(), (float)p1.y()));
polygon.setPoint(1, sf::Vector2f((float)p2.x(), (float)p2.y()));
polygon.setPoint(2, sf::Vector2f((float)p3.x(), (float)p3.y()));
polygon.setPoint(3, sf::Vector2f((float)p4.x(), (float)p4.y()));
polygon.setFillColor(color);
_window->draw(polygon);
2021-09-13 15:53:43 +03:00
}
void Screen::drawText(const std::string& string, const Point4D &position, int size, sf::Color color) {
sf::Text text;
2021-09-13 15:53:43 +03:00
2021-10-09 13:41:12 +03:00
text.setFont(*ResourceManager::loadFont("engine/fonts/Roboto-Medium.ttf"));
2021-09-13 15:53:43 +03:00
text.setCharacterSize(size);
text.setFillColor(color);
text.setStyle(sf::Text::Italic);
text.setPosition((float)position.x(), (float)position.y());
2021-09-13 15:53:43 +03:00
text.setString(string);
_window->draw(text);
2021-09-13 15:53:43 +03:00
}
void Screen::drawSprite(const sf::Sprite &sprite) {
_window->draw(sprite);
}
2021-09-13 15:53:43 +03:00
void Screen::drawText(const sf::Text &text) {
_window->draw(text);
}
2021-09-13 15:53:43 +03:00
void Screen::attachMouse(std::shared_ptr<Mouse> mouse) {
mouse->setWindow(_window);
2021-09-13 15:53:43 +03:00
}