pushGLStates & popGLStates optimizations

master
Vectozavr 2021-11-06 01:28:04 +07:00
parent a3124306bd
commit a68a28313c
5 changed files with 10 additions and 17 deletions

View File

@ -182,7 +182,6 @@ void Shooter::update() {
} }
void Shooter::gui() { void Shooter::gui() {
sf::Sprite sprite; sf::Sprite sprite;
sprite.setTexture(*ResourceManager::loadTexture(ShooterConsts::MAIN_MENU_GUI)); sprite.setTexture(*ResourceManager::loadTexture(ShooterConsts::MAIN_MENU_GUI));
sprite.setTextureRect(sf::IntRect(243, 3, 9, 9)); sprite.setTextureRect(sf::IntRect(243, 3, 9, 9));

View File

@ -10,7 +10,7 @@ using namespace std;
int main() { int main() {
Shooter game; Shooter game;
game.create(1280, 720, ShooterConsts::PROJECT_NAME, true); game.create(1280, 720, ShooterConsts::PROJECT_NAME, false);
//game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen); //game.create(1920, 1080, ShooterConsts::PROJECT_NAME, true, Consts::BACKGROUND_COLOR, sf::Style::Fullscreen);
//game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false); //game.create(2048, 1152, ShooterConsts::PROJECT_NAME, false);

View File

@ -84,14 +84,15 @@ void Engine::create(int screenWidth, int screenHeight, const std::string &name,
} }
Time::stopTimer("d projections"); Time::stopTimer("d projections");
screen->pushGLStates();
if (Consts::SHOW_FPS_COUNTER) { if (Consts::SHOW_FPS_COUNTER) {
screen->drawText(std::to_string(Time::fps()) + " fps", screen->drawText(std::to_string(Time::fps()) + " fps",
Vec2D(static_cast<double>(screen->width()) - 100.0, 10.0), 25, Vec2D(static_cast<double>(screen->width()) - 100.0, 10.0), 25,
sf::Color(100, 100, 100)); sf::Color(100, 100, 100));
} }
printDebugInfo(); printDebugInfo();
gui(); gui();
screen->popGLStates();
} }
screen->display(); screen->display();
@ -162,7 +163,7 @@ void Engine::printDebugInfo() const {
Vec2D{xPos, yPos + height + height * i}, Vec2D{xPos, yPos + height + height * i},
{static_cast<sf::Uint8>(255.0 * static_cast<double>(width) / timerWidth), {static_cast<sf::Uint8>(255.0 * static_cast<double>(width) / timerWidth),
static_cast<sf::Uint8>(255.0 * (1.0 - static_cast<double>(width) / timerWidth)), static_cast<sf::Uint8>(255.0 * (1.0 - static_cast<double>(width) / timerWidth)),
0, 150}); 0, 100});
screen->drawText( screen->drawText(
@ -170,7 +171,7 @@ void Engine::printDebugInfo() const {
std::to_string((int) (1.0 / timer.elapsedMilliseconds())) + " / s \t (" + std::to_string((int) (1.0 / timer.elapsedMilliseconds())) + " / s \t (" +
std::to_string((int) (100 * timer.elapsedMilliseconds() / totalTime)) + "%)", std::to_string((int) (100 * timer.elapsedMilliseconds() / totalTime)) + "%)",
Vec2D{xPos + 10, yPos + height * i + 5}, 30, Vec2D{xPos + 10, yPos + height * i + 5}, 30,
sf::Color(0, 0, 0, 200)); sf::Color(0, 0, 0, 150));
i++; i++;
timeSum += timer.elapsedMilliseconds(); timeSum += timer.elapsedMilliseconds();
@ -183,13 +184,13 @@ void Engine::printDebugInfo() const {
Vec2D{xPos, yPos + height + height * i}, Vec2D{xPos, yPos + height + height * i},
{static_cast<sf::Uint8>(255.0 * static_cast<double>(width) / timerWidth), {static_cast<sf::Uint8>(255.0 * static_cast<double>(width) / timerWidth),
static_cast<sf::Uint8>(255.0 * (1.0 - static_cast<double>(width) / timerWidth)), static_cast<sf::Uint8>(255.0 * (1.0 - static_cast<double>(width) / timerWidth)),
0, 150}); 0, 100});
screen->drawText("other:\t" + std::to_string((int) (1.0 / (totalTime - timeSum))) + " / s \t (" + screen->drawText("other:\t" + std::to_string((int) (1.0 / (totalTime - timeSum))) + " / s \t (" +
std::to_string((int) (100 * (totalTime - timeSum) / totalTime)) + "%)", std::to_string((int) (100 * (totalTime - timeSum) / totalTime)) + "%)",
Vec2D{xPos + 10, yPos + height * i + 5}, 30, Vec2D{xPos + 10, yPos + height * i + 5}, 30,
sf::Color(0, 0, 0, 200)); sf::Color(0, 0, 0, 150));
} }
} }

View File

@ -57,9 +57,7 @@ void Screen::drawTriangle(const Triangle &triangle) {
triangle.color()) triangle.color())
}; };
_window->pushGLStates();
_window->draw(tris, 3, sf::Triangles); _window->draw(tris, 3, sf::Triangles);
_window->popGLStates();
} }
void Screen::setTitle(const std::string &title) { void Screen::setTitle(const std::string &title) {
@ -87,9 +85,7 @@ void Screen::drawTetragon(const Vec2D &p1, const Vec2D &p2, const Vec2D &p3, con
polygon.setPoint(3, sf::Vector2f(static_cast<float>(p4.x()), static_cast<float>(p4.y()))); polygon.setPoint(3, sf::Vector2f(static_cast<float>(p4.x()), static_cast<float>(p4.y())));
polygon.setFillColor(color); polygon.setFillColor(color);
_window->pushGLStates();
_window->draw(polygon); _window->draw(polygon);
_window->popGLStates();
} }
void Screen::drawText(const std::string &string, const Vec2D &position, int size, sf::Color color) { void Screen::drawText(const std::string &string, const Vec2D &position, int size, sf::Color color) {
@ -104,21 +100,15 @@ void Screen::drawText(const std::string &string, const Vec2D &position, int size
text.setString(string); text.setString(string);
_window->pushGLStates();
_window->draw(text); _window->draw(text);
_window->popGLStates();
} }
void Screen::drawSprite(const sf::Sprite &sprite) { void Screen::drawSprite(const sf::Sprite &sprite) {
_window->pushGLStates();
_window->draw(sprite); _window->draw(sprite);
_window->popGLStates();
} }
void Screen::drawText(const sf::Text &text) { void Screen::drawText(const sf::Text &text) {
_window->pushGLStates();
_window->draw(text); _window->draw(text);
_window->popGLStates();
} }
// OpenGL functions // OpenGL functions

View File

@ -65,6 +65,9 @@ public:
static GLfloat *glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh); static GLfloat *glMeshToGLfloatArray(std::shared_ptr<Mesh> mesh);
[[nodiscard]] std::shared_ptr<sf::RenderWindow> renderWindow() { return _window; } [[nodiscard]] std::shared_ptr<sf::RenderWindow> renderWindow() { return _window; }
void pushGLStates() { _window->pushGLStates(); };
void popGLStates() { _window->popGLStates(); };
}; };