From 9d399c62d401ec5214e3d1312d47184a1ca9dfbd Mon Sep 17 00:00:00 2001 From: bailwillharr Date: Wed, 7 Sep 2022 10:02:01 +0100 Subject: [PATCH] Do lots of stuff --- CMakeLists.txt | 12 ++- TODO | 17 +++++ graphics/CMakeLists.txt | 26 +++++++ graphics/include/gfx.hpp | 44 +++++++++++ graphics/include/gfx_api.h | 13 ++++ graphics/include/gfx_device.hpp | 27 +++++++ graphics/src/gfx_device.cpp | 7 ++ include/components/camera.hpp | 2 +- include/components/component.hpp | 2 +- include/components/custom.hpp | 2 +- include/components/mesh_renderer.hpp | 2 +- include/components/text_ui_renderer.hpp | 2 +- include/{export.h => engine_api.h} | 0 include/input.hpp | 2 +- include/object.hpp | 2 +- include/resource_manager.hpp | 2 +- include/resources/font.hpp | 2 +- include/resources/mesh.hpp | 2 +- include/resources/resource.hpp | 2 +- include/resources/shader.hpp | 2 +- include/resources/texture.hpp | 2 +- include/sceneroot.hpp | 2 +- include/transform.hpp | 2 +- include/window.hpp | 2 +- renderer/CMakeLists.txt | 97 ------------------------- 25 files changed, 160 insertions(+), 115 deletions(-) create mode 100644 TODO create mode 100644 graphics/CMakeLists.txt create mode 100644 graphics/include/gfx.hpp create mode 100644 graphics/include/gfx_api.h create mode 100644 graphics/include/gfx_device.hpp create mode 100644 graphics/src/gfx_device.cpp rename include/{export.h => engine_api.h} (100%) delete mode 100644 renderer/CMakeLists.txt diff --git a/CMakeLists.txt b/CMakeLists.txt index f46deeb..fb3937c 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -32,7 +32,7 @@ add_library(${PROJECT_NAME} SHARED # PUBLIC API - "include/export.h" + "include/engine_api.h" "include/engine.hpp" @@ -84,7 +84,15 @@ target_include_directories(${PROJECT_NAME} PRIVATE src) configure_file(config.h.in config.h) target_include_directories(${PROJECT_NAME} PRIVATE ${CMAKE_CURRENT_BINARY_DIR}) -# libraries: +# project submodules: + +add_subdirectory(graphics) +target_include_directories(${PROJECT_NAME} PUBLIC graphics/include) +target_link_libraries(${PROJECT_NAME} PUBLIC graphics) + + + +# external libraries: # MinGW library if using it if (MINGW) diff --git a/TODO b/TODO new file mode 100644 index 0000000..bd9deb9 --- /dev/null +++ b/TODO @@ -0,0 +1,17 @@ +Place all instances of a particular component in contiguous memory: I.e., a +scene holds many std::vectors, one for each type of component. These vectors are +looped through every frame. This should optimise things by improving the memory +layout of the program, significantly reducing cache misses. + +At some point, add game controller support. Make sure it works well with the +'Input' class. + +For mesh rendering, give every mesh-renderer a ShaderMaterial which, depending +on the shader, defines how the mesh reacts to light and also stores a reference +to its texture(s). -- Also make a model loader that works with multiple meshes +(by creating many objects). + +Add support for shadows and other complex lighting. Also add post-processing. + +For font rendering, put all ASCII characters in one large texture and use +'instancing' (and uniform buffer objects?) to reduce draw calls. diff --git a/graphics/CMakeLists.txt b/graphics/CMakeLists.txt new file mode 100644 index 0000000..bf83c4a --- /dev/null +++ b/graphics/CMakeLists.txt @@ -0,0 +1,26 @@ +cmake_minimum_required(VERSION 3.4) + +# options + +project(graphics LANGUAGES CXX) + +add_library(${PROJECT_NAME} STATIC + + "src/gfx_device.cpp" + + # PUBLIC API + + "include/gfx_api.h" + "include/gfx.hpp" + "include/gfx_device.hpp" + +) + +# compiling options: + +target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "GFX_EXPORTS") + +target_include_directories(${PROJECT_NAME} PUBLIC include) +target_include_directories(${PROJECT_NAME} PRIVATE src) + +# libraries: diff --git a/graphics/include/gfx.hpp b/graphics/include/gfx.hpp new file mode 100644 index 0000000..6210e26 --- /dev/null +++ b/graphics/include/gfx.hpp @@ -0,0 +1,44 @@ +#pragma once + +#include +#include + +namespace gfx { + + enum class ShaderType { + VERTEX, + FRAGMENT, + }; + + struct Shader { + ShaderType type; + uint32_t handle; + }; + + typedef uint32_t Program; + + enum class BufferType { + VERTEX, + INDEX, + }; + + struct Buffer { + BufferType type; + uint32_t handle; + }; + + enum class Primitive { + POINTS, + LINES, + LINE_STRIP, + TRIANGLES, + TRIANGLE_STRIP, + }; + + enum class IndexBufferFormat { + UNSIGNED_8_BITS, + UNSIGNED_16_BITS, + UNSIGNED_32_BITS, + }; + +} diff --git a/graphics/include/gfx_api.h b/graphics/include/gfx_api.h new file mode 100644 index 0000000..e153b5e --- /dev/null +++ b/graphics/include/gfx_api.h @@ -0,0 +1,13 @@ +#pragma once + +#ifndef GFX_API +# ifdef _MSC_VER +# ifdef GFX_EXPORTS +# define GFX_API __declspec(dllexport) +# else +# define GFX_API __declspec(dllimport) +# endif +# else +# define GFX_API +# endif +#endif diff --git a/graphics/include/gfx_device.hpp b/graphics/include/gfx_device.hpp new file mode 100644 index 0000000..09ae6a8 --- /dev/null +++ b/graphics/include/gfx_device.hpp @@ -0,0 +1,27 @@ +#pragma once + +#include "gfx_api.h" + +#include "gfx.hpp" + +namespace gfx { + + struct GFX_API Device { + + virtual void setViewport(uint32_t top_left_x, uint32_t top_left_y, uint32_t width, uint32_t height) = 0; + + virtual bool createShader(ShaderType type, const char* source, Shader& out) = 0; + virtual bool createProgram(int count, const Shader* shaders, Program& out) = 0; + + virtual bool createBuffer(BufferType type, const void* data, Buffer& out) = 0; + virtual void bufferData(const void* data, Buffer buffer) = 0; + virtual void bufferSubData(uint32_t offset, uint32_t size, const void* data, Buffer buffer) = 0; + + virtual void drawElements(Primitive primitive, IndexBufferFormat format, uint32_t count, uint32_t offset); + virtual void drawArrays() = 0; + + }; + + + +} diff --git a/graphics/src/gfx_device.cpp b/graphics/src/gfx_device.cpp new file mode 100644 index 0000000..9013025 --- /dev/null +++ b/graphics/src/gfx_device.cpp @@ -0,0 +1,7 @@ +#include "gfx_device.hpp" + +#include + +namespace gfx { + +} diff --git a/include/components/camera.hpp b/include/components/camera.hpp index e4b1874..dc6e91f 100644 --- a/include/components/camera.hpp +++ b/include/components/camera.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "component.hpp" diff --git a/include/components/component.hpp b/include/components/component.hpp index c6348f1..5d3f950 100644 --- a/include/components/component.hpp +++ b/include/components/component.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" class Object; class Window; diff --git a/include/components/custom.hpp b/include/components/custom.hpp index 289a66e..17c8c66 100644 --- a/include/components/custom.hpp +++ b/include/components/custom.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "component.hpp" diff --git a/include/components/mesh_renderer.hpp b/include/components/mesh_renderer.hpp index 2ba163e..bd68556 100644 --- a/include/components/mesh_renderer.hpp +++ b/include/components/mesh_renderer.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "component.hpp" diff --git a/include/components/text_ui_renderer.hpp b/include/components/text_ui_renderer.hpp index 176e428..7a82b0e 100644 --- a/include/components/text_ui_renderer.hpp +++ b/include/components/text_ui_renderer.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "component.hpp" diff --git a/include/export.h b/include/engine_api.h similarity index 100% rename from include/export.h rename to include/engine_api.h diff --git a/include/input.hpp b/include/input.hpp index c3c9840..bc4cb08 100644 --- a/include/input.hpp +++ b/include/input.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "inputs/mouse.hpp" #include "inputs/keyboard.hpp" diff --git a/include/object.hpp b/include/object.hpp index 1552c5d..cfc28f4 100644 --- a/include/object.hpp +++ b/include/object.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include diff --git a/include/resource_manager.hpp b/include/resource_manager.hpp index 5baa20b..5077098 100644 --- a/include/resource_manager.hpp +++ b/include/resource_manager.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "resources/resource.hpp" diff --git a/include/resources/font.hpp b/include/resources/font.hpp index 82a5b96..b284bfd 100644 --- a/include/resources/font.hpp +++ b/include/resources/font.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "resource.hpp" diff --git a/include/resources/mesh.hpp b/include/resources/mesh.hpp index 043f835..4eb5349 100644 --- a/include/resources/mesh.hpp +++ b/include/resources/mesh.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "resource.hpp" diff --git a/include/resources/resource.hpp b/include/resources/resource.hpp index 8bf4480..f18ae00 100644 --- a/include/resources/resource.hpp +++ b/include/resources/resource.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include #include diff --git a/include/resources/shader.hpp b/include/resources/shader.hpp index 99951cd..60ed5bf 100644 --- a/include/resources/shader.hpp +++ b/include/resources/shader.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "resource.hpp" diff --git a/include/resources/texture.hpp b/include/resources/texture.hpp index 2a14ec1..2c6e334 100644 --- a/include/resources/texture.hpp +++ b/include/resources/texture.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "resource.hpp" diff --git a/include/sceneroot.hpp b/include/sceneroot.hpp index 74b9e26..5096b50 100644 --- a/include/sceneroot.hpp +++ b/include/sceneroot.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "object.hpp" diff --git a/include/transform.hpp b/include/transform.hpp index b7effe9..dbb3122 100644 --- a/include/transform.hpp +++ b/include/transform.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include #include diff --git a/include/window.hpp b/include/window.hpp index 26f09f2..7856d63 100644 --- a/include/window.hpp +++ b/include/window.hpp @@ -1,6 +1,6 @@ #pragma once -#include "export.h" +#include "engine_api.h" #include "inputs/keyboard.hpp" #include "inputs/mouse.hpp" diff --git a/renderer/CMakeLists.txt b/renderer/CMakeLists.txt deleted file mode 100644 index 2152845..0000000 --- a/renderer/CMakeLists.txt +++ /dev/null @@ -1,97 +0,0 @@ -cmake_minimum_required(VERSION 3.4) - -# options - -project( - engine_renderer - LANGUAGES CXX - VERSION "0.1.0" -) - -add_library(${PROJECT_NAME} SHARED - - src/renderer.cpp - - # API - - include/renderer.hpp - -) - -# compiling options: - -target_compile_definitions(${PROJECT_NAME} PRIVATE DEFINITIONS "PROJECT_EXPORTS") - -set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 20) -set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON) - -if (MSVC) - target_compile_options(${PROJECT_NAME} PRIVATE /W3) - target_compile_options(${PROJECT_NAME} PRIVATE /MP) -endif() - -target_include_directories(${PROJECT_NAME} PUBLIC include) -target_include_directories(${PROJECT_NAME} PRIVATE src) - -# Pass some project information into the source code -configure_file(config.h.in config.h) -target_include_directories(${PROJECT_NAME} PUBLIC ${CMAKE_CURRENT_BINARY_DIR}) - -# libraries: - -# MinGW library if using it -if (MINGW) - target_link_libraries(${PROJECT_NAME} PUBLIC mingw32) -endif() - -# SDL2: -set(SDL2_DISABLE_INSTALL ON CACHE INTERNAL "" FORCE) -set(SDL_SHARED ON CACHE INTERNAL "" FORCE) -set(SDL_STATIC OFF CACHE INTERNAL "" FORCE) -set(SDL_TEST OFF CACHE INTERNAL "" FORCE) -set(BUILD_SHARED_LIBS ON) -add_subdirectory(dependencies/SDL) -target_include_directories(${PROJECT_NAME} PUBLIC dependencies/SDL/include) -target_link_libraries(${PROJECT_NAME} PUBLIC SDL2::SDL2) -target_link_libraries(${PROJECT_NAME} PUBLIC SDL2::SDL2main) - -# GLM: -set(BUILD_SHARED_LIBS OFF) -add_subdirectory(dependencies/glm) -target_include_directories(${PROJECT_NAME} PUBLIC dependencies/glm) - -# GLAD: -set(GLAD_PROFILE "core" CACHE INTERNAL "" FORCE) -set(GLAD_API "gl=3.3" CACHE INTERNAL "" FORCE) -if (CMAKE_BUILD_TYPE STREQUAL "Debug") - set(GLAD_GENERATOR "c-debug" CACHE INTERNAL "" FORCE) -else() - set(GLAD_GENERATOR "c" CACHE INTERNAL "" FORCE) -endif() -set(GLAD_SPEC "gl" CACHE INTERNAL "" FORCE) -set(BUILD_SHARED_LIBS OFF) -add_subdirectory(dependencies/glad) -set_property(TARGET glad PROPERTY POSITION_INDEPENDENT_CODE ON) -target_link_libraries(${PROJECT_NAME} PUBLIC glad) -target_include_directories(${PROJECT_NAME} PUBLIC dependencies/glad/include) - -# spdlog -set(SPDLOG_BUILD_SHARED ON CACHE INTERNAL "" FORCE) -set(BUILD_SHARED_LIBS ON) -add_subdirectory(dependencies/spdlog) -target_link_libraries(${PROJECT_NAME} PUBLIC spdlog) -target_include_directories(${PROJECT_NAME} PUBLIC dependencies/spdlog/include) - -# freetype -set(FT_DISABLE_ZLIB TRUE CACHE INTERNAL "" FORCE) -set(FT_DISABLE_BZIP2 TRUE CACHE INTERNAL "" FORCE) -set(FT_DISABLE_PNG TRUE CACHE INTERNAL "" FORCE) -set(FT_DISABLE_HARFBUZZ TRUE CACHE INTERNAL "" FORCE) -set(FT_DISABLE_BROTLI TRUE CACHE INTERNAL "" FORCE) -set(BUILD_SHARED_LIBS ON) -add_subdirectory(dependencies/freetype) -target_link_libraries(${PROJECT_NAME} PRIVATE freetype) -target_include_directories(${PROJECT_NAME} PRIVATE dependencies/freetype/include) - -# stb -target_include_directories(${PROJECT_NAME} PRIVATE dependencies/stb)