prosperon/source/engine/input.c

119 lines
2.5 KiB
C
Raw Normal View History

2021-11-30 21:29:18 -06:00
#include "input.h"
2022-08-12 14:03:56 -05:00
#include "vec.h"
2021-11-30 21:29:18 -06:00
int32_t mouseWheelX = 0;
int32_t mouseWheelY = 0;
int ychange = 0;
int xchange = 0;
float deltaT = 0;
int quit = 0;
2022-02-06 10:14:57 -06:00
static double c_xpos;
static double c_ypos;
2022-08-12 14:03:56 -05:00
static struct vec downkeys;
2022-02-06 10:14:57 -06:00
static void cursor_pos_cb(GLFWwindow *w, double xpos, double ypos)
{
xchange = (int)xpos - c_xpos;
ychange = (int)ypos - c_ypos;
c_xpos = xpos;
c_ypos = ypos;
}
static void scroll_cb(GLFWwindow *w, double xoffset, double yoffset)
{
mouseWheelY = yoffset;
mouseWheelX = xoffset;
}
void input_init()
{
glfwSetCursorPosCallback(mainwin->window, cursor_pos_cb);
glfwSetScrollCallback(mainwin->window, scroll_cb);
2022-08-12 14:03:56 -05:00
}
void call_input_down(int *key) {
const char *keyname = glfwGetKeyName(*key, 0);
char keystr[50] = {'\0'};
snprintf(keystr, 50, "input_%s_down", keyname);
script_call(keystr);
2022-02-06 10:14:57 -06:00
}
2021-11-30 21:29:18 -06:00
2022-08-12 14:03:56 -05:00
/* This is called once every frame - or more if we want it more! */
void input_poll(double wait)
2021-11-30 21:29:18 -06:00
{
ychange = 0;
xchange = 0;
mouseWheelX = 0;
mouseWheelY = 0;
2022-08-12 14:03:56 -05:00
glfwWaitEventsTimeout(wait);
2022-02-06 10:14:57 -06:00
//editor_input(&e);
2022-08-12 14:03:56 -05:00
vec_walk(&downkeys, call_input_down);
}
2022-02-06 10:14:57 -06:00
2022-08-12 14:03:56 -05:00
int same_key(int *key1, int *key2) {
return *key1 == *key2;
}
void win_key_callback(GLFWwindow *w, int key, int scancode, int action, int mods)
{
if (downkeys.data == NULL) {
downkeys = vec_init(sizeof(key), 10);
}
char keystr[50] = {'\0'};
strcat(keystr, "input_");
strcat(keystr, glfwGetKeyName(key, 0));
switch (action) {
case GLFW_PRESS:
strcat(keystr, "_pressed");
int *foundkey = vec_find(&downkeys, same_key, &key);
if (foundkey == NULL) {
vec_add(&downkeys, &key);
}
break;
case GLFW_RELEASE:
strcat(keystr, "_released");
int found = vec_find_n(&downkeys, same_key, &key);
if (found != -1) {
vec_delete(&downkeys, found);
}
break;
case GLFW_REPEAT:
strcat(keystr, "_rep");
break;
}
script_call(keystr);
2022-02-06 10:14:57 -06:00
}
2021-11-30 21:29:18 -06:00
2022-02-06 10:14:57 -06:00
void cursor_hide()
{
glfwSetInputMode(mainwin->window, GLFW_CURSOR, GLFW_CURSOR_HIDDEN);
}
2021-11-30 21:29:18 -06:00
2022-02-06 10:14:57 -06:00
void cursor_show()
{
glfwSetInputMode(mainwin->window, GLFW_CURSOR, GLFW_CURSOR_NORMAL);
}
2021-11-30 21:29:18 -06:00
2022-02-06 10:14:57 -06:00
int action_down(int scancode)
{
2022-08-12 14:03:56 -05:00
int *foundkey = vec_find(&downkeys, same_key, &scancode);
return foundkey != NULL;
2021-11-30 21:29:18 -06:00
}
2022-08-12 14:03:56 -05:00
int action_up(int scancode)
{
int *foundkey = vec_find(&downkeys, same_key, &scancode);
return foundkey == NULL;
}