Skip to content

Commit 1a94d88

Browse files
committed
initial open source
0 parents  commit 1a94d88

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+7271
-0
lines changed

.clang-format

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
---
2+
BasedOnStyle: Google
3+
TabWidth: '4'
4+
UseTab: Never
5+
IndentWidth: 4
6+
ColumnLimit: 120
7+
SortIncludes: false
8+
AccessModifierOffset: -4

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
build/
2+
.vscode/
3+
.cache/

.gitmodules

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[submodule "vendor/libspng"]
2+
path = vendor/libspng
3+
url = https://github.com/randy408/libspng
4+
[submodule "vendor/glfw"]
5+
path = vendor/glfw
6+
url = https://github.com/glfw/glfw
7+
[submodule "vendor/FastNoise2"]
8+
path = vendor/FastNoise2
9+
url = https://github.com/Auburn/FastNoise2
10+
[submodule "vendor/tracy"]
11+
path = vendor/tracy
12+
url = https://github.com/wolfpld/tracy

CMakeLists.txt

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
cmake_minimum_required(VERSION 3.9.4...3.26.0)
2+
set(CMAKE_CXX_STANDARD 23)
3+
set(CMAKE_CXX_STANDARD_REQUIRED ON)
4+
set(CMAKE_CXX_EXTENSIONS OFF)
5+
6+
project(voxel VERSION 0.1.0)
7+
8+
add_executable(voxel vendor/glad/src/glad.c src/main.cpp src/debug.cpp src/render/vertexarray.cpp src/render/image.cpp src/gfxm/camera.cpp src/mgr/manager.cpp src/mgr/threadpool.cpp src/mgr/chunkstore.cpp src/render/renderer.cpp src/worldgen/generator.cpp)
9+
include_directories(include vendor/glad/include vendor/glfw/include vendor/libspng/spng vendor vendor/FastNoise2/include vendor/tracy/public)
10+
11+
set(GLFW_BUILD_DOCS OFF CACHE BOOL "" FORCE)
12+
set(GLFW_BUILD_TESTS OFF CACHE BOOL "" FORCE)
13+
set(GLFW_BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
14+
add_subdirectory(vendor/glfw)
15+
16+
set(SPNG_SHARED OFF CACHE BOOL "" FORCE)
17+
set(BUILD_EXAMPLES OFF CACHE BOOL "" FORCE)
18+
add_subdirectory(vendor/libspng)
19+
20+
set(FASTNOISE2_NOISETOOL OFF CACHE BOOL "" FORCE)
21+
add_subdirectory(vendor/FastNoise2)
22+
23+
option(TRACY_ENABLE "Enable Tracy" OFF)
24+
option(TRACY_ON_DEMAND "" ON)
25+
add_subdirectory(vendor/tracy)
26+
27+
target_compile_options(voxel PRIVATE -Wall -Werror -mavx2)
28+
29+
if (CMAKE_BUILD_TYPE STREQUAL "Release")
30+
set_property(TARGET voxel PROPERTY INTERPROCEDURAL_OPTIMIZATION TRUE)
31+
endif()
32+
33+
target_link_libraries(voxel glfw spng_static FastNoise Tracy::TracyClient)

README.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Voxel Game Engine
2+
3+
A voxel game engine using C++23 and modern OpenGL.
4+
5+
![](screenshot.png)
6+
7+
## Compiling
8+
9+
```sh
10+
mkdir build
11+
cd build
12+
cmake .. -DCMAKE_BUILD_TYPE=Release
13+
make -j
14+
# Run: ./voxel
15+
```

assets/dirt.png

3.06 KB
Loading

assets/stone.png

1.33 KB
Loading

include/app.h

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
#pragma once
2+
3+
#include "gfxm/gfxm.h"
4+
#include <glad/glad.h>
5+
#include <atomic>
6+
7+
class App {
8+
int _width, _height;
9+
double prev_mouse_x, prev_mouse_y;
10+
gfxm::Camera _camera;
11+
float _fov;
12+
bool camera_locked = false;
13+
14+
public:
15+
App(int width, int height, float fov) : _width(width), _height(height), _fov(fov) {}
16+
17+
void set_framebuffer_size(int width, int height) {
18+
glViewport(0, 0, width, height);
19+
_width = width;
20+
_height = height;
21+
}
22+
23+
// Sets the mouse pos used to calculate the mouse delta
24+
void set_prev_mouse_pos(double x, double y) {
25+
prev_mouse_x = x;
26+
prev_mouse_y = y;
27+
}
28+
29+
void handle_mouse_move(double x, double y) {
30+
if (camera_locked) return;
31+
32+
double sens = 0.01;
33+
34+
double dx = x - prev_mouse_x;
35+
double dy = y - prev_mouse_y;
36+
37+
float yaw = _camera.yaw() + dx * sens;
38+
float pitch = _camera.pitch() - dy * sens;
39+
pitch = std::clamp(pitch, -gfxm::HALF_PI_F * 0.98f, gfxm::HALF_PI_F * 0.98f);
40+
41+
_camera.set_angles(pitch, yaw);
42+
43+
this->set_prev_mouse_pos(x, y);
44+
}
45+
46+
int width() const { return _width; }
47+
48+
int height() const { return _height; }
49+
50+
float fov() const { return _fov; }
51+
52+
void toggle_camera_lock() { camera_locked = !camera_locked; }
53+
54+
gfxm::Camera& camera() { return _camera; }
55+
56+
const gfxm::Camera& camera() const { return _camera; }
57+
};

include/config.h

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
#pragma once
2+
3+
#include <chrono>
4+
#include <thread>
5+
6+
namespace config {
7+
// Should be even...
8+
constexpr unsigned int BLOCK_SIZE = 80;
9+
10+
constexpr std::chrono::duration<float> MGR_TICK_DURATION = std::chrono::milliseconds(1000 / 20);
11+
12+
inline size_t mgr_thread_count() { return std::max(1u, std::thread::hardware_concurrency() - 1); }
13+
14+
// The maximum chunk indices that can exist
15+
16+
// Must be between -2^23 and 2^23 - 1
17+
constexpr int MIN_CHUNK_X = -8000000;
18+
constexpr int MAX_CHUNK_X = 8000000 - 1;
19+
constexpr int MIN_CHUNK_Z = -8000000;
20+
constexpr int MAX_CHUNK_Z = 8000000 - 1;
21+
22+
// Must be between -2^15 and 2^15 - 1
23+
constexpr int MIN_CHUNK_Y = -3;
24+
constexpr int MAX_CHUNK_Y = 2;
25+
26+
// Render distance in chunks
27+
// Should not be more than a few thousand ish...
28+
constexpr int RENDER_DISTANCE = 10;
29+
30+
// The maximum number of chunks that can be loaded at once
31+
constexpr size_t MAX_CHUNKS_LOADED =
32+
2 * (2 * (RENDER_DISTANCE + 2) + 1) * (2 * (RENDER_DISTANCE + 2) + 1) * (2 * (RENDER_DISTANCE + 2) + 1);
33+
} // namespace config

include/debug.h

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
#pragma once
2+
3+
#include <glad/glad.h>
4+
5+
void GLAPIENTRY debug_message_callback(GLenum source, GLenum type, GLuint id, GLenum severity, GLsizei length,
6+
const char *message, const void *userParam);
7+
8+
void debug_log(const char *message);
9+
10+
inline constexpr bool debug_enabled() {
11+
#ifdef NDEBUG
12+
return false;
13+
#else
14+
return true;
15+
#endif
16+
}

0 commit comments

Comments
 (0)