Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 11 additions & 3 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
---
Language: Cpp
BasedOnStyle: LLVM
AlignAfterOpenBracket: BlockIndent
AlignAfterOpenBracket: AlwaysBreak
AlignOperands: AlignAfterOperator
AlignArrayOfStructures: Right
AlignArrayOfStructures: None
AlignConsecutiveAssignments:
Enabled: true
AlignCompound: true
Expand Down Expand Up @@ -49,18 +49,25 @@ BraceWrapping:
AfterUnion: true
BeforeCatch: false
BeforeElse: false
IndentBraces: false
SplitEmptyFunction: false
SplitEmptyRecord: false
SplitEmptyNamespace: false
BracedInitializerIndentWidth: 2
BinPackParameters: OnePerLine
BinPackLongBracedList: false
BitFieldColonSpacing: Both
BreakBeforeBinaryOperators: All
BreakConstructorInitializers: BeforeComma
# BreakAfterOpenBracketBracedList: true
BreakInheritanceList: BeforeComma
BreakStringLiterals: true
ConstructorInitializerIndentWidth: 2
ContinuationIndentWidth: 2
ColumnLimit: 100
EmptyLineAfterAccessModifier: Leave
EmptyLineBeforeAccessModifier: Always
FixNamespaceComments: true
FixNamespaceComments: false
JavaScriptQuotes: Single
KeepEmptyLines:
AtEndOfFile: true
Expand Down Expand Up @@ -100,5 +107,6 @@ SpaceBeforeParensOptions:
AfterRequiresInExpression: true
SpaceBeforeRangeBasedForLoopColon: true
UseTab: Never
Cpp11BracedListStyle: false
...

1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
.vs/
.idea/
.cache/
ignore/

# CMake
CMakeSettings.json
Expand Down
80 changes: 50 additions & 30 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -2,58 +2,78 @@ cmake_minimum_required (VERSION 3.25)

project (game VERSION 0.0.1 LANGUAGES CXX)

set (CORE core)
set (ENGINE engine)
set (GAME ${PROJECT_NAME})

option (BUILD_TESTS "Build tests" OFF)
option (ENABLE_LINTER "Run fast static analysis (clang-tidy) during builds" OFF)
option (ENABLE_DEEP_ANALYSIS "Run slow, deep static analysis (-fanalyzer or /analyze)" OFF)

include (FetchContent)

FetchContent_Declare (
SDL3
GIT_REPOSITORY https://github.com/libsdl-org/SDL.git
GIT_TAG release-3.4.10
GIT_SHALLOW TRUE
FIND_PACKAGE_ARGS 3.4.0
)
set (DSDL_SHARED ON)
set (DSDL_STATIC OFF)
set (DSDL_TEST_LIBRARY OFF)
FetchContent_MakeAvailable (SDL3)

FetchContent_Declare (
glm
GIT_REPOSITORY https://github.com/g-truc/glm.git
GIT_TAG 1.0.3
GIT_SHALLOW TRUE
FIND_PACKAGE_ARGS 1.0.0
)
FetchContent_MakeAvailable (glm)

if (BUILD_TESTS)
enable_testing ()
FetchContent_Declare (
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.15.0
GIT_SHALLOW TRUE
FIND_PACKAGE_ARGS 3.0.0
)
FetchContent_MakeAvailable (Catch2)
enable_testing ()
FetchContent_Declare (
Catch2
GIT_REPOSITORY https://github.com/catchorg/Catch2.git
GIT_TAG v3.15.0
GIT_SHALLOW TRUE
FIND_PACKAGE_ARGS 3.0.0
)
FetchContent_MakeAvailable (Catch2)
endif ()

add_subdirectory (core)
add_subdirectory (engine)
add_subdirectory (app)
add_subdirectory (gfx)
add_subdirectory (game)

add_library(project_warnings INTERFACE)

if (MSVC)
target_compile_options(project_warnings INTERFACE /W4)
target_compile_options(project_warnings INTERFACE /W4)
else ()
target_compile_options(project_warnings INTERFACE -Wall -Wextra -Wpedantic)
target_compile_options(project_warnings INTERFACE -Wall -Wextra -Wpedantic)
endif()

if (ENABLE_DEEP_ANALYSIS)
if (MSVC)
target_compile_options (project_warnings INTERFACE /analyze)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options (project_warnings INTERFACE -fanalyzer)
else ()
message (FATAL_ERROR "ENABLE_DEEP_ANALYSIS is ON, but ${CMAKE_CXX_COMPILER_ID} doesn't have an analyzer.")
endif ()
if (MSVC)
target_compile_options (project_warnings INTERFACE /analyze)
elseif (CMAKE_CXX_COMPILER_ID STREQUAL "GNU")
target_compile_options (project_warnings INTERFACE -fanalyzer)
else ()
message (FATAL_ERROR "ENABLE_DEEP_ANALYSIS is ON, but ${CMAKE_CXX_COMPILER_ID} doesn't have an analyzer.")
endif ()
endif ()

target_link_libraries(${CORE} PRIVATE project_warnings)
target_link_libraries(${ENGINE} PRIVATE project_warnings)
target_link_libraries(${GAME} PRIVATE project_warnings)
target_link_libraries (core PRIVATE project_warnings)
target_link_libraries (app PRIVATE project_warnings)
target_link_libraries (gfx PRIVATE project_warnings)
target_link_libraries (game PRIVATE project_warnings)

if (ENABLE_LINTER)
find_program(CLANG_TIDY_EXE NAMES clang-tidy REQUIRED)
set_target_properties (${CORE} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
set_target_properties (${ENGINE} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
set_target_properties (${GAME} PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
find_program(CLANG_TIDY_EXE NAMES clang-tidy REQUIRED)
set_target_properties (core PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
set_target_properties (app PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
set_target_properties (gfx PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
set_target_properties (game PROPERTIES CXX_CLANG_TIDY "${CLANG_TIDY_EXE}")
endif ()

17 changes: 17 additions & 0 deletions app/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
add_library (app)

target_include_directories (
app
PUBLIC ${SDL3_INCLUDE_DIRS}
PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include
)

target_link_libraries (
app
PUBLIC SDL3::SDL3
PUBLIC glm::glm
PUBLIC core
)

add_subdirectory (src)

Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,25 @@
#include "core/bitmask.hh"
#include "core/types.hh"
#include <SDL3/SDL_scancode.h>
// It's not clear that this is necessary

struct Keyboard;
struct Mouse;

// Button state
enum class On : u8 {
Press = 1 << 0,
Release = 1 << 1,
Repeat = 1 << 2,
};

// Keyboard input modifiers
enum class With : u8 {
None = 0,
Ctrl = 1 << 0,
Shift = 1 << 1,
Alt = 1 << 2,
};

DEFINE_ENUM_BITWISE_OPERATORS(On)
DEFINE_ENUM_BITWISE_OPERATORS(With)

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#pragma once
#include <SDL3/SDL_events.h>

bool push_event(SDL_Event event);
SDL_Event make_quit_event();
File renamed without changes.
12 changes: 5 additions & 7 deletions engine/include/engine/app.hh → app/include/app/main.hh
Original file line number Diff line number Diff line change
Expand Up @@ -2,15 +2,13 @@

// These functions need to be defined by an external binary linking against the engine.

#include "engine/render/window.hh"
#include "engine/time.hh"
#include "app/time.hh"

union SDL_Event;

struct AppConfig
{
Hertz step_rate = 32u;
WindowConfig window;
Hertz step_rate = 32u;
};

// An opaque type defined by the application; the engine only holds and passes a pointer to it.
Expand All @@ -23,17 +21,17 @@ struct AppState;
// Create `AppState` here. Returning null indicates application failure and `app_quit` will follow.
[[nodiscard]] AppState* start();

// Called 0 or more times per frame, once per elapsed timestep. Update simulation systems and
// Called 0 or more times per frame, once per elapsed timestep. Update simulation systems and
// anything that needs determinism here. Time that can't be processed in this frame will be deferred
// to the next frame, accumulating lag.
void step(AppState& state);

// Called once per frame. Advances by the time elapsed since the last frame, capped by 4 hertz.
// Update user-interface, graphics, animations, and anything that doesn't need determinism here.
void update(AppState& state, Nanoseconds dt);
void update(AppState& state, f32 delta);

// Called once per frame after `app_update` and `app_step`.
void render(AppState& state);
void render(AppState& state, f32 alpha);

void handle_event(AppState& state, SDL_Event const& event);

Expand Down
File renamed without changes.
16 changes: 10 additions & 6 deletions engine/include/engine/time.hh → app/include/app/time.hh
Original file line number Diff line number Diff line change
@@ -1,22 +1,24 @@
#pragma once

// TODO: adopt std::chrono over SDL time

#include "core/types.hh"

using Hertz = u64;
using Nanoseconds = u64;
using Seconds = u64;

// hertz to nanoseconds
[[nodiscard]] constexpr Nanoseconds from_hertz(Hertz time) noexcept { return 1'000'000'000 / time; }

// nanoseconds to seconds
[[nodiscard]] constexpr Seconds to_seconds(Nanoseconds time) noexcept
[[nodiscard]] constexpr f32 to_seconds(Nanoseconds time) noexcept
{
return time / 1'000'000'000;
return f32(time) / 1'000'000'000.f;
}

struct TimeStep
{
constexpr explicit TimeStep(u64 size) noexcept : size_{size}, lag_{0u}, age_{0u}
constexpr explicit TimeStep(u64 size) noexcept : size_{ size }, lag_{ 0u }, age_{ 0u }
{
// allow a step size of 0?
}
Expand All @@ -27,7 +29,7 @@ struct TimeStep
if (size_ == 0u) return 0u;
lag_ += dt;
u64 steps = lag_ / size_;
lag_ -= size_ * steps;
lag_ -= steps * size_;
age_ += steps;
return steps;
}
Expand All @@ -36,9 +38,11 @@ struct TimeStep

[[nodiscard]] u64 age() const noexcept { return age_; }

[[nodiscard]] f32 alpha() const noexcept { return f32(lag_) / f32(size_); }

[[nodiscard]] constexpr u64 size() const noexcept { return size_; }

void reset(u64 size)
void resize(u64 size)
{
size_ = size;
lag_ = 0.f;
Expand Down
38 changes: 38 additions & 0 deletions app/include/app/window.hh
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
#pragma once
#include "core/error.hh"
#include "core/types.hh"
#include <glm/ext/vector_int2.hpp>

struct Window;

struct SDL_Window;

[[nodiscard]] Result<Window> create_window(std::string_view title, i32 width, i32 height);

struct Window
{
Window() noexcept = default;
Window(Window const&) = delete;
Window& operator=(Window const&) = delete;

explicit Window(SDL_Window*) noexcept;
Window(Window&&) noexcept;
Window& operator=(Window&&) noexcept;
~Window() noexcept;

std::string_view title() const;

void set_title(std::string_view title);

[[nodiscard]] glm::ivec2 size() const noexcept;

void resize(i32 width, i32 height) noexcept;

[[nodiscard]] bool valid() const noexcept { return handle_ != nullptr; }

[[nodiscard]] SDL_Window* handle() noexcept { return handle_; }

private:
SDL_Window* handle_ = nullptr;
};

1 change: 1 addition & 0 deletions app/src/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
target_sources (app PRIVATE main.cc event.cc keyboard.cc mouse.cc cue.cc window.cc)
6 changes: 3 additions & 3 deletions engine/src/input/cue.cc → app/src/cue.cc
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#include "engine/input/cue.hh"
#include "engine/input/keyboard.hh"
#include "engine/input/mouse.hh"
#include "app/cue.hh"
#include "app/keyboard.hh"
#include "app/mouse.hh"

static bool keyboard_matches_modifier(Keyboard const& kb, With with)
{
Expand Down
2 changes: 1 addition & 1 deletion engine/src/event.cc → app/src/event.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "engine/event.hh"
#include "app/event.hh"
#include <SDL3/SDL_log.h>
#include <SDL3/SDL_timer.h>

Expand Down
2 changes: 1 addition & 1 deletion engine/src/input/keyboard.cc → app/src/keyboard.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include "engine/input/keyboard.hh"
#include "app/keyboard.hh"

bool Keyboard::operator[](SDL_Scancode key) const { return state_.test(key); }

Expand Down
Loading
Loading