Add .clang-format

This commit is contained in:
Fabien Freling 2019-07-16 13:31:07 +02:00
parent 90526946e6
commit 20041deef5
6 changed files with 158 additions and 143 deletions

6
src/qt/.clang-format Normal file
View file

@ -0,0 +1,6 @@
---
BasedOnStyle: Mozilla
IndentWidth: 4
TabWidth: 4
ColumnLimit: 100
UseTab: ForIndentation

View file

@ -8,21 +8,22 @@
#include <caml/callback.h> #include <caml/callback.h>
int main(int argc, char *argv[]) int
main(int argc, char* argv[])
{ {
QCoreApplication::setApplicationName("OBoy"); QCoreApplication::setApplicationName("OBoy");
QCoreApplication::setOrganizationName("ffreling"); QCoreApplication::setOrganizationName("ffreling");
QCoreApplication::setOrganizationDomain("ffreling.com"); QCoreApplication::setOrganizationDomain("ffreling.com");
QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling); QCoreApplication::setAttribute(Qt::AA_EnableHighDpiScaling);
QGuiApplication app(argc, argv); QGuiApplication app(argc, argv);
caml_main(argv); caml_main(argv);
qmlRegisterType<OBoy>("com.oboy.oboy", 1, 0, "OBoy"); qmlRegisterType<OBoy>("com.oboy.oboy", 1, 0, "OBoy");
QQmlApplicationEngine engine; QQmlApplicationEngine engine;
engine.addImageProvider(QLatin1String("oboy"), new OImageProvider(engine)); engine.addImageProvider(QLatin1String("oboy"), new OImageProvider(engine));
engine.load(QUrl(QStringLiteral("qrc:/main.qml"))); engine.load(QUrl(QStringLiteral("qrc:/main.qml")));
return app.exec(); return app.exec();
} }

View file

@ -4,126 +4,135 @@
#include <QUrl> #include <QUrl>
#include <QtGlobal> #include <QtGlobal>
#include <caml/mlvalues.h>
#include <caml/callback.h>
#include <caml/alloc.h> #include <caml/alloc.h>
#include <caml/bigarray.h> #include <caml/bigarray.h>
#include <caml/callback.h>
#include <caml/memory.h> #include <caml/memory.h>
#include <caml/misc.h> #include <caml/misc.h>
#include <caml/mlvalues.h>
#include <caml/osdeps.h> #include <caml/osdeps.h>
OBoy::OBoy(QObject *parent) : QObject(parent) OBoy::OBoy(QObject* parent)
: QObject(parent)
{ {
const QStringList args = QCoreApplication::arguments(); const QStringList args = QCoreApplication::arguments();
if (args.size() > 1) { if (args.size() > 1) {
const QUrl path = QUrl::fromLocalFile(args.at(1)); const QUrl path = QUrl::fromLocalFile(args.at(1));
load(path); load(path);
} }
} }
value *fetch_caml_callback(const char *name) { value*
value * closure_f = caml_named_value(name); fetch_caml_callback(const char* name)
if (closure_f == nullptr) { {
qFatal("Cannot find OCaml function: %s", name); value* closure_f = caml_named_value(name);
} if (closure_f == nullptr) {
return closure_f; qFatal("Cannot find OCaml function: %s", name);
}
return closure_f;
} }
// https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html // https://caml.inria.fr/pub/docs/manual-ocaml/intfc.html
// http://www.mega-nerd.com/erikd/Blog/CodeHacking/Ocaml/calling_ocaml.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"); static value* closure_f = fetch_caml_callback("oboy_name");
if (closure_f == nullptr) { if (closure_f == nullptr) {
CAMLdrop; CAMLdrop;
return QString("<Unreachable>"); return QString("<Unreachable>");
} }
const char *str = String_val(caml_callback(*closure_f, Val_unit)); const char* str = String_val(caml_callback(*closure_f, Val_unit));
CAMLdrop; CAMLdrop;
return QString(str); return QString(str);
} }
QString OBoy::version() const QString
OBoy::version() const
{ {
CAMLparam0(); CAMLparam0();
static value * closure_f = fetch_caml_callback("oboy_version"); static value* closure_f = fetch_caml_callback("oboy_version");
if (closure_f == nullptr) { if (closure_f == nullptr) {
CAMLdrop; CAMLdrop;
return QString("<Unreachable>"); return QString("<Unreachable>");
} }
const char *str = String_val(caml_callback(*closure_f, Val_unit)); const char* str = String_val(caml_callback(*closure_f, Val_unit));
CAMLdrop; CAMLdrop;
return QString(str); return QString(str);
} }
bool OBoy::load(const QUrl &path) { bool
CAMLparam0(); OBoy::load(const QUrl& path)
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
{ {
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(); return _loaded;
CAMLlocal1(ocaml_index); }
ocaml_index = index;
QImage
static value * closure_f = fetch_caml_callback("oboy_bg_map"); OBoy::backgroundMap(int index) const
if (closure_f == nullptr) { {
CAMLdrop; CAMLparam0();
return QImage(0, 0, QImage::Format_Indexed8); CAMLlocal1(ocaml_index);
} ocaml_index = index;
const auto bg_array = Caml_ba_array_val(caml_callback(*closure_f, Val_int(ocaml_index))); static value* closure_f = fetch_caml_callback("oboy_bg_map");
const auto bg_raw_data = static_cast<caml_ba_uint8 *>(bg_array->data); if (closure_f == nullptr) {
CAMLdrop;
Q_ASSERT(bg_array->num_dims == 2); return QImage(0, 0, QImage::Format_Indexed8);
Q_ASSERT(bg_array->flags & CAML_BA_UINT8); }
const int width = bg_array->dim[0]; const auto bg_array = Caml_ba_array_val(caml_callback(*closure_f, Val_int(ocaml_index)));
const int height = bg_array->dim[1]; const auto bg_raw_data = static_cast<caml_ba_uint8*>(bg_array->data);
QImage img(width, height, QImage::Format_Indexed8);
Q_ASSERT(bg_array->num_dims == 2);
QVector<QRgb> green_palette = { Q_ASSERT(bg_array->flags & CAML_BA_UINT8);
qRgb(15, 56, 15),
qRgb(48, 98, 48), const int width = bg_array->dim[0];
qRgb(139, 172, 15), const int height = bg_array->dim[1];
qRgb(155, 188, 15), QImage img(width, height, QImage::Format_Indexed8);
};
img.setColorTable(green_palette); QVector<QRgb> green_palette = {
qRgb(15, 56, 15),
for (int y = 0; y < height; ++y) { qRgb(48, 98, 48),
for (int x = 0; x < width; ++x) { qRgb(139, 172, 15),
img.setPixel(x, y, bg_raw_data[y * width + x] % 4); qRgb(155, 188, 15),
} };
} img.setColorTable(green_palette);
CAMLdrop; for (int y = 0; y < height; ++y) {
return img; for (int x = 0; x < width; ++x) {
img.setPixel(x, y, bg_raw_data[y * width + x] % 4);
}
}
CAMLdrop;
return img;
} }

View file

@ -1,27 +1,27 @@
#pragma once #pragma once
#include <QObject>
#include <QImage> #include <QImage>
#include <QObject>
#include <QString> #include <QString>
class OBoy : public QObject class OBoy : public QObject
{ {
Q_OBJECT Q_OBJECT
Q_PROPERTY(QString name READ name CONSTANT) Q_PROPERTY(QString name READ name CONSTANT)
Q_PROPERTY(QString version READ version CONSTANT) Q_PROPERTY(QString version READ version CONSTANT)
Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged) Q_PROPERTY(bool loaded READ loaded NOTIFY loadedChanged)
public: public:
explicit OBoy(QObject *parent = nullptr); explicit OBoy(QObject* parent = nullptr);
QString name() const; QString name() const;
QString version() const; QString version() const;
bool loaded() const; bool loaded() const;
Q_INVOKABLE bool load(const QUrl &path); Q_INVOKABLE bool load(const QUrl& path);
QImage backgroundMap(int index) const; QImage backgroundMap(int index) const;
signals: signals:
void loadedChanged(bool loaded); void loadedChanged(bool loaded);
private: private:
bool _loaded = false; bool _loaded = false;
}; };

View file

@ -1,22 +1,21 @@
#include "oimageprovider.h" #include "oimageprovider.h"
#include "oboy.h" #include "oboy.h"
OImageProvider::OImageProvider(QQmlApplicationEngine &engine) OImageProvider::OImageProvider(QQmlApplicationEngine& engine)
: QQuickImageProvider(QQuickImageProvider::Image) : QQuickImageProvider(QQuickImageProvider::Image)
, _engine(engine) , _engine(engine)
{}
QImage
OImageProvider::requestImage(const QString& id, QSize* size, const QSize& requestedSize)
{ {
QList<QObject*> roots = _engine.rootObjects();
OBoy* oboy = roots[0]->findChild<OBoy*>("qml_oboy");
if (!oboy) {
qFatal("Cannot find oboy instance.");
}
Q_ASSERT(oboy->loaded());
} QImage img = oboy->backgroundMap(0);
return img;
QImage OImageProvider::requestImage(const QString &id, QSize *size, const QSize &requestedSize)
{
QList<QObject*> roots = _engine.rootObjects();
OBoy *oboy = roots[0]->findChild<OBoy*>("qml_oboy");
if (!oboy) {
qFatal("Cannot find oboy instance.");
}
Q_ASSERT(oboy->loaded());
QImage img = oboy->backgroundMap(0);
return img;
} }

View file

@ -5,10 +5,10 @@
class OImageProvider : public QQuickImageProvider class OImageProvider : public QQuickImageProvider
{ {
public: public:
OImageProvider(QQmlApplicationEngine &engine); OImageProvider(QQmlApplicationEngine& engine);
QImage requestImage(const QString &id, QSize *size, const QSize &requestedSize) override; QImage requestImage(const QString& id, QSize* size, const QSize& requestedSize) override;
private: private:
QQmlApplicationEngine &_engine; QQmlApplicationEngine& _engine;
}; };