Resource loading from tar, Restructuring

This commit is contained in:
MrLetsplay 2022-10-08 14:15:43 +02:00
parent 396e5611c4
commit e65297de1e
13 changed files with 104 additions and 19 deletions

3
.gitmodules vendored Normal file
View File

@ -0,0 +1,3 @@
[submodule "dependencies/microtar"]
path = dependencies/microtar
url = https://github.com/MrLetsplay2003/microtar.git

View File

@ -3,23 +3,54 @@ cmake_minimum_required(VERSION 3.10)
project(KekEngine VERSION 1.0)
option(BUILD_SHARED_LIBS "Build as a shared library" ON)
option(KEKENGINE_DEBUG "Build with debug information" OFF)
option(KEKENGINE_BUILD_KEKGAME "Build kekgame" OFF)
file(GLOB KEKENGINE_SOURCE_FILES "src/kekengine/cpp/*.cpp")
file(GLOB KEKGAME_SOURCE_FILES "src/kekgame/cpp/*.cpp")
message("Build shared libs: ${BUILD_SHARED_LIBS}")
message("Debug: ${KEKENGINE_DEBUG}")
message("Build kekgame: ${KEKENGINE_BUILD_KEKGAME}")
set(CMAKE_ARCHIVE_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
set(KEKENGINE_RESOURCE_DIR "${CMAKE_SOURCE_DIR}/src/kekengine/res")
set(KEKGAME_RESOURCE_DIR "${CMAKE_SOURCE_DIR}/src/kekgame/res")
file(GLOB KEKENGINE_SOURCE_FILES "src/kekengine/cpp/*.cpp")
file(GLOB_RECURSE KEKENGINE_RESOURCE_FILES "${KEKENGINE_RESOURCE_DIR}/*")
file(GLOB_RECURSE KEKENGINE_RELATIVE_RESOURCE_FILES RELATIVE ${KEKENGINE_RESOURCE_DIR} "${KEKENGINE_RESOURCE_DIR}/*")
file(GLOB KEKGAME_SOURCE_FILES "src/kekgame/cpp/*.cpp")
file(GLOB_RECURSE KEKGAME_RESOURCE_FILES "${KEKGAME_RESOURCE_DIR}/*")
file(GLOB_RECURSE KEKGAME_RELATIVE_RESOURCE_FILES RELATIVE ${KEKGAME_RESOURCE_DIR} "${KEKGAME_RESOURCE_DIR}/*")
add_compile_definitions(FT_CONFIG_OPTION_ERROR_STRINGS) # Freetype error strings
add_compile_options(-Wall -g)
if(${KEKENGINE_DEBUG})
add_compile_options(-Wall -g)
message("Compiler flags A")
else()
add_compile_options(-Wall -O3)
message("Compiler flags B")
endif()
add_subdirectory(dependencies/microtar)
# kekengine resources
add_custom_target(kekengine_res ALL DEPENDS ${KEKENGINE_RESOURCE_FILES})
add_custom_command(TARGET kekengine_res PRE_BUILD
BYPRODUCTS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources.tar
COMMAND ${CMAKE_COMMAND} -E
chdir ${KEKENGINE_RESOURCE_DIR}
tar cf ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources.tar ${KEKENGINE_RELATIVE_RESOURCE_FILES})
# libkekengine
add_library(kekengine ${KEKENGINE_SOURCE_FILES})
target_link_libraries(kekengine PUBLIC glfw GLEW GL)
target_link_libraries(kekengine PUBLIC glfw GLEW GL microtar)
target_include_directories(kekengine PRIVATE "src/kekengine/include")
target_compile_definitions(kekengine PUBLIC KEKENGINE_TARGET_LINUX)
@ -28,8 +59,17 @@ pkg_check_modules(GTK3 REQUIRED gtk+-3.0)
target_link_libraries(kekengine PUBLIC ${GTK3_LIBRARIES})
target_include_directories(kekengine PRIVATE ${GTK3_INCLUDE_DIRS})
# kekgame
# kekgame resources
add_custom_target(kekgame_res ALL
DEPENDS ${KEKGAME_RESOURCE_FILES} kekengine_res)
add_custom_command(TARGET kekgame_res PRE_BUILD
BYPRODUCTS ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources.tar
COMMAND ${CMAKE_COMMAND} -E
chdir ${KEKGAME_RESOURCE_DIR}
tar rf ${CMAKE_RUNTIME_OUTPUT_DIRECTORY}/resources.tar ${KEKGAME_RELATIVE_RESOURCE_FILES})
# kekgame
add_executable(kekgame ${KEKGAME_SOURCE_FILES})
target_link_libraries(kekgame PUBLIC kekengine)

1
dependencies/microtar vendored Submodule

@ -0,0 +1 @@
Subproject commit b240d953b12dc87c625743a9fb55634b0f0f3608

View File

@ -119,8 +119,13 @@ int init() {
glEnable(GL_DEPTH_TEST);
glEnable(GL_MULTISAMPLE);
//glEnable(GL_CULL_FACE);
//glCullFace(GL_BACK);
glEnable(GL_CULL_FACE);
glCullFace(GL_BACK);
if(Resource::init() != KEK_SUCCESS) {
glfwTerminate();
return KEK_ERROR;
}
glfwSetCursorPosCallback(window, onCursorPosCallback);
glfwSetInputMode(window, GLFW_CURSOR, GLFW_CURSOR_DISABLED);
@ -129,10 +134,9 @@ int init() {
cam = new Camera();
kek::MemoryBuffer *buf = kek::Resource::loadResource("../src/res/object/sphere/Sphere.obj");
if(!buf) std::cout << "err buf" << std::endl;
kek::Mesh *mesh = kek::ObjParser::parse(buf);
kek::Shader *shader = new kek::Shader("../src/res/shader/mesh/vertex.glsl", "../src/res/shader/mesh/fragment.glsl");
MemoryBuffer *buf = Resource::loadResource("object/sphere/Sphere.obj");
Mesh *mesh = ObjParser::parse(buf);
Shader *shader = new Shader("shader/mesh/vertex.glsl", "shader/mesh/fragment.glsl");
while(1) {
// Clear the screen
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);

View File

@ -74,10 +74,6 @@ Mesh *parse(MemoryBuffer *buf) {
}
}
for(glm::vec3 v : vertexNormals) {
std::cout << v.x << " " << v.y << " " << v.z << std::endl;
}
return new Mesh(vertices, indices);
}

View File

@ -1,8 +1,29 @@
#include "resource.h"
#include "constants.h"
#include "errordialog.h"
#include "microtar.h"
namespace kek::Resource {
MemoryBuffer *loadResource(std::string path) {
static mtar_t resources;
int init() {
int r = mtar_open(&resources, "resources.tar", "r");
if(r != MTAR_ESUCCESS) {
ErrorDialog::showError("Failed to open resources.tar: " + std::string(mtar_strerror(r)));
return KEK_ERROR;
}
return KEK_SUCCESS;
}
void exit() {
mtar_close(&resources);
}
/*MemoryBuffer *loadResource(std::string path) {
std::ifstream in = std::ifstream(path, std::ios::binary);
if(!in.good()) return nullptr;
in.ignore(std::numeric_limits<std::streamsize>::max());
@ -12,6 +33,24 @@ MemoryBuffer *loadResource(std::string path) {
char *data = (char *) malloc(size);
in.read(data, size);
return new MemoryBuffer(data, size);
}*/
MemoryBuffer *loadResource(std::string path) {
mtar_header_t h;
int r = mtar_find(&resources, path.c_str(), &h);
if(r != MTAR_ESUCCESS) {
if(r == MTAR_ENOTFOUND) std::cerr << "Failed to find resource '" << path << "': " << mtar_strerror(r) << std::endl;
return nullptr;
}
void *buf = malloc(h.size);
r = mtar_read_data(&resources, buf, h.size);
if(r != MTAR_ESUCCESS) {
std::cerr << "Failed to read resource '" << path << "': " << mtar_strerror(r) << std::endl;
return nullptr;
}
return new MemoryBuffer((char *) buf, h.size);
}
}

View File

@ -7,6 +7,8 @@
namespace kek::Resource {
int init();
MemoryBuffer *loadResource(std::string path);
}