スポンサーリンク

glfwを試す(2)イベント

ドキュメントトップ

イベントの扱いについて。

キーの定数一覧:

ソースコード

#include <cstdio>

#include <Windows.h>
#include <gl/GL.h>
#include <GLFW/glfw3.h>

#pragma comment(lib,"opengl32.lib")
#pragma comment(lib,"glfw3.lib")

// 参考文献 ////////////////////////////////////////
// https://www.glfw.org/docs/latest/input_guide.html
 
//キー入力を処理するコールバック関数 void key_callback(GLFWwindow* pwin, int key, int scancode, int action, int mods) { // https://www.glfw.org/docs/latest/group__keys.html#ga99aacc875b6b27a072552631e13775c7 // action ... GLFW_PRESS , GLFW_REPEAT , GLFW_RELEASE //特殊キー if (key == GLFW_KEY_UP && action == GLFW_PRESS) { printf("key up\n"); } if (key == GLFW_KEY_DOWN && action == GLFW_PRESS) { printf("key down\n"); } if (key == GLFW_KEY_LEFT && action == GLFW_PRESS) { printf("key left\n"); } if (key == GLFW_KEY_RIGHT && action == GLFW_PRESS) { printf("key right\n"); } if (key >= GLFW_KEY_A && key <= GLFW_KEY_Z) { if (action == GLFW_PRESS) { const char* key_name = glfwGetKeyName(key, 0); printf("key - %s\n", key_name); } } }
// マウスクリックを処理するコールバック関数 void mouse_button_callback(GLFWwindow* pwin, int button, int action, int mods) { if (button == GLFW_MOUSE_BUTTON_LEFT && action == GLFW_PRESS) { printf("L - down\n"); } if (button == GLFW_MOUSE_BUTTON_RIGHT && action == GLFW_PRESS) { printf("R - down\n"); } if (button == GLFW_MOUSE_BUTTON_MIDDLE && action == GLFW_PRESS) { printf("M - down\n"); } }
// マウスホイールを処理するコールバック関数 void mouse_wheel_callback(GLFWwindow* window, double xoffset, double yoffset) { if (yoffset < 0) { printf("wheel down \n"); } if (yoffset > 0) { printf("wheel up \n"); } }
int main() { if (glfwInit() == GL_FALSE) { return 1; } GLFWwindow* window = glfwCreateWindow( 400, 400, "window title", NULL, NULL ); if (window == nullptr) { glfwTerminate(); return 1; } //////////////////////////////////////////// // コールバック関数の設定 glfwSetKeyCallback(window, key_callback); glfwSetMouseButtonCallback(window, mouse_button_callback); glfwSetScrollCallback(window, mouse_wheel_callback); // //////////////////////////////////////////// glfwMakeContextCurrent(window); while (glfwWindowShouldClose(window) == GL_FALSE) { int width, height; glfwGetFramebufferSize(window, &width, &height); ///////////////////// // 描画 glViewport(0, 0, width, height); glOrtho(-1, 1, -1, 1, -1, 1); glClearColor(0.2, 0.2, 0.2, 1); glClear(GL_COLOR_BUFFER_BIT); // ///////////////////// glfwSwapBuffers(window); // イベント取得 glfwWaitEvents(); } glfwTerminate(); }

glfwを試す(1)

glfwを試す(2)イベント

glfwを試す(3)複数ウィンドウ

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

日本語が含まれない投稿は無視されますのでご注意ください。(スパム対策)


この記事のトラックバックURL: