2021-08-16 16:35:36 -07:00
|
|
|
//
|
|
|
|
|
// main.cpp
|
|
|
|
|
// Kraken Engine
|
|
|
|
|
//
|
2022-04-03 21:56:23 -07:00
|
|
|
// Copyright 2022 Kearwood Gilbert. All rights reserved.
|
2021-08-16 16:35:36 -07:00
|
|
|
//
|
|
|
|
|
// Redistribution and use in source and binary forms, with or without modification, are
|
|
|
|
|
// permitted provided that the following conditions are met:
|
|
|
|
|
//
|
|
|
|
|
// 1. Redistributions of source code must retain the above copyright notice, this list of
|
|
|
|
|
// conditions and the following disclaimer.
|
|
|
|
|
//
|
|
|
|
|
// 2. Redistributions in binary form must reproduce the above copyright notice, this list
|
|
|
|
|
// of conditions and the following disclaimer in the documentation and/or other materials
|
|
|
|
|
// provided with the distribution.
|
|
|
|
|
//
|
|
|
|
|
// THIS SOFTWARE IS PROVIDED BY KEARWOOD GILBERT ''AS IS'' AND ANY EXPRESS OR IMPLIED
|
|
|
|
|
// WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
|
|
|
|
|
// FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL KEARWOOD GILBERT OR
|
|
|
|
|
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
|
|
|
|
|
// CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
|
|
|
|
|
// SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
|
|
|
|
|
// ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
|
|
|
|
|
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
|
|
|
|
|
// ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
|
|
|
|
//
|
|
|
|
|
// The views and conclusions contained in the software and documentation are those of the
|
|
|
|
|
// authors and should not be interpreted as representing official policies, either expressed
|
|
|
|
|
// or implied, of Kearwood Gilbert.
|
|
|
|
|
//
|
2019-07-20 13:55:16 -07:00
|
|
|
#include "main.h"
|
|
|
|
|
|
|
|
|
|
#include <stdio.h>
|
2020-08-09 19:05:43 -07:00
|
|
|
#include <vector>
|
|
|
|
|
#include <string>
|
|
|
|
|
#include <iostream>
|
|
|
|
|
#include <fstream>
|
2019-07-20 13:55:16 -07:00
|
|
|
#include "kraken.h"
|
|
|
|
|
|
|
|
|
|
using namespace kraken;
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
enum ResourceMapping
|
|
|
|
|
{
|
2020-08-06 18:15:58 -07:00
|
|
|
output_bundle = 0,
|
|
|
|
|
loaded_resource = 1,
|
|
|
|
|
shader_compile_log = 2,
|
|
|
|
|
};
|
|
|
|
|
|
2022-08-08 01:07:26 -07:00
|
|
|
int main(int argc, char* argv[])
|
2019-07-20 13:55:16 -07:00
|
|
|
{
|
2020-08-06 18:15:58 -07:00
|
|
|
bool failed = false;
|
2019-07-20 13:55:16 -07:00
|
|
|
printf("Kraken Convert\n");
|
|
|
|
|
printf("Initializing Kraken...\n");
|
|
|
|
|
KrInitializeInfo init_info = {};
|
|
|
|
|
init_info.sType = KR_STRUCTURE_TYPE_INITIALIZE;
|
2019-07-28 16:46:46 -07:00
|
|
|
init_info.resourceMapSize = 1024;
|
2022-09-21 23:55:24 -07:00
|
|
|
init_info.nodeMapSize = 1024;
|
2019-07-20 13:55:16 -07:00
|
|
|
KrResult res = KrInitialize(&init_info);
|
|
|
|
|
if (res != KR_SUCCESS) {
|
|
|
|
|
printf("Failed to initialize Kraken!\n");
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-28 16:46:46 -07:00
|
|
|
KrCreateBundleInfo create_bundle_info = {};
|
|
|
|
|
create_bundle_info.sType = KR_STRUCTURE_TYPE_CREATE_BUNDLE;
|
2020-08-06 18:15:58 -07:00
|
|
|
create_bundle_info.resourceHandle = ResourceMapping::output_bundle;
|
2019-07-28 16:46:46 -07:00
|
|
|
create_bundle_info.pBundleName = "output";
|
|
|
|
|
res = KrCreateBundle(&create_bundle_info);
|
|
|
|
|
if (res != KR_SUCCESS) {
|
|
|
|
|
printf("Failed to create bundle.\n");
|
|
|
|
|
KrShutdown();
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
2019-07-20 13:55:16 -07:00
|
|
|
KrLoadResourceInfo load_resource_info = {};
|
2019-07-28 16:46:46 -07:00
|
|
|
load_resource_info.sType = KR_STRUCTURE_TYPE_LOAD_RESOURCE;
|
2020-08-06 18:15:58 -07:00
|
|
|
load_resource_info.resourceHandle = ResourceMapping::loaded_resource;
|
2019-07-28 16:46:46 -07:00
|
|
|
|
|
|
|
|
KrMoveToBundleInfo move_to_bundle_info = {};
|
|
|
|
|
move_to_bundle_info.sType = KR_STRUCTURE_TYPE_MOVE_TO_BUNDLE;
|
2020-08-06 18:15:58 -07:00
|
|
|
move_to_bundle_info.bundleHandle = ResourceMapping::output_bundle;
|
2019-07-20 13:55:16 -07:00
|
|
|
|
2019-11-30 17:57:45 -08:00
|
|
|
char* output_bundle = nullptr;
|
2020-08-06 18:15:58 -07:00
|
|
|
bool compile_shaders = false;
|
2020-08-09 19:05:43 -07:00
|
|
|
char* input_list_file = nullptr;
|
|
|
|
|
|
|
|
|
|
std::vector<std::string> input_files;
|
2019-11-30 17:57:45 -08:00
|
|
|
|
2020-08-06 18:52:06 -07:00
|
|
|
char command = '\0';
|
2020-08-06 18:15:58 -07:00
|
|
|
for (int i = 1; i < argc && !failed; i++) {
|
2022-08-08 01:07:26 -07:00
|
|
|
char* arg = argv[i];
|
2020-08-06 18:52:06 -07:00
|
|
|
if (arg[0] == '-') {
|
|
|
|
|
if (command != '\0') {
|
|
|
|
|
// The last command is expecting a parameter, not another command.
|
|
|
|
|
printf("Invalid syntax. '%s' not expected after '-%c'\n", arg, command);
|
|
|
|
|
failed = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (arg[1] == '\0') {
|
|
|
|
|
// We received a lonely '-'
|
|
|
|
|
printf("Invalid syntax. '-' must be followed by a command.\n");
|
|
|
|
|
failed = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
if (arg[2] != '\0') {
|
|
|
|
|
// All commands are currently one character long
|
|
|
|
|
printf("Unknown command: '%s'\n", arg);
|
|
|
|
|
failed = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
command = arg[1];
|
2019-11-30 17:57:45 -08:00
|
|
|
switch (command) {
|
2020-08-06 18:15:58 -07:00
|
|
|
case 'c':
|
|
|
|
|
compile_shaders = true;
|
2020-08-06 18:52:06 -07:00
|
|
|
command = '\0';
|
2020-08-06 18:15:58 -07:00
|
|
|
break;
|
2020-08-09 19:05:43 -07:00
|
|
|
case 'i':
|
2020-08-06 18:52:06 -07:00
|
|
|
case 'o':
|
|
|
|
|
// Next arg will be the output path
|
2019-11-30 17:57:45 -08:00
|
|
|
break;
|
2020-08-06 18:52:06 -07:00
|
|
|
default:
|
|
|
|
|
printf("Unknown command: '%s'\n", arg);
|
|
|
|
|
failed = true;
|
|
|
|
|
continue;
|
2019-11-30 17:57:45 -08:00
|
|
|
}
|
|
|
|
|
continue;
|
2020-08-06 18:52:06 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Process commands that receive arguments
|
|
|
|
|
switch (command) {
|
2022-08-08 01:07:26 -07:00
|
|
|
case 'i':
|
|
|
|
|
input_list_file = arg;
|
|
|
|
|
command = '\0';
|
|
|
|
|
continue;
|
|
|
|
|
case 'o':
|
|
|
|
|
output_bundle = arg;
|
|
|
|
|
command = '\0';
|
|
|
|
|
continue;
|
2020-08-06 18:52:06 -07:00
|
|
|
}
|
2019-11-30 17:57:45 -08:00
|
|
|
|
2020-08-09 19:05:43 -07:00
|
|
|
input_files.push_back(arg);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (input_list_file != nullptr) {
|
|
|
|
|
printf("Reading %s... ", input_list_file);
|
|
|
|
|
std::ifstream in(input_list_file);
|
|
|
|
|
if (!in) {
|
|
|
|
|
printf("[FAIL]\n");
|
|
|
|
|
failed = true;
|
|
|
|
|
} else {
|
|
|
|
|
for (std::string line; std::getline(in, line); ) {
|
|
|
|
|
const std::string ws = "\t ";
|
|
|
|
|
line.erase(0, line.find_first_not_of(ws));
|
|
|
|
|
line.erase(line.find_last_not_of(ws) + 1);
|
|
|
|
|
if (!line.empty()) {
|
|
|
|
|
input_files.push_back(line);
|
|
|
|
|
}
|
2019-07-20 13:55:16 -07:00
|
|
|
}
|
|
|
|
|
}
|
2020-08-09 19:05:43 -07:00
|
|
|
printf("[GOOD]\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
for (const std::string& file_name : input_files) {
|
|
|
|
|
load_resource_info.pResourcePath = file_name.c_str();
|
|
|
|
|
printf("loading %s... ", load_resource_info.pResourcePath);
|
|
|
|
|
res = KrLoadResource(&load_resource_info);
|
|
|
|
|
if (res != KR_SUCCESS) {
|
|
|
|
|
printf("[FAIL] (KrLoadResource)\n");
|
|
|
|
|
failed = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
move_to_bundle_info.resourceHandle = ResourceMapping::loaded_resource;
|
|
|
|
|
res = KrMoveToBundle(&move_to_bundle_info);
|
|
|
|
|
if (res != KR_SUCCESS) {
|
|
|
|
|
printf("[FAIL] (KrMoveToBundle)\n");
|
|
|
|
|
failed = true;
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
printf("[GOOD]\n");
|
2019-07-20 13:55:16 -07:00
|
|
|
}
|
|
|
|
|
|
2020-08-06 18:15:58 -07:00
|
|
|
if (compile_shaders && !failed) {
|
2020-08-06 18:52:06 -07:00
|
|
|
printf("Compiling Shaders... ");
|
2020-08-06 18:15:58 -07:00
|
|
|
KrCompileAllShadersInfo compile_all_shaders_info = {};
|
|
|
|
|
compile_all_shaders_info.sType = KR_STRUCTURE_TYPE_COMPILE_ALL_SHADERS;
|
2020-12-30 17:03:18 -08:00
|
|
|
compile_all_shaders_info.bundleHandle = ResourceMapping::output_bundle;
|
2020-08-06 18:15:58 -07:00
|
|
|
compile_all_shaders_info.logHandle = ResourceMapping::shader_compile_log;
|
|
|
|
|
res = KrCompileAllShaders(&compile_all_shaders_info);
|
|
|
|
|
if (res != KR_SUCCESS) {
|
|
|
|
|
printf("[FAIL] (Error %i)\n", res);
|
|
|
|
|
failed = true;
|
2020-12-30 17:03:18 -08:00
|
|
|
} else {
|
2020-08-06 18:15:58 -07:00
|
|
|
printf("[GOOD]\n");
|
|
|
|
|
}
|
2020-12-30 17:03:18 -08:00
|
|
|
KrGetResourceDataInfo get_resource_data_info = {};
|
|
|
|
|
get_resource_data_info.sType = KR_STRUCTURE_TYPE_GET_RESOURCE_DATA;
|
|
|
|
|
get_resource_data_info.resourceHandle = ResourceMapping::shader_compile_log;
|
|
|
|
|
res = KrGetResourceData(&get_resource_data_info, [](const KrGetResourceDataResult& result) {
|
|
|
|
|
// TODO - This will later be asynchronous... Will need to block rest of execution until returned
|
|
|
|
|
if (result.result != KR_SUCCESS) {
|
|
|
|
|
printf("Failed to get shader compile log. (Error %i)\n", result.result);
|
|
|
|
|
} else {
|
|
|
|
|
// result.data will be a null terminated string
|
2020-12-30 18:24:36 -08:00
|
|
|
if (result.data != nullptr && result.length > 0) {
|
2022-08-08 01:07:26 -07:00
|
|
|
printf("Shader compile log:\n%s\n", static_cast<char*>(result.data));
|
2020-12-30 17:03:18 -08:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
});
|
2020-08-06 18:15:58 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (output_bundle && !failed) {
|
2019-11-30 17:57:45 -08:00
|
|
|
printf("Bundling %s... ", output_bundle);
|
|
|
|
|
KrSaveResourceInfo save_resource_info = {};
|
|
|
|
|
save_resource_info.sType = KR_STRUCTURE_TYPE_SAVE_RESOURCE;
|
2020-08-06 18:15:58 -07:00
|
|
|
save_resource_info.resourceHandle = ResourceMapping::output_bundle;
|
2019-11-30 17:57:45 -08:00
|
|
|
save_resource_info.pResourcePath = output_bundle;
|
|
|
|
|
res = KrSaveResource(&save_resource_info);
|
|
|
|
|
if (res != KR_SUCCESS) {
|
|
|
|
|
printf("[FAIL] (Error %i)\n", res);
|
2020-08-06 18:15:58 -07:00
|
|
|
failed = true;
|
2019-11-30 17:57:45 -08:00
|
|
|
} else {
|
|
|
|
|
printf("[GOOD]\n");
|
|
|
|
|
}
|
2019-07-28 16:46:46 -07:00
|
|
|
}
|
|
|
|
|
|
2019-07-20 13:55:16 -07:00
|
|
|
KrShutdown();
|
2020-08-06 18:15:58 -07:00
|
|
|
return failed ? 1 : 0;
|
2019-07-20 13:55:16 -07:00
|
|
|
}
|