diff --git a/src/nostalgia/studio/CMakeLists.txt b/src/nostalgia/studio/CMakeLists.txt index 40cc2248..c6b5f8bb 100644 --- a/src/nostalgia/studio/CMakeLists.txt +++ b/src/nostalgia/studio/CMakeLists.txt @@ -21,6 +21,7 @@ target_link_libraries( NostalgiaCommon NostalgiaCore NostalgiaStudio + NostalgiaTool ) install( diff --git a/src/nostalgia/studio/lib/oxfstreeview.cpp b/src/nostalgia/studio/lib/oxfstreeview.cpp index 0e433af3..6c4691d4 100644 --- a/src/nostalgia/studio/lib/oxfstreeview.cpp +++ b/src/nostalgia/studio/lib/oxfstreeview.cpp @@ -101,7 +101,6 @@ Qt::ItemFlags OxFSModel::flags(const QModelIndex &index) const { } QVariant OxFSModel::headerData(int section, Qt::Orientation orientation, int role) const { - return QVariant(); if (orientation == Qt::Horizontal && role == Qt::DisplayRole) { return m_rootItem->data(section); } else { diff --git a/src/nostalgia/tools/CMakeLists.txt b/src/nostalgia/tools/CMakeLists.txt index be2c6a08..0014df93 100644 --- a/src/nostalgia/tools/CMakeLists.txt +++ b/src/nostalgia/tools/CMakeLists.txt @@ -23,3 +23,5 @@ install( RUNTIME DESTINATION bin ) + +add_subdirectory(lib) diff --git a/src/nostalgia/tools/lib/CMakeLists.txt b/src/nostalgia/tools/lib/CMakeLists.txt new file mode 100644 index 00000000..3bb7e77c --- /dev/null +++ b/src/nostalgia/tools/lib/CMakeLists.txt @@ -0,0 +1,33 @@ +cmake_minimum_required(VERSION 2.8.11) + +set(CMAKE_INCLUDE_CURRENT_DIR ON) +set(CMAKE_AUTOMOC ON) + +add_library( + NostalgiaTool + tilesetimport.cpp +) + +target_link_libraries( + NostalgiaTool + Qt5::Widgets + OxClArgs + OxFS + OxStd + NostalgiaCommon + NostalgiaCore +) + +install( + FILES + tilesetimport.hpp + DESTINATION + include/nostalgia/tools/lib +) + +install( + TARGETS + NostalgiaTool + LIBRARY DESTINATION lib/ox + ARCHIVE DESTINATION lib/ox +) diff --git a/src/nostalgia/tools/lib/tilesetimport.cpp b/src/nostalgia/tools/lib/tilesetimport.cpp new file mode 100644 index 00000000..7bdee5b2 --- /dev/null +++ b/src/nostalgia/tools/lib/tilesetimport.cpp @@ -0,0 +1,96 @@ +/* + * Copyright 2016-2017 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 +#include +#include + +#include +#include + +#include +#include + +namespace nostalgia { + +using namespace ox; +using namespace nostalgia::core; +using namespace nostalgia::common; + +uint16_t toGbaColor(QColor c) { + auto r = ((uint32_t) c.red()) >> 3; + auto g = ((uint32_t) c.green()) >> 3; + auto b = ((uint32_t) c.blue()) >> 3; + return (r << 10) | (g << 5) | (b << 0); +} + +int pointToIdx(int w, int x, int y) { + const auto colLength = 64; + const auto rowLength = (w / 8) * colLength; + const auto colStart = colLength * (x / 8); + const auto rowStart = rowLength * (y / 8); + const auto colOffset = x % 8; + const auto rowOffset = (y % 8) * 8; + return colStart + colOffset + rowStart + rowOffset; +} + +Error importTileSet(FileSystem *fs, QString romPath, QString importPath, int bpp) { + Error err = 0; + + QImage src(importPath); + if (!src.isNull()) { + QMap colors; + auto tileCount = (src.width() * src.height()) / 64; + const auto imgDataBuffSize = sizeof(GbaImageData) + 1 + tileCount * 64; + uint8_t imgDataBuff[imgDataBuffSize]; + memset(&imgDataBuff, 0, imgDataBuffSize); + GbaImageData *id = (GbaImageData*) imgDataBuff; + 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, imgDataBuffSize); + } + } else { + err = 4; + } + + return err; +} + +} diff --git a/src/nostalgia/tools/lib/tilesetimport.hpp b/src/nostalgia/tools/lib/tilesetimport.hpp new file mode 100644 index 00000000..382138c6 --- /dev/null +++ b/src/nostalgia/tools/lib/tilesetimport.hpp @@ -0,0 +1,17 @@ +/* + * Copyright 2016-2017 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 + +#include + +namespace nostalgia { + +ox::Error importTileSet(FileSystem *fs, QString romPath, QString importPath, int bpp); + +}