Add progress bar for SDL backend

This commit is contained in:
Cacodemon345 2021-01-22 23:18:23 +06:00 committed by Rachael Alexanderson
parent 421b6a0343
commit 3c5f5f392f
2 changed files with 54 additions and 5 deletions

View file

@ -45,6 +45,7 @@
#include <fcntl.h>
#include <time.h>
#include <unistd.h>
#include <sys/ioctl.h>
#include <SDL.h>
#include "x86.h"
@ -57,6 +58,7 @@
#include "v_font.h"
#include "c_cvars.h"
#include "palutil.h"
#include "st_start.h"
#ifndef NO_GTK
@ -71,6 +73,8 @@ double PerfToSec, PerfToMillisec;
CVAR(Bool, con_printansi, true, CVAR_GLOBALCONFIG|CVAR_ARCHIVE);
CVAR(Bool, con_4bitansi, false, CVAR_GLOBALCONFIG|CVAR_ARCHIVE);
extern FStartupScreen *StartScreen;
void I_SetIWADInfo()
{
}
@ -132,6 +136,42 @@ void CalculateCPUSpeed()
{
}
void CleanProgressBar()
{
if (!isatty(STDOUT_FILENO)) return;
struct winsize sizeOfWindow;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &sizeOfWindow);
fprintf(stdout,"\0337\033[%d;%dH\033[0J\0338",sizeOfWindow.ws_row, 0);
fflush(stdout);
}
static int ProgressBarCurPos, ProgressBarMaxPos;
void RedrawProgressBar(int CurPos, int MaxPos)
{
if (!isatty(STDOUT_FILENO)) return;
CleanProgressBar();
struct winsize sizeOfWindow;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &sizeOfWindow);
double progVal = std::clamp((double)CurPos / (double)MaxPos,0.0,1.0);
int curProgVal = std::clamp(int(sizeOfWindow.ws_col * progVal),0,(int)sizeOfWindow.ws_col);
char progressBuffer[512];
memset(progressBuffer,'.',512);
progressBuffer[sizeOfWindow.ws_col - 1] = 0;
int lengthOfStr = 0;
while (curProgVal-- > 0)
{
progressBuffer[lengthOfStr++] = '=';
if (lengthOfStr >= sizeOfWindow.ws_col - 1) break;
}
fprintf(stdout, "\0337\033[%d;%dH\033[2K[%s\033[%d;%dH]\0338", sizeOfWindow.ws_row, 0, progressBuffer, sizeOfWindow.ws_row, sizeOfWindow.ws_col);
fflush(stdout);
ProgressBarCurPos = CurPos;
ProgressBarMaxPos = MaxPos;
}
void I_PrintStr(const char *cp)
{
const char * srcp = cp;
@ -183,8 +223,10 @@ void I_PrintStr(const char *cp)
}
}
if (StartScreen) CleanProgressBar();
fputs(printData.GetChars(),stdout);
fputs("\033[0m",stdout);
if (StartScreen) RedrawProgressBar(ProgressBarCurPos,ProgressBarMaxPos);
}
int I_PickIWad (WadStuff *wads, int numwads, bool showwin, int defaultiwad)

View file

@ -68,6 +68,9 @@ class FTTYStartupScreen : public FStartupScreen
// EXTERNAL FUNCTION PROTOTYPES --------------------------------------------
extern void RedrawProgressBar(int CurPos, int MaxPos);
extern void CleanProgressBar();
// PUBLIC FUNCTION PROTOTYPES ----------------------------------------------
// PRIVATE FUNCTION PROTOTYPES ---------------------------------------------
@ -139,13 +142,15 @@ FTTYStartupScreen::~FTTYStartupScreen()
//
// FTTYStartupScreen :: Progress
//
// If there was a progress bar, this would move it. But the basic TTY
// startup screen doesn't have one, so this function does nothing.
//
//===========================================================================
void FTTYStartupScreen::Progress()
{
if (CurPos < MaxPos)
{
++CurPos;
}
RedrawProgressBar(CurPos, MaxPos);
}
//===========================================================================
@ -163,6 +168,7 @@ void FTTYStartupScreen::NetInit(const char *message, int numplayers)
{
termios rawtermios;
CleanProgressBar();
fprintf (stderr, "Press 'Q' to abort network game synchronization.");
// Set stdin to raw mode so we can get keypresses in ST_CheckNetAbort()
// immediately without waiting for an EOL.
@ -198,6 +204,7 @@ void FTTYStartupScreen::NetInit(const char *message, int numplayers)
void FTTYStartupScreen::NetDone()
{
CleanProgressBar();
// Restore stdin settings
if (DidNetInit)
{