move input handling from main into dedicated file

pull/10/head
EmaMaker 2023-10-03 18:16:45 +02:00
parent ca043bac68
commit 2a57796ed2
5 changed files with 65 additions and 44 deletions

14
include/controls.hpp Normal file
View File

@ -0,0 +1,14 @@
#ifndef CONTROLS_H
#define CONTROLS_H
#include <glad/glad.h>
#include <GLFW/glfw3.h>
#define BLOCKPICK_TIMEOUT 0.15f
namespace controls{
void init();
void update(GLFWwindow* window);
};
#endif

View File

@ -3,6 +3,5 @@
void framebuffer_size_callback(GLFWwindow *, int, int); void framebuffer_size_callback(GLFWwindow *, int, int);
void mouse_callback(GLFWwindow *window, double xpos, double ypos); void mouse_callback(GLFWwindow *window, double xpos, double ypos);
void processInput(GLFWwindow *);
#endif #endif

View File

@ -1,7 +1,7 @@
cmake_minimum_required(VERSION 3.2) cmake_minimum_required(VERSION 3.2)
project(OpenGLTest) project(OpenGLTest)
set(SOURCE_FILES main.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenerator.cpp set(SOURCE_FILES main.cpp controls.cpp chunk.cpp chunkmanager.cpp chunkmesher.cpp chunkgenerator.cpp
debugwindow.cpp renderer.cpp spacefilling.cpp stb_image.cpp utils.cpp OpenSimplexNoise.cpp) debugwindow.cpp renderer.cpp spacefilling.cpp stb_image.cpp utils.cpp OpenSimplexNoise.cpp)
add_executable(OpenGLTest ${SOURCE_FILES}) add_executable(OpenGLTest ${SOURCE_FILES})

39
src/controls.cpp Normal file
View File

@ -0,0 +1,39 @@
#include "controls.hpp"
#include "renderer.hpp"
namespace controls{
float lastBlockPick=0.0;
bool blockpick = false;
bool cursor = false;
void init(){
}
void update(GLFWwindow* window){
float current_time = glfwGetTime();
// Reset blockpicking timeout has passed
if(current_time - lastBlockPick > BLOCKPICK_TIMEOUT) blockpick = false;
// Reset blockpicking if both mouse buttons are released
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_RELEASE) blockpick = false;
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_PRESS && !blockpick){
//chunkmanager::blockpick(false);
blockpick=true;
lastBlockPick=glfwGetTime();
}
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS && !blockpick){
//chunkmanager::blockpick(true);
blockpick=true;
lastBlockPick=glfwGetTime();
}
if(glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS) renderer::saveScreenshot();
if(glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS) renderer::saveScreenshot(true);
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
cursor = !cursor;
glfwSetInputMode(window, GLFW_CURSOR, cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
}
}
};

View File

@ -4,26 +4,23 @@
#include <iostream> #include <iostream>
#include <thread> #include <thread>
#include "main.hpp"
#define GLOBALS_DEFINER #define GLOBALS_DEFINER
#include "globals.hpp" #include "globals.hpp"
#undef GLOBALS_DEFINER #undef GLOBALS_DEFINER
#include "chunkmanager.hpp" #include "chunkmanager.hpp"
#include "main.hpp" #include "controls.hpp"
#include "debugwindow.hpp" #include "debugwindow.hpp"
#include "renderer.hpp" #include "renderer.hpp"
#include "spacefilling.hpp"
#include "shader.hpp" #include "shader.hpp"
#include "spacefilling.hpp"
float deltaTime = 0.0f; // Time between current frame and last frame float deltaTime = 0.0f; // Time between current frame and last frame
float lastFrame = 0.0f; // Time of last frame float lastFrame = 0.0f; // Time of last frame
float lastFPSFrame = 0.0f; float lastFPSFrame = 0.0f;
int frames = 0; int frames = 0;
float lastBlockPick=0.0;
bool blockpick = false;
bool cursor = false;
int main() int main()
{ {
@ -70,6 +67,7 @@ int main()
} }
SpaceFilling::initLUT(); SpaceFilling::initLUT();
controls::init();
chunkmanager::init(); chunkmanager::init();
debug::window::init(window); debug::window::init(window);
renderer::init(window); renderer::init(window);
@ -94,8 +92,12 @@ int main()
glClearColor(0.431f, 0.694f, 1.0f, 1.0f); glClearColor(0.431f, 0.694f, 1.0f, 1.0f);
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
// Input processing // Input handling
processInput(window); // Only close event is handles by main
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
// the rest of input processing is handled by controls.cpp
controls::update(window);
// Camera // Camera
theCamera.update(window, deltaTime); theCamera.update(window, deltaTime);
@ -108,9 +110,6 @@ int main()
debug::window::set_parameter("lx", theCamera.getFront().x); debug::window::set_parameter("lx", theCamera.getFront().x);
debug::window::set_parameter("ly", theCamera.getFront().y); debug::window::set_parameter("ly", theCamera.getFront().y);
debug::window::set_parameter("lz", theCamera.getFront().z); debug::window::set_parameter("lz", theCamera.getFront().z);
// Reset blockping timeout if 200ms have passed
if(glfwGetTime() - lastBlockPick > 0.1) blockpick = false;
// Render pass // Render pass
renderer::render(); renderer::render();
@ -143,33 +142,3 @@ void mouse_callback(GLFWwindow *window, double xpos, double ypos)
{ {
theCamera.mouseCallback(window, xpos, ypos); theCamera.mouseCallback(window, xpos, ypos);
} }
void processInput(GLFWwindow *window)
{
if (glfwGetKey(window, GLFW_KEY_ESCAPE) == GLFW_PRESS)
glfwSetWindowShouldClose(window, true);
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_PRESS && !blockpick){
chunkmanager::blockpick(false);
blockpick=true;
lastBlockPick=glfwGetTime();
}
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_PRESS && !blockpick){
chunkmanager::blockpick(true);
blockpick=true;
lastBlockPick=glfwGetTime();
}
// Reset blockpicking if enough time has passed
if(glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) == GLFW_RELEASE && glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_2) == GLFW_RELEASE) blockpick = false;
if(glfwGetKey(window, GLFW_KEY_F2) == GLFW_PRESS) renderer::saveScreenshot();
if(glfwGetKey(window, GLFW_KEY_F3) == GLFW_PRESS) renderer::saveScreenshot(true);
if(glfwGetKey(window, GLFW_KEY_M) == GLFW_PRESS) {
cursor = !cursor;
glfwSetInputMode(window, GLFW_CURSOR, cursor ? GLFW_CURSOR_NORMAL : GLFW_CURSOR_DISABLED);
}
}