From 20041deef5dc1f676555ef465755e3fb911ae45b Mon Sep 17 00:00:00 2001 From: Fabien Freling Date: Tue, 16 Jul 2019 13:31:07 +0200 Subject: [PATCH] Add .clang-format --- src/qt/.clang-format | 6 ++ src/qt/main.cpp | 25 ++--- src/qt/oboy.cpp | 197 ++++++++++++++++++++------------------ src/qt/oboy.h | 32 +++---- src/qt/oimageprovider.cpp | 31 +++--- src/qt/oimageprovider.h | 10 +- 6 files changed, 158 insertions(+), 143 deletions(-) create mode 100644 src/qt/.clang-format diff --git a/src/qt/.clang-format b/src/qt/.clang-format new file mode 100644 index 0000000..8d4ef89 --- /dev/null +++ b/src/qt/.clang-format @@ -0,0 +1,6 @@ +--- +BasedOnStyle: Mozilla +IndentWidth: 4 +TabWidth: 4 +ColumnLimit: 100 +UseTab: ForIndentation diff --git a/src/qt/main.cpp b/src/qt/main.cpp index 7e3d945..e6b8d23 100644 --- a/src/qt/main.cpp +++ b/src/qt/main.cpp @@ -8,21 +8,22 @@ #include -int main(int argc, char *argv[]) +int +main(int argc, char* argv[]) { - QCoreApplication::setApplicationName("OBoy"); - QCoreApplication::setOrganizationName("ffreling"); - QCoreApplication::setOrganizationDomain("ffreling.com"); - QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); - QGuiApplication app(argc, argv); + QCoreApplication::setApplicationName("OBoy"); + QCoreApplication::setOrganizationName("ffreling"); + QCoreApplication::setOrganizationDomain("ffreling.com"); + QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); + QGuiApplication app(argc, argv); - caml_main(argv); + caml_main(argv); - qmlRegisterType("com.oboy.oboy", 1, 0, "OBoy"); + qmlRegisterType("com.oboy.oboy", 1, 0, "OBoy"); - QQmlApplicationEngine engine; - engine.addImageProvider(QLatin1String("oboy"), new OImageProvider(engine)); - engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); + QQmlApplicationEngine engine; + engine.addImageProvider(QLatin1String("oboy"), new OImageProvider(engine)); + engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); - return app.exec(); + return app.exec(); } diff --git a/src/qt/oboy.cpp b/src/qt/oboy.cpp index 54d7cb2..609413f 100644 --- a/src/qt/oboy.cpp +++ b/src/qt/oboy.cpp @@ -4,126 +4,135 @@ #include #include -#include -#include #include #include +#include #include #include +#include #include -OBoy::OBoy(QObject *parent) : QObject(parent) +OBoy::OBoy(QObject* parent) + : QObject(parent) { - const QStringList args = QCoreApplication::arguments(); - if (args.size() > 1) { - const QUrl path = QUrl::fromLocalFile(args.at(1)); - load(path); - } + const QStringList args = QCoreApplication::arguments(); + if (args.size() > 1) { + const QUrl path = QUrl::fromLocalFile(args.at(1)); + load(path); + } } -value *fetch_caml_callback(const char *name) { - value * closure_f = caml_named_value(name); - if (closure_f == nullptr) { - qFatal("Cannot find OCaml function: %s", name); - } - return closure_f; +value* +fetch_caml_callback(const char* name) +{ + value* closure_f = caml_named_value(name); + if (closure_f == nullptr) { + qFatal("Cannot find OCaml function: %s", name); + } + return closure_f; } // https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html // http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/calling_ocaml.html -QString OBoy::name() const +QString +OBoy::name() const { - CAMLparam0(); + CAMLparam0(); - static value * closure_f = fetch_caml_callback("oboy_name"); - if (closure_f == nullptr) { - CAMLdrop; - return QString(""); - } + static value* closure_f = fetch_caml_callback("oboy_name"); + if (closure_f == nullptr) { + CAMLdrop; + return QString(""); + } - const char *str = String_val(caml_callback(*closure_f, Val_unit)); - CAMLdrop; - return QString(str); + const char* str = String_val(caml_callback(*closure_f, Val_unit)); + CAMLdrop; + return QString(str); } -QString OBoy::version() const +QString +OBoy::version() const { - CAMLparam0(); + CAMLparam0(); - static value * closure_f = fetch_caml_callback("oboy_version"); - if (closure_f == nullptr) { - CAMLdrop; - return QString(""); - } + static value* closure_f = fetch_caml_callback("oboy_version"); + if (closure_f == nullptr) { + CAMLdrop; + return QString(""); + } - const char *str = String_val(caml_callback(*closure_f, Val_unit)); - CAMLdrop; - return QString(str); + const char* str = String_val(caml_callback(*closure_f, Val_unit)); + CAMLdrop; + return QString(str); } -bool OBoy::load(const QUrl &path) { - CAMLparam0(); - CAMLlocal1(ocaml_path) ; - - static value * closure_f = fetch_caml_callback("oboy_load"); - if (closure_f == nullptr) { - CAMLdrop; - return false; - } - - const QString local_path = path.toLocalFile(); - const QByteArray ba = local_path.toLocal8Bit(); - - ocaml_path = caml_copy_string_of_os(ba.data()); - - _loaded = Bool_val(caml_callback(*closure_f, ocaml_path)); - this->loadedChanged(_loaded); - CAMLdrop; - return _loaded; -} - -bool OBoy::loaded() const +bool +OBoy::load(const QUrl& path) { - return _loaded; + CAMLparam0(); + CAMLlocal1(ocaml_path); + + static value* closure_f = fetch_caml_callback("oboy_load"); + if (closure_f == nullptr) { + CAMLdrop; + return false; + } + + const QString local_path = path.toLocalFile(); + const QByteArray ba = local_path.toLocal8Bit(); + + ocaml_path = caml_copy_string_of_os(ba.data()); + + _loaded = Bool_val(caml_callback(*closure_f, ocaml_path)); + this->loadedChanged(_loaded); + CAMLdrop; + return _loaded; } -QImage OBoy::backgroundMap(int index) const +bool +OBoy::loaded() const { - CAMLparam0(); - CAMLlocal1(ocaml_index); - ocaml_index = index; - - static value * closure_f = fetch_caml_callback("oboy_bg_map"); - if (closure_f == nullptr) { - CAMLdrop; - return QImage(0, 0, QImage::Format_Indexed8); - } - - const auto bg_array = Caml_ba_array_val(caml_callback(*closure_f, Val_int(ocaml_index))); - const auto bg_raw_data = static_cast(bg_array->data); - - Q_ASSERT(bg_array->num_dims == 2); - Q_ASSERT(bg_array->flags & CAML_BA_UINT8); - - const int width = bg_array->dim[0]; - const int height = bg_array->dim[1]; - QImage img(width, height, QImage::Format_Indexed8); - - QVector green_palette = { - qRgb(15, 56, 15), - qRgb(48, 98, 48), - qRgb(139, 172, 15), - qRgb(155, 188, 15), - }; - img.setColorTable(green_palette); - - for (int y = 0; y < height; ++y) { - for (int x = 0; x < width; ++x) { - img.setPixel(x, y, bg_raw_data[y * width + x] % 4); - } - } - - CAMLdrop; - return img; + return _loaded; +} + +QImage +OBoy::backgroundMap(int index) const +{ + CAMLparam0(); + CAMLlocal1(ocaml_index); + ocaml_index = index; + + static value* closure_f = fetch_caml_callback("oboy_bg_map"); + if (closure_f == nullptr) { + CAMLdrop; + return QImage(0, 0, QImage::Format_Indexed8); + } + + const auto bg_array = Caml_ba_array_val(caml_callback(*closure_f, Val_int(ocaml_index))); + const auto bg_raw_data = static_cast(bg_array->data); + + Q_ASSERT(bg_array->num_dims == 2); + Q_ASSERT(bg_array->flags & CAML_BA_UINT8); + + const int width = bg_array->dim[0]; + const int height = bg_array->dim[1]; + QImage img(width, height, QImage::Format_Indexed8); + + QVector green_palette = { + qRgb(15, 56, 15), + qRgb(48, 98, 48), + qRgb(139, 172, 15), + qRgb(155, 188, 15), + }; + img.setColorTable(green_palette); + + for (int y = 0; y < height; ++y) { + for (int x = 0; x < width; ++x) { + img.setPixel(x, y, bg_raw_data[y * width + x] % 4); + } + } + + CAMLdrop; + return img; } diff --git a/src/qt/oboy.h b/src/qt/oboy.h index 432d243..51b2a1c 100644 --- a/src/qt/oboy.h +++ b/src/qt/oboy.h @@ -1,27 +1,27 @@ #pragma once -#include #include +#include #include class OBoy : public QObject { - Q_OBJECT - Q_PROPERTY(QString name READ name CONSTANT) - Q_PROPERTY(QString version READ version CONSTANT) - Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged) -public: - explicit OBoy(QObject *parent = nullptr); + Q_OBJECT + Q_PROPERTY(QString name READ name CONSTANT) + Q_PROPERTY(QString version READ version CONSTANT) + Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged) + public: + explicit OBoy(QObject* parent = nullptr); - QString name() const; - QString version() const; - bool loaded() const; - Q_INVOKABLE bool load(const QUrl &path); - QImage backgroundMap(int index) const; + QString name() const; + QString version() const; + bool loaded() const; + Q_INVOKABLE bool load(const QUrl& path); + QImage backgroundMap(int index) const; -signals: - void loadedChanged(bool loaded); + signals: + void loadedChanged(bool loaded); -private: - bool _loaded = false; + private: + bool _loaded = false; }; diff --git a/src/qt/oimageprovider.cpp b/src/qt/oimageprovider.cpp index 88a6780..6f72696 100644 --- a/src/qt/oimageprovider.cpp +++ b/src/qt/oimageprovider.cpp @@ -1,22 +1,21 @@ #include "oimageprovider.h" #include "oboy.h" -OImageProvider::OImageProvider(QQmlApplicationEngine &engine) - : QQuickImageProvider(QQuickImageProvider::Image) - , _engine(engine) +OImageProvider::OImageProvider(QQmlApplicationEngine& engine) + : QQuickImageProvider(QQuickImageProvider::Image) + , _engine(engine) +{} + +QImage +OImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize) { + QList roots = _engine.rootObjects(); + OBoy* oboy = roots[0]->findChild("qml_oboy"); + if (!oboy) { + qFatal("Cannot find oboy instance."); + } + Q_ASSERT(oboy->loaded()); -} - -QImage OImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize) -{ - QList roots = _engine.rootObjects(); - OBoy *oboy = roots[0]->findChild("qml_oboy"); - if (!oboy) { - qFatal("Cannot find oboy instance."); - } - Q_ASSERT(oboy->loaded()); - - QImage img = oboy->backgroundMap(0); - return img; + QImage img = oboy->backgroundMap(0); + return img; } diff --git a/src/qt/oimageprovider.h b/src/qt/oimageprovider.h index 0e2d689..5215f3c 100644 --- a/src/qt/oimageprovider.h +++ b/src/qt/oimageprovider.h @@ -5,10 +5,10 @@ class OImageProvider : public QQuickImageProvider { -public: - OImageProvider(QQmlApplicationEngine &engine); - QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override; + public: + OImageProvider(QQmlApplicationEngine& engine); + QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize) override; -private: - QQmlApplicationEngine &_engine; + private: + QQmlApplicationEngine& _engine; };