Make a loading screen
Jul 20, 2016 at 1:12pm UTC
I am trying to make a text RPG and I have a series of class creators, for example, it takes a long time to make all the NPC's in the game, so, for the users benefit, I wanted to have a loading screen. And, I want the loading screen to actually work and display how far it really is.
I want it to look something like:
[= ] 5%
[== ] 10%
[========= ] 50 %
(You get the idea)
Any help would be greatly appreciated, thanks!
Last edited on Jul 20, 2016 at 1:14pm UTC
Jul 20, 2016 at 8:59pm UTC
Here you go.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include <chrono>
#include <iomanip>
#include <iostream>
#include <string>
#include <thread>
double do_something()
{
// This function is just a stub to pretend it is doing something useful.
// It returns the percent completion of the task (in 0..1).
static auto start = std::chrono::system_clock::now();
static auto stop = start + std::chrono::seconds( 10 );
std::this_thread::sleep_for( std::chrono::milliseconds( 500 ) );
auto now = std::chrono::system_clock::now();
return (double )(now - start).count() / (stop - start).count();
}
void show_progress( int width, double percent_done )
{
int n = (width - 2) * percent_done + 0.5;
std::cout
<< "\r["
<< std::string( n, '=' )
<< std::string( width - 2 - n, ' ' )
<< "]"
<< std::setw( 4 ) << (int )(percent_done * 100 + 0.5) << "%"
<< std::flush;
}
int main()
{
double percent_done;
show_progress( 50, 0 );
while ((percent_done = do_something()) < 1)
show_progress( 50, percent_done );
show_progress( 50, 1 );
std::cout << "\n" ;
}
The CR character
'\r'
is the magic that causes the cursor to return to the left side of the screen.
Hope this helps.
Last edited on Jul 20, 2016 at 9:01pm UTC
Topic archived. No new replies allowed.