mirror of
https://github.com/zhenyan121/Cubed.git
synced 2026-08-08 17:57:02 +08:00
chore: add clang-format and pre-commit configuration
This commit is contained in:
242
src/app.cpp
242
src/app.cpp
@@ -1,32 +1,26 @@
|
||||
#include <Cubed/app.hpp>
|
||||
#include <Cubed/debug_collector.hpp>
|
||||
#include <Cubed/gameplay/player.hpp>
|
||||
#include <Cubed/map_table.hpp>
|
||||
#include <Cubed/tools/cubed_assert.hpp>
|
||||
#include <Cubed/tools/log.hpp>
|
||||
#include <Cubed/tools/cubed_random.hpp>
|
||||
#include <Cubed/tools/system_info.hpp>
|
||||
#include <Cubed/tools/perlin_noise.hpp>
|
||||
#include "Cubed/app.hpp"
|
||||
|
||||
#include <imgui_impl_glfw.h>
|
||||
#include "Cubed/config.hpp"
|
||||
#include "Cubed/debug_collector.hpp"
|
||||
#include "Cubed/gameplay/player.hpp"
|
||||
#include "Cubed/tools/cubed_assert.hpp"
|
||||
#include "Cubed/tools/log.hpp"
|
||||
#include "Cubed/tools/system_info.hpp"
|
||||
|
||||
#include <exception>
|
||||
#include <imgui_impl_glfw.h>
|
||||
|
||||
namespace Cubed {
|
||||
|
||||
App::App() {}
|
||||
|
||||
App::App() {
|
||||
|
||||
}
|
||||
|
||||
App::~App() {
|
||||
|
||||
}
|
||||
void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos) {
|
||||
App::~App() {}
|
||||
void App::cursor_position_callback(GLFWwindow* window, double xpos,
|
||||
double ypos) {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
|
||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||
|
||||
|
||||
ASSERT_MSG(app, "nullptr");
|
||||
if (io.WantCaptureMouse && app->m_window.is_mouse_enable()) {
|
||||
ImGui_ImplGlfw_CursorPosCallback(window, xpos, ypos);
|
||||
@@ -35,7 +29,6 @@ void App::cursor_position_callback(GLFWwindow* window, double xpos, double ypos)
|
||||
if (!app->m_window.is_mouse_enable()) {
|
||||
app->m_camera.update_cursor_position_camera(xpos, ypos);
|
||||
}
|
||||
|
||||
}
|
||||
void App::init() {
|
||||
m_window.init();
|
||||
@@ -43,21 +36,26 @@ void App::init() {
|
||||
Logger::info("Window Init Success");
|
||||
|
||||
glfwSetWindowUserPointer(m_window.get_glfw_window(), this);
|
||||
|
||||
glfwSetCursorPosCallback(m_window.get_glfw_window(), cursor_position_callback);
|
||||
glfwSetMouseButtonCallback(m_window.get_glfw_window(), mouse_button_callback);
|
||||
glfwSetWindowFocusCallback(m_window.get_glfw_window(), window_focus_callback);
|
||||
glfwSetWindowSizeCallback(m_window.get_glfw_window(), window_reshape_callback);
|
||||
|
||||
glfwSetCursorPosCallback(m_window.get_glfw_window(),
|
||||
cursor_position_callback);
|
||||
glfwSetMouseButtonCallback(m_window.get_glfw_window(),
|
||||
mouse_button_callback);
|
||||
glfwSetWindowFocusCallback(m_window.get_glfw_window(),
|
||||
window_focus_callback);
|
||||
glfwSetWindowSizeCallback(m_window.get_glfw_window(),
|
||||
window_reshape_callback);
|
||||
glfwSetKeyCallback(m_window.get_glfw_window(), key_callback);
|
||||
glfwSetScrollCallback(m_window.get_glfw_window(), mouse_scroll_callback);
|
||||
glfwSetCursorEnterCallback(m_window.get_glfw_window(), cursor_enter_callback);
|
||||
glfwSetCursorEnterCallback(m_window.get_glfw_window(),
|
||||
cursor_enter_callback);
|
||||
glfwSetCharCallback(m_window.get_glfw_window(), char_callback);
|
||||
ChunkGenerator::init();
|
||||
|
||||
|
||||
m_renderer.init();
|
||||
Logger::info("Renderer Init Success");
|
||||
m_window.update_viewport();
|
||||
//MapTable::init_map();
|
||||
// MapTable::init_map();
|
||||
m_texture_manager.init_texture();
|
||||
Logger::info("Texture Load Success");
|
||||
m_world.init_world();
|
||||
@@ -67,14 +65,17 @@ void App::init() {
|
||||
m_dev_panel.init();
|
||||
}
|
||||
|
||||
void App::key_callback(GLFWwindow* window, int key, int scancode, int action, int mods) {
|
||||
void App::key_callback(GLFWwindow* window, int key, int scancode, int action,
|
||||
int mods) {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
|
||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||
ASSERT_MSG(app, "nullptr");
|
||||
//ImGui_ImplGlfw_CursorEnterCallback(window, !app->m_window.is_mouse_enable());
|
||||
// ImGui_ImplGlfw_CursorEnterCallback(window,
|
||||
// !app->m_window.is_mouse_enable());
|
||||
if (io.WantCaptureKeyboard && app->m_window.is_mouse_enable()) {
|
||||
if ((key == GLFW_KEY_LEFT_ALT || key == GLFW_KEY_ESCAPE) && action == GLFW_PRESS) {
|
||||
if ((key == GLFW_KEY_LEFT_ALT || key == GLFW_KEY_ESCAPE) &&
|
||||
action == GLFW_PRESS) {
|
||||
app->m_window.toggle_mouse_able();
|
||||
app->m_camera.reset_camera();
|
||||
return;
|
||||
@@ -82,39 +83,39 @@ void App::key_callback(GLFWwindow* window, int key, int scancode, int action, in
|
||||
ImGui_ImplGlfw_KeyCallback(window, key, scancode, action, mods);
|
||||
return;
|
||||
}
|
||||
switch(key) {
|
||||
case GLFW_KEY_Q:
|
||||
if (action == GLFW_PRESS) {
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_ESCAPE:
|
||||
if (action == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_F11:
|
||||
if (action == GLFW_PRESS) {
|
||||
app->m_window.toggle_fullscreen();
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_R:
|
||||
if (action == GLFW_PRESS) {
|
||||
app->m_texture_manager.need_reload();
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_LEFT_ALT:
|
||||
if (action == GLFW_PRESS) {
|
||||
app->m_window.toggle_mouse_able();
|
||||
app->m_camera.reset_camera();
|
||||
}
|
||||
break;
|
||||
|
||||
switch (key) {
|
||||
case GLFW_KEY_Q:
|
||||
if (action == GLFW_PRESS) {
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_ESCAPE:
|
||||
if (action == GLFW_PRESS) {
|
||||
glfwSetWindowShouldClose(window, GLFW_TRUE);
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_F11:
|
||||
if (action == GLFW_PRESS) {
|
||||
app->m_window.toggle_fullscreen();
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_R:
|
||||
if (action == GLFW_PRESS) {
|
||||
app->m_texture_manager.need_reload();
|
||||
}
|
||||
break;
|
||||
case GLFW_KEY_LEFT_ALT:
|
||||
if (action == GLFW_PRESS) {
|
||||
app->m_window.toggle_mouse_able();
|
||||
app->m_camera.reset_camera();
|
||||
}
|
||||
break;
|
||||
}
|
||||
|
||||
app->m_world.get_player("TestPlayer").update_player_move_state(key, action);
|
||||
}
|
||||
|
||||
void App::mouse_button_callback(GLFWwindow* window, int button, int action, int mods) {
|
||||
void App::mouse_button_callback(GLFWwindow* window, int button, int action,
|
||||
int mods) {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||
ASSERT_MSG(app, "nullptr");
|
||||
@@ -123,27 +124,28 @@ void App::mouse_button_callback(GLFWwindow* window, int button, int action, int
|
||||
return;
|
||||
}
|
||||
switch (button) {
|
||||
case GLFW_MOUSE_BUTTON_LEFT:
|
||||
if (action == GLFW_PRESS) {
|
||||
if(app->m_window.is_mouse_enable()) {
|
||||
app->m_window.toggle_mouse_able();
|
||||
app->m_camera.reset_camera();
|
||||
break;;
|
||||
}
|
||||
Input::get_input_state().mouse_state.left = true;
|
||||
case GLFW_MOUSE_BUTTON_LEFT:
|
||||
if (action == GLFW_PRESS) {
|
||||
if (app->m_window.is_mouse_enable()) {
|
||||
app->m_window.toggle_mouse_able();
|
||||
app->m_camera.reset_camera();
|
||||
break;
|
||||
;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
Input::get_input_state().mouse_state.left = false;
|
||||
}
|
||||
break;
|
||||
case GLFW_MOUSE_BUTTON_RIGHT:
|
||||
if (action == GLFW_PRESS) {
|
||||
Input::get_input_state().mouse_state.right = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
Input::get_input_state().mouse_state.right = false;
|
||||
}
|
||||
break;
|
||||
Input::get_input_state().mouse_state.left = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
Input::get_input_state().mouse_state.left = false;
|
||||
}
|
||||
break;
|
||||
case GLFW_MOUSE_BUTTON_RIGHT:
|
||||
if (action == GLFW_PRESS) {
|
||||
Input::get_input_state().mouse_state.right = true;
|
||||
}
|
||||
if (action == GLFW_RELEASE) {
|
||||
Input::get_input_state().mouse_state.right = false;
|
||||
}
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -158,19 +160,20 @@ void App::window_focus_callback(GLFWwindow* window, int focused) {
|
||||
if (focused) {
|
||||
app->m_camera.reset_camera();
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
void App::window_reshape_callback(GLFWwindow* window, int new_width, int new_height) {
|
||||
|
||||
void App::window_reshape_callback(GLFWwindow* window, int new_width,
|
||||
int new_height) {
|
||||
|
||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||
ASSERT_MSG(app, "nullptr");
|
||||
app->m_window.update_viewport();
|
||||
}
|
||||
|
||||
void App::mouse_scroll_callback(GLFWwindow* window, double xoffset, double yoffset) {
|
||||
void App::mouse_scroll_callback(GLFWwindow* window, double xoffset,
|
||||
double yoffset) {
|
||||
ImGuiIO& io = ImGui::GetIO();
|
||||
|
||||
|
||||
App* app = static_cast<App*>(glfwGetWindowUserPointer(window));
|
||||
ASSERT_MSG(app, "nullptr");
|
||||
if (io.WantCaptureMouse && app->m_window.is_mouse_enable()) {
|
||||
@@ -201,25 +204,23 @@ void App::char_callback(GLFWwindow* window, unsigned int c) {
|
||||
}
|
||||
|
||||
void App::render() {
|
||||
|
||||
if (glfwGetWindowAttrib(m_window.get_glfw_window(), GLFW_ICONIFIED) != 0)
|
||||
{
|
||||
ImGui_ImplGlfw_Sleep(10);
|
||||
return;
|
||||
}
|
||||
|
||||
if (glfwGetWindowAttrib(m_window.get_glfw_window(), GLFW_ICONIFIED) != 0) {
|
||||
ImGui_ImplGlfw_Sleep(10);
|
||||
return;
|
||||
}
|
||||
m_renderer.render();
|
||||
|
||||
glfwSwapBuffers(m_window.get_glfw_window());
|
||||
}
|
||||
|
||||
void App::run() {
|
||||
|
||||
|
||||
last_time = glfwGetTime();
|
||||
while(!glfwWindowShouldClose(m_window.get_glfw_window())) {
|
||||
|
||||
while (!glfwWindowShouldClose(m_window.get_glfw_window())) {
|
||||
|
||||
update();
|
||||
render();
|
||||
|
||||
render();
|
||||
}
|
||||
}
|
||||
static Gait player_gait = Gait::WALK;
|
||||
@@ -236,13 +237,16 @@ void App::update() {
|
||||
glfwSetWindowTitle(m_window.get_glfw_window(), title.c_str());
|
||||
frame_count = 0;
|
||||
fps_time_count = 0.0f;
|
||||
DebugCollector::get().report("fps", std::string{"FPS: " + std::to_string(fps)});
|
||||
DebugCollector::get().report("rss", std::format("RSS: {}mb", Tools::get_current_rss() / (1024 * 1024)));
|
||||
DebugCollector::get().report(
|
||||
"fps", std::string{"FPS: " + std::to_string(fps)});
|
||||
DebugCollector::get().report(
|
||||
"rss",
|
||||
std::format("RSS: {}mb", Tools::get_current_rss() / (1024 * 1024)));
|
||||
}
|
||||
m_texture_manager.update();
|
||||
m_world.update(delta_time);
|
||||
m_camera.update_move_camera();
|
||||
const auto& player= m_world.get_player("TestPlayer");
|
||||
const auto& player = m_world.get_player("TestPlayer");
|
||||
if (player_gait != player.get_gait()) {
|
||||
player_gait = player.get_gait();
|
||||
float fov = static_cast<float>(Config::get().get<double>("player.fov"));
|
||||
@@ -253,7 +257,6 @@ void App::update() {
|
||||
m_renderer.update_fov(fov + 5.0f);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
int App::start_cubed_application(int argc, char** argv) {
|
||||
@@ -268,39 +271,22 @@ int App::start_cubed_application(int argc, char** argv) {
|
||||
return 0;
|
||||
} catch (std::exception& e) {
|
||||
Logger::error("{}", e.what());
|
||||
|
||||
|
||||
} catch (...) {
|
||||
Logger::error("Unkown error");
|
||||
|
||||
}
|
||||
return 1;
|
||||
}
|
||||
|
||||
float App::delte_time() {
|
||||
return delta_time;
|
||||
}
|
||||
float App::delte_time() { return delta_time; }
|
||||
|
||||
float App::get_fps() {
|
||||
return fps;
|
||||
}
|
||||
float App::get_fps() { return fps; }
|
||||
|
||||
Camera& App::camera() {
|
||||
return m_camera;
|
||||
}
|
||||
DevPanel& App::dev_panel() {
|
||||
return m_dev_panel;
|
||||
}
|
||||
Renderer& App::renderer() {
|
||||
return m_renderer;
|
||||
}
|
||||
TextureManager& App::texture_manager() {
|
||||
return m_texture_manager;
|
||||
}
|
||||
Window& App::window() {
|
||||
return m_window;
|
||||
}
|
||||
World& App::world() {
|
||||
return m_world;
|
||||
}
|
||||
Camera& App::camera() { return m_camera; }
|
||||
DevPanel& App::dev_panel() { return m_dev_panel; }
|
||||
Renderer& App::renderer() { return m_renderer; }
|
||||
TextureManager& App::texture_manager() { return m_texture_manager; }
|
||||
Window& App::window() { return m_window; }
|
||||
World& App::world() { return m_world; }
|
||||
|
||||
}
|
||||
} // namespace Cubed
|
||||
Reference in New Issue
Block a user