2021-09-13 15:53:43 +03:00
|
|
|
//
|
|
|
|
// Created by Иван Ильин on 14.01.2021.
|
|
|
|
//
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
#include <utility>
|
|
|
|
#include <cmath>
|
|
|
|
|
|
|
|
#include <SFML/OpenGL.hpp>
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
#include "Screen.h"
|
|
|
|
#include "utils/Time.h"
|
|
|
|
#include "utils/Log.h"
|
|
|
|
#include "ResourceManager.h"
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void Screen::open(int screenWidth, int screenHeight, const std::string &name, bool verticalSync, sf::Color background,
|
|
|
|
sf::Uint32 style) {
|
2021-09-19 15:44:31 +03:00
|
|
|
_title = name;
|
2021-09-19 11:25:10 +03:00
|
|
|
_background = background;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
|
|
|
sf::ContextSettings settings;
|
2021-11-05 00:05:06 +03:00
|
|
|
settings.depthBits = 12;
|
|
|
|
settings.antialiasingLevel = 1;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-31 19:01:06 +03:00
|
|
|
_window->create(sf::VideoMode(screenWidth, screenHeight), name, style, settings);
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->setVerticalSyncEnabled(verticalSync);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::display() {
|
|
|
|
sf::Event event{};
|
2021-09-19 15:44:31 +03:00
|
|
|
while (_window->pollEvent(event)) {
|
2021-09-13 15:53:43 +03:00
|
|
|
if (event.type == sf::Event::Closed) {
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->close();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
std::string title = _title + " (" + std::to_string(Time::fps()) + " fps)";
|
|
|
|
_window->setTitle(title);
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-11-11 22:47:00 +03:00
|
|
|
if(_renderVideo) {
|
|
|
|
sf::Texture copyTexture;
|
|
|
|
copyTexture.create(_window->getSize().x, _window->getSize().y);
|
|
|
|
copyTexture.update(*_window);
|
|
|
|
// most of the time of video rendering is wasting on .png sequence saving
|
|
|
|
// that's why we will save all images in the end
|
|
|
|
// TODO: sometimes we have a huge time delay here for no obvious reason
|
|
|
|
_renderSequence.push_back(copyTexture);
|
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->display();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-11-11 22:47:00 +03:00
|
|
|
void Screen::startRender() {
|
|
|
|
stopRender();
|
|
|
|
_renderVideo = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::stopRender() {
|
|
|
|
if(_renderVideo) {
|
|
|
|
|
|
|
|
int i = 0;
|
|
|
|
for(; i < _renderSequence.size(); i++) {
|
|
|
|
_renderSequence[i].copyToImage().saveToFile("film/png/" + std::to_string(i) + ".png");
|
|
|
|
}
|
|
|
|
_renderSequence.clear();
|
|
|
|
|
|
|
|
std::string c = "ffmpeg -stats -r 60 -i film/png/%d.png -vcodec libx264 -crf 1 -pix_fmt yuv420p -frames " + std::to_string(i) + " film/mp4/" + std::to_string(_scene) + "_" + _title + ".mp4";
|
|
|
|
popen(c.c_str(), "w");
|
|
|
|
_scene++;
|
|
|
|
_renderVideo = false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-13 15:53:43 +03:00
|
|
|
void Screen::clear() {
|
2021-10-22 19:42:32 +03:00
|
|
|
// Clear the depth buffer
|
|
|
|
glClear(GL_DEPTH_BUFFER_BIT | GL_COLOR_BUFFER_BIT);
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->clear(_background);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void Screen::drawTriangle(const Triangle &triangle) {
|
2021-09-13 15:53:43 +03:00
|
|
|
sf::Vertex tris[3] =
|
|
|
|
{
|
2021-10-31 12:01:31 +03:00
|
|
|
sf::Vertex(sf::Vector2f(static_cast<float>(triangle[0].x()), static_cast<float>(triangle[0].y())),
|
|
|
|
triangle.color()),
|
|
|
|
sf::Vertex(sf::Vector2f(static_cast<float>(triangle[1].x()), static_cast<float>(triangle[1].y())),
|
|
|
|
triangle.color()),
|
|
|
|
sf::Vertex(sf::Vector2f(static_cast<float>(triangle[2].x()), static_cast<float>(triangle[2].y())),
|
|
|
|
triangle.color())
|
2021-09-13 15:53:43 +03:00
|
|
|
};
|
2021-10-22 19:42:32 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(tris, 3, sf::Triangles);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void Screen::setTitle(const std::string &title) {
|
2021-09-19 15:44:31 +03:00
|
|
|
_title = title;
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
bool Screen::isOpen() {
|
2021-09-19 15:44:31 +03:00
|
|
|
return _window->isOpen();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
void Screen::close() {
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->close();
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-28 16:58:02 +03:00
|
|
|
void Screen::setMouseCursorVisible(bool visible) {
|
|
|
|
_window->setMouseCursorVisible(visible);
|
|
|
|
}
|
|
|
|
|
2021-10-12 17:12:47 +03:00
|
|
|
void Screen::drawTetragon(const Vec2D &p1, const Vec2D &p2, const Vec2D &p3, const Vec2D &p4, sf::Color color) {
|
2021-09-19 15:44:31 +03:00
|
|
|
sf::ConvexShape polygon;
|
|
|
|
polygon.setPointCount(4);
|
2021-10-31 12:01:31 +03:00
|
|
|
polygon.setPoint(0, sf::Vector2f(static_cast<float>(p1.x()), static_cast<float>(p1.y())));
|
|
|
|
polygon.setPoint(1, sf::Vector2f(static_cast<float>(p2.x()), static_cast<float>(p2.y())));
|
|
|
|
polygon.setPoint(2, sf::Vector2f(static_cast<float>(p3.x()), static_cast<float>(p3.y())));
|
|
|
|
polygon.setPoint(3, sf::Vector2f(static_cast<float>(p4.x()), static_cast<float>(p4.y())));
|
2021-09-19 15:44:31 +03:00
|
|
|
polygon.setFillColor(color);
|
2021-10-22 19:42:32 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(polygon);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-10-31 11:39:08 +03:00
|
|
|
void Screen::drawText(const std::string &string, const Vec2D &position, int size, sf::Color color) {
|
2021-09-19 15:44:31 +03:00
|
|
|
sf::Text text;
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-16 20:22:55 +03:00
|
|
|
text.setFont(*ResourceManager::loadFont(Consts::MEDIUM_FONT));
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
text.setCharacterSize(size);
|
|
|
|
text.setFillColor(color);
|
|
|
|
text.setStyle(sf::Text::Italic);
|
2021-10-31 12:01:31 +03:00
|
|
|
text.setPosition(static_cast<float>(position.x()), static_cast<float>(position.y()));
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
text.setString(string);
|
2021-10-22 19:42:32 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
_window->draw(text);
|
2021-09-13 15:53:43 +03:00
|
|
|
}
|
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Screen::drawSprite(const sf::Sprite &sprite) {
|
|
|
|
_window->draw(sprite);
|
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-09-19 15:44:31 +03:00
|
|
|
void Screen::drawText(const sf::Text &text) {
|
|
|
|
_window->draw(text);
|
|
|
|
}
|
2021-09-13 15:53:43 +03:00
|
|
|
|
2021-10-22 19:42:32 +03:00
|
|
|
// OpenGL functions
|
2021-11-06 00:34:13 +03:00
|
|
|
void Screen::prepareToGlDrawMesh() {
|
2021-11-05 00:05:06 +03:00
|
|
|
if (!sf::Shader::isAvailable())
|
|
|
|
{
|
|
|
|
Log::log("Shaders are not available!");
|
|
|
|
}
|
|
|
|
|
2021-11-06 00:34:13 +03:00
|
|
|
sf::Shader::bind(NULL);
|
|
|
|
|
2021-10-22 19:42:32 +03:00
|
|
|
glEnable(GL_CULL_FACE); // enable culling face
|
|
|
|
glCullFace(GL_BACK); // cull faces from back
|
|
|
|
glFrontFace(GL_CCW); // vertex order (counter clock wise)
|
|
|
|
|
|
|
|
// Enable Z-buffer read and write
|
|
|
|
glEnable(GL_DEPTH_TEST);
|
|
|
|
glDepthMask(GL_TRUE);
|
|
|
|
glClearDepth(1.f);
|
|
|
|
|
|
|
|
// Disable lighting
|
|
|
|
glDisable(GL_LIGHTING);
|
|
|
|
|
|
|
|
// enable alpha channel:
|
|
|
|
glEnable(GL_BLEND);
|
|
|
|
glBlendFunc(GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA);
|
|
|
|
|
|
|
|
// Configure the viewport (the same size as the window)
|
|
|
|
glViewport(0, 0, _window->getSize().x, _window->getSize().y);
|
|
|
|
|
|
|
|
// Setup a perspective projection
|
|
|
|
glMatrixMode(GL_PROJECTION);
|
|
|
|
glLoadIdentity();
|
|
|
|
GLfloat ratio = static_cast<float>(_window->getSize().x) / _window->getSize().y;
|
|
|
|
glFrustum(-ratio, ratio, -1.f, 1.f, 1.0f, 500.f);
|
|
|
|
|
|
|
|
// Enable position and texture coordinates vertex components
|
|
|
|
glEnableClientState(GL_VERTEX_ARRAY);
|
|
|
|
glEnableClientState(GL_COLOR_ARRAY);
|
|
|
|
|
|
|
|
// Disable normal and color vertex components
|
|
|
|
glDisableClientState(GL_NORMAL_ARRAY);
|
|
|
|
glDisableClientState(GL_TEXTURE_COORD_ARRAY);
|
|
|
|
|
2021-11-06 00:34:13 +03:00
|
|
|
// Prepare to apply some transformations
|
2021-10-22 19:42:32 +03:00
|
|
|
glMatrixMode(GL_MODELVIEW);
|
2021-11-06 00:34:13 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
// OpenGL functions
|
|
|
|
void Screen::glDrawMesh(GLfloat* geometry, GLfloat* view, GLfloat* model, size_t count) {
|
|
|
|
glVertexPointer(3, GL_FLOAT, 7 * sizeof(GLfloat), geometry);
|
|
|
|
glColorPointer(4, GL_FLOAT, 7 * sizeof(GLfloat), geometry + 3);
|
|
|
|
|
2021-10-22 19:42:32 +03:00
|
|
|
glLoadIdentity();
|
|
|
|
|
|
|
|
glLoadMatrixf(view);
|
2021-11-05 00:05:06 +03:00
|
|
|
glMultMatrixf(model);
|
2021-10-22 19:42:32 +03:00
|
|
|
|
|
|
|
// Draw the mesh
|
|
|
|
glDrawArrays(GL_TRIANGLES, 0, count);
|
|
|
|
}
|