Scrollwheel and "raw" keyboard input

This commit is contained in:
Kevin Trogant 2022-12-08 12:59:54 +01:00
parent 8ec2da45a2
commit d8be6fc06f
2 changed files with 56 additions and 21 deletions

View File

@ -194,18 +194,23 @@ void NativeEngine::gameLoop()
// den GameState, input_events und input_event_count und die Displaygröße bekommen sollte:
//
// kUpdate(&m_state, input_events, input_event_count,
// 0.0,
// nullptr, 0,
// nullptr, 0,
// m_display_width, m_display_height)
//
// (Für Kompatibilität zu Windows sollte die funktion auch tastatur input akzeptieren,
// (Für Kompatibilität zu Windows sollte die funktion auch Tastatur und Scroll input akzeptieren,
// den es unter Android aber momentan nicht gibt.)
//
// Die Funktion könnte folgende Definition haben:
// void kUpdate(GameState* state,
// const TouchInputEvent* touch_events,
// unsigned int touch_event_count,
// unsigned int* pressed_codepoints,
// double scroll_offset,
// const unsigned int* pressed_codepoints,
// unsigned int codepoint_count,
// const int* pressed_keys,
// unsigned int pressed_key_count,
// float display_width,
// float display_height)
// ENDE INTEGRATION

View File

@ -21,6 +21,12 @@ struct CodePointsArray
unsigned int cap;
};
struct UserData
{
CodePointsArray cp_array;
double scroll_offset;
};
static void errorCallback(int error, const char* desc)
{
MessageBoxA(NULL, desc, "Error", MB_ICONERROR | MB_OK);
@ -28,10 +34,17 @@ static void errorCallback(int error, const char* desc)
static void charCallback(GLFWwindow* window, unsigned int cp)
{
CodePointsArray* cp_array = reinterpret_cast<CodePointsArray*>(glfwGetWindowUserPointer(window));
assert(cp_array != nullptr);
if (cp_array->count < cp_array->cap)
cp_array->cps[cp_array->count++] = cp;
UserData* ud = reinterpret_cast<UserData*>(glfwGetWindowUserPointer(window));
assert(ud != nullptr);
if (ud->cp_array.count < ud->cp_array.cap)
ud->cp_array.cps[ud->cp_array.count++] = cp;
}
static void scrollCallback(GLFWwindow* window, double /* xoffset */, double yoffset)
{
UserData* ud = reinterpret_cast<UserData*>(glfwGetWindowUserPointer(window));
assert(ud != nullptr);
ud->scroll_offset = yoffset;
}
int main()
@ -43,10 +56,8 @@ int main()
}
unsigned int pressed_codepoints[100];
CodePointsArray cp_array;
cp_array.cps = pressed_codepoints;
cp_array.cap = 100;
cp_array.count = 0;
UserData ud = {};
ud.cp_array = {pressed_codepoints, 0, 100};
glfwWindowHint(GLFW_CLIENT_API, GLFW_OPENGL_ES_API);
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
@ -56,8 +67,9 @@ int main()
glfwTerminate();
return 1;
}
glfwSetWindowUserPointer(window, &cp_array);
glfwSetWindowUserPointer(window, &ud);
glfwSetCharCallback(window, charCallback);
glfwSetScrollCallback(window, scrollCallback);
glfwMakeContextCurrent(window);
int ret = gladLoadGLES2Loader((GLADloadproc)glfwGetProcAddress);
@ -82,14 +94,21 @@ int main()
glfwSwapInterval(1);
while (!glfwWindowShouldClose(window)) {
/* Reset the pressed codepoints array,
* because glfwPollEvents() will call our characterCallback
/* Reset the pressed codepoints array and scroll offset
* because glfwPollEvents() will call our callbacks
*/
cp_array.count = 0;
ud.cp_array.count = 0;
ud.scroll_offset = 0.0;
glfwPollEvents();
/* Gather keyboard input */
int pressed_keys[GLFW_KEY_LAST + 1];
unsigned int pressed_key_count = 0;
for (int key = GLFW_KEY_SPACE; key <= GLFW_KEY_LAST; ++key) {
if (glfwGetKey(window, key) == GLFW_PRESS) {
pressed_keys[pressed_key_count++] = key;
}
}
/* Gather mouse input */
TouchInputEvent input_events[1] = {};
@ -103,10 +122,6 @@ int main()
input_event_count = 1;
}
if (cp_array.count > 0) {
printf("CP: %u\n", cp_array.cps[0]);
}
int w, h;
glfwGetFramebufferSize(window, &w, &h);
@ -117,7 +132,9 @@ int main()
// den GameState, input_events und input_event_count und die Displaygröße bekommen sollte:
//
// kUpdate(&state, input_events, input_event_count,
// cp_array.cps, cp_array.count,
// ud.scroll_offset,
// ud.cp_array.cps, ud.cp_array.count,
// pressed_keys, pressed_key_count,
// display_width, display_height)
//
//
@ -125,8 +142,11 @@ int main()
// void kUpdate(GameState* state,
// const TouchInputEvent* touch_events,
// unsigned int touch_event_count,
// unsigned int* pressed_codepoints,
// double scroll_offset,
// const unsigned int* pressed_codepoints,
// unsigned int codepoint_count,
// const int* pressed_keys,
// unsigned int pressed_key_count,
// float display_width,
// float display_height)
// ENDE INTEGRATION
@ -145,6 +165,16 @@ int main()
smiley_pos.x -= 250;
smiley_pos.y -= 250;
}
if (ud.scroll_offset != 0) {
smiley_pos.y += ud.scroll_offset;
}
if (pressed_key_count > 0) {
for (unsigned int i = 0; i < pressed_key_count; ++i) {
if (pressed_keys[i] == GLFW_KEY_LEFT_CONTROL)
smiley_pos = {100, 100};
}
}
Renderer::ptr->addRect(100, 100, 500, 500, 0.3f, 0.3f, 0.3f, 1.f);
Renderer::ptr->addRect(smiley_pos.x, smiley_pos.y, 500, 500, 0.f, x * x, 1.f - x * x, 1.f, smiley);