Skip to content

Instantly share code, notes, and snippets.

@alexanderankin
Created September 19, 2023 18:24
Show Gist options
  • Select an option

  • Save alexanderankin/91ca45c7abcd11e0ac3b82f28b9fac23 to your computer and use it in GitHub Desktop.

Select an option

Save alexanderankin/91ca45c7abcd11e0ac3b82f28b9fac23 to your computer and use it in GitHub Desktop.
start a window, listen for keyboard events and change color by clearing the color bit/buffer
plugins {
id 'java'
}
repositories.mavenCentral()
dependencies {
// for lwjgl demos
implementation 'org.joml:joml:1.10.5'
implementation "org.lwjgl:lwjgl:$lwjglVersion"
implementation "org.lwjgl:lwjgl:$lwjglVersion:natives-linux"
implementation "org.lwjgl:lwjgl-glfw:$lwjglVersion"
implementation "org.lwjgl:lwjgl-glfw:$lwjglVersion:natives-linux"
implementation "org.lwjgl:lwjgl-tinyfd:$lwjglVersion"
implementation "org.lwjgl:lwjgl-tinyfd:$lwjglVersion:natives-linux"
implementation "org.lwjgl:lwjgl-opengl:$lwjglVersion"
implementation "org.lwjgl:lwjgl-opengl:$lwjglVersion:natives-linux"
}
package com.youtube.ElegantWhelp.tutorial;
import lombok.RequiredArgsConstructor;
import org.lwjgl.glfw.GLFW;
import org.lwjgl.glfw.GLFWVidMode;
import org.lwjgl.opengl.GL;
import org.lwjgl.opengl.GL11C;
import javax.swing.*;
import java.awt.*;
import java.util.Objects;
@SuppressWarnings("CommentedOutCode")
public class OpenGlChangeColors {
public static final Color LIGHT_BLUE = new Color(152, 152, 255);
public static final Color LIGHT_PURPLE = new Color(190, 152, 255);
public static final Color LIGHT_VIOLET = new Color(152, 86, 225);
public static final Color LIGHT_MANGO = new Color(255, 174, 124);
public static final Color LIGHT_RED = new Color(255, 124, 124);
public static final Color LIGHT_GREEN = new Color(191, 255, 215);
public static void main(String[] args) {
GLFW.glfwInit();
GLFWVidMode glfwVidMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
Objects.requireNonNull(glfwVidMode);
String[] options = {"Windowed (small)", "Windowed (medium)", "Full Screen"};
var selection = JOptionPane.showOptionDialog(
null,
"Select one:",
"Resolution Selection",
JOptionPane.YES_NO_OPTION,
JOptionPane.QUESTION_MESSAGE,
null,
options,
options[0]);
long window = switch (selection) {
case 0 -> GLFW.glfwCreateWindow(
glfwVidMode.width() / 3,
glfwVidMode.height() / 3,
"ok",
0,
0);
case 1 -> GLFW.glfwCreateWindow(
glfwVidMode.width() / 2,
glfwVidMode.height() / 2,
"ok",
0,
0);
case 2 -> GLFW.glfwCreateWindow(
glfwVidMode.width(),
glfwVidMode.height(),
"ok",
GLFW.glfwGetPrimaryMonitor(),
0);
default -> throw new IllegalStateException("Unexpected value: " + selection);
};
if (selection != 2) GLFW.glfwSetWindowPos(window,
glfwVidMode.width() / 10,
glfwVidMode.height() / 5);
GLFW.glfwShowWindow(window);
GLFW.glfwMakeContextCurrent(window);
GL.createCapabilities();
/*
GL11C.glClearColor(210.9f, .1f, 0.1f, 1f);
GL11C.glClear(GL11C.GL_COLOR_BUFFER_BIT);
GLFW.glfwSwapBuffers(window);
*/
ColorManager colorManager = new ColorManager(window);
colorManager.setColor(Color.LIGHT_GRAY);
// noinspection resource
GLFW.glfwSetKeyCallback(window, (long window1, int key, int scancode, int action, int mods) -> {
if (colorManager.window == window1 && action == GLFW.GLFW_PRESS) {
switch (key) {
case GLFW.GLFW_KEY_R -> colorManager.setColor(LIGHT_RED);
case GLFW.GLFW_KEY_G ->
colorManager.setColor(((mods & GLFW.GLFW_MOD_CONTROL) == 0) ? Color.LIGHT_GRAY : LIGHT_GREEN);
case GLFW.GLFW_KEY_B -> colorManager.setColor(LIGHT_BLUE);
case GLFW.GLFW_KEY_W -> colorManager.setColor(Color.WHITE);
case GLFW.GLFW_KEY_P -> colorManager.setColor(LIGHT_PURPLE);
case GLFW.GLFW_KEY_M -> colorManager.setColor(LIGHT_MANGO);
case GLFW.GLFW_KEY_V -> colorManager.setColor(LIGHT_VIOLET);
case GLFW.GLFW_KEY_C -> colorManager.setColor(Color.CYAN);
case GLFW.GLFW_KEY_O -> colorManager.setColor(Color.ORANGE);
}
}
switch (key) {
case GLFW.GLFW_KEY_ESCAPE, GLFW.GLFW_KEY_Q -> GLFW.glfwSetWindowShouldClose(window1, true);
}
});
while (!GLFW.glfwWindowShouldClose(window)) {
Thread.onSpinWait();
GLFW.glfwPollEvents();
if (GLFW.glfwGetKey(window, GLFW.GLFW_KEY_ESCAPE) == GLFW.GLFW_PRESS) {
GLFW.glfwSetWindowShouldClose(window, true);
}
}
GLFW.glfwTerminate();
}
@RequiredArgsConstructor
static class ColorManager {
final long window;
void setColor(Color color) {
GL11C.glClearColor(
(float) color.getRed() / 255,
(float) color.getGreen() / 255,
(float) color.getBlue() / 255,
1.0f
);
GL11C.glClear(GL11C.GL_COLOR_BUFFER_BIT);
GLFW.glfwSwapBuffers(window);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment