Home Game Development opengl – Increased CPU usage when rendering sprites

opengl – Increased CPU usage when rendering sprites

0
opengl – Increased CPU usage when rendering sprites

[ad_1]

I’m creating a game in modern OpenGL, C++ but I’m having problems with optimization. Whenever I render the sprites the CPU usage starts to increase.

Code:

void GameController::Render() {
    shaders.Use();
    glBindVertexArray(vao);

    for (const auto& layers : chuncks) {
        for (const auto& layer : layers) {
            const bool isVisible =
                (layer.position.x + layer.size.x >= camera->pixelRect.min().x) &&
                (layer.position.x - layer.size.x <= camera->pixelRect.max().x) &&
                (layer.position.y + layer.size.y >= camera->pixelRect.min().y) &&
                (layer.position.y - layer.size.y <= camera->pixelRect.max().y);

            if (isVisible) {
                textures[layer.id - 1]->Usage(GL_TEXTURE0);

                shaders.SetUniform("textureSampler", 0);
                shaders.SetUniform("projection", camera->projectionMatrix);
                shaders.SetUniform("translate", layer.position);
                shaders.SetUniform("scale", layer.size);

                glDrawArrays(GL_QUADS, 0, 4);

                textures[layer.id - 1]->Unused();
            }
        }
    }
    glBindVertexArray(0);
    shaders.Unuse();
}

CPU usage is starting to get unusually high. I’ve thought about implementing separate threads, one for updating and one for rendering, but I don’t know if it will be efficient.

[ad_2]