Q
Q
Quark2021-09-25 21:27:37
C++ / C#
Quark, 2021-09-25 21:27:37

How can I speed up the process of interacting with the console?

I recently wrote my own graphics library that helps me draw objects in the console. However, as I noticed, the entire "render" process takes no more than 80 ms, but commands such as printf and SetConsoleColour , as well as SetConsoleCursorPosition slow down the work time more than 2 TIMES !!!. I tried many alternatives to the printf command (namely, the one that slows down the execution time the most), but none of them had any effect on the render time. How can I speed it up?

Here is my code:
https://pastebin.com/8LeFA2Lv

//TODO:Make render relatively camera position
public:
    __declspec(dllexport) static bool RenderFrame(Camera camera) {
 
        for (size_t y = 0; y < ScreenClass::SizeY; y++)
        {
            for (size_t x = 0; x < ScreenClass::SizeX; x++)
            {
                bool IsDraw = false;//Костыль
 
                Vector2 currentPos;
                currentPos.x = x;
                currentPos.y = y;
 
                ConsoleInfo::SetCursorPosition({ static_cast<SHORT>(currentPos.x), static_cast<SHORT>(currentPos.y) });
 
                for (size_t z = 0; z < SpriteObject::AllSpriteObjects.size(); z++)
                {
                    vector<Vector2> triangles = SpriteObject::AllSpriteObjects[z]->Mesh.Triangles;
 
                    for (size_t t = 0; t < triangles.size(); t += 3)
                    {
                        float x1 = ((triangles[t].x + SpriteObject::AllSpriteObjects[z]->Position->x) - currentPos.x) * ((triangles[t + 1].y + SpriteObject::AllSpriteObjects[z]->Position->y) - (triangles[t].y + SpriteObject::AllSpriteObjects[z]->Position->y)) - ((triangles[t + 1].x + SpriteObject::AllSpriteObjects[z]->Position->x) - (triangles[t].x + SpriteObject::AllSpriteObjects[z]->Position->x)) * ((triangles[t].y + SpriteObject::AllSpriteObjects[z]->Position->y) - currentPos.y);
                        float x2 = ((triangles[t + 1].x + SpriteObject::AllSpriteObjects[z]->Position->x) - currentPos.x) * ((triangles[t + 2].y + SpriteObject::AllSpriteObjects[z]->Position->y) - (triangles[t + 1].y + SpriteObject::AllSpriteObjects[z]->Position->y)) - ((triangles[t + 2].x + SpriteObject::AllSpriteObjects[z]->Position->x) - (triangles[t + 1].x + SpriteObject::AllSpriteObjects[z]->Position->x)) * ((triangles[t + 1].y + SpriteObject::AllSpriteObjects[z]->Position->y) - currentPos.y);
                        float x3 = ((triangles[t + 2].x + SpriteObject::AllSpriteObjects[z]->Position->x) - currentPos.x) * ((triangles[t].y + SpriteObject::AllSpriteObjects[z]->Position->y) - (triangles[t + 2].y + SpriteObject::AllSpriteObjects[z]->Position->y)) - ((triangles[t].x + SpriteObject::AllSpriteObjects[z]->Position->x) - (triangles[t + 2].x + SpriteObject::AllSpriteObjects[z]->Position->x)) * ((triangles[t + 2].y + SpriteObject::AllSpriteObjects[z]->Position->y) - currentPos.y);
 
                        if (x1 >= 0 && x2 >= 0 && x3 >= 0)
                        {      
                            ConsoleInfo::SetConsoleColour(SpriteObject::AllSpriteObjects[z]->Color);
                            IsDraw = true;//Костыль
                            printf(&SpriteObject::AllSpriteObjects[z]->Texture);
                            break;
                        }
                        else if (x1 <= 0 && x2 <= 0 && x3 <= 0)
                        {
                            ConsoleInfo::SetConsoleColour(SpriteObject::AllSpriteObjects[z]->Color);
                            IsDraw = true;//Костыль
                            printf(&SpriteObject::AllSpriteObjects[z]->Texture);
                            break;
                        }
                    }
                }
                if (IsDraw == false)
                {
                    printf(" ");
                }
            }
        }
        return true;
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
J
jcmvbkbc, 2021-09-26
@jcmvbkbc

I have tried many alternatives to the printf command

function calls printf(…)in this code generally look alien, it would be correct to replace them with fputs(…, stdout).
How can I speed it up?

I think not: the console output under Windows is unhurried in nature.

A
Airog, 2021-09-26
@Airog

You need to dig into the WinApi side, specifically use the WriteConsole function.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question