SFML community forums

Help => Graphics => Topic started by: UMJC on July 12, 2020, 09:12:44 pm

Title: Problem with slow VertexArray draw
Post by: UMJC on July 12, 2020, 09:12:44 pm
I'm trying to create a Tetris clone and I used Vertex Array for drawing the fields of the games.
Playing it with my AI, however, I found my keyboard input laggy for some reason.
So I profiled the CPU usage, and the 'draw()' function for the field was the problem.
(I don't know how to attach pictures here, so here's the link to Youtube video to show what is going on instead:https://youtu.be/23UAhHYxGMM (https://youtu.be/23UAhHYxGMM))
There are 1920 vertices at maximum in FieldVertA and only 7 draw calls in ShowVisual function.
I can't grasp the cause of this situation, so I'm here to get help, even though I'm so afraid of this since this is my first time to post a question on the Internet.

Here is ShowVisual function in GameScreen.cpp and its header. (It's very long... Also, comments are in my native language.)

void ShowVisual():
void ScreenPrinter::ShowVisual()
{
        clock_t fpsclock = clock();
        double blockSize = (window->getSize().y / 60.0f) * 2;
        sf::RectangleShape bgsq;
        bgsq.setSize(sf::Vector2f(window->getSize().x, window->getSize().y));
        bgsq.setTexture(&bgtex);
        window->draw(bgsq);

        if (games.size() == 1) {
                //There were originally codes for drawing single-player game screen, but I've omitted them for this short post and these don't matter on multiplayer games anyway.
        }
        else {
        //2(+α)p 게임 화면을 출력합니다.
                if (games.size() != 0) {
                        //필드를 출력합니다.
                        switch (games.size()) {
                                case 2:
                                {
                                        sf::VertexArray FieldVertA(sf::Quads);
                                        sf::VertexArray NextHoldVertA(sf::Quads, 2 * 4 * 11);
                                        //첫 번째 필드(인간 플레이어/혹은 설정한 CPU의 것)
                                        {
                                        //필드를 그립니다.
                                        sf::RectangleShape gridsq;
                                        gridsq.setPosition(sf::Vector2f(0, 4 * blockSize));
                                        gridsq.setSize(sf::Vector2f(20 * blockSize, 24 * blockSize));
                                        gridsq.setTexture(&grid);
                                        window->draw(gridsq);

                                        for (int i = 0; i < 20; i++) {
                                                for (int j = 0; j < 10; j++) {
                                                        if (games[0]->Tetris_Field_Matrix[i + 25][j]) {
                                                                sf::Vertex v1;
                                                                sf::Vertex v2;
                                                                sf::Vertex v3;
                                                                sf::Vertex v4;

                                                                v1.position = sf::Vector2f((5 + j) * blockSize, (5 + i) * blockSize);
                                                                v2.position = sf::Vector2f((5 + j) * blockSize, (6 + i) * blockSize);
                                                                v3.position = sf::Vector2f((6 + j) * blockSize, (6 + i) * blockSize);
                                                                v4.position = sf::Vector2f((6 + j) * blockSize, (5 + i) * blockSize);
                                                       
                                                                v1.color = sf::Color(255, 255, 255, 255);
                                                                v2.color = sf::Color(255, 255, 255, 255);
                                                                v3.color = sf::Color(255, 255, 255, 255);
                                                                v4.color = sf::Color(255, 255, 255, 255);

                                                                v1.texCoords = sf::Vector2f(blockTexture.getSize().y * games[0]->Color_Matrix[i + 25][j], 0);
                                                                v2.texCoords = sf::Vector2f(blockTexture.getSize().y * games[0]->Color_Matrix[i + 25][j], blockTexture.getSize().y);
                                                                v3.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[0]->Color_Matrix[i + 25][j] + 1), blockTexture.getSize().y);
                                                                v4.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[0]->Color_Matrix[i + 25][j] + 1), 0);
                                                               
                                                                FieldVertA.append(v1);
                                                                FieldVertA.append(v2);
                                                                FieldVertA.append(v3);
                                                                FieldVertA.append(v4);
                                                               
                                                        }
                                                }
                                        }

                                        //고스트를 그립니다.

                                        //현재 위치에서 고스트가 얼마나 아래로 내려가 있는지 확인합니다.
                                        if (games[0]->CurrentBlockData.Type != Block_Base::Tet_None) {
                                                int ghostoffset = 0;
                                                for (int i = 1; i <= 45; i++) {
                                                        Block_Base base;
                                                        if (!games[0]->CollisionTest(
                                                                games[0]->ExtractPartMatrix(
                                                                        games[0]->CurrentBlockData.x,
                                                                        games[0]->CurrentBlockData.y + i, games[0]->Tetris_Field_Matrix).Part,
                                                                base.Tetrimino_Set[games[0]->CurrentBlockData.Rotation][games[0]->CurrentBlockData.Type])) {

                                                        }
                                                        else {
                                                                ghostoffset = i - 1;
                                                                break;
                                                        }
                                                }

                                                for (int y = 0; y < 4; y++) {
                                                        for (int x = 0; x < 4; x++) {
                                                                Block_Base base;
                                                                int coord[2] = { games[0]->CurrentBlockData.x + x , games[0]->CurrentBlockData.y + y + ghostoffset };
                                                                if (base.Tetrimino_Set[games[0]->CurrentBlockData.Rotation][games[0]->CurrentBlockData.Type][y][x]
                                                                        && 0 <= coord[0] && coord[0] <= 9
                                                                        && 25 <= coord[1] && coord[1] <= 44) {
                                                                        sf::Vertex v1;
                                                                        sf::Vertex v2;
                                                                        sf::Vertex v3;
                                                                        sf::Vertex v4;

                                                                        v1.position = sf::Vector2f((5 + coord[0]) * blockSize, (5 + (coord[1] - 25)) * blockSize);
                                                                        v2.position = sf::Vector2f((5 + coord[0]) * blockSize, (6 + (coord[1] - 25)) * blockSize);
                                                                        v3.position = sf::Vector2f((6 + coord[0]) * blockSize, (6 + (coord[1] - 25)) * blockSize);
                                                                        v4.position = sf::Vector2f((6 + coord[0]) * blockSize, (5 + (coord[1] - 25)) * blockSize);

                                                                        v1.color = sf::Color(255, 255, 255, 127);
                                                                        v2.color = sf::Color(255, 255, 255, 127);
                                                                        v3.color = sf::Color(255, 255, 255, 127);
                                                                        v4.color = sf::Color(255, 255, 255, 127);

                                                                        v1.texCoords = sf::Vector2f(blockTexture.getSize().y * games[0]->CurrentBlockData.Type, 0);
                                                                        v2.texCoords = sf::Vector2f(blockTexture.getSize().y * games[0]->CurrentBlockData.Type, blockTexture.getSize().y);
                                                                        v3.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[0]->CurrentBlockData.Type + 1), blockTexture.getSize().y);
                                                                        v4.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[0]->CurrentBlockData.Type + 1), 0);
                                                                       
                                                                        FieldVertA.append(v1);
                                                                        FieldVertA.append(v2);
                                                                        FieldVertA.append(v3);
                                                                        FieldVertA.append(v4);
                                                                       
                                                                }
                                                        }
                                                }
                                        }

                                        //블록을 그립니다.
                                        if (games[0]->CurrentBlockData.Type != Block_Base::Tet_None) {
                                                for (int y = 0; y < 4; y++) {
                                                        for (int x = 0; x < 4; x++) {
                                                                Block_Base base;
                                                                int coord[2] = { games[0]->CurrentBlockData.x + x , games[0]->CurrentBlockData.y + y };
                                                                if (base.Tetrimino_Set[games[0]->CurrentBlockData.Rotation][games[0]->CurrentBlockData.Type][y][x]
                                                                        && 0 <= coord[0] && coord[0] <= 9
                                                                        && 25 <= coord[1] && coord[1] <= 44) {
                                                                        sf::Vertex v1;
                                                                        sf::Vertex v2;
                                                                        sf::Vertex v3;
                                                                        sf::Vertex v4;

                                                                        v1.position = sf::Vector2f((5 + coord[0]) * blockSize, (5 + (coord[1] - 25)) * blockSize);
                                                                        v2.position = sf::Vector2f((5 + coord[0]) * blockSize, (6 + (coord[1] - 25)) * blockSize);
                                                                        v3.position = sf::Vector2f((6 + coord[0]) * blockSize, (6 + (coord[1] - 25)) * blockSize);
                                                                        v4.position = sf::Vector2f((6 + coord[0]) * blockSize, (5 + (coord[1] - 25)) * blockSize);

                                                                        v1.color = sf::Color(255, 255, 255, 255);
                                                                        v2.color = sf::Color(255, 255, 255, 255);
                                                                        v3.color = sf::Color(255, 255, 255, 255);
                                                                        v4.color = sf::Color(255, 255, 255, 255);

                                                                        v1.texCoords = sf::Vector2f(blockTexture.getSize().y * games[0]->CurrentBlockData.Type, 0);
                                                                        v2.texCoords = sf::Vector2f(blockTexture.getSize().y * games[0]->CurrentBlockData.Type, blockTexture.getSize().y);
                                                                        v3.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[0]->CurrentBlockData.Type + 1), blockTexture.getSize().y);
                                                                        v4.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[0]->CurrentBlockData.Type + 1), 0);
                                                                       
                                                                        FieldVertA.append(v1);
                                                                        FieldVertA.append(v2);
                                                                        FieldVertA.append(v3);
                                                                        FieldVertA.append(v4);
                                                                       
                                                                }
                                                        }
                                                }
                                        }

                                        //대미지 카운터를 그립니다.
                                        for (int i = 0; i < 40; i++) {
                                                if (i < games[0]->attacked) {
                                                        sf::Vertex v1;
                                                        sf::Vertex v2;
                                                        sf::Vertex v3;
                                                        sf::Vertex v4;

                                                        v1.position = sf::Vector2f(15 * blockSize, (blockSize / 2) * (-i + 50));
                                                        v2.position = sf::Vector2f(15 * blockSize, (blockSize / 2) * (-i + 49));
                                                        v3.position = sf::Vector2f(15.5 * blockSize, (blockSize / 2) * (-i + 49));
                                                        v4.position = sf::Vector2f(15.5 * blockSize, (blockSize / 2) * (-i + 50));

                                                        v1.texCoords = sf::Vector2f(blockTexture.getSize().y * 7, blockTexture.getSize().y);
                                                        v2.texCoords = sf::Vector2f(blockTexture.getSize().y * 7, 0);
                                                        v3.texCoords = sf::Vector2f(blockTexture.getSize().y * 8, 0);
                                                        v4.texCoords = sf::Vector2f(blockTexture.getSize().y * 8, blockTexture.getSize().y);

                                                        v1.color = sf::Color(255, 0, 0, 255);
                                                        v2.color = sf::Color(255, 0, 0, 255);
                                                        v3.color = sf::Color(255, 0, 0, 255);
                                                        v4.color = sf::Color(255, 0, 0, 255);

                                                        FieldVertA.append(v1);
                                                        FieldVertA.append(v2);
                                                        FieldVertA.append(v3);
                                                        FieldVertA.append(v4);
                                                       
                                                }
                                        }

                                        //Next를 그립니다.

                                        NextHoldVertA[0].position = sf::Vector2f(16 * blockSize, 6 * blockSize);
                                        NextHoldVertA[1].position = sf::Vector2f(16 * blockSize, 8 * blockSize);
                                        NextHoldVertA[2].position = sf::Vector2f(19 * blockSize, 8 * blockSize);
                                        NextHoldVertA[3].position = sf::Vector2f(19 * blockSize, 6 * blockSize);

                                        NextHoldVertA[0].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[1].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[2].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[3].color = sf::Color(255, 255, 255, 0);

                                        for (int i = 0; i < 10; i++) {
                                                if (i != 0) {
                                                        NextHoldVertA[0 + 4 * i].position = sf::Vector2f(16 * blockSize, (6.4 + 2.1 * i) * blockSize);
                                                        NextHoldVertA[1 + 4 * i].position = sf::Vector2f(16 * blockSize, (8.0 + 2.1 * i) * blockSize);
                                                        NextHoldVertA[2 + 4 * i].position = sf::Vector2f(18.4 * blockSize, (8.0 + 2.1 * i) * blockSize);
                                                        NextHoldVertA[3 + 4 * i].position = sf::Vector2f(18.4 * blockSize, (6.4 + 2.1 * i) * blockSize);

                                                        NextHoldVertA[0 + 4 * i].color = sf::Color(255, 255, 255, 0);
                                                        NextHoldVertA[1 + 4 * i].color = sf::Color(255, 255, 255, 0);
                                                        NextHoldVertA[2 + 4 * i].color = sf::Color(255, 255, 255, 0);
                                                        NextHoldVertA[3 + 4 * i].color = sf::Color(255, 255, 255, 0);
                                                }
                                                if (games[0]->Gamemode->CurrentSettings.NextView > i) {
                                                        NextHoldVertA[0 + 4 * i].color = sf::Color(255, 255, 255, 255);
                                                        NextHoldVertA[1 + 4 * i].color = sf::Color(255, 255, 255, 255);
                                                        NextHoldVertA[2 + 4 * i].color = sf::Color(255, 255, 255, 255);
                                                        NextHoldVertA[3 + 4 * i].color = sf::Color(255, 255, 255, 255);

                                                        NextHoldVertA[0 + 4 * i].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[0]->NextQueue[i]), 0);
                                                        NextHoldVertA[1 + 4 * i].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[0]->NextQueue[i]), nextblocktex.getSize().y);
                                                        NextHoldVertA[2 + 4 * i].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[0]->NextQueue[i] + 1), nextblocktex.getSize().y);
                                                        NextHoldVertA[3 + 4 * i].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[0]->NextQueue[i] + 1), 0);
                                                }
                                        }

                                        //Hold를 그립니다.
                                        NextHoldVertA[0 + 4 * 10].position = sf::Vector2f(1 * blockSize, 6 * blockSize);
                                        NextHoldVertA[1 + 4 * 10].position = sf::Vector2f(1 * blockSize, 8 * blockSize);
                                        NextHoldVertA[2 + 4 * 10].position = sf::Vector2f(4 * blockSize, 8 * blockSize);
                                        NextHoldVertA[3 + 4 * 10].position = sf::Vector2f(4 * blockSize, 6 * blockSize);

                                        NextHoldVertA[0 + 4 * 10].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[1 + 4 * 10].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[2 + 4 * 10].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[3 + 4 * 10].color = sf::Color(255, 255, 255, 0);
                                        if (games[0]->HoldPiece != Block_Base::Tet_None) {
                                                sf::RectangleShape Hold;
                                                Hold.setSize(sf::Vector2f(3 * blockSize, 2 * blockSize));
                                                Hold.setPosition(sf::Vector2f(blockSize, 6 * blockSize));
                                                Hold.setTexture(&nextblocktex);
                                                Hold.setTextureRect(sf::IntRect((nextblocktex.getSize().x / 7) * (games[0]->HoldPiece), 0, (nextblocktex.getSize().x / 7), nextblocktex.getSize().y));
                                                NextHoldVertA[0 + 4 * 10].color = sf::Color(255, 255, 255, 255);
                                                NextHoldVertA[1 + 4 * 10].color = sf::Color(255, 255, 255, 255);
                                                NextHoldVertA[2 + 4 * 10].color = sf::Color(255, 255, 255, 255);
                                                NextHoldVertA[3 + 4 * 10].color = sf::Color(255, 255, 255, 255);

                                                NextHoldVertA[0 + 4 * 10].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[0]->HoldPiece), 0);
                                                NextHoldVertA[1 + 4 * 10].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[0]->HoldPiece), nextblocktex.getSize().y);
                                                NextHoldVertA[2 + 4 * 10].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[0]->HoldPiece + 1), nextblocktex.getSize().y);
                                                NextHoldVertA[3 + 4 * 10].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[0]->HoldPiece + 1), 0);
                                        }

                                        //점수를 나타냅니다.
                                        sf::Text ScoreText;
                                        ScoreText.setFont(ScoreFont);
                                        ScoreText.setString(std::to_string(games[0]->Gamemode->Score));
                                        ScoreText.setCharacterSize(blockSize);
                                        ScoreText.setPosition(5 * blockSize, 26 * blockSize);
                                        window->draw(ScoreText);
                                }
                                        //두 번째 필드(CPU의 것)
                                        {}
                                        {
                                        //필드를 그립니다.
                                        sf::RectangleShape gridsq;
                                        gridsq.setPosition(sf::Vector2f(20 * blockSize, 4 * blockSize));
                                        gridsq.setSize(sf::Vector2f(20 * blockSize, 24 * blockSize));
                                        gridsq.setTexture(&grid);
                                        window->draw(gridsq);
                                        for (int i = 0; i < 20; i++) {
                                                for (int j = 0; j < 10; j++) {
                                                        if (games[1]->Tetris_Field_Matrix[i + 25][j]) {
                                                                sf::Vertex v1;
                                                                sf::Vertex v2;
                                                                sf::Vertex v3;
                                                                sf::Vertex v4;

                                                                v1.position = sf::Vector2f((25 + j) * blockSize, (5 + i) * blockSize);
                                                                v2.position = sf::Vector2f((25 + j) * blockSize, (6 + i) * blockSize);
                                                                v3.position = sf::Vector2f((26 + j) * blockSize, (6 + i) * blockSize);
                                                                v4.position = sf::Vector2f((26 + j) * blockSize, (5 + i) * blockSize);

                                                                v1.texCoords = sf::Vector2f(blockTexture.getSize().y * games[1]->Color_Matrix[i + 25][j], 0);
                                                                v2.texCoords = sf::Vector2f(blockTexture.getSize().y * games[1]->Color_Matrix[i + 25][j], blockTexture.getSize().y);
                                                                v3.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[1]->Color_Matrix[i + 25][j] + 1), blockTexture.getSize().y);
                                                                v4.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[1]->Color_Matrix[i + 25][j] + 1), 0);
                                                               
                                                                FieldVertA.append(v1);
                                                                FieldVertA.append(v2);
                                                                FieldVertA.append(v3);
                                                                FieldVertA.append(v4);
                                                               
                                                        }
                                                }
                                        }

                                        //고스트를 그립니다.
                                        if (games[1]->CurrentBlockData.Type != Block_Base::Tet_None) {
                                                int ghostoffset = 0;
                                                for (int i = 1; i <= 45; i++) {
                                                        Block_Base base;
                                                        if (!games[1]->CollisionTest(
                                                                games[1]->ExtractPartMatrix(
                                                                        games[1]->CurrentBlockData.x,
                                                                        games[1]->CurrentBlockData.y + i, games[1]->Tetris_Field_Matrix).Part,
                                                                base.Tetrimino_Set[games[1]->CurrentBlockData.Rotation][games[1]->CurrentBlockData.Type])) {

                                                        }
                                                        else {
                                                                ghostoffset = i - 1;
                                                                break;
                                                        }
                                                }

                                                for (int y = 0; y < 4; y++) {
                                                        for (int x = 0; x < 4; x++) {
                                                                Block_Base base;
                                                                int coord[2] = { games[1]->CurrentBlockData.x + x , games[1]->CurrentBlockData.y + y + ghostoffset };
                                                                if (base.Tetrimino_Set[games[1]->CurrentBlockData.Rotation][games[1]->CurrentBlockData.Type][y][x]
                                                                        && 0 <= coord[0] && coord[0] <= 9
                                                                        && 25 <= coord[1] && coord[1] <= 44) {
                                                                        sf::Vertex v1;
                                                                        sf::Vertex v2;
                                                                        sf::Vertex v3;
                                                                        sf::Vertex v4;

                                                                        v1.position = sf::Vector2f((25 + coord[0]) * blockSize, (5 + (coord[1] - 25)) * blockSize);
                                                                        v2.position = sf::Vector2f((25 + coord[0]) * blockSize, (6 + (coord[1] - 25)) * blockSize);
                                                                        v3.position = sf::Vector2f((26 + coord[0]) * blockSize, (6 + (coord[1] - 25)) * blockSize);
                                                                        v4.position = sf::Vector2f((26 + coord[0]) * blockSize, (5 + (coord[1] - 25)) * blockSize);

                                                                        v1.color = sf::Color(255, 255, 255, 127);
                                                                        v2.color = sf::Color(255, 255, 255, 127);
                                                                        v3.color = sf::Color(255, 255, 255, 127);
                                                                        v4.color = sf::Color(255, 255, 255, 127);

                                                                        v1.texCoords = sf::Vector2f(blockTexture.getSize().y * games[1]->CurrentBlockData.Type, 0);
                                                                        v2.texCoords = sf::Vector2f(blockTexture.getSize().y * games[1]->CurrentBlockData.Type, blockTexture.getSize().y);
                                                                        v3.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[1]->CurrentBlockData.Type + 1), blockTexture.getSize().y);
                                                                        v4.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[1]->CurrentBlockData.Type + 1), 0);
                                                                       
                                                                        FieldVertA.append(v1);
                                                                        FieldVertA.append(v2);
                                                                        FieldVertA.append(v3);
                                                                        FieldVertA.append(v4);
                                                                       
                                                                }
                                                        }
                                                }
                                        }

                                        //블록을 그립니다.
                                        if (games[1]->CurrentBlockData.Type != Block_Base::Tet_None) {
                                                for (int y = 0; y < 4; y++) {
                                                        for (int x = 0; x < 4; x++) {
                                                                Block_Base base;
                                                                int coord[2] = { games[1]->CurrentBlockData.x + x , games[1]->CurrentBlockData.y + y };
                                                                if (base.Tetrimino_Set[games[1]->CurrentBlockData.Rotation][games[1]->CurrentBlockData.Type][y][x]
                                                                        && 0 <= coord[0] && coord[0] <= 9
                                                                        && 25 <= coord[1] && coord[1] <= 44) {
                                                                        sf::Vertex v1;
                                                                        sf::Vertex v2;
                                                                        sf::Vertex v3;
                                                                        sf::Vertex v4;

                                                                        v1.position = sf::Vector2f((25 + coord[0]) * blockSize, (5 + (coord[1] - 25)) * blockSize);
                                                                        v2.position = sf::Vector2f((25 + coord[0]) * blockSize, (6 + (coord[1] - 25)) * blockSize);
                                                                        v3.position = sf::Vector2f((26 + coord[0]) * blockSize, (6 + (coord[1] - 25)) * blockSize);
                                                                        v4.position = sf::Vector2f((26 + coord[0]) * blockSize, (5 + (coord[1] - 25)) * blockSize);

                                                                        v1.texCoords = sf::Vector2f(blockTexture.getSize().y * games[1]->CurrentBlockData.Type, 0);
                                                                        v2.texCoords = sf::Vector2f(blockTexture.getSize().y * games[1]->CurrentBlockData.Type, blockTexture.getSize().y);
                                                                        v3.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[1]->CurrentBlockData.Type + 1), blockTexture.getSize().y);
                                                                        v4.texCoords = sf::Vector2f(blockTexture.getSize().y * (games[1]->CurrentBlockData.Type + 1), 0);
                                                                       
                                                                        FieldVertA.append(v1);
                                                                        FieldVertA.append(v2);
                                                                        FieldVertA.append(v3);
                                                                        FieldVertA.append(v4);
                                                                       
                                                                }
                                                        }
                                                }
                                        }



                                        //대미지 카운터를 그립니다.
                                        for (int i = 0; i < 40; i++) {
                                                if (i < games[1]->attacked) {
                                                        sf::Vertex v1;
                                                        sf::Vertex v2;
                                                        sf::Vertex v3;
                                                        sf::Vertex v4;

                                                        v1.position = sf::Vector2f(35 * blockSize, (blockSize / 2) * (-i + 50));
                                                        v2.position = sf::Vector2f(35 * blockSize, (blockSize / 2) * (-i + 49));
                                                        v3.position = sf::Vector2f(35.5 * blockSize, (blockSize / 2) * (-i + 49));
                                                        v4.position = sf::Vector2f(35.5 * blockSize, (blockSize / 2) * (-i + 50));

                                                        v1.texCoords = sf::Vector2f(blockTexture.getSize().y * 7, blockTexture.getSize().y);
                                                        v2.texCoords = sf::Vector2f(blockTexture.getSize().y * 7, 0);
                                                        v3.texCoords = sf::Vector2f(blockTexture.getSize().y * 8, 0);
                                                        v4.texCoords = sf::Vector2f(blockTexture.getSize().y * 8, blockTexture.getSize().y);

                                                        v1.color = sf::Color(255, 0, 0, 255);
                                                        v2.color = sf::Color(255, 0, 0, 255);
                                                        v3.color = sf::Color(255, 0, 0, 255);
                                                        v4.color = sf::Color(255, 0, 0, 255);
                                                       
                                                        FieldVertA.append(v1);
                                                        FieldVertA.append(v2);
                                                        FieldVertA.append(v3);
                                                        FieldVertA.append(v4);
                                                       
                                                }
                                        }

                                        //Next를 그립니다.

                                        NextHoldVertA[44].position = sf::Vector2f(36 * blockSize, 6 * blockSize);
                                        NextHoldVertA[45].position = sf::Vector2f(36 * blockSize, 8 * blockSize);
                                        NextHoldVertA[46].position = sf::Vector2f(39 * blockSize, 8 * blockSize);
                                        NextHoldVertA[47].position = sf::Vector2f(39 * blockSize, 6 * blockSize);

                                        NextHoldVertA[44].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[45].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[46].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[47].color = sf::Color(255, 255, 255, 0);

                                        for (int i = 0; i < 10; i++) {
                                                if (i != 0) {
                                                        NextHoldVertA[44 + 4 * i].position = sf::Vector2f(36 * blockSize, (6.4 + 2.1 * i) * blockSize);
                                                        NextHoldVertA[45 + 4 * i].position = sf::Vector2f(36 * blockSize, (8.0 + 2.1 * i) * blockSize);
                                                        NextHoldVertA[46 + 4 * i].position = sf::Vector2f(38.4 * blockSize, (8.0 + 2.1 * i) * blockSize);
                                                        NextHoldVertA[47 + 4 * i].position = sf::Vector2f(38.4 * blockSize, (6.4 + 2.1 * i) * blockSize);

                                                        NextHoldVertA[44 + 4 * i].color = sf::Color(255, 255, 255, 0);
                                                        NextHoldVertA[45 + 4 * i].color = sf::Color(255, 255, 255, 0);
                                                        NextHoldVertA[46 + 4 * i].color = sf::Color(255, 255, 255, 0);
                                                        NextHoldVertA[47 + 4 * i].color = sf::Color(255, 255, 255, 0);
                                                }
                                                if (games[1]->Gamemode->CurrentSettings.NextView > i) {
                                                        NextHoldVertA[44 + 4 * i].color = sf::Color(255, 255, 255, 255);
                                                        NextHoldVertA[45 + 4 * i].color = sf::Color(255, 255, 255, 255);
                                                        NextHoldVertA[46 + 4 * i].color = sf::Color(255, 255, 255, 255);
                                                        NextHoldVertA[47 + 4 * i].color = sf::Color(255, 255, 255, 255);

                                                        NextHoldVertA[44 + 4 * i].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[1]->NextQueue[i]), 0);
                                                        NextHoldVertA[45 + 4 * i].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[1]->NextQueue[i]), nextblocktex.getSize().y);
                                                        NextHoldVertA[46 + 4 * i].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[1]->NextQueue[i] + 1), nextblocktex.getSize().y);
                                                        NextHoldVertA[47 + 4 * i].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[1]->NextQueue[i] + 1), 0);
                                                }
                                        }

                                        //Hold를 그립니다.
                                        NextHoldVertA[44 + 4 * 10].position = sf::Vector2f(21 * blockSize, 6 * blockSize);
                                        NextHoldVertA[45 + 4 * 10].position = sf::Vector2f(21 * blockSize, 8 * blockSize);
                                        NextHoldVertA[46 + 4 * 10].position = sf::Vector2f(24 * blockSize, 8 * blockSize);
                                        NextHoldVertA[47 + 4 * 10].position = sf::Vector2f(24 * blockSize, 6 * blockSize);

                                        NextHoldVertA[44 + 4 * 10].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[45 + 4 * 10].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[46 + 4 * 10].color = sf::Color(255, 255, 255, 0);
                                        NextHoldVertA[47 + 4 * 10].color = sf::Color(255, 255, 255, 0);
                                        if (games[1]->HoldPiece != Block_Base::Tet_None) {
                                                sf::RectangleShape Hold;
                                                Hold.setSize(sf::Vector2f(3 * blockSize, 2 * blockSize));
                                                Hold.setPosition(sf::Vector2f(blockSize, 6 * blockSize));
                                                Hold.setTexture(&nextblocktex);
                                                Hold.setTextureRect(sf::IntRect((nextblocktex.getSize().x / 7) * (games[1]->HoldPiece), 0, (nextblocktex.getSize().x / 7), nextblocktex.getSize().y));
                                                NextHoldVertA[44 + 4 * 10].color = sf::Color(255, 255, 255, 255);
                                                NextHoldVertA[45 + 4 * 10].color = sf::Color(255, 255, 255, 255);
                                                NextHoldVertA[46 + 4 * 10].color = sf::Color(255, 255, 255, 255);
                                                NextHoldVertA[47 + 4 * 10].color = sf::Color(255, 255, 255, 255);

                                                NextHoldVertA[44 + 4 * 10].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[1]->HoldPiece), 0);
                                                NextHoldVertA[45 + 4 * 10].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[1]->HoldPiece), nextblocktex.getSize().y);
                                                NextHoldVertA[46 + 4 * 10].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[1]->HoldPiece + 1), nextblocktex.getSize().y);
                                                NextHoldVertA[47 + 4 * 10].texCoords = sf::Vector2f((nextblocktex.getSize().x / 7) * (games[1]->HoldPiece + 1), 0);
                                        }

                                        //점수를 나타냅니다.

                                        sf::Text ScoreText;
                                        ScoreText.setFont(ScoreFont);
                                        ScoreText.setString(std::to_string(games[1]->Gamemode->Score));
                                        ScoreText.setCharacterSize(blockSize);
                                        ScoreText.setPosition(25 * blockSize, 26 * blockSize);
                                        window->draw(ScoreText);
                                }
                                        window->draw(FieldVertA, &blockTexture);
                                        window->draw(NextHoldVertA, &nextblocktex);
                                }
                                break;
                                }
                        }
                }
       
        return;
}

and GameScreen.h:
#pragma once
#include <vector>
#include <SFML/Graphics.hpp>

class Tetris_Gamemode_Base;
class Tetris_Game_Base;

class ScreenPrinter {
        public:
        //생성자입니다.
        ScreenPrinter();
        ScreenPrinter(sf::RenderWindow* Window, std::vector<Tetris_Game_Base*> Games);

        //게임 화면을 출력할 윈도우입니다.
        sf::RenderWindow* window;

        //독립된 게임 클래스입니다.
        std::vector<Tetris_Game_Base*> games;

        //화면을 출력하는 함수입니다. 이는 전에 Tetris_Gamemode_Base에 있었던 ShowVisual()을 가져온 것입니다.
        //window->display() 코드가 포함되어 있지 않으니 임의로 호출하십시오.
        void ShowVisual();

        //자주 쓰이는 텍스쳐들을 전역 선언하겠습니다.
        sf::Texture blockTexture;
        sf::Texture grid;
        sf::Texture nextblocktex;
        sf::Texture bgtex;
};

 
Title: Re: Problem with slow VertexArray draw
Post by: Hapax on July 12, 2020, 10:38:41 pm
It doesn't look like it's the draw method that's the problem, rather the ShowVisual method in general.

If you really want to know whether it's the draw method or not, remove the draws from that method and check ;D

ShowVisual does a lot of things. A couple of things that could be made more efficient:
1) don't create the vertex arrays every time. Store it and manipulate it in the method. Vertex arrays are wrappers around std::vectors so if the memory is being created and destroyed every time.
2) don't create the text every time. Although texts are generally quite lightweight objects, changing the font or its size could cause it to re-render the font for its size - a relatively slow process. Create the text outside of this method and store it. If it changes, change it once when it does, not every frame.

Also, I don't know what Block_Base is but you are looping and creating a lot of them in that method. Similar to the vertex arrays, when you add an item to it, it has a chance of having to resize the memory block and move the entire thing. This could happen multiple times in just one call of ShowVisual.

Hope this helps :)
Title: Re: Problem with slow VertexArray draw
Post by: UMJC on July 12, 2020, 11:25:24 pm
Thanks for the great advice!
I've only learned C++ and SFML by just googling, I didn't notice those.
I'll try and see how it goes.

//Block_Base is just a class consisted of tetromino pieces for every rotation states and their wallkick data.
Title: Re: Problem with slow VertexArray draw
Post by: UMJC on July 13, 2020, 03:14:02 pm
Update) Turns out SFML didn't have any problem and my game algorithms were.
I see a significant CPU usage increase when I press a key and start a timer. I'll fix it by myself.
Thanks for the help, anyway.
Title: Re: Problem with slow VertexArray draw
Post by: Hapax on July 13, 2020, 11:26:31 pm
You're welcome. Glad it helped.

Remember that constantly creating, destroying and relocating memory can be a bottleneck if done a lot in every frame. A simple solution is to create the object in memory - preferably the maximum size from the start - at the start and only change it in the loop. By change, I mean modify the elements - not add or remove elements. You can even only use some of the elements and ignore others to avoid resizing.
If you absolutely must change the number of elements/change memory, consider allocating an amount of memory first for the maximum amount it will need.
e.g. std::vector::reserve (http://www.cplusplus.com/reference/vector/vector/reserve/)