[nostalgia] Switch to strong int Error

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

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) {