feat: engine as library

This commit is contained in:
2025-10-16 19:43:51 +02:00
parent 165073c36d
commit aa7aafe944
42 changed files with 160 additions and 147 deletions

View File

@ -1,6 +1,8 @@
cmake_minimum_required(VERSION 3.16) cmake_minimum_required(VERSION 3.16)
project(CodingGame LANGUAGES C CXX) project(CodingGame LANGUAGES C CXX)
set(ENGINE_TARGET engine)
# ---------- Build-type defaults (only affects single-config generators like Ninja/Make) ---------- # ---------- Build-type defaults (only affects single-config generators like Ninja/Make) ----------
if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE) if(NOT CMAKE_CONFIGURATION_TYPES AND NOT CMAKE_BUILD_TYPE)
set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE) set(CMAKE_BUILD_TYPE Release CACHE STRING "Choose the type of build." FORCE)
@ -12,7 +14,7 @@ if (UNIX)
FetchContent_Declare( FetchContent_Declare(
glm glm
GIT_REPOSITORY https://github.com/g-truc/glm.git GIT_REPOSITORY https://github.com/g-truc/glm.git
GIT_TAG bf71a834948186f4097caa076cd2663c69a10e1e # refs/tags/1.0.1 GIT_TAG a532f5b1cf27d6a3c099437e6959cf7e398a0a67 # refs/tags/1.0.2
) )
FetchContent_MakeAvailable(glm) FetchContent_MakeAvailable(glm)
@ -34,99 +36,5 @@ elseif (MSVC) # vcpkg
find_package(EnTT CONFIG REQUIRED) find_package(EnTT CONFIG REQUIRED)
endif() endif()
add_executable(CodingGame add_subdirectory(engine)
src/IO/parser.cpp add_subdirectory(sandbox)
src/IO/file_manager.cpp
src/window/window.cpp
src/components/batch.cpp
src/renderer/debug.cpp
src/renderer/mesh.cpp
src/renderer/shader.cpp
src/renderer/texture.cpp
src/renderer/wavefront.cpp
src/renderer/engine.cpp
src/renderer/renderer.cpp
src/main.cpp
)
set_property(TARGET CodingGame PROPERTY CXX_STANDARD 17)
set_property(TARGET CodingGame PROPERTY CXX_STANDARD_REQUIRED ON)
file(COPY src/shaders DESTINATION ${CMAKE_BINARY_DIR}/)
target_include_directories(CodingGame PRIVATE
${CMAKE_SOURCE_DIR}/include
${CMAKE_SOURCE_DIR}/contrib
)
target_link_libraries(CodingGame PRIVATE
SDL3::SDL3
OpenGL::GL
GLEW::GLEW
glm::glm
EnTT::EnTT
)
# ---------- Visibility (helps optimizer & smaller binaries on Release) ----------
# Only affects non-Windows compilers
set_target_properties(CodingGame PROPERTIES
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
)
# ---------- Per-config flags ----------
# Debug flags (your original intent, kept)
if (MSVC)
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/Zi>)
target_link_options(CodingGame PRIVATE $<$<CONFIG:Debug>:/DEBUG:FULL>)
else()
target_compile_options(CodingGame PRIVATE $<$<CONFIG:Debug>:-ggdb>)
endif()
# Release flags
if (MSVC)
# /O2: optimize speed, /GL: whole program opt (LTCG), /DNDEBUG: disable asserts
target_compile_options(CodingGame PRIVATE
$<$<CONFIG:Release>:/O2>
$<$<CONFIG:Release>:/DNDEBUG>
$<$<CONFIG:RelWithDebInfo>:/O2>
)
# Link-time codegen & extra linker opts for smaller/faster binaries
target_link_options(CodingGame PRIVATE
$<$<CONFIG:Release>:/LTCG /OPT:ICF /OPT:REF>
$<$<CONFIG:RelWithDebInfo>:/LTCG /OPT:ICF /OPT:REF>
)
else()
# GCC/Clang
# -O3 for max opts, -ffast-math optional but can be risky; we keep it OFF by default.
option(CODINGGAME_USE_MARCH_NATIVE "Enable -march=native on Release for this machine" ON)
target_compile_options(CodingGame PRIVATE
$<$<CONFIG:Release>:-O3>
$<$<AND:$<CONFIG:Release>,$<BOOL:${CODINGGAME_USE_MARCH_NATIVE}>>:-march=native>
$<$<CONFIG:Release>:-DNDEBUG>
$<$<CONFIG:RelWithDebInfo>:-O3 -g>
)
# Linker: enable LTO when available; optionally strip symbols on non-Apple
include(CheckIPOSupported) # IPO == LTO in CMake terms
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_msg)
if(ipo_supported)
set_property(TARGET CodingGame PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
set_property(TARGET CodingGame PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT APPLE)
# -s strips symbols at link stage (use only for pure Release)
target_link_options(CodingGame PRIVATE $<$<CONFIG:Release>:-s>)
endif()
endif()
# ---------- Windows: copy runtime DLLs ----------
if (WIN32)
add_custom_command(TARGET CodingGame POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_RUNTIME_DLLS:CodingGame> $<TARGET_FILE_DIR:CodingGame>
COMMAND_EXPAND_LISTS)
endif()

89
engine/CMakeLists.txt Normal file
View File

@ -0,0 +1,89 @@
add_library(${ENGINE_TARGET} STATIC
src/IO/parser.cpp
src/IO/file_manager.cpp
src/window/window.cpp
src/components/batch.cpp
src/renderer/debug.cpp
src/renderer/mesh.cpp
src/renderer/shader.cpp
src/renderer/texture.cpp
src/renderer/wavefront.cpp
src/renderer/engine.cpp
src/renderer/renderer.cpp
src/main.cpp
)
set_target_properties(${ENGINE_TARGET} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
CXX_VISIBILITY_PRESET hidden
VISIBILITY_INLINES_HIDDEN YES
)
target_include_directories(${ENGINE_TARGET} PUBLIC
${CMAKE_CURRENT_SOURCE_DIR}/include
${CMAKE_CURRENT_SOURCE_DIR}/contrib
)
target_link_libraries(${ENGINE_TARGET} PUBLIC
SDL3::SDL3
OpenGL::GL
GLEW::GLEW
glm::glm
EnTT::EnTT
)
if (MSVC)
target_compile_options(${ENGINE_TARGET} PRIVATE $<$<CONFIG:Debug>:/Zi>)
target_link_options(${ENGINE_TARGET} PRIVATE $<$<CONFIG:Debug>:/DEBUG:FULL>)
else()
target_compile_options(${ENGINE_TARGET} PRIVATE $<$<CONFIG:Debug>:-ggdb>)
endif()
# Release flags
if (MSVC)
# /O2: optimize speed, /GL: whole program opt (LTCG), /DNDEBUG: disable asserts
target_compile_options(${ENGINE_TARGET} PRIVATE
$<$<CONFIG:Release>:/O2>
$<$<CONFIG:Release>:/DNDEBUG>
$<$<CONFIG:RelWithDebInfo>:/O2>
)
# Link-time codegen & extra linker opts for smaller/faster binaries
target_link_options(${ENGINE_TARGET} PRIVATE
$<$<CONFIG:Release>:/LTCG /OPT:ICF /OPT:REF>
$<$<CONFIG:RelWithDebInfo>:/LTCG /OPT:ICF /OPT:REF>
)
else()
# GCC/Clang
# -O3 for max opts, -ffast-math optional but can be risky; we keep it OFF by default.
option(CODINGGAME_USE_MARCH_NATIVE "Enable -march=native on Release for this machine" ON)
target_compile_options(${ENGINE_TARGET} PRIVATE
$<$<CONFIG:Release>:-O3>
$<$<AND:$<CONFIG:Release>,$<BOOL:${CODINGGAME_USE_MARCH_NATIVE}>>:-march=native>
$<$<CONFIG:Release>:-DNDEBUG>
$<$<CONFIG:RelWithDebInfo>:-O3 -g>
)
# Linker: enable LTO when available; optionally strip symbols on non-Apple
include(CheckIPOSupported) # IPO == LTO in CMake terms
check_ipo_supported(RESULT ipo_supported OUTPUT ipo_msg)
if(ipo_supported)
set_property(TARGET ${ENGINE_TARGET} PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELEASE TRUE)
set_property(TARGET ${ENGINE_TARGET} PROPERTY INTERPROCEDURAL_OPTIMIZATION_RELWITHDEBINFO TRUE)
endif()
if (CMAKE_CXX_COMPILER_ID STREQUAL "GNU" AND NOT APPLE)
# -s strips symbols at link stage (use only for pure Release)
target_link_options(${ENGINE_TARGET} PRIVATE $<$<CONFIG:Release>:-s>)
endif()
endif()
# ---------- Windows: copy runtime DLLs ----------
if (WIN32)
add_custom_command(TARGET ${ENGINE_TARGET} POST_BUILD
COMMAND ${CMAKE_COMMAND} -E copy_if_different
$<TARGET_RUNTIME_DLLS:${ENGINE_TARGET}> $<TARGET_FILE_DIR:${ENGINE_TARGET}>
COMMAND_EXPAND_LISTS)
endif()

View File

@ -1,7 +1,7 @@
#ifndef COMPONENT_BATCH_H_ #ifndef COMPONENT_BATCH_H_
#define COMPONENT_BATCH_H_ #define COMPONENT_BATCH_H_
#include "renderer/renderer.h" #include "engine/renderer/renderer.h"
// requires mesh component // requires mesh component
struct batch { struct batch {

View File

@ -2,7 +2,7 @@
#define COMPONENTS_LIGHT_H_ #define COMPONENTS_LIGHT_H_
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "renderer/renderer.h" #include "engine/renderer/renderer.h"
struct light { struct light {
friend class Renderer; friend class Renderer;

View File

@ -2,7 +2,7 @@
#define COMPONENTS_MESH_H_ #define COMPONENTS_MESH_H_
#include <memory> #include <memory>
#include "renderer/wavefront.h" #include "engine/renderer/wavefront.h"
struct mesh { struct mesh {
std::shared_ptr<Object> object; std::shared_ptr<Object> object;

View File

@ -3,7 +3,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "renderer/mesh.h" #include "engine/renderer/mesh.h"
class Vertex { class Vertex {
friend class Mesh; friend class Mesh;

View File

@ -4,10 +4,10 @@
#include <memory> #include <memory>
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include "window/window.h" #include "engine/window/window.h"
#include "window/events/window.h" #include "engine/window/events/window.h"
#include "app/app.h" #include "engine/app/app.h"
class Engine { class Engine {
public: public:

View File

@ -4,7 +4,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <memory> #include <memory>
#include "texture.h" #include "engine/renderer/texture.h"
class Material { class Material {
private: private:

View File

@ -5,7 +5,7 @@
#include <string> #include <string>
#include <GL/glew.h> #include <GL/glew.h>
#include "renderer/basics.h" #include "engine/renderer/basics.h"
class Mesh { class Mesh {
public: // TODO: abstract away public: // TODO: abstract away

View File

@ -4,7 +4,7 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <entt/entity/registry.hpp> #include <entt/entity/registry.hpp>
#include "renderer/shader.h" #include "engine/renderer/shader.h"
// TODO: make static or singleton // TODO: make static or singleton
class Renderer { class Renderer {

View File

@ -7,10 +7,10 @@
#include <glm/glm.hpp> #include <glm/glm.hpp>
#include <memory> #include <memory>
#include "shader.h" #include "engine/renderer/shader.h"
#include "renderer/renderer.h" #include "engine/renderer/renderer.h"
#include "renderer/material.h" #include "engine/renderer/material.h"
#include "renderer/mesh.h" #include "engine/renderer/mesh.h"
enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN }; enum ObjElement { OHASH, MTLLIB, USEMTL, O, V, VN, VT, F, OUNKNOWN };
enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN }; enum MtlElement { MHASH, NEWMTL, NS, KA, KS, KD, NI, D, ILLUM, MAP_KD, MAP_KA, MUNKNOWN };

View File

@ -3,7 +3,7 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include <memory> #include <memory>
#include "event.h" #include "engine/window/event.h"
#define ENGINE_GL_MAJOR_VERSION 4 #define ENGINE_GL_MAJOR_VERSION 4
#define ENGINE_GL_MINOR_VERSION 6 #define ENGINE_GL_MINOR_VERSION 6

View File

@ -1,4 +1,4 @@
#include "IO/file_manager.h" #include "engine/IO/file_manager.h"
#include <fstream> #include <fstream>
#include <iostream> #include <iostream>

View File

@ -2,7 +2,7 @@
#include <cstdlib> // for strtof (fallback) #include <cstdlib> // for strtof (fallback)
#include <cstring> #include <cstring>
#include "IO/parser.h" #include "engine/IO/parser.h"
// Skip whitespace // Skip whitespace
void Parser::SkipSpaces() { void Parser::SkipSpaces() {

View File

@ -1,6 +1,6 @@
#include <GL/glew.h> #include <GL/glew.h>
#include "components/batch.h" #include "engine/components/batch.h"
unsigned int batch::LastID = 0; unsigned int batch::LastID = 0;

View File

@ -12,19 +12,19 @@
#include <glm/ext/matrix_clip_space.hpp> #include <glm/ext/matrix_clip_space.hpp>
#include <glm/ext/matrix_transform.hpp> #include <glm/ext/matrix_transform.hpp>
#include "renderer/shader.h" #include "engine/renderer/shader.h"
#include "renderer/wavefront.h" #include "engine/renderer/wavefront.h"
#include "renderer/engine.h" #include "engine/renderer/engine.h"
#include "renderer/renderer.h" #include "engine/renderer/renderer.h"
#include "IO/file_manager.h" #include "engine/IO/file_manager.h"
#include "components/transform.h" #include "engine/components/transform.h"
#include "components/light.h" #include "engine/components/light.h"
#include "components/camera.h" #include "engine/components/camera.h"
#include "components/mesh.h" #include "engine/components/mesh.h"
#include "components/rotate.h" #include "engine/components/rotate.h"
#include "components/batch.h" #include "engine/components/batch.h"
class Game : public IApplication { class Game : public IApplication {
public: public:

View File

@ -1,4 +1,4 @@
#include "renderer/debug.h" #include "engine/renderer/debug.h"
#include <iostream> #include <iostream>

View File

@ -1,9 +1,9 @@
#include <memory> #include <memory>
#include "renderer/engine.h" #include "engine/renderer/engine.h"
#include "window/event.h" #include "engine/window/event.h"
#include "renderer/wavefront.h" #include "engine/renderer/wavefront.h"
std::unique_ptr<IApplication> Engine::s_app = nullptr; std::unique_ptr<IApplication> Engine::s_app = nullptr;
std::shared_ptr<Window> Engine::s_window = nullptr; std::shared_ptr<Window> Engine::s_window = nullptr;

View File

@ -1,6 +1,6 @@
#include <cstddef> #include <cstddef>
#include "renderer/mesh.h" #include "engine/renderer/mesh.h"
Mesh::Mesh() { Mesh::Mesh() {
m_vao = 0; m_vao = 0;

View File

@ -9,15 +9,15 @@
#define GLM_ENABLE_EXPERIMENTAL #define GLM_ENABLE_EXPERIMENTAL
#include <glm/gtx/euler_angles.hpp> #include <glm/gtx/euler_angles.hpp>
#include "renderer/renderer.h" #include "engine/renderer/renderer.h"
#include "window/window.h" #include "engine/window/window.h"
#include "IO/file_manager.h" #include "engine/IO/file_manager.h"
#include "components/transform.h" #include "engine/components/transform.h"
#include "components/camera.h" #include "engine/components/camera.h"
#include "components/light.h" #include "engine/components/light.h"
#include "components/mesh.h" #include "engine/components/mesh.h"
#include "components/batch.h" #include "engine/components/batch.h"
Renderer::Renderer(entt::registry& registry) : m_registry(registry) Renderer::Renderer(entt::registry& registry) : m_registry(registry)
{ {

View File

@ -1,6 +1,6 @@
#include <iostream> #include <iostream>
#include <GL/glew.h> #include <GL/glew.h>
#include "renderer/shader.h" #include "engine/renderer/shader.h"
Shader::Shader() Shader::Shader()
{ {

View File

@ -2,7 +2,7 @@
#include <memory> #include <memory>
#include <GL/glew.h> #include <GL/glew.h>
#include "renderer/texture.h" #include "engine/renderer/texture.h"
#define STB_IMAGE_IMPLEMENTATION #define STB_IMAGE_IMPLEMENTATION
#include "stb_image.h" #include "stb_image.h"

View File

@ -5,9 +5,9 @@
#include <filesystem> #include <filesystem>
#include <GL/glew.h> #include <GL/glew.h>
#include "IO/parser.h" #include "engine/IO/parser.h"
#include "renderer/mesh.h" #include "engine/renderer/mesh.h"
#include "renderer/wavefront.h" #include "engine/renderer/wavefront.h"
#define DEFAULT_MATERIAL_NAME "default" #define DEFAULT_MATERIAL_NAME "default"

View File

@ -1,12 +1,12 @@
#include <SDL3/SDL.h> #include <SDL3/SDL.h>
#include "window/window.h" #include "engine/window/window.h"
#include "window/events/window.h" #include "engine/window/events/window.h"
#include <iostream> #include <iostream>
#include <GL/glew.h> #include <GL/glew.h>
#include "renderer/debug.h" #include "engine/renderer/debug.h"
std::shared_ptr<Window> Window::s_instance = nullptr; std::shared_ptr<Window> Window::s_instance = nullptr;

10
sandbox/CMakeLists.txt Normal file
View File

@ -0,0 +1,10 @@
set(SANDBOX_TARGET sandbox)
add_executable(${SANDBOX_TARGET} src/main.cpp)
set_target_properties(${SANDBOX_TARGET} PROPERTIES
CXX_STANDARD 17
CXX_STANDARD_REQUIRED ON
)
target_link_libraries(${SANDBOX_TARGET} PRIVATE ${ENGINE_TARGET})

6
sandbox/src/main.cpp Normal file
View File

@ -0,0 +1,6 @@
#include <iostream>
#include "engine/renderer/engine.h"
int main(int argc, char **argv) {
return 0;
}