キー入力についてなのですが場当たりなことはせず将来性を考えた設計をする際にどうやってキー入力を実装したらいいコードになるのか知りたいです。現状はWindowクラスにウインドウコンテキスト当を管理しているのですが"ここに実装するべき"コメント部に実装するべきなのでしょうか?またゲーム内容とフレームワーク部でオブジェクト指向を分けたいのでゲームプログラム内でwindowコンテキスト変数を直接触りたくなりません。現在は描画物は一つですがGame クラスを作ってそこにゲーム内容を記述してくといった規模の大きなプロジェクトになる場合どういった手法でプロ人は実装するのでしょうか?
cpp
1//#define GLEW_STATIC //スタティックリンク 2#include <iostream> 3#include <fstream> 4#include <cstdlib> 5#include <vector> 6#include <glew/include/GL/glew.h> 7#include <glfw/include/GLFW/glfw3.h> 8 9 10 11#include "Window.hpp" 12#include "Shape.hpp" 13#include "Shader.hpp" 14 15Object::Vertex rectangleVertex[4] = 16{ 17 {-0.5f,-0.5f}, 18 {0.5f,-0.5f}, 19 {0.5f,0.5f}, 20 {-0.5f,0.5f} 21}; 22 23 24int main() 25{ 26 if (glfwInit() == GL_FALSE) 27 { 28 std::cerr << "glfw初期化失敗。" << std::endl; 29 return -1; 30 } 31 32 33 atexit(glfwTerminate); //プログラム終了時の処理を登録 34 Window window; 35 36 //OpenGL Verison 3.2 Core Profile を選択する 37 glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR,3); 38 glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR,2); 39 glfwWindowHint(GLFW_OPENGL_FORWARD_COMPAT,GL_TRUE); 40 glfwWindowHint(GLFW_OPENGL_PROFILE,GLFW_OPENGL_CORE_PROFILE); 41 42 Shape* shape = new Shape(2, 4, rectangleVertex); 43 44 Shader shader("Test.vert","Test.frag"); 45 shader.setBindAttribVertex(0, "Position"); 46 shader.setBindAttribFragment(0, "fragment"); 47 48 49 glClearColor(1.0, 0.0, 0.0, 1.0); //背景色 50 while (window) 51 { 52 glClear(GL_COLOR_BUFFER_BIT); //カラーバッファをクリア 53 shader.Active(); 54 55 shader.setUniform2fv("location",window.getLocation()); 56 shader.setUniform1f("scale",window.getScale()); 57 shader.setUniform2fv("size",window.getSize()); 58 59 60 61 shape->Draw(); //表示物 62 63 64 65 66 67 window.SwapBuffers(); //ダブルバッファリング 68 } 69 70} 71
cpp
1#include "Window.hpp" 2 3Window::Window(int width, int height, const char* title) 4 :window(glfwCreateWindow(width,height,title,NULL,NULL)), 5 scale(100.0f), 6 location{ 0.0f,0.0f } 7 8{ 9 if (window == NULL) 10 { 11 std::cerr << "ウインドウ生成失敗" << std::endl; 12 exit(1); 13 } 14 15 glfwMakeContextCurrent(window); //コンテキストを作成 16 17 glewExperimental = GL_TRUE; 18 if (glewInit() != GLEW_OK) 19 { 20 std::cerr << "GLFW 初期化失敗" << std::endl; 21 exit(1); 22 } 23 24 atexit(glfwTerminate); //プログラム終了時の処理を登録 25 glfwSwapInterval(1); //垂直同期 26 27 glfwSetWindowUserPointer(window, this); //このインスタンスのthis 28 29 glfwSetWindowSizeCallback(window, Resize); //ウインドウサイズを変更する時に呼び出す処理 30 glfwSetScrollCallback(window,Wheel); //マウスホイール操作時に呼び出す処理 31 32 Resize(window, width, height); 33 34} 35 36//サイズ変更 37void Window::Resize(GLFWwindow* const window, int width, int height) 38{ 39 int fbWidth, fbHeight; 40 glfwGetFramebufferSize(window, &fbWidth, &fbHeight); 41 glViewport(0, 0, fbWidth, fbHeight); 42 43 Window* const instance = (Window*)glfwGetWindowUserPointer(window); 44 45 if (instance != NULL) 46 { 47 instance->size[0] = (GLfloat)width; 48 instance->size[1] = (GLfloat)height; 49 } 50} 51 52const glm::vec2 Window::getSize() const 53{ 54 return size; 55} 56 57GLfloat Window::getScale() const 58{ 59 return scale; 60} 61 62const glm::vec2 Window::getLocation()const 63{ 64 return location; 65} 66 67 68Window::~Window() 69{ 70 glfwDestroyWindow(window); 71} 72 73void Window::Wheel(GLFWwindow* win, double x, double y) 74{ 75 Window* const instance = (Window*)glfwGetWindowUserPointer(win); 76 77 if (instance != NULL) 78 { 79 instance->scale += (GLfloat)y; 80 } 81} 82 83Window::operator bool() 84{ 85 glfwPollEvents(); //イベントを取り出す 86////////////////////////////////////////////////////////////////// 87//ここに書くべきか? 88////////////////////////////////////////////////////////////////// 89 90 //マウスをクリックしたとき 91 if (glfwGetMouseButton(window, GLFW_MOUSE_BUTTON_1) != GLFW_RELEASE) 92 { 93 //マウスカーソルの座標を取得 94 double x, y; 95 glfwGetCursorPos(window, &x, &y); 96 97 //マウスカーソルの正規化デバイス座標系上での座標を求める 98 location.x = (GLfloat)x * 2.0f / size.x - 1.0f; 99 location.y = 1.0f - (GLfloat)y * 2.0f / size.y; 100 } 101 102 //ESCキーを押したら強制終了 103 if (glfwGetKey(window, GLFW_KEY_ESCAPE)) 104 { 105 return false; 106 } 107 108 //ウインドウを閉じる必要があれば false 109 if (glfwWindowShouldClose(window) != 0) 110 { 111 return false; 112 } 113 else { 114 return true; 115 } 116} 117 118//ダブルバッファリング 119void Window::SwapBuffers()const 120{ 121 glfwSwapBuffers(window); 122} 123
cpp
1#ifndef ___WINDOW_HPP 2#define ___WINDOW_HPP 3 4#include <iostream> 5#include <glew/include/GL/glew.h> 6#include <glfw/include/GLFW/glfw3.h> 7#include "glm/glm.hpp" 8 9 10class Window 11{ 12public: 13 Window(int width = 640, int height = 480, const char* title = "Hello!"); 14 virtual ~Window(); 15 16 explicit operator bool(); 17 void SwapBuffers()const; //ダブルバッファリング 18 static void Resize(GLFWwindow* const window, int width, int height); //サイズ変更 19 const glm::vec2 getSize() const; 20 GLfloat getScale() const; 21 const glm::vec2 getLocation()const; 22 static void Wheel(GLFWwindow* win, double x, double y); 23 24 GLfloat scale; //ワールド座標系に対するデバイス座標系の拡大率 25 26private: 27 28 GLFWwindow *const window; //ウインドウコンテキスト 29 30 glm::vec2 size; //ウインドサイズ 31 glm::vec2 location; //図形の正規化デバイス座標上での位置 32 33 34}; 35 36#endif 37 38


回答1件
あなたの回答
tips
プレビュー