diff --git a/GameEngine/src/main/java/org/example/ColorController.java b/GameEngine/src/main/java/org/example/ColorController.java new file mode 100644 index 0000000..d4d0a0c --- /dev/null +++ b/GameEngine/src/main/java/org/example/ColorController.java @@ -0,0 +1,63 @@ +package org.example; +import static org.lwjgl.glfw.GLFW.*; + + + +public class ColorController { + + private float red = 1.0f; + private float green = 0.0f; + private float blue = 0.0f; + + public void setRed(float red) { + this.red = red; + } + + public void setGreen(float green) { + this.green = green; + } + + public void setBlue(float blue) { + this.blue = blue; + } + + + public float getRed() { + return red; + } + + public float getGreen() { + return green; + } + + public float getBlue() { + return blue; + } + + + + public void handleInput(int key) { + switch (key) { + case GLFW_KEY_R: // Rキーで赤に変更 + setRed(1.0f); + setGreen(0.0f); + setBlue(0.0f); + break; + case GLFW_KEY_G: // Gキーで緑に変更 + setRed(0.0f); + setGreen(1.0f); + setBlue(0.0f); + break; + case GLFW_KEY_B: // Bキーで青に変更 + setRed(0.0f); + setGreen(0.0f); + setBlue(1.0f); + break; + case GLFW_KEY_Y: // Yキーで黄色に変更 + setRed(1.0f); + setGreen(1.0f); + setBlue(0.0f); + break; + } + } +} \ No newline at end of file diff --git a/GameEngine/src/main/java/org/example/Main.java b/GameEngine/src/main/java/org/example/Main.java index 363caf4..cb192a9 100644 --- a/GameEngine/src/main/java/org/example/Main.java +++ b/GameEngine/src/main/java/org/example/Main.java @@ -14,8 +14,9 @@ public class Main { - // The window handle + // ウィンドウハンドル private long window; + private final ColorController colorController = new ColorController(); public void run() { System.out.println("Hello LWJGL " + Version.getVersion() + "!"); @@ -23,90 +24,84 @@ init(); loop(); - // Free the window callbacks and destroy the window + // ウィンドウのコールバックを解放し、ウィンドウを破棄 glfwFreeCallbacks(window); glfwDestroyWindow(window); - // Terminate GLFW and free the error callback + // GLFWを終了し、エラーコールバックを解放 glfwTerminate(); glfwSetErrorCallback(null).free(); } private void init() { - // Setup an error callback. The default implementation - // will print the error message in System.err. + // エラーコールバックをセットアップします。デフォルトの実装はエラーメッセージをSystem.errに出力します。 GLFWErrorCallback.createPrint(System.err).set(); - // Initialize GLFW. Most GLFW functions will not work before doing this. + // GLFWを初期化します。これを行う前にほとんどのGLFW関数は動作しません。 if ( !glfwInit() ) - throw new IllegalStateException("Unable to initialize GLFW"); + throw new IllegalStateException("GLFWの初期化に失敗しました"); - // Configure GLFW - glfwDefaultWindowHints(); // optional, the current window hints are already the default - glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation - glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable + // GLFWの設定 + glfwDefaultWindowHints(); // オプション、現在のウィンドウヒントはすでにデフォルト + glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // ウィンドウ非表示 + glfwWindowHint(GLFW_RESIZABLE, GLFW_FALSE); // ウィンドウサイズ変更を行わない - // Create the window - window = glfwCreateWindow(300, 300, "Hello World!", NULL, NULL); + // ウィンドウの作成 + window = glfwCreateWindow(800, 600, "Hello World!", NULL, NULL); if ( window == NULL ) - throw new RuntimeException("Failed to create the GLFW window"); + throw new RuntimeException("GLFWウィンドウの作成に失敗しました"); - // Setup a key callback. It will be called every time a key is pressed, repeated or released. + // キーのコールバックを設定します。キーが押された、繰り返された、または放されたときに呼び出されます。 glfwSetKeyCallback(window, (window, key, scancode, action, mods) -> { if ( key == GLFW_KEY_ESCAPE && action == GLFW_RELEASE ) - glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop + glfwSetWindowShouldClose(window, true); // レンダリングループでこれを検出します + if (action == GLFW_PRESS || action == GLFW_REPEAT) { + colorController.handleInput(key); // キー入力に応じて色を変更 + } }); - // Get the thread stack and push a new frame + // スレッドスタックを取得し、新しいフレームをプッシュします try ( MemoryStack stack = stackPush() ) { IntBuffer pWidth = stack.mallocInt(1); // int* IntBuffer pHeight = stack.mallocInt(1); // int* - // Get the window size passed to glfwCreateWindow + // glfwCreateWindowに渡されたウィンドウサイズを取得 glfwGetWindowSize(window, pWidth, pHeight); - // Get the resolution of the primary monitor + // プライマリモニターの解像度を取得 GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); - // Center the window + // ウィンドウを中央に配置 glfwSetWindowPos( window, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2 ); - } // the stack frame is popped automatically + } // スタックフレームは自動的にポップされます - // Make the OpenGL context current + // OpenGLコンテキストを現在のスレッドに設定 glfwMakeContextCurrent(window); - // Enable v-sync + // 垂直同期を有効化 glfwSwapInterval(1); - // Make the window visible + // ウィンドウを表示 glfwShowWindow(window); } private void loop() { - // This line is critical for LWJGL's interoperation with GLFW's - // OpenGL context, or any context that is managed externally. - // LWJGL detects the context that is current in the current thread, - // creates the GLCapabilities instance and makes the OpenGL - // bindings available for use. GL.createCapabilities(); - // Set the clear color - glClearColor(1.0f, 0.0f, 0.0f, 0.0f); + // レンダリングループ + while (!glfwWindowShouldClose(window)) { + // 現在の色でクリア + glClearColor(colorController.getRed(), colorController.getGreen(), colorController.getBlue(), 0.0f); + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // フレームバッファをクリア - // Run the rendering loop until the user has attempted to close - // the window or has pressed the ESCAPE key. - while ( !glfwWindowShouldClose(window) ) { - glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer + glfwSwapBuffers(window); // カラーバッファを交換 - glfwSwapBuffers(window); // swap the color buffers - - // Poll for window events. The key callback above will only be - // invoked during this call. - glfwPollEvents(); + glfwPollEvents(); // ウィンドウイベントをポーリング } + } public static void main(String[] args) {