diff --git a/deerith_engine_client/CMakeLists.txt b/deerith_engine_client/CMakeLists.txt index 01f9b9d..6ce2041 100644 --- a/deerith_engine_client/CMakeLists.txt +++ b/deerith_engine_client/CMakeLists.txt @@ -2,6 +2,9 @@ find_package(glfw3 REQUIRED) find_package(OpenGL REQUIRED) find_package(glm REQUIRED) +find_package(PkgConfig REQUIRED) +pkg_check_modules(IMGUI REQUIRED imgui) + add_library(DeerithClient) @@ -18,6 +21,7 @@ target_sources(DeerithClient target_include_directories(DeerithClient PUBLIC ${CMAKE_CURRENT_SOURCE_DIR}/include + ${IMGUI_INCLUDE_DIRS} ) target_link_libraries(DeerithClient @@ -26,7 +30,9 @@ target_link_libraries(DeerithClient OpenGL::GL glfw glm::glm + ${IMGUI_LIBRARIES} ) + add_custom_target(DeerithClientShaders ALL COMMAND ${CMAKE_COMMAND} -E copy_directory ${CMAKE_CURRENT_SOURCE_DIR}/shaders diff --git a/deerith_engine_client/include/deerith/graphics/input.h b/deerith_engine_client/include/deerith/graphics/input.h new file mode 100644 index 0000000..bb297b6 --- /dev/null +++ b/deerith_engine_client/include/deerith/graphics/input.h @@ -0,0 +1,7 @@ +#pragma once +#include + +namespace deerith +{ + glm::vec2 get_joystick_axes(int controller_id, int axe_id); +} \ No newline at end of file diff --git a/deerith_engine_client/include/deerith/graphics/window.h b/deerith_engine_client/include/deerith/graphics/window.h index 26e9a68..7ead6d4 100644 --- a/deerith_engine_client/include/deerith/graphics/window.h +++ b/deerith_engine_client/include/deerith/graphics/window.h @@ -1,4 +1,5 @@ #pragma once +#include namespace deerith { @@ -9,5 +10,8 @@ namespace deerith bool begin_window_display(); void finish_window_display(); + glm::ivec2 get_window_size(); + float get_window_aspect_ratio(); + int get_current_frame_id(); } \ No newline at end of file diff --git a/deerith_engine_client/src/input.cpp b/deerith_engine_client/src/input.cpp new file mode 100644 index 0000000..bb59c7a --- /dev/null +++ b/deerith_engine_client/src/input.cpp @@ -0,0 +1,31 @@ +#include +#include +#include + +#include + +glm::vec2 deerith::get_joystick_axes(int controller_id, int axe_id) +{ + int count; + const float *axes = glfwGetJoystickAxes(controller_id, &count); + + if (count < 2 || axe_id * 2 >= count) + { + return glm::vec2(0, 0); + } + + float corrected_axe_x = axes[axe_id * 2 + 0]; + float corrected_axe_y = -axes[axe_id * 2 + 1]; + + if (std::abs(corrected_axe_x) < 0.1f) + { + corrected_axe_x = 0; + } + + if (std::abs(corrected_axe_y) < 0.1f) + { + corrected_axe_y = 0; + } + + return glm::vec2(corrected_axe_x, corrected_axe_y); +} \ No newline at end of file diff --git a/deerith_engine_client/src/window.cpp b/deerith_engine_client/src/window.cpp index 17ccfc9..8b6f7b8 100644 --- a/deerith_engine_client/src/window.cpp +++ b/deerith_engine_client/src/window.cpp @@ -4,6 +4,10 @@ #include #include +#include +#include +#include + #include #include @@ -16,6 +20,9 @@ namespace deerith constexpr double TARGET_FRAME_TIME = 1.0 / 60.0; std::chrono::steady_clock::time_point last_frame_time; + std::chrono::steady_clock::time_point frame_start; + + void init_imgui(); } int deerith::init_window() @@ -63,42 +70,63 @@ int deerith::init_window() glDisable(GL_CULL_FACE); init_primitives(); + init_imgui(); return 0; } bool deerith::begin_window_display() { + glfwPollEvents(); if (glfwWindowShouldClose(window)) { return false; } - glClearColor(0.1f, 0.1f, 0.15f, 1.0f); - glClear(GL_COLOR_BUFFER_BIT); + ImGui_ImplOpenGL3_NewFrame(); + ImGui_ImplGlfw_NewFrame(); + ImGui::NewFrame(); + return true; } + void deerith::finish_window_display() { + + ImGui::Render(); + int display_w, display_h; + glfwGetFramebufferSize(window, &display_w, &display_h); + glViewport(0, 0, display_w, display_h); + ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); + using clock = std::chrono::steady_clock; const auto now = clock::now(); const std::chrono::duration elapsed = now - last_frame_time; - if (elapsed.count() < TARGET_FRAME_TIME) + using clock = std::chrono::steady_clock; + + while (std::chrono::duration( + clock::now() - frame_start) + .count() < TARGET_FRAME_TIME) { - std::this_thread::sleep_for( - std::chrono::duration( - TARGET_FRAME_TIME - elapsed.count())); + std::this_thread::sleep_for(std::chrono::milliseconds(5)); } frame_id++; glfwSwapBuffers(window); - glfwPollEvents(); + + glClearColor(0.1f, 0.1f, 0.15f, 1.0f); + glClear(GL_COLOR_BUFFER_BIT); } void deerith::shutdown_window() { + + ImGui_ImplOpenGL3_Shutdown(); + ImGui_ImplGlfw_Shutdown(); + ImGui::DestroyContext(); + shutdown_primitives(); glfwDestroyWindow(window); @@ -108,4 +136,43 @@ void deerith::shutdown_window() int deerith::get_current_frame_id() { return frame_id; +} + +glm::ivec2 deerith::get_window_size() +{ + + glm::ivec2 size; + glfwGetWindowSize(window, &size.x, &size.y); + + return size; +} + +float deerith::get_window_aspect_ratio() +{ + glm::ivec2 size; + glfwGetWindowSize(window, &size.x, &size.y); + + return (float)size.x / (float)size.y; +} + +void deerith::init_imgui() +{ + float main_scale = ImGui_ImplGlfw_GetContentScaleForMonitor(glfwGetPrimaryMonitor()); + + IMGUI_CHECKVERSION(); + ImGui::CreateContext(); + ImGuiIO &io = ImGui::GetIO(); + (void)io; + io.ConfigFlags |= ImGuiConfigFlags_NavEnableKeyboard; // Enable Keyboard Controls + io.ConfigFlags |= ImGuiConfigFlags_NavEnableGamepad; // Enable Gamepad Controls + + ImGui::StyleColorsDark(); + + ImGuiStyle &style = ImGui::GetStyle(); + style.ScaleAllSizes(main_scale); + style.FontScaleDpi = main_scale; + + const char *glsl_version = nullptr; + ImGui_ImplGlfw_InitForOpenGL(window, true); + ImGui_ImplOpenGL3_Init(glsl_version); } \ No newline at end of file diff --git a/sandbox/src/main.cpp b/sandbox/src/main.cpp index 999eb69..3ae4de2 100644 --- a/sandbox/src/main.cpp +++ b/sandbox/src/main.cpp @@ -1,11 +1,17 @@ #include #include +#include #include #include #include +#include #include "deerith/core/log.h" +glm::vec2 player_pos(5, 5); + +float vel = 1; + int main(int args_count, const char **args) { deerith::init_deerith_core(); @@ -20,13 +26,19 @@ int main(int args_count, const char **args) while (deerith::begin_window_display()) { - deerith::define_primitive_viewspace(glm::vec2(0, 0), glm::vec2(1, 1)); + deerith::define_primitive_viewspace(glm::vec2(0, 0), glm::vec2(10 * deerith::get_window_aspect_ratio(), 10)); - deerith::define_primitive_color(glm::vec4(0.9f, 0, 0, 1)); - deerith::draw_primitive_rectangle(glm::vec2(0, 0), glm::vec2(0.5f, 1)); + player_pos += deerith::get_joystick_axes(0, 0) * (vel / 60.0f); - deerith::define_primitive_color(glm::vec4(0.2f, 0.4f, (deerith::get_current_frame_id() % 100) / 100.0f, 1)); - deerith::draw_primitive_rectangle(glm::vec2(0.5, 0), glm::vec2(1, 0.5f)); + deerith::define_primitive_color(glm::vec4(0.4f, 0.7f, 1.0f, 1)); + deerith::draw_primitive_rectangle(player_pos - glm::vec2(0.5f, 0.5f), player_pos + glm::vec2(0.5f, 0.5f)); + + if (ImGui::Begin("Context")) + { + ImGui::SliderFloat("Velocity", &vel, 0, 4); + + ImGui::End(); + } deerith::finish_window_display(); }