Started fixing texture tool (incomplete)

This commit is contained in:
2023-04-02 17:15:49 -07:00
parent 7cb9846e1b
commit b3f58ed0ec
14 changed files with 304 additions and 11 deletions

View File

@ -0,0 +1,53 @@
# Copyright (c) 2021 Dominic Msters
#
# This software is released under the MIT License.
# https://opensource.org/licenses/MIT
# Texture Build Tool
project(texturetool VERSION 1.0)
add_executable(texturetool)
target_sources(texturetool
PRIVATE
${DAWN_SHARED_SOURCES}
${DAWN_TOOL_SOURCES}
TextureTool.cpp
../util/Image.cpp
)
target_include_directories(texturetool
PUBLIC
${DAWN_SHARED_INCLUDES}
${DAWN_TOOL_INCLUDES}
${CMAKE_CURRENT_LIST_DIR}
)
# Definitions
target_compile_definitions(texturetool
PUBLIC
${DAWN_SHARED_DEFINITIONS}
DAWN_TOOL_INSTANCE=TextureTool
DAWN_TOOL_HEADER="TextureTool.hpp"
)
# Libraries
target_link_libraries(texturetool
PUBLIC
${DAWN_BUILD_HOST_LIBS}
stb
)
# Tool Function
function(tool_texture target in)
set(DEPS "")
if(DAWN_BUILD_TOOLS)
set(DEPS texturetool)
endif()
add_custom_target(${target}
COMMAND texturetool --input="${DAWN_ASSETS_SOURCE_DIR}/${in}" --output="${DAWN_ASSETS_BUILD_DIR}/${target}"
COMMENT "Generating texture ${target} from ${in}"
DEPENDS ${DEPS}
)
add_dependencies(${DAWN_TARGET_NAME} ${target})
endfunction()

View File

@ -0,0 +1,61 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#include "TextureTool.hpp"
using namespace Dawn;
std::vector<std::string> TextureTool::getRequiredFlags() {
return std::vector<std::string>{ "input", "output" };
}
int32_t TextureTool::start() {
// Finished with XML data, now we can write data out.
File fileOut(flags["output"] + ".texture");
if(fileOut.exists()) return 0;
// Load input file
File in(flags["input"]);
if(!in.open(FILE_MODE_READ)) return 1;
int w, h, channels;
auto dataImage = stbi_load_from_file(in.file, &w, &h, &channels, STBI_rgb_alpha);
if(dataImage == NULL) {
std::cout << "Failed to load input texture!" << std::endl;
return 1;
}
in.close();
// Convert to floating points
size_t len = STBI_rgb_alpha * w * h;
// Open and create output
File out(flags["output"] + ".texture");
if(!out.mkdirp()) {
std::cout << "Failed to make output dir " << out.filename << std::endl;
return 1;
}
if(!out.open(FILE_MODE_WRITE)) {
std::cout << "Failed to open texture file for writing " << out.filename << std::endl;
return 1;
}
// Write info
char headerBuffer[64];
size_t headerBufferLength = sprintf((char *)headerBuffer, "%i|%i|", w, h);
if(!out.writeRaw(headerBuffer, headerBufferLength)) {
std::cout << "Failed to write texture header for " << out.filename << std::endl;
return 1;
}
// Write texture
if(!out.writeRaw((char *)dataImage, sizeof(uint8_t) * len)) {
std::cout << "Failed to write texture data for " << out.filename << std::endl;
return 1;
}
stbi_image_free(dataImage);
return 0;
}

View File

@ -0,0 +1,19 @@
// Copyright (c) 2023 Dominic Masters
//
// This software is released under the MIT License.
// https://opensource.org/licenses/MIT
#pragma once
#include "util/DawnTool.hpp"
#include "util/File.hpp"
#include "util/Image.hpp"
namespace Dawn {
class TextureTool : public DawnTool {
protected:
std::vector<std::string> getRequiredFlags() override;
public:
int32_t start();
};
}