[nostalgia] Switch to strong int Error

This commit is contained in:
Gary Talent 2019-07-28 00:32:42 -05:00
parent f4b336dd77
commit b107dc756b
21 changed files with 204 additions and 133 deletions

View File

@ -1,7 +1,7 @@
{
"log_functions": [
{
"function": "ox::trace::gdblogger::captureLogFunc",
"function": "oxTraceHook",
"ignore_frames": 3,
"file_var": "file",
"line_var": "line",

View File

@ -38,7 +38,7 @@ class Bounds {
template<typename T>
ox::Error model(T *io, Bounds *obj) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("nostalgia::common::Bounds", 4);
err |= io->field("x", &obj->x);
err |= io->field("y", &obj->y);

View File

@ -60,7 +60,7 @@ class Point {
template<typename T>
ox::Error model(T *io, Point *obj) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("nostalgia::common::Bounds", 2);
err |= io->field("x", &obj->x);
err |= io->field("y", &obj->y);

View File

@ -1,5 +1,3 @@
cmake_minimum_required(VERSION 2.8.11)
if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA")
enable_language(C ASM)
set(

View File

@ -12,7 +12,7 @@
namespace nostalgia::core {
ox::Error init(Context *ctx) {
ox::Error err = 0;
auto err = OxError(0);
err = initGfx(ctx);
initHeap(); // this does nothing in userland builds
return err;

View File

@ -11,15 +11,15 @@
namespace nostalgia::core {
ox::Error initGfx(Context*) {
return 1;
return OxError(1);
}
ox::Error initConsole(Context*) {
return 1;
return OxError(1);
}
ox::Error loadTileSheet(Context*, InodeId_t) {
return 1;
return OxError(1);
}
void puts(Context*, int, const char*) {

View File

@ -1,8 +1,17 @@
add_executable(
nostalgia
main.cpp
)
if(NOSTALGIA_BUILD_TYPE STREQUAL "GBA")
enable_language(C ASM)
add_executable(
nostalgia
main.cpp
startup.s
)
else()
add_executable(
nostalgia
main.cpp
)
endif()
if(COMMAND OBJCOPY_FILE)
set_target_properties(nostalgia

View File

@ -14,25 +14,23 @@ using namespace nostalgia::core;
using namespace nostalgia::world;
int run() {
while(1);
ox::FileSystem32 fs(ox::FileStore32(loadRom(), 32 * ox::units::MB));
Context ctx;
init(&ctx);
ctx.rom = &fs;
Zone zone(&ctx, Bounds{0, 0, 40, 40}, 102);
zone.draw(&ctx);
//Zone zone(&ctx, Bounds{0, 0, 40, 40}, 102);
//zone.draw(&ctx);
while (1);
return 0;
}
#ifndef OX_USE_STDLIB
extern "C"
void _start() {
extern "C" void _start() {
run();
}
#else
int main() {

View File

@ -18,7 +18,7 @@ ox::Error JsonReader::field(QString fieldName, int *dest) {
if (m_src.contains(fieldName)) {
return field(m_src[fieldName], dest);
} else {
return JSON_ERR_FIELD_MISSING;
return OxError(JSON_ERR_FIELD_MISSING);
}
}
@ -26,7 +26,7 @@ ox::Error JsonReader::field(QString fieldName, bool *dest) {
if (m_src.contains(fieldName)) {
return field(m_src[fieldName], dest);
} else {
return JSON_ERR_FIELD_MISSING;
return OxError(JSON_ERR_FIELD_MISSING);
}
}
@ -34,7 +34,7 @@ ox::Error JsonReader::field(QString fieldName, double *dest) {
if (m_src.contains(fieldName)) {
return field(m_src[fieldName], dest);
} else {
return JSON_ERR_FIELD_MISSING;
return OxError(JSON_ERR_FIELD_MISSING);
}
}
@ -42,7 +42,7 @@ ox::Error JsonReader::field(QString fieldName, QString *dest) {
if (m_src.contains(fieldName)) {
return field(m_src[fieldName], dest);
} else {
return JSON_ERR_FIELD_MISSING;
return OxError(JSON_ERR_FIELD_MISSING);
}
}
@ -51,36 +51,36 @@ ox::Error JsonReader::field(QString fieldName, QString *dest) {
ox::Error JsonReader::field(QJsonValueRef src, int *dest) {
if (src.isDouble()) {
*dest = src.toInt();
return 0;
return OxError(0);
} else {
return JSON_ERR_UNEXPECTED_TYPE;
return OxError(JSON_ERR_UNEXPECTED_TYPE);
}
}
ox::Error JsonReader::field(QJsonValueRef src, bool *dest) {
if (src.isBool()) {
*dest = src.toBool();
return 0;
return OxError(0);
} else {
return JSON_ERR_UNEXPECTED_TYPE;
return OxError(JSON_ERR_UNEXPECTED_TYPE);
}
}
ox::Error JsonReader::field(QJsonValueRef src, double *dest) {
if (src.isDouble()) {
*dest = src.toDouble();
return 0;
return OxError(0);
} else {
return JSON_ERR_UNEXPECTED_TYPE;
return OxError(JSON_ERR_UNEXPECTED_TYPE);
}
}
ox::Error JsonReader::field(QJsonValueRef src, QString *dest) {
if (src.isString()) {
*dest = src.toString();
return 0;
return OxError(0);
} else {
return JSON_ERR_UNEXPECTED_TYPE;
return OxError(JSON_ERR_UNEXPECTED_TYPE);
}
}

View File

@ -16,8 +16,7 @@
#include "json_err.hpp"
namespace nostalgia {
namespace studio {
namespace nostalgia::studio {
class JsonReader {
@ -64,21 +63,21 @@ ox::Error JsonReader::field(QString fieldName, T *dest) {
auto reader = JsonReader(obj);
return model(&reader, dest);
} else {
return JSON_ERR_FIELD_MISSING;
return OxError(JSON_ERR_FIELD_MISSING);
}
}
template<typename T>
ox::Error JsonReader::field(QString fieldName, QVector<T> *dest) {
ox::Error err = 0;
auto err = OxError(0);
if (m_src.contains(fieldName)) {
auto a = m_src[fieldName].toArray();
dest->resize(a.size());
for (int i = 0; i < dest->size(); i++) {
err |= field(a[i], &(*dest)[i]);
oxReturnError(field(a[i], &(*dest)[i]));
}
} else {
err |= JSON_ERR_FIELD_MISSING;
err = OxError(JSON_ERR_FIELD_MISSING);
}
return err;
}
@ -98,4 +97,3 @@ ox::Error readJson(QString json, T *dest) {
}
}
}

View File

@ -7,6 +7,7 @@
*/
#include <iostream>
#include <ox/std/assert.hpp>
#include "json.hpp"
using namespace std;
@ -22,7 +23,7 @@ struct TestStructNest {
template<typename T>
Error model(T *io, TestStructNest *obj) {
Error err = 0;
auto err = OxError(0);
err |= io->setTypeInfo("TestStructNest", 4);
err |= io->field("Bool", &obj->Bool);
err |= io->field("Int", &obj->Int);
@ -41,7 +42,7 @@ struct TestStruct {
template<typename T>
Error model(T *io, TestStruct *obj) {
Error err = 0;
auto err = OxError(0);
err |= io->setTypeInfo("TestStruct", 5);
err |= io->field("Bool", &obj->Bool);
err |= io->field("Int", &obj->Int);
@ -52,7 +53,7 @@ Error model(T *io, TestStruct *obj) {
}
int main() {
int err = 0;
auto err = OxError(0);
QString json;
TestStruct ts = {
true,
@ -75,15 +76,15 @@ int main() {
cout << tsOut.Double << endl;
cout << tsOut.String.toStdString() << endl;
err |= !(tsOut.Bool) << 0;
err |= !(tsOut.Int == 42) << 1;
err |= !(tsOut.Double == 42.42) << 2;
err |= !(tsOut.String == "Test String") << 3;
oxAssert(tsOut.Bool, "Arg 1 failed");
oxAssert(tsOut.Int == 42, "Arg 2 failed");
oxAssert(tsOut.Double == 42.42, "Arg 3 failed");
oxAssert(tsOut.String == "Test String", "Arg 4 failed");
err |= !(tsOut.Struct.Bool) << 4;
err |= !(tsOut.Struct.Int == 42) << 5;
err |= !(tsOut.Struct.Double == 42.42) << 6;
err |= !(tsOut.Struct.String == "Test String") << 7;
oxAssert(tsOut.Struct.Bool, "Arg 5 failed");
oxAssert(tsOut.Struct.Int == 42, "Arg 6 failed");
oxAssert(tsOut.Struct.Double == 42.42, "Arg 7 failed");
oxAssert(tsOut.Struct.String == "Test String", "Arg 8 failed");
return err;
return static_cast<int>(err);
}

View File

@ -8,31 +8,29 @@
#include "json_write.hpp"
namespace nostalgia {
namespace studio {
namespace nostalgia::studio {
JsonWriter::JsonWriter(QJsonObject &obj): m_dest(obj) {
}
ox::Error JsonWriter::field(QString fieldName, int *src) {
m_dest[fieldName] = *src;
return 0;
return OxError(0);
}
ox::Error JsonWriter::field(QString fieldName, bool *src) {
m_dest[fieldName] = *src;
return 0;
return OxError(0);
}
ox::Error JsonWriter::field(QString fieldName, double *src) {
m_dest[fieldName] = *src;
return 0;
return OxError(0);
}
ox::Error JsonWriter::field(QString fieldName, QString *src) {
m_dest[fieldName] = *src;
return 0;
return OxError(0);
}
}
}

View File

@ -16,8 +16,7 @@
#include "json_err.hpp"
namespace nostalgia {
namespace studio {
namespace nostalgia::studio {
class JsonWriter {
@ -56,7 +55,7 @@ ox::Error JsonWriter::field(QString fieldName, T *src) {
template<typename T>
ox::Error JsonWriter::field(QString fieldName, QVector<T> *src) {
ox::Error err = 0;
auto err = OxError(0);
QJsonArray a;
for (int i = 0; i < src->size(); i++) {
err |= field(a[i], &src->at(i));
@ -75,4 +74,3 @@ ox::Error writeJson(QString *json, T *src) {
}
}
}

View File

@ -36,9 +36,9 @@ struct NostalgiaStudioState {
template<typename T>
ox::Error model(T *io, NostalgiaStudioState *obj) {
ox::Error err = 0;
err |= io->setTypeInfo("NostalgiaStudioState", 1);
err |= io->field("project_path", &obj->projectPath);
auto err = OxError(0);
oxReturnError(io->setTypeInfo("NostalgiaStudioState", 1));
oxReturnError(io->field("project_path", &obj->projectPath));
return err;
}
@ -50,10 +50,10 @@ struct NostalgiaStudioPluginDef {
template<typename T>
ox::Error model(T *io, NostalgiaStudioPluginDef *obj) {
ox::Error err = 0;
err |= io->setTypeInfo("NostalgiaStudioPluginDef", 2);
err |= io->field("dir", &obj->dir);
err |= io->field("lib_name", &obj->libName);
auto err = OxError(0);
oxReturnError(io->setTypeInfo("NostalgiaStudioPluginDef", 2));
oxReturnError(io->field("dir", &obj->dir));
oxReturnError(io->field("lib_name", &obj->libName));
return err;
}
@ -66,11 +66,11 @@ struct NostalgiaStudioProfile {
template<typename T>
ox::Error model(T *io, NostalgiaStudioProfile *obj) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("NostalgiaStudioProfile", 3);
err |= io->field("app_name", &obj->appName);
err |= io->field("org_name", &obj->orgName);
err |= io->field("plugins_path", &obj->pluginsPath);
oxReturnError(io->field("app_name", &obj->appName));
oxReturnError(io->field("org_name", &obj->orgName));
oxReturnError(io->field("plugins_path", &obj->pluginsPath));
return err;
}

View File

@ -1,10 +1,9 @@
cmake_minimum_required(VERSION 2.8.11)
set(CMAKE_INCLUDE_CURRENT_DIR ON)
set(CMAKE_AUTOMOC ON)
add_library(
NostalgiaTool SHARED
pack.cpp
tilesetimport.cpp
)
@ -19,6 +18,7 @@ target_link_libraries(
install(
FILES
pack.hpp
tilesetimport.hpp
DESTINATION
include/nostalgia/tools/lib

View File

@ -0,0 +1,27 @@
/*
* Copyright 2016 - 2018 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#include <QDebug>
#include <QDir>
#include <ox/fs/fs.hpp>
#include "tilesetimport.hpp"
namespace nostalgia {
ox::Error pack(QDir src, ox::FileSystem *dest) {
for (auto entry : src.entryList()) {
qDebug() << entry;
oxReturnError(importTileSet(dest, entry, entry));
}
return OxError(0);
}
}

View File

@ -0,0 +1,20 @@
/*
* Copyright 2016 - 2018 gtalent2@gmail.com
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <QDir>
#include <ox/fs/fs.hpp>
namespace nostalgia {
ox::Error pack(QDir src, ox::FileSystem *dest);
}

View File

@ -19,7 +19,6 @@
namespace nostalgia {
using namespace ox;
using namespace nostalgia::core;
using namespace nostalgia::common;
@ -30,8 +29,8 @@ using namespace nostalgia::common;
return (r << 10) | (g << 5) | (b << 0);
}
int pointToIdx(int w, int x, int y) {
const auto colLength = 64;
[[nodiscard]] constexpr int pointToIdx(int w, int x, int y) noexcept {
constexpr auto colLength = 64;
const auto rowLength = (w / 8) * colLength;
const auto colStart = colLength * (x / 8);
const auto rowStart = rowLength * (y / 8);
@ -40,58 +39,81 @@ int pointToIdx(int w, int x, int y) {
return colStart + colOffset + rowStart + rowOffset;
}
Error importTileSet(FileSystem *fs, QString romPath, QString importPath, int bpp) {
Error err = 0;
[[nodiscard]] ox::ValErr<int> countColors(QString importPath) {
QImage src(importPath);
if (!src.isNull()) {
QMap<QRgb, int> colors;
auto tileCount = (src.width() * src.height()) / 64;
const auto imgDataBuffSize = sizeof(GbaImageData) + 1 + tileCount * 64;
QVector<uint8_t> imgDataBuff(imgDataBuffSize);
memset(imgDataBuff.data(), 0, imgDataBuffSize);
GbaImageData *id = reinterpret_cast<GbaImageData*>(imgDataBuff.data());
id->header.bpp = bpp;
id->header.tileCount = tileCount;
int colorId = 0;
// copy pixels as color ids
for (int x = 0; x < src.width(); x++) {
for (int y = 0; y < src.height(); y++) {
auto destI = pointToIdx(src.width(), x, y);
auto c = src.pixel(x, y);
// assign color a color id for the palette
if (!colors.contains(c)) {
colors[c] = colorId;
colorId++;
}
// set pixel color
if (bpp == 4) {
if (destI % 2) { // is odd number pixel
id->tiles[destI / 2] |= colors[c] << 4;
} else {
id->tiles[destI / 2] |= colors[c];
}
} else {
id->tiles[destI] = colors[c];
}
}
}
// store colors in palette with the corresponding color id
for (auto key : colors.keys()) {
auto colorId = colors[key];
id->pal[colorId] = toGbaColor(key);
}
if (!err) {
err |= fs->write(romPath.toUtf8().data(), imgDataBuff.data(), imgDataBuffSize);
}
} else {
err = 4;
return {{}, OxError(1)};
}
return err;
QMap<QRgb, bool> colors;
// copy pixels as color ids
for (int x = 0; x < src.width(); x++) {
for (int y = 0; y < src.height(); y++) {
auto c = src.pixel(x, y);
// assign color a color id for the palette
if (!colors.contains(c)) {
colors[c] = true;
}
}
}
return colors.size();
}
[[nodiscard]] ox::ValErr<QVector<uint8_t>> convertImg(QString importPath, int bpp) {
QImage src(importPath);
if (!src.isNull()) {
return {{}, OxError(1)};
}
QMap<QRgb, int> colors;
auto tileCount = (src.width() * src.height()) / 64;
const auto imgDataBuffSize = sizeof(GbaImageData) + 1 + tileCount * 64;
QVector<uint8_t> imgDataBuff(imgDataBuffSize);
memset(imgDataBuff.data(), 0, imgDataBuffSize);
GbaImageData *id = reinterpret_cast<GbaImageData*>(imgDataBuff.data());
id->header.bpp = bpp;
id->header.tileCount = tileCount;
int colorId = 0;
// copy pixels as color ids
for (int x = 0; x < src.width(); x++) {
for (int y = 0; y < src.height(); y++) {
auto destI = pointToIdx(src.width(), x, y);
auto c = src.pixel(x, y);
// assign color a color id for the palette
if (!colors.contains(c)) {
colors[c] = colorId;
colorId++;
}
// set pixel color
if (bpp == 4) {
if (destI % 2) { // is odd number pixel
id->tiles[destI / 2] |= colors[c] << 4;
} else {
id->tiles[destI / 2] |= colors[c];
}
} else {
id->tiles[destI] = colors[c];
}
}
}
// store colors in palette with the corresponding color id
for (auto key : colors.keys()) {
auto colorId = colors[key];
id->pal[colorId] = toGbaColor(key);
}
return imgDataBuff;
}
ox::Error importTileSet(ox::FileSystem *fs, QString romPath, QString importPath) {
const auto bpp = countColors(importPath) > 16 ? 8 : 4;
auto [imgDataBuff, err] = convertImg(importPath, bpp);
oxReturnError(err);
return fs->write(romPath.toUtf8().data(), imgDataBuff.data(), imgDataBuff.size());
}
}

View File

@ -8,10 +8,12 @@
#include <QString>
#include <ox/std/types.hpp>
#include <ox/fs/fs.hpp>
namespace nostalgia {
ox::Error importTileSet(FileSystem *fs, QString romPath, QString importPath, int bpp);
[[nodiscard]] ox::ValErr<int> countColors(QString importPath);
ox::Error importTileSet(ox::FileSystem *fs, QString romPath, QString importPath);
}

View File

@ -66,7 +66,7 @@ namespace {
[[nodiscard]] ox::Error verifyFile(ox::FileSystem32 *fs, const std::string &path, const std::vector<char> &expected) noexcept {
std::vector<char> buff(expected.size());
oxReturnError(fs->read(path.c_str(), buff.data(), buff.size()));
return buff == expected ? 0 : OxError(1);
return OxError(buff == expected ? 0 : 1);
}
[[nodiscard]] ox::Error copy(ox::PassThroughFS *src, ox::FileSystem32 *dest, std::string path) {

View File

@ -26,7 +26,7 @@ struct Tile {
template<typename T>
ox::Error modelRead(T *io, Tile *obj) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("nostalgia::world::Tile", Tile::Fields);
err |= io->field("bgTile", &obj->bgTile);
err |= io->field("type", &obj->type);
@ -62,7 +62,7 @@ struct Zone {
template<typename T>
ox::Error modelRead(T *io, Zone *obj) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("nostalgia::world::Zone", Zone::Fields);
err |= io->field("bounds", &obj->m_bounds);
return err;
@ -70,7 +70,7 @@ ox::Error modelRead(T *io, Zone *obj) {
template<typename T>
ox::Error modelWrite(T *io, Zone *obj) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("nostalgia::world::Zone", Zone::Fields);
err |= io->field("bounds", &obj->m_bounds);
return err;
@ -95,14 +95,14 @@ struct Region {
template<typename T>
ox::Error modelRead(T *io, Region*) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("nostalgia::World::Region", Region::Fields);
return err;
}
template<typename T>
ox::Error modelWrite(T *io, Region*) {
ox::Error err = 0;
auto err = OxError(0);
io->setTypeInfo("nostalgia::World::Region", Region::Fields);
return err;
}