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: // den GameState, input_events und input_event_count und die Displaygröße bekommen sollte:
// //
// kUpdate(&m_state, input_events, input_event_count, // kUpdate(&m_state, input_events, input_event_count,
// 0.0,
// nullptr, 0,
// nullptr, 0, // nullptr, 0,
// m_display_width, m_display_height) // 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.) // den es unter Android aber momentan nicht gibt.)
// //
// Die Funktion könnte folgende Definition haben: // Die Funktion könnte folgende Definition haben:
// void kUpdate(GameState* state, // void kUpdate(GameState* state,
// const TouchInputEvent* touch_events, // const TouchInputEvent* touch_events,
// unsigned int touch_event_count, // unsigned int touch_event_count,
// unsigned int* pressed_codepoints, // double scroll_offset,
// const unsigned int* pressed_codepoints,
// unsigned int codepoint_count, // unsigned int codepoint_count,
// const int* pressed_keys,
// unsigned int pressed_key_count,
// float display_width, // float display_width,
// float display_height) // float display_height)
// ENDE INTEGRATION // ENDE INTEGRATION

View File

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