From a4edc9568b48dad69d0d43e5e266eb607199ae78 Mon Sep 17 00:00:00 2001 From: Michael Lemaire Date: Wed, 12 Aug 2015 22:11:26 +0200 Subject: [PATCH] Restored progress display in CLI render --- src/interface/commandline/main.cpp | 47 +++++++++++++++++++++++------- src/system/Thread.h | 33 ++++++++++++--------- 2 files changed, 56 insertions(+), 24 deletions(-) diff --git a/src/interface/commandline/main.cpp b/src/interface/commandline/main.cpp index 5de8824..7047661 100644 --- a/src/interface/commandline/main.cpp +++ b/src/interface/commandline/main.cpp @@ -9,17 +9,48 @@ #include "Scenery.h" #include "RenderConfig.h" #include "ColorProfile.h" +#include "Thread.h" -void startRender(SoftwareCanvasRenderer *renderer, char *outputpath) +class RenderThread: public Thread { +public: + RenderThread(SoftwareCanvasRenderer *renderer, char *outputpath): + renderer(renderer), outputpath(outputpath) + { + } + + virtual void run() override + { + renderer->render(); + } + +private: + SoftwareCanvasRenderer *renderer; + char *outputpath; +}; + +static void startRender(SoftwareCanvasRenderer *renderer, char *outputpath) +{ + RenderThread thread(renderer, outputpath); + printf("\rRendering %s ... \n", outputpath); - renderer->render(); + thread.start(); + + while (thread.isWorking()) + { + Thread::timeSleepMs(200); + + printf("\rProgress : %0.1f%% ", renderer->getProgress() * 100.0); + fflush(stdout); + } + thread.join(); + printf("\rSaving %s ... \n", outputpath); remove(outputpath); renderer->saveToDisk(outputpath); } -void displayHelp() +static void displayHelp() { printf("Usage : paysages-cli [options]\n"); printf("Options :\n"); @@ -37,17 +68,11 @@ void displayHelp() printf(" -cz z Camera Z step (double)\n"); } -void _previewUpdate(double progress) -{ - printf("\rProgress : %0.1f%% ", progress * 100.0); - fflush(stdout); -} - int main(int argc, char** argv) { SoftwareCanvasRenderer* renderer; char* conf_file_path = NULL; - RenderConfig conf_render_params(800, 600, 1, 5); + RenderConfig conf_render_params(400, 300, 1, 3); int conf_first_picture = 0; int conf_nb_pictures = 1; double conf_daytime_start = 0.4; @@ -185,7 +210,7 @@ int main(int argc, char** argv) if (outputcount >= conf_first_picture) { - sprintf(outputpath, "output/pic%05d.png", outputcount); + sprintf(outputpath, "pic%05d.png", outputcount); startRender(renderer, outputpath); } diff --git a/src/system/Thread.h b/src/system/Thread.h index 5c49233..3adbdb0 100644 --- a/src/system/Thread.h +++ b/src/system/Thread.h @@ -11,37 +11,44 @@ namespace system typedef void* (*ThreadFunction)(void* data); -/*! - * \brief System thread +/** + * System thread */ class SYSTEMSHARED_EXPORT Thread: private QThread { public: - /*! - * \brief Create a new thread + /** + * Create a new thread. * * The thread is not started automatically. A call to method start() needs to be done. - * \param function Function to call inside the thread once it is started + * + * Either the *function* argument should be provided (with the function to call from the + * thread), or the *run* method overridden. */ Thread(ThreadFunction function=0); - /*! - * \brief Start the thread - * \param data User data to pass to the threaded function + /** + * Start the thread, with custom data. */ void start(void* data=0); - /*! - * \brief Wait for the thread to end, and collect its result. - * \return The value returned by the threaded function. + /** + * Wait for the thread to end, and collect its result. + * + * Returns the value returned by the threaded function. */ void* join(); + /** + * Return true if the thread is currently running. + */ + inline bool isWorking() const {return not isFinished();} + static inline void timeSleepMs(unsigned long ms){ QThread::msleep(ms); } protected: - /*! - * \brief Function to reimplement if no ThreadFunction has been passed to the constructor. + /** + * Function to reimplement if no ThreadFunction has been passed to the constructor. */ virtual void run();