repo: initialize and link with necessary libraries

This commit is contained in:
Kevin J. 2024-11-04 14:19:28 +01:00
commit 61faff439b
11 changed files with 341 additions and 0 deletions

3
.gitignore vendored Normal file
View File

@ -0,0 +1,3 @@
thirdparty/*/
build/
.cache/

15
.gitmodules vendored Normal file
View File

@ -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

52
CMakeLists.txt Normal file
View File

@ -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()

38
README Normal file
View File

@ -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.

40
assets/models/cube.obj Normal file
View File

@ -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

1
compile_commands.json Symbolic link
View File

@ -0,0 +1 @@
build/compile_commands.json

6
shaders/fs_basic.sc Normal file
View File

@ -0,0 +1,6 @@
$input v_color0
void main()
{
gl_FragColor = v_color0;
}

4
shaders/varying.def.sc Normal file
View File

@ -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);

8
shaders/vs_basic.sc Normal file
View File

@ -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;
}

173
src/eo.cpp Normal file
View File

@ -0,0 +1,173 @@
#include "bx/allocator.h"
#include "bx/file.h"
#include "bx/filepath.h"
#include <cstdint>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <GLFW/glfw3.h>
#define GLFW_EXPOSE_NATIVE_X11
#include "GLFW/glfw3native.h"
#include <bx/bx.h>
#include <bgfx/bgfx.h>
#include <bgfx/platform.h>
#include <bgfx/defines.h>
#include <assimp/Importer.hpp>
#include <assimp/scene.h>
#include <assimp/postprocess.h>
#include <glm/glm.hpp>
#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;
}

1
tools/shaderc Symbolic link
View File

@ -0,0 +1 @@
../build/thirdparty/bgfx.cmake/cmake/bgfx/shaderc