From 61faff439b75a1d67206696338d232607ba19dff Mon Sep 17 00:00:00 2001 From: Kevin Jerebica Date: Mon, 4 Nov 2024 14:19:28 +0100 Subject: [PATCH] repo: initialize and link with necessary libraries --- .gitignore | 3 + .gitmodules | 15 ++++ CMakeLists.txt | 52 +++++++++++++ README | 38 +++++++++ assets/models/cube.obj | 40 ++++++++++ compile_commands.json | 1 + shaders/fs_basic.sc | 6 ++ shaders/varying.def.sc | 4 + shaders/vs_basic.sc | 8 ++ src/eo.cpp | 173 +++++++++++++++++++++++++++++++++++++++++ tools/shaderc | 1 + 11 files changed, 341 insertions(+) create mode 100644 .gitignore create mode 100644 .gitmodules create mode 100644 CMakeLists.txt create mode 100644 README create mode 100644 assets/models/cube.obj create mode 120000 compile_commands.json create mode 100644 shaders/fs_basic.sc create mode 100644 shaders/varying.def.sc create mode 100644 shaders/vs_basic.sc create mode 100644 src/eo.cpp create mode 120000 tools/shaderc diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..96dcbc1 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +thirdparty/*/ +build/ +.cache/ diff --git a/.gitmodules b/.gitmodules new file mode 100644 index 0000000..0364b4e --- /dev/null +++ b/.gitmodules @@ -0,0 +1,15 @@ +[submodule "thirdparty/bgfx.cmake"] + path = thirdparty/bgfx.cmake + url = https://github.com/bkaradzic/bgfx.cmake +[submodule "thirdparty/imgui.cmake"] + path = thirdparty/imgui.cmake + url = https://github.com/tamaskenez/imgui-cmake +[submodule "thirdparty/glfw.cmake"] + path = thirdparty/glfw.cmake + url = https://github.com/glfw/glfw +[submodule "thirdparty/assimp.cmake"] + path = thirdparty/assimp.cmake + url = https://github.com/assimp/assimp +[submodule "thirdparty/glm.cmake"] + path = thirdparty/glm.cmake + url = https://github.com/icaven/glm diff --git a/CMakeLists.txt b/CMakeLists.txt new file mode 100644 index 0000000..782d8b0 --- /dev/null +++ b/CMakeLists.txt @@ -0,0 +1,52 @@ +cmake_minimum_required(VERSION 3.30.0) +project(eo + VERSION 0.1 + DESCRIPTION "Report to Houston" + HOMEPAGE_URL "http://git.0xdeadbeer.xyz/0xdeadbeer/eo" + LANGUAGES CXX) + +add_subdirectory("thirdparty/bgfx.cmake") +add_subdirectory("thirdparty/glfw.cmake") +add_subdirectory("thirdparty/imgui.cmake") +add_subdirectory("thirdparty/assimp.cmake") +add_subdirectory("thirdparty/glm.cmake") +add_executable(eo src/eo.cpp) +target_link_libraries(eo bx) +target_link_libraries(eo bgfx) +target_link_libraries(eo bimg) +target_link_libraries(eo glfw) +target_link_libraries(eo imgui) +target_link_libraries(eo assimp) +target_link_libraries(eo glm) + +# build shaders into /build/shaders/ +file(GLOB SHADER_SOURCE_FILES "${PROJECT_SOURCE_DIR}/shaders/*.sc") +set(SHADER_PLATFORM "linux") +set(SHADER_PROFILE "440") +file(MAKE_DIRECTORY "${CMAKE_CURRENT_BINARY_DIR}/shaders") +foreach(SHADER_SOURCE ${SHADER_SOURCE_FILES}) + cmake_path(ABSOLUTE_PATH SHADER_SOURCE NORMALIZE) + cmake_path(GET SHADER_SOURCE FILENAME SHADER_NAME) + cmake_path(GET SHADER_SOURCE STEM SHADER_STEM) + + set(VERTEX_SHADER_TYPE -1) + set(FRAGMENT_SHADER_TYPE -1) + set(SHADER_TYPE) + string(FIND "${SHADER_NAME}" "vs" VERTEX_SHADER_TYPE) + string(FIND "${SHADER_NAME}" "fs" FRAGMENT_SHADER_TYPE) + if(${VERTEX_SHADER_TYPE} GREATER -1) + set(SHADER_TYPE "vertex") + elseif(${FRAGMENT_SHADER_TYPE} GREATER -1) + set(SHADER_TYPE "fragment") + else() + continue() + endif() + + set(SHADER_COMMAND_ARGUMENTS "-f ${SHADER_SOURCE} -o ${CMAKE_CURRENT_BINARY_DIR}/shaders/${SHADER_LANGUAGE}/${SHADER_STEM}.bin --type ${SHADER_TYPE} --platform ${SHADER_PLATFORM} --profile ${SHADER_PROFILE}") + set(SHADER_COMMAND "${PROJECT_SOURCE_DIR}/tools/shaderc ${SHADER_COMMAND_ARGUMENTS}") + + add_custom_target(${SHADER_NAME} ALL + COMMAND /bin/bash -c "${SHADER_COMMAND}" + ) +endforeach() + diff --git a/README b/README new file mode 100644 index 0000000..68e900e --- /dev/null +++ b/README @@ -0,0 +1,38 @@ +EO + + RETRO 2D GAME + FRONT-BACK + CRAFTING SYSTEM + +STORY + + PRESTORY + + Nei, a computer engineer, electrician, as well as a soldier. You are stranded + in a lost colony of EnO, a planet once deemed all-important for the members of + the ISN - Inter-Stellar Nations. For hundreds of stellar revolutions, the + planet served as an ingress to the Cycos system. Inside the Cycos system, one + found 6 planets, all extremely abundant in rich and valuable materials. The + star's radius spanned millions of kilometers and was seen as a perfect + candidate for building a megastructure around and extrapolating all energy. + Although abundant in materials, these planets housed the most extreme of + environments in the whole universe. At first, EnO was the only candidate that + seemed to have a normal terrain, relatively safe conditions for a planet with + an extremely thin atmosphere, and lack of planetary hazards like strong storms, + tsunamis, strong earthquakes, and similar. Many ISN nations started colonies on + this planet to study the rich planets from up close. But due to the ignorance + of the whole ISN, none of the nations sought to resolve the sharing of land + through peace, but only through extensive imperialism. Which led to everything + but victory. Through revolutions, the nations saw that there was no place + left for EnO. The planet itself turned out to be a hazard because of the + explosives and traps still active. The ones still alive on that planet have + been left there to rot with the already rotten. You are one of them. And you + have to escape this new-born hell. + + MAIN STORY + + + + ENDING + + Nei crafts the ship and traverses the Cycos system, leaving the hell behind. diff --git a/assets/models/cube.obj b/assets/models/cube.obj new file mode 100644 index 0000000..6103977 --- /dev/null +++ b/assets/models/cube.obj @@ -0,0 +1,40 @@ +# Blender 4.2.3 LTS +# www.blender.org +mtllib cube.mtl +o Cube +v 1.000000 1.000000 -1.000000 +v 1.000000 -1.000000 -1.000000 +v 1.000000 1.000000 1.000000 +v 1.000000 -1.000000 1.000000 +v -1.000000 1.000000 -1.000000 +v -1.000000 -1.000000 -1.000000 +v -1.000000 1.000000 1.000000 +v -1.000000 -1.000000 1.000000 +vn -0.0000 1.0000 -0.0000 +vn -0.0000 -0.0000 1.0000 +vn -1.0000 -0.0000 -0.0000 +vn -0.0000 -1.0000 -0.0000 +vn 1.0000 -0.0000 -0.0000 +vn -0.0000 -0.0000 -1.0000 +vt 0.625000 0.500000 +vt 0.875000 0.500000 +vt 0.875000 0.750000 +vt 0.625000 0.750000 +vt 0.375000 0.750000 +vt 0.625000 1.000000 +vt 0.375000 1.000000 +vt 0.375000 0.000000 +vt 0.625000 0.000000 +vt 0.625000 0.250000 +vt 0.375000 0.250000 +vt 0.125000 0.500000 +vt 0.375000 0.500000 +vt 0.125000 0.750000 +s 0 +usemtl Material +f 1/1/1 5/2/1 7/3/1 3/4/1 +f 4/5/2 3/4/2 7/6/2 8/7/2 +f 8/8/3 7/9/3 5/10/3 6/11/3 +f 6/12/4 2/13/4 4/5/4 8/14/4 +f 2/13/5 1/1/5 3/4/5 4/5/5 +f 6/11/6 5/10/6 1/1/6 2/13/6 diff --git a/compile_commands.json b/compile_commands.json new file mode 120000 index 0000000..25eb4b2 --- /dev/null +++ b/compile_commands.json @@ -0,0 +1 @@ +build/compile_commands.json \ No newline at end of file diff --git a/shaders/fs_basic.sc b/shaders/fs_basic.sc new file mode 100644 index 0000000..041c98a --- /dev/null +++ b/shaders/fs_basic.sc @@ -0,0 +1,6 @@ +$input v_color0 + +void main() +{ + gl_FragColor = v_color0; +} diff --git a/shaders/varying.def.sc b/shaders/varying.def.sc new file mode 100644 index 0000000..a61d477 --- /dev/null +++ b/shaders/varying.def.sc @@ -0,0 +1,4 @@ +vec3 a_position : POSITION; +vec4 a_color0 : COLOR0; + +vec4 v_color0 : COLOR0 = vec4(1.0, 0.0, 0.0, 1.0); diff --git a/shaders/vs_basic.sc b/shaders/vs_basic.sc new file mode 100644 index 0000000..73b4093 --- /dev/null +++ b/shaders/vs_basic.sc @@ -0,0 +1,8 @@ +$input a_position, a_color0 +$output v_color0 + +void main() +{ + gl_Position = vec4(a_position, 1.0); + v_color0 = a_color0; +} diff --git a/src/eo.cpp b/src/eo.cpp new file mode 100644 index 0000000..40d9777 --- /dev/null +++ b/src/eo.cpp @@ -0,0 +1,173 @@ +#include "bx/allocator.h" +#include "bx/file.h" +#include "bx/filepath.h" +#include +#include +#include +#include +#include +#define GLFW_EXPOSE_NATIVE_X11 +#include "GLFW/glfw3native.h" +#include +#include +#include +#include +#include +#include +#include +#include + +#define DEFAULT_WIDTH 1280 +#define DEFAULT_HEIGHT 720 + +int width = DEFAULT_WIDTH; +int height = DEFAULT_HEIGHT; +bx::DefaultAllocator g_allocator; +bgfx::ProgramHandle program; + +struct ColorVertex { + glm::vec3 pos; + uint32_t col; +}; + + +void glfw_error_callback(int error, const char *desc) { + std::cout << "--error: " << desc << std::endl; +} + +bool load_model(std::string path) { + Assimp::Importer importer; + + const aiScene *scene = importer.ReadFile(path, + aiProcess_CalcTangentSpace | aiProcess_Triangulate | + aiProcess_JoinIdenticalVertices | aiProcess_SortByPType); + if (scene == nullptr) { + std::cout << "--error: failed loading model" << std::endl; + return false; + } + + return true; +} + +const bgfx::Memory* load_mem(bx::FileReader* reader, bx::FilePath& filepath) { + if (bx::open(reader, filepath) ) + { + uint32_t size = (uint32_t)bx::getSize(reader); + const bgfx::Memory* mem = bgfx::alloc(size+1); + bx::read(reader, mem->data, size, bx::ErrorAssert{}); + bx::close(reader); + mem->data[mem->size-1] = '\0'; + return mem; + } + + std::cout << "--error: failed to load " << filepath.getCPtr() << std::endl; + return NULL; +} + +bgfx::ShaderHandle load_shader(bx::FileReader* reader, const char *filename) { + bx::FilePath filepath(filename); + + bgfx::ShaderHandle handle = bgfx::createShader(load_mem(reader, filepath)); + + fprintf(stdout, "loading shader: %s %d\n", filename, (int) strlen(filename)); + bgfx::setName(handle, filename, strlen(filename)); + + return handle; +} + +bgfx::ProgramHandle load_program(bx::FileReader *reader, const char *vs_name, const char *fs_name) { + bgfx::ShaderHandle vsh = load_shader(reader, vs_name); + bgfx::ShaderHandle fsh = load_shader(reader, fs_name); + + return bgfx::createProgram(vsh, fsh, true); +} + +int main(int argc, char *argv[]) { + glfwSetErrorCallback(glfw_error_callback); + int ret = glfwInit(); + if (ret < 0) { + std::cout << "--error: failed initializing glfw" << std::endl; + return -1; + } + + // glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API); + + GLFWwindow *w = glfwCreateWindow(800, 600, "eo", NULL, NULL); + if (!w) { + std::cout << "--error: failed creating window" << std::endl; + return -1; + } + + bx::AllocatorI* allocator = &g_allocator; + bx::FileReader* _reader = BX_NEW(allocator, bx::FileReader); + + bgfx::Init init; + init.platformData.ndt = glfwGetX11Display(); + init.platformData.nwh = (void *)(uintptr_t) glfwGetX11Window(w); + int width = 0; + int height = 0; + glfwGetWindowSize(w, &width, &height); + init.resolution.width = width; + init.resolution.height = height; + init.resolution.reset = BGFX_RESET_VSYNC; + if (!bgfx::init(init)) { + std::cout << "--error: failed initializing bgfx" << std::endl; + return -1; + } + + + bgfx::ViewId clear_view = 0; + bgfx::setViewClear(clear_view, BGFX_CLEAR_COLOR, 0x000000FF); + bgfx::setViewRect(clear_view, 0, 0, width, height); + + ColorVertex kTriangleVertices[] = { + {{-0.5f, -0.5f, 0.0f}, 0x339933FF}, + {{0.5f, -0.5f, 0.0f}, 0x993333FF}, + {{0.0f, 0.5f, 0.0f}, 0x333399FF}, + }; + + const uint16_t kTriangleIndices[] = { + 0, 1, 2, + }; + + bgfx::VertexLayout layout; + layout.begin() + .add(bgfx::Attrib::Position, 3, bgfx::AttribType::Float) + .add(bgfx::Attrib::Color0, 4, bgfx::AttribType::Uint8, true) + .end(); + + bgfx::VertexBufferHandle vertex_buffer = bgfx::createVertexBuffer(bgfx::makeRef(kTriangleVertices, sizeof(kTriangleVertices)), layout); + bgfx::IndexBufferHandle index_buffer = bgfx::createIndexBuffer(bgfx::makeRef(kTriangleIndices, sizeof(kTriangleIndices))); + + program = load_program(_reader, "shaders/vs_basic.bin", "shaders/fs_basic.bin"); + + while (!glfwWindowShouldClose(w)) { + glfwPollEvents(); + + std::int32_t display_w, display_h; + glfwGetFramebufferSize(w, &display_w, &display_h); + if (display_w != width || display_h != height) { + bgfx::reset(display_w,display_h, BGFX_RESET_VSYNC); + bgfx::setViewRect(clear_view, 0, 0, bgfx::BackbufferRatio::Equal); + width = display_w; + height = display_h; + } + + bgfx::touch(clear_view); + bgfx::setState( + BGFX_STATE_WRITE_R + | BGFX_STATE_WRITE_G + | BGFX_STATE_WRITE_B + | BGFX_STATE_WRITE_A + ); + bgfx::setVertexBuffer(0, vertex_buffer); + bgfx::setIndexBuffer(index_buffer); + bgfx::submit(clear_view, program); + bgfx::frame(); + } + + bgfx::shutdown(); + glfwTerminate(); + + return 0; +} diff --git a/tools/shaderc b/tools/shaderc new file mode 120000 index 0000000..772ba19 --- /dev/null +++ b/tools/shaderc @@ -0,0 +1 @@ +../build/thirdparty/bgfx.cmake/cmake/bgfx/shaderc \ No newline at end of file