diff --git a/Makefile b/Makefile index 2158814..901cc06 100644 --- a/Makefile +++ b/Makefile @@ -8,7 +8,7 @@ clean: cd lib_paysages && make clean cd cli && make clean cd gui_gtk && make clean - cd gui_qt && make clean && rm -f Makefile paysages-qt + cd gui_qt && make clean && rm -f paysages-qt run_cli: LD_LIBRARY_PATH=lib_paysages ./cli/paysages-cli diff --git a/gui_qt/dialognoise.cpp b/gui_qt/dialognoise.cpp index adc847f..e27fc7e 100644 --- a/gui_qt/dialognoise.cpp +++ b/gui_qt/dialognoise.cpp @@ -2,12 +2,63 @@ #include #include +#include #include #include #include #include "../lib_paysages/shared/functions.h" +/**************** Previews ****************/ +class PreviewLevel:public Preview +{ +public: + PreviewLevel(QWidget* parent, NoiseGenerator* noise): + Preview(parent), + _noise(noise) + { + } +protected: + QColor getColor(double x, double y) + { + /*Vector3 eye = {0.0, 0.0, 0.0}; + Vector3 look; + + look.x = cos(M_PI * (x / 128.0 + 0.5)) * cos(M_PI * (y / 256.0)); + look.y = -sin(M_PI * (y / 256.0)); + look.z = sin(M_PI * (x / 128.0 + 0.5)) * cos(M_PI * (y / 256.0)); + + return colorToQColor(skyGetColorCustom(eye, look, &_definition, NULL, NULL));*/ + } +private: + NoiseGenerator* _noise; +}; + +class PreviewTotal:public Preview +{ +public: + PreviewTotal(QWidget* parent, NoiseGenerator* noise): + Preview(parent), + _noise(noise) + { + } +protected: + QColor getColor(double x, double y) + { + if (y > noiseGet1DTotal(_noise, x)) + { + return QColor(255, 255, 255); + } + else + { + return QColor(0, 0, 0); + } + } +private: + NoiseGenerator* _noise; +}; + +/**************** Dialog form ****************/ DialogNoise::DialogNoise(QWidget *parent, NoiseGenerator* value): QDialog(parent) { @@ -17,12 +68,25 @@ DialogNoise::DialogNoise(QWidget *parent, NoiseGenerator* value): noiseCopy(_base, _current); setLayout(new QVBoxLayout()); - _form = new FormNoise(this, _current); - layout()->addWidget(_form); + + previewLevel = new PreviewLevel(this, _current); + layout()->addWidget(new QLabel("Level preview")); + layout()->addWidget(previewLevel); + previewTotal = new PreviewTotal(this, _current); + layout()->addWidget(new QLabel("Total preview")); + layout()->addWidget(previewTotal); setWindowTitle("Paysages 3D - Noise editor"); } +DialogNoise::~DialogNoise() +{ + delete previewLevel; + delete previewTotal; + + noiseDeleteGenerator(_current); +} + bool DialogNoise::getNoise(QWidget* parent, NoiseGenerator* noise) { int result; @@ -43,10 +107,5 @@ void DialogNoise::closeEvent(QCloseEvent* e) void DialogNoise::accept() { noiseCopy(_current, _base); - reject(); -} - -void DialogNoise::reject() -{ - noiseDeleteGenerator(_current); + QDialog::accept(); } diff --git a/gui_qt/dialognoise.h b/gui_qt/dialognoise.h index c912638..10da810 100644 --- a/gui_qt/dialognoise.h +++ b/gui_qt/dialognoise.h @@ -2,7 +2,7 @@ #define _PAYSAGES_QT_DIALOGNOISE_H_ #include -#include "formnoise.h" +#include "preview.h" #include "../lib_paysages/shared/types.h" @@ -11,11 +11,12 @@ class DialogNoise : public QDialog Q_OBJECT public: explicit DialogNoise(QWidget* parent, NoiseGenerator* noise); + ~DialogNoise(); + static bool getNoise(QWidget* parent, NoiseGenerator* noise); public slots: virtual void accept(); - virtual void reject(); protected: virtual void closeEvent(QCloseEvent* e); @@ -24,7 +25,8 @@ private: NoiseGenerator* _base; NoiseGenerator* _current; NoiseLevel _current_level; - FormNoise* _form; + Preview* previewLevel; + Preview* previewTotal; }; #endif diff --git a/gui_qt/dialogrender.cpp b/gui_qt/dialogrender.cpp index 5497115..f503a2d 100644 --- a/gui_qt/dialogrender.cpp +++ b/gui_qt/dialogrender.cpp @@ -4,7 +4,6 @@ #include #include #include -#include #include "../lib_paysages/shared/functions.h" @@ -26,6 +25,7 @@ static void _renderResize(int width, int height) _current_dialog->area->setMinimumSize(width, height); _current_dialog->area->setMaximumSize(width, height); _current_dialog->area->resize(width, height); + _current_dialog->scroll->setMinimumSize(width > 800 ? 850 : width + 50, height > 600 ? 650 : height + 50); } static void _renderClear(Color col) @@ -62,20 +62,18 @@ public: } }; -DialogRender::DialogRender(QWidget *parent, int quality, int width, int height): +DialogRender::DialogRender(QWidget *parent): QDialog(parent) { - QScrollArea* scroll; - pixbuf = new QImage(1, 1, QImage::Format_ARGB32); _current_dialog = this; + render_thread = NULL; setModal(true); setWindowTitle("Paysages 3D - Render"); setLayout(new QVBoxLayout()); scroll = new QScrollArea(this); - scroll->setMinimumSize(width > 800 ? 850 : width + 50, height > 600 ? 650 : height + 50); scroll->setAlignment(Qt::AlignCenter); area = new RenderArea(scroll); scroll->setWidget(area); @@ -87,7 +85,10 @@ DialogRender::DialogRender(QWidget *parent, int quality, int width, int height): progress->setValue(0); layout()->addWidget(progress); progress_value = 0; +} +void DialogRender::startRender(int quality, int width, int height) +{ renderSetSize(width, height); autoSetRenderQuality(quality); @@ -95,28 +96,28 @@ DialogRender::DialogRender(QWidget *parent, int quality, int width, int height): render_thread = new RenderThread(); render_thread->start(); + + exec(); } -void DialogRender::closeEvent(QCloseEvent* e) +void DialogRender::loadLastRender() { - renderInterrupt(); - render_thread->wait(); + progress->hide(); + renderSetPreviewCallbacks(_renderResize, _renderClear, _renderDraw, _renderUpdate); + + exec(); +} - renderSetPreviewCallbacks(NULL, NULL, NULL, NULL); +DialogRender::~DialogRender() +{ + if (render_thread) + { + renderInterrupt(); + render_thread->wait(); - delete render_thread; + renderSetPreviewCallbacks(NULL, NULL, NULL, NULL); + + delete render_thread; + } delete pixbuf; } - -void DialogRender::reject() -{ - renderInterrupt(); - render_thread->wait(); - - renderSetPreviewCallbacks(NULL, NULL, NULL, NULL); - - delete render_thread; - delete pixbuf; - - QDialog::reject(); -} diff --git a/gui_qt/dialogrender.h b/gui_qt/dialogrender.h index 6b34063..633b8b7 100644 --- a/gui_qt/dialogrender.h +++ b/gui_qt/dialogrender.h @@ -4,23 +4,24 @@ #include #include #include +#include class DialogRender : public QDialog { Q_OBJECT public: - explicit DialogRender(QWidget *parent, int quality, int width, int height); + explicit DialogRender(QWidget *parent); + ~DialogRender(); + + void startRender(int quality, int width, int height); + void loadLastRender(); + QImage* pixbuf; QWidget* area; + QScrollArea* scroll; QProgressBar* progress; int progress_value; -public slots: - virtual void reject(); - -protected: - virtual void closeEvent(QCloseEvent* e); - private: QThread* render_thread; }; diff --git a/gui_qt/formnoise.cpp b/gui_qt/formnoise.cpp deleted file mode 100644 index 5d2c013..0000000 --- a/gui_qt/formnoise.cpp +++ /dev/null @@ -1,94 +0,0 @@ -#include "formnoise.h" - -#include "tools.h" - -#include -#include -#include - -#include "../lib_paysages/sky.h" -#include "../lib_paysages/shared/functions.h" -#include "../lib_paysages/shared/constants.h" - -/**************** Previews ****************/ -class PreviewLevel:public Preview -{ -public: - PreviewLevel(QWidget* parent, NoiseGenerator* noise): - Preview(parent), - _noise(noise) - { - } -protected: - QColor getColor(double x, double y) - { - /*Vector3 eye = {0.0, 0.0, 0.0}; - Vector3 look; - - look.x = cos(M_PI * (x / 128.0 + 0.5)) * cos(M_PI * (y / 256.0)); - look.y = -sin(M_PI * (y / 256.0)); - look.z = sin(M_PI * (x / 128.0 + 0.5)) * cos(M_PI * (y / 256.0)); - - return colorToQColor(skyGetColorCustom(eye, look, &_definition, NULL, NULL));*/ - } -private: - NoiseGenerator* _noise; -}; - -class PreviewTotal:public Preview -{ -public: - PreviewTotal(QWidget* parent, NoiseGenerator* noise): - Preview(parent), - _noise(noise) - { - } -protected: - QColor getColor(double x, double y) - { - if (y > noiseGet1DTotal(_noise, x)) - { - return QColor(255, 255, 255); - } - else - { - return QColor(0, 0, 0); - } - } -private: - NoiseGenerator* _noise; -}; - -/**************** Form ****************/ -FormNoise::FormNoise(QWidget *parent, NoiseGenerator* noise): - BaseForm(parent) -{ - /*_definition = skyCreateDefinition();*/ - - previewLevel = new PreviewLevel(this, noise); - addPreview(previewLevel, QString("Level preview")); - previewTotal = new PreviewTotal(this, noise); - addPreview(previewTotal, QString("Total preview")); - - /*addInputDouble("Day time", &_definition.daytime, 0.0, 1.0, 0.01, 0.1);*/ - - revertConfig(); -} - -void FormNoise::revertConfig() -{ - /*skyCopyDefinition(skyGetDefinition(), &_definition); - BaseForm::revertConfig();*/ -} - -void FormNoise::applyConfig() -{ - /*skySetDefinition(_definition); - BaseForm::applyConfig();*/ -} - -void FormNoise::applyConfigPreview() -{ - /*skyValidateDefinition(&_definition); - BaseForm::applyConfigPreview();*/ -} diff --git a/gui_qt/formnoise.h b/gui_qt/formnoise.h deleted file mode 100644 index ad334cd..0000000 --- a/gui_qt/formnoise.h +++ /dev/null @@ -1,30 +0,0 @@ -#ifndef _PAYSAGES_QT_FORMNOISE_H_ -#define _PAYSAGES_QT_FORMNOISE_H_ - -#include -#include "preview.h" -#include "baseform.h" - -#include "../lib_paysages/shared/types.h" - -class FormNoise : public BaseForm -{ - Q_OBJECT - -public: - FormNoise(QWidget* parent, NoiseGenerator* noise); - -public slots: - virtual void revertConfig(); - virtual void applyConfig(); - -protected slots: - virtual void applyConfigPreview(); - -private: - Preview* previewLevel; - Preview* previewTotal; - NoiseGenerator* generator; -}; - -#endif diff --git a/gui_qt/formrender.cpp b/gui_qt/formrender.cpp index 4fe13d6..3ba2caa 100644 --- a/gui_qt/formrender.cpp +++ b/gui_qt/formrender.cpp @@ -1,6 +1,10 @@ #include "formrender.h" +#include +#include + #include "dialogrender.h" +#include "../lib_paysages/shared/functions.h" /**************** Form ****************/ FormRender::FormRender(QWidget *parent) : @@ -27,18 +31,28 @@ FormRender::FormRender(QWidget *parent) : void FormRender::startRender() { - DialogRender* dialog = new DialogRender(this, _quality, _width, _height); - dialog->exec(); - + DialogRender* dialog = new DialogRender(this); + dialog->startRender(_quality, _width, _height); + delete dialog; } void FormRender::showRender() { - + DialogRender* dialog = new DialogRender(this); + dialog->loadLastRender(); + + delete dialog; } void FormRender::saveRender() { - + QString filepath; + + filepath = QFileDialog::getSaveFileName(this, "Choose a filename to save the last render"); + if (!filepath.isNull()) + { + renderSaveToFile((char*)filepath.toStdString().c_str()); + QMessageBox::information(this, "Message", "The picture " + filepath + " has been saved."); + } } diff --git a/gui_qt/mainwindow.cpp b/gui_qt/mainwindow.cpp index 900d77a..13e9f91 100644 --- a/gui_qt/mainwindow.cpp +++ b/gui_qt/mainwindow.cpp @@ -20,8 +20,6 @@ int main(int argc, char** argv) window.show(); - Preview::startUpdater(); - return app.exec(); } @@ -87,5 +85,8 @@ void MainWindow::fileLoad() void MainWindow::quickPreview() { - DialogRender(this, 3, 400, 300).exec(); + DialogRender* dialog = new DialogRender(this); + dialog->startRender(3, 400, 300); + + delete dialog; } diff --git a/gui_qt/paysages-qt.pro b/gui_qt/paysages-qt.pro index b2cc76d..27eac72 100644 --- a/gui_qt/paysages-qt.pro +++ b/gui_qt/paysages-qt.pro @@ -27,7 +27,6 @@ HEADERS += ../lib_paysages/shared/functions.h ../lib_paysages/shared/types.h \ dialognoise.h \ inputcolorgradation.h \ formsky.h \ - formnoise.h \ inputnoise.h \ tools.h FORMS += @@ -45,5 +44,4 @@ SOURCES += \ dialognoise.cpp \ inputcolorgradation.cpp \ formsky.cpp \ - formnoise.cpp \ inputnoise.cpp diff --git a/gui_qt/preview.cpp b/gui_qt/preview.cpp index a256b18..2a86015 100644 --- a/gui_qt/preview.cpp +++ b/gui_qt/preview.cpp @@ -1,7 +1,6 @@ #include "preview.h" #include #include -#include #include static QVector _previews; @@ -9,22 +8,29 @@ static QVector _previews; class PreviewDrawer:public QThread { public: - PreviewDrawer() + PreviewDrawer(Preview* preview): + QThread(), + _preview(preview) { + _running = false; + } + void askStop() + { + _running = false; } protected: void run() { - while (true) + _running = true; + while (_running) { - QVectorIterator iter(_previews); - while (iter.hasNext()) - { - iter.next()->doRender(); - } + _preview->doRender(); QThread::usleep(50000); } } +private: + Preview* _preview; + bool _running; }; Preview::Preview(QWidget* parent) : @@ -51,22 +57,17 @@ Preview::Preview(QWidget* parent) : this->setMaximumSize(256, 256); this->resize(256, 256); - _previews.append(this); + this->updater = new PreviewDrawer(this); + this->updater->start(); } Preview::~Preview() { - _previews.remove(_previews.indexOf(this)); - - lock->lock(); - alive = true; + alive = false; + ((PreviewDrawer*)updater)->askStop(); + updater->wait(); + delete updater; delete pixbuf; - lock->unlock(); -} - -void Preview::startUpdater() -{ - (new PreviewDrawer())->start(); } void Preview::doRender() diff --git a/gui_qt/preview.h b/gui_qt/preview.h index 715818b..554c169 100644 --- a/gui_qt/preview.h +++ b/gui_qt/preview.h @@ -4,6 +4,7 @@ #include #include #include +#include class Preview:public QWidget { @@ -47,6 +48,8 @@ protected: bool alive; bool need_rerender; bool need_render; +private: + QThread* updater; }; #endif diff --git a/lib_paysages/Makefile b/lib_paysages/Makefile index 0fa5196..0c08d99 100644 --- a/lib_paysages/Makefile +++ b/lib_paysages/Makefile @@ -2,7 +2,7 @@ SOURCES=$(wildcard *.c) OBJECTS=${SOURCES:.c=.o} HEADERS=$(wildcard shared/*.h *.h) RESULT=libpaysages.so -CC_FLAGS=-g -pg -Wall $(shell pkg-config --cflags glib-2.0 gthread-2.0) +CC_FLAGS=-g -pg -Wall -fPIC $(shell pkg-config --cflags glib-2.0 gthread-2.0) CC_LDFLAGS=$(shell pkg-config --libs glib-2.0 gthread-2.0) -lIL -lILU all:${RESULT} diff --git a/lib_paysages/render.c b/lib_paysages/render.c index 04331ad..af0d262 100644 --- a/lib_paysages/render.c +++ b/lib_paysages/render.c @@ -260,6 +260,24 @@ static void _processDirtyPixels() _dirty_count = 0; } +static void _setAllDirty() +{ + int x, y; + + _dirty_left = 0; + _dirty_right = render_width - 1; + _dirty_down = 0; + _dirty_up = render_height - 1; + + for (y = _dirty_down; y <= _dirty_up; y++) + { + for (x = _dirty_left; x <= _dirty_right; x++) + { + (render_zone + y * render_width + x)->dirty = 1; + } + } +} + void renderAddFragment(RenderFragment* fragment) { Array* pixel_data; @@ -781,8 +799,14 @@ void renderSetPreviewCallbacks(PreviewCallbackResize resize, PreviewCallbackClea _cb_preview_clear = clear ? clear : _previewClear; _cb_preview_draw = draw ? draw : _previewDraw; _cb_preview_update = update ? update : _previewUpdate; + _cb_preview_resize(render_width, render_height); - /* TODO Send all pixels ? */ + _cb_preview_clear(background_color); + + _setAllDirty(); + _processDirtyPixels(); + + _cb_preview_update(1.0); } int renderSetNextProgressStep(double start, double end)