Answer the question
In order to leave comments, you need to log in
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
I have tried many alternatives to the printf command
printf(…)
in this code generally look alien, it would be correct to replace them with fputs(…, stdout)
.How can I speed it up?
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question