Input + imgui
This commit is contained in:
parent
26edf2c408
commit
738c48fffd
@ -2,6 +2,9 @@ find_package(glfw3 REQUIRED)
|
|||||||
find_package(OpenGL REQUIRED)
|
find_package(OpenGL REQUIRED)
|
||||||
find_package(glm REQUIRED)
|
find_package(glm REQUIRED)
|
||||||
|
|
||||||
|
find_package(PkgConfig REQUIRED)
|
||||||
|
pkg_check_modules(IMGUI REQUIRED imgui)
|
||||||
|
|
||||||
|
|
||||||
add_library(DeerithClient)
|
add_library(DeerithClient)
|
||||||
|
|
||||||
@ -18,6 +21,7 @@ target_sources(DeerithClient
|
|||||||
target_include_directories(DeerithClient
|
target_include_directories(DeerithClient
|
||||||
PUBLIC
|
PUBLIC
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/include
|
${CMAKE_CURRENT_SOURCE_DIR}/include
|
||||||
|
${IMGUI_INCLUDE_DIRS}
|
||||||
)
|
)
|
||||||
|
|
||||||
target_link_libraries(DeerithClient
|
target_link_libraries(DeerithClient
|
||||||
@ -26,7 +30,9 @@ target_link_libraries(DeerithClient
|
|||||||
OpenGL::GL
|
OpenGL::GL
|
||||||
glfw
|
glfw
|
||||||
glm::glm
|
glm::glm
|
||||||
|
${IMGUI_LIBRARIES}
|
||||||
)
|
)
|
||||||
|
|
||||||
add_custom_target(DeerithClientShaders ALL
|
add_custom_target(DeerithClientShaders ALL
|
||||||
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
COMMAND ${CMAKE_COMMAND} -E copy_directory
|
||||||
${CMAKE_CURRENT_SOURCE_DIR}/shaders
|
${CMAKE_CURRENT_SOURCE_DIR}/shaders
|
||||||
|
|||||||
7
deerith_engine_client/include/deerith/graphics/input.h
Normal file
7
deerith_engine_client/include/deerith/graphics/input.h
Normal file
@ -0,0 +1,7 @@
|
|||||||
|
#pragma once
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
|
namespace deerith
|
||||||
|
{
|
||||||
|
glm::vec2 get_joystick_axes(int controller_id, int axe_id);
|
||||||
|
}
|
||||||
@ -1,4 +1,5 @@
|
|||||||
#pragma once
|
#pragma once
|
||||||
|
#include <glm/glm.hpp>
|
||||||
|
|
||||||
namespace deerith
|
namespace deerith
|
||||||
{
|
{
|
||||||
@ -9,5 +10,8 @@ namespace deerith
|
|||||||
bool begin_window_display();
|
bool begin_window_display();
|
||||||
void finish_window_display();
|
void finish_window_display();
|
||||||
|
|
||||||
|
glm::ivec2 get_window_size();
|
||||||
|
float get_window_aspect_ratio();
|
||||||
|
|
||||||
int get_current_frame_id();
|
int get_current_frame_id();
|
||||||
}
|
}
|
||||||
31
deerith_engine_client/src/input.cpp
Normal file
31
deerith_engine_client/src/input.cpp
Normal file
@ -0,0 +1,31 @@
|
|||||||
|
#include <deerith/graphics/input.h>
|
||||||
|
#include <deerith/core/log.h>
|
||||||
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#include <math.h>
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
@ -4,6 +4,10 @@
|
|||||||
#include <glad/gl.h>
|
#include <glad/gl.h>
|
||||||
#include <GLFW/glfw3.h>
|
#include <GLFW/glfw3.h>
|
||||||
|
|
||||||
|
#include <imgui.h>
|
||||||
|
#include <backends/imgui_impl_glfw.h>
|
||||||
|
#include <backends/imgui_impl_opengl3.h>
|
||||||
|
|
||||||
#include <chrono>
|
#include <chrono>
|
||||||
#include <thread>
|
#include <thread>
|
||||||
|
|
||||||
@ -16,6 +20,9 @@ namespace deerith
|
|||||||
|
|
||||||
constexpr double TARGET_FRAME_TIME = 1.0 / 60.0;
|
constexpr double TARGET_FRAME_TIME = 1.0 / 60.0;
|
||||||
std::chrono::steady_clock::time_point last_frame_time;
|
std::chrono::steady_clock::time_point last_frame_time;
|
||||||
|
std::chrono::steady_clock::time_point frame_start;
|
||||||
|
|
||||||
|
void init_imgui();
|
||||||
}
|
}
|
||||||
|
|
||||||
int deerith::init_window()
|
int deerith::init_window()
|
||||||
@ -63,42 +70,63 @@ int deerith::init_window()
|
|||||||
glDisable(GL_CULL_FACE);
|
glDisable(GL_CULL_FACE);
|
||||||
|
|
||||||
init_primitives();
|
init_primitives();
|
||||||
|
init_imgui();
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool deerith::begin_window_display()
|
bool deerith::begin_window_display()
|
||||||
{
|
{
|
||||||
|
glfwPollEvents();
|
||||||
if (glfwWindowShouldClose(window))
|
if (glfwWindowShouldClose(window))
|
||||||
{
|
{
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
glClearColor(0.1f, 0.1f, 0.15f, 1.0f);
|
ImGui_ImplOpenGL3_NewFrame();
|
||||||
glClear(GL_COLOR_BUFFER_BIT);
|
ImGui_ImplGlfw_NewFrame();
|
||||||
|
ImGui::NewFrame();
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
void deerith::finish_window_display()
|
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;
|
using clock = std::chrono::steady_clock;
|
||||||
|
|
||||||
const auto now = clock::now();
|
const auto now = clock::now();
|
||||||
const std::chrono::duration<double> elapsed = now - last_frame_time;
|
const std::chrono::duration<double> elapsed = now - last_frame_time;
|
||||||
|
|
||||||
if (elapsed.count() < TARGET_FRAME_TIME)
|
using clock = std::chrono::steady_clock;
|
||||||
|
|
||||||
|
while (std::chrono::duration<double>(
|
||||||
|
clock::now() - frame_start)
|
||||||
|
.count() < TARGET_FRAME_TIME)
|
||||||
{
|
{
|
||||||
std::this_thread::sleep_for(
|
std::this_thread::sleep_for(std::chrono::milliseconds(5));
|
||||||
std::chrono::duration<double>(
|
|
||||||
TARGET_FRAME_TIME - elapsed.count()));
|
|
||||||
}
|
}
|
||||||
|
|
||||||
frame_id++;
|
frame_id++;
|
||||||
glfwSwapBuffers(window);
|
glfwSwapBuffers(window);
|
||||||
glfwPollEvents();
|
|
||||||
|
glClearColor(0.1f, 0.1f, 0.15f, 1.0f);
|
||||||
|
glClear(GL_COLOR_BUFFER_BIT);
|
||||||
}
|
}
|
||||||
|
|
||||||
void deerith::shutdown_window()
|
void deerith::shutdown_window()
|
||||||
{
|
{
|
||||||
|
|
||||||
|
ImGui_ImplOpenGL3_Shutdown();
|
||||||
|
ImGui_ImplGlfw_Shutdown();
|
||||||
|
ImGui::DestroyContext();
|
||||||
|
|
||||||
shutdown_primitives();
|
shutdown_primitives();
|
||||||
|
|
||||||
glfwDestroyWindow(window);
|
glfwDestroyWindow(window);
|
||||||
@ -109,3 +137,42 @@ int deerith::get_current_frame_id()
|
|||||||
{
|
{
|
||||||
return 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);
|
||||||
|
}
|
||||||
@ -1,11 +1,17 @@
|
|||||||
#include <deerith/graphics/window.h>
|
#include <deerith/graphics/window.h>
|
||||||
#include <deerith/graphics/primitive.h>
|
#include <deerith/graphics/primitive.h>
|
||||||
|
#include <deerith/graphics/input.h>
|
||||||
#include <deerith/core/core.h>
|
#include <deerith/core/core.h>
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
#include <math.h>
|
#include <math.h>
|
||||||
|
#include <imgui.h>
|
||||||
|
|
||||||
#include "deerith/core/log.h"
|
#include "deerith/core/log.h"
|
||||||
|
|
||||||
|
glm::vec2 player_pos(5, 5);
|
||||||
|
|
||||||
|
float vel = 1;
|
||||||
|
|
||||||
int main(int args_count, const char **args)
|
int main(int args_count, const char **args)
|
||||||
{
|
{
|
||||||
deerith::init_deerith_core();
|
deerith::init_deerith_core();
|
||||||
@ -20,13 +26,19 @@ int main(int args_count, const char **args)
|
|||||||
|
|
||||||
while (deerith::begin_window_display())
|
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));
|
player_pos += deerith::get_joystick_axes(0, 0) * (vel / 60.0f);
|
||||||
deerith::draw_primitive_rectangle(glm::vec2(0, 0), glm::vec2(0.5f, 1));
|
|
||||||
|
|
||||||
deerith::define_primitive_color(glm::vec4(0.2f, 0.4f, (deerith::get_current_frame_id() % 100) / 100.0f, 1));
|
deerith::define_primitive_color(glm::vec4(0.4f, 0.7f, 1.0f, 1));
|
||||||
deerith::draw_primitive_rectangle(glm::vec2(0.5, 0), glm::vec2(1, 0.5f));
|
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();
|
deerith::finish_window_display();
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user