Skip to content

Commit

Permalink
Add .clang-format, reformat code
Browse files Browse the repository at this point in the history
  • Loading branch information
loryruta committed Sep 14, 2023
1 parent 1b0dd15 commit 1d44828
Show file tree
Hide file tree
Showing 76 changed files with 3,990 additions and 4,104 deletions.
20 changes: 20 additions & 0 deletions .clang-format
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
---
BasedOnStyle: Google
---
Language: Cpp
IndentWidth: 4
ColumnLimit: 150
BreakBeforeBraces: Allman
PackConstructorInitializers: Never
SpaceBeforeCtorInitializerColon: true
BreakConstructorInitializers: AfterColon
AllowShortFunctionsOnASingleLine: Inline
BreakBeforeBinaryOperators: None
AlignAfterOpenBracket: BlockIndent
AllowShortLambdasOnASingleLine: Empty
BinPackArguments: false
BinPackParameters: false
NamespaceIndentation: All
ReferenceAlignment: Left
AllowAllParametersOfDeclarationOnNextLine: true
---
167 changes: 88 additions & 79 deletions src/Game.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,149 +4,158 @@

using namespace explo;

Game::Game(GlfwWindow& window) :
/* Misc utils */
m_main_thread_executor{},
m_thread_pool(std::thread::hardware_concurrency()),
Game::Game(GlfwWindow &window) :
/* Misc utils */
m_main_thread_executor{},
m_thread_pool(std::thread::hardware_concurrency()),

/* Video */
m_window(window)
{
m_debug_ui = std::make_unique<DebugUi>(RenderApi::renderer());

RenderApi::ui_draw([this]() { m_debug_ui->display(); });

glfwSetWindowUserPointer(m_window.handle(), this);

glfwSetKeyCallback(m_window.handle(), [](GLFWwindow* window, int key, int scancode, int action, int mods)
{
Game* game = static_cast<Game*>(glfwGetWindowUserPointer(window));
game->on_key_change(key, action);
});

run_on_main_thread([this]() { late_initialize(); });
m_debug_ui = std::make_unique<DebugUi>(RenderApi::renderer());

RenderApi::ui_draw(
[this]()
{
m_debug_ui->display();
}
);

glfwSetWindowUserPointer(m_window.handle(), this);

glfwSetKeyCallback(
m_window.handle(),
[](GLFWwindow *window, int key, int scancode, int action, int mods)
{
Game *game = static_cast<Game *>(glfwGetWindowUserPointer(window));
game->on_key_change(key, action);
}
);

run_on_main_thread(
[this]()
{
late_initialize();
}
);
}

Game::~Game()
{
RenderApi::ui_draw([]() {});
RenderApi::ui_draw([]() {});
}

void Game::late_initialize()
{
m_world = std::make_shared<World>(
m_volume_generator,
m_surface_generator
);
m_world = std::make_shared<World>(m_volume_generator, m_surface_generator);

m_player = std::make_shared<Entity>(*m_world, glm::vec3(0, 10, 0));
m_player_controller = std::make_unique<EntityController>(*m_player);
m_player = std::make_shared<Entity>(*m_world, glm::vec3(0, 10, 0));
m_player_controller = std::make_unique<EntityController>(*m_player);

const glm::ivec3 k_render_distance(20, 0, 20);
m_player->recreate_world_view(k_render_distance);
const glm::ivec3 k_render_distance(20, 0, 20);
m_player->recreate_world_view(k_render_distance);

RenderApi::camera_set_position(m_player->get_position());
RenderApi::camera_set_rotation(m_player->get_yaw(), m_player->get_pitch());
RenderApi::camera_set_projection_params(90.0f, 1.0f, 0.01f, 1000.0f);
RenderApi::camera_set_position(m_player->get_position());
RenderApi::camera_set_rotation(m_player->get_yaw(), m_player->get_pitch());
RenderApi::camera_set_projection_params(90.0f, 1.0f, 0.01f, 1000.0f);
}

void Game::run_on_main_thread(std::function<void()> const& job)
void Game::run_on_main_thread(std::function<void()> const &job)
{
m_main_thread_executor.enqueue_job(job);
m_main_thread_executor.enqueue_job(job);
}

void Game::run_async(std::function<void()> const& job)
void Game::run_async(std::function<void()> const &job)
{
m_thread_pool.enqueue_job(job);
m_thread_pool.enqueue_job(job);
}

void Game::on_window_resize(uint32_t width, uint32_t height)
{
RenderApi::window_resize(width, height);
RenderApi::window_resize(width, height);
}

void Game::render()
{
m_fps_counter++;
m_fps_counter++;

// FPS
if (!m_last_fps_time || (glfwGetTime() - *m_last_fps_time) >= 1.0f)
{
m_fps = m_fps_counter;
m_fps_counter = 0;
m_last_fps_time = (float) glfwGetTime();
}
// FPS
if (!m_last_fps_time || (glfwGetTime() - *m_last_fps_time) >= 1.0f)
{
m_fps = m_fps_counter;
m_fps_counter = 0;
m_last_fps_time = (float)glfwGetTime();
}

// dt
if (m_last_frame_time)
{
m_dt = float(glfwGetTime()) - *m_last_frame_time;
}
// dt
if (m_last_frame_time)
{
m_dt = float(glfwGetTime()) - *m_last_frame_time;
}

m_last_frame_time = (float) glfwGetTime();
m_last_frame_time = (float)glfwGetTime();

m_main_thread_executor.process(); // Process main thread jobs
m_main_thread_executor.process(); // Process main thread jobs

if (m_player_controller->update_position())
RenderApi::camera_set_position(m_player->get_position());
if (m_player_controller->update_position()) RenderApi::camera_set_position(m_player->get_position());

if (m_player_controller->update_rotation())
RenderApi::camera_set_rotation(m_player->get_yaw(), m_player->get_pitch());
if (m_player_controller->update_rotation()) RenderApi::camera_set_rotation(m_player->get_yaw(), m_player->get_pitch());

RenderApi::render();
RenderApi::render();
}

void Game::on_key_change(int key, int action)
{
// Cursor logic
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
if (m_window.is_cursor_disabled())
{
m_window.enable_cursor();
}
else m_window.close();
}
else if (key == GLFW_KEY_ENTER && action == GLFW_PRESS)
{
if (!m_window.is_cursor_disabled()) m_window.disable_cursor();
}
// Cursor logic
if (key == GLFW_KEY_ESCAPE && action == GLFW_PRESS)
{
if (m_window.is_cursor_disabled())
{
m_window.enable_cursor();
}
else
m_window.close();
}
else if (key == GLFW_KEY_ENTER && action == GLFW_PRESS)
{
if (!m_window.is_cursor_disabled()) m_window.disable_cursor();
}
}

// --------------------------------------------------------------------------------------------------------------------------------

std::unique_ptr<Game> s_game;

void explo::init(GlfwWindow& window)
void explo::init(GlfwWindow &window)
{
RenderApi::init(window.handle());
RenderApi::init(window.handle());

s_game = std::make_unique<Game>(window);
s_game = std::make_unique<Game>(window);
}

void explo::shutdown()
{
s_game.reset();
s_game.reset();

RenderApi::destroy();
RenderApi::destroy();
}

void explo::render()
{
s_game->render();
s_game->render();
}

Game& explo::game()
Game &explo::game()
{
return *s_game;
return *s_game;
}

void explo::run_on_main_thread(std::function<void()> const& job)
void explo::run_on_main_thread(std::function<void()> const &job)
{
s_game->run_on_main_thread(job);
s_game->run_on_main_thread(job);
}

void explo::run_async(std::function<void()> const& job)
void explo::run_async(std::function<void()> const &job)
{
s_game->run_async(job);
s_game->run_async(job);
}
90 changes: 45 additions & 45 deletions src/Game.hpp
Original file line number Diff line number Diff line change
@@ -1,79 +1,79 @@
#pragma once

#include "GlfwWindow.hpp"
#include "input/EntityController.hpp"
#include "world/BlockRegistry.hpp"
#include "world/Entity.hpp"
#include "video/DebugUi.hpp"
#include "util/SyncJobExecutor.hpp"
#include "util/ThreadPool.hpp"
#include "world/volume/SinCosVolumeGenerator.hpp"
#include "world/volume/PerlinNoiseGenerator.hpp"
#include "video/DebugUi.hpp"
#include "world/BlockRegistry.hpp"
#include "world/Entity.hpp"
#include "world/surface/BlockySurfaceGenerator.hpp"
#include "GlfwWindow.hpp"
#include "world/volume/PerlinNoiseGenerator.hpp"
#include "world/volume/SinCosVolumeGenerator.hpp"

namespace explo
{
class Game
{
public:
/* Misc utils */
SyncJobExecutor m_main_thread_executor;
ThreadPool m_thread_pool;
public:
/* Misc utils */
SyncJobExecutor m_main_thread_executor;
ThreadPool m_thread_pool;

/* World */
BlockRegistry m_block_registry;
PerlinNoiseGenerator m_volume_generator;
BlockySurfaceGenerator m_surface_generator;
BlockRegistry m_block_registry;
PerlinNoiseGenerator m_volume_generator;
BlockySurfaceGenerator m_surface_generator;

/* Video */
GlfwWindow& m_window;
std::unique_ptr<DebugUi> m_debug_ui;
GlfwWindow &m_window;
std::unique_ptr<DebugUi> m_debug_ui;

float m_dt = 0.0f;
std::optional<float> m_last_frame_time;
float m_dt = 0.0f;
std::optional<float> m_last_frame_time;

// FPS
std::optional<float> m_last_fps_time;
int m_fps_counter = 0;
int m_fps = 0;
// FPS
std::optional<float> m_last_fps_time;
int m_fps_counter = 0;
int m_fps = 0;

/* Late initialize */
std::shared_ptr<World> m_world;
std::shared_ptr<Entity> m_player;
std::unique_ptr<EntityController> m_player_controller;
/* Late initialize */
std::shared_ptr<World> m_world;
std::shared_ptr<Entity> m_player;
std::unique_ptr<EntityController> m_player_controller;

public:
explicit Game(GlfwWindow& window);
public:
explicit Game(GlfwWindow &window);
~Game();

GlfwWindow& get_window() { return m_window; };
float get_dt() const { return m_dt; }
GlfwWindow &get_window() { return m_window; };
float get_dt() const { return m_dt; }

/* Job executors */
/* Job executors */

void run_on_main_thread(std::function<void()> const& job);
void run_async(std::function<void()> const& job);
void run_on_main_thread(std::function<void()> const &job);
void run_async(std::function<void()> const &job);

void on_window_resize(uint32_t width, uint32_t height);
void on_window_resize(uint32_t width, uint32_t height);
void render();

private:
void late_initialize();
private:
void late_initialize();

void on_key_change(int key, int action);
void on_key_change(int key, int action);
};

// --------------------------------------------------------------------------------------------------------------------------------
// --------------------------------------------------------------------------------------------------------------------------------

void init(GlfwWindow& window);
void shutdown();
void init(GlfwWindow &window);
void shutdown();

void render();
void render();

Game& game();
Game &game();

/* Helpers */
/* Helpers */

void run_on_main_thread(std::function<void()> const& job);
void run_async(std::function<void()> const& job);
}
void run_on_main_thread(std::function<void()> const &job);
void run_async(std::function<void()> const &job);
} // namespace explo
Loading

0 comments on commit 1d44828

Please sign in to comment.