Added new inputHandler.c and changed main accordingly

This commit is contained in:
JonasJan2 2024-06-24 17:41:33 +02:00
parent cdc86eebad
commit 145947b784
3 changed files with 102 additions and 53 deletions

80
src/inputHandler.c Normal file
View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <stdbool.h>
#include <GLFW/glfw3.h>
#include "matrixMath.h"
#include "inputHandler.h"
// Definition der externen Variablen
extern bool exitRequested;
extern GLFWwindow *window;
extern vec3 cameraPosition;
extern vec3 objectPosition;
extern GLfloat radius;
/**
* Input handler for camera movement.
*/
void handleInputs(double deltaTime)
{
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS)
{
cameraPosition.z += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS)
{
cameraPosition.z -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS)
{
cameraPosition.x += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS)
{
cameraPosition.x -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS)
{
cameraPosition.y += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS)
{
cameraPosition.y -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS)
{
objectPosition.x += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS)
{
objectPosition.x -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS)
{
objectPosition.z += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS)
{
objectPosition.z -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS)
{
radius += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_U) == GLFW_PRESS)
{
radius -= deltaTime * 10;
}
}
// input handler to quit with ESC
void keyboardHandler(GLFWwindow *window, int key, int scancode, int action, int mods)
{
if (action == GLFW_PRESS)
{
if (key == GLFW_KEY_ESCAPE)
{
exitRequested = true;
}
}
}

16
src/inputHandler.h Normal file
View File

@ -0,0 +1,16 @@
#ifndef INPUT_HANDLER_H
#define INPUT_HANDLER_H
#include <GLFW/glfw3.h>
#include "matrixMath.h"
extern bool exitRequested;
extern GLFWwindow *window;
extern vec3 cameraPosition;
extern vec3 objectPosition;
extern GLfloat radius;
void handleInputs(double deltaTime);
void keyboardHandler(GLFWwindow *window, int key, int scancode, int action, int mods);
#endif

View File

@ -1,5 +1,8 @@
#include <stdio.h>
#include <stdlib.h>
#include <math.h>
#include <string.h>
#include <stdbool.h>
#include <GL/glew.h>
#include <GLFW/glfw3.h>
@ -9,6 +12,8 @@
#include "matrixMath.h"
#include "transformation.h"
#include "wavefrontobj.h"
#include "inputHandler.h"
#include "inputHandler.c"
#define STB_IMAGE_IMPLEMENTATION
#include "../lib/stb_image.h"
@ -67,58 +72,6 @@ char* models[] = {
"../obj/Zblock.obj",
};
/**
* Input handler for camera movement.
* */
void handleInputs(double deltaTime) {
if (glfwGetKey(window, GLFW_KEY_S) == GLFW_PRESS) {
cameraPosition.z += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_W) == GLFW_PRESS) {
cameraPosition.z -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_D) == GLFW_PRESS) {
cameraPosition.x += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_A) == GLFW_PRESS) {
cameraPosition.x -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_F) == GLFW_PRESS) {
cameraPosition.y += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_R) == GLFW_PRESS) {
cameraPosition.y -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_L) == GLFW_PRESS) {
objectPosition.x += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_J) == GLFW_PRESS) {
objectPosition.x -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_I) == GLFW_PRESS) {
objectPosition.z += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_K) == GLFW_PRESS) {
objectPosition.z -= deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_O) == GLFW_PRESS) {
radius += deltaTime * 10;
}
if (glfwGetKey(window, GLFW_KEY_U) == GLFW_PRESS) {
radius -= deltaTime * 10;
}
}
// input handler to quit with ESC
void keyboardHandler(GLFWwindow* window, int key, int scancode, int action, int mods) {
if (action == GLFW_PRESS) {
if (key == GLFW_KEY_ESCAPE) {
exitRequested = true;
}
}
}
/**
* Loads textures.
*/