add: own branch

This commit is contained in:
ramollia
2025-09-16 11:23:48 +02:00
parent 8a5dafebc8
commit 9538b42cea
28 changed files with 882 additions and 394 deletions

109
.gitignore vendored
View File

@@ -1,38 +1,93 @@
target/
!.mvn/wrapper/maven-wrapper.jar
!**/src/main/**/target/
!**/src/test/**/target/
##############################
## Java
##############################
.mtj.tmp/
*.class
*.jar
*.war
*.ear
*.nar
hs_err_pid*
replay_pid*
### IntelliJ IDEA ###
.idea/modules.xml
.idea/jarRepositories.xml
.idea/compiler.xml
.idea/libraries/
*.iws
##############################
## Maven
##############################
target/
pom.xml.tag
pom.xml.releaseBackup
pom.xml.versionsBackup
pom.xml.next
pom.xml.bak
release.properties
dependency-reduced-pom.xml
buildNumber.properties
.mvn/timing.properties
.mvn/wrapper/maven-wrapper.jar
##############################
## Gradle
##############################
bin/
build/
.gradle
.gradletasknamecache
gradle-app.setting
!gradle-wrapper.jar
##############################
## IntelliJ
##############################
out/
.idea/
.idea_modules/
*.iml
*.ipr
*.iws
### Eclipse ###
.apt_generated
##############################
## Eclipse
##############################
.settings/
bin/
tmp/
.metadata
.classpath
.factorypath
.project
.settings
.springBeans
.sts4-cache
*.tmp
*.bak
*.swp
*~.nib
local.properties
.loadpath
.factorypath
### NetBeans ###
/nbproject/private/
/nbbuild/
/dist/
/nbdist/
/.nb-gradle/
##############################
## NetBeans
##############################
nbproject/private/
build/
!**/src/main/**/build/
!**/src/test/**/build/
nbbuild/
dist/
nbdist/
nbactions.xml
nb-configuration.xml
### VS Code ###
##############################
## Visual Studio Code
##############################
.vscode/
.code-workspace
### Mac OS ###
.DS_Store
##############################
## OS X
##############################
.DS_Store
##############################
## Miscellaneous
##############################
*.log
# Ignore Gradle build output directory
build

238
pom.xml
View File

@@ -1,148 +1,102 @@
<?xml version="1.0" encoding="UTF-8"?>
<project xmlns="http://maven.apache.org/POM/4.0.0"
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xsi:schemaLocation="http://maven.apache.org/POM/4.0.0 http://maven.apache.org/xsd/maven-4.0.0.xsd">
<modelVersion>4.0.0</modelVersion>
<groupId>org.toop</groupId>
<artifactId>pis</artifactId>
<version>1.0-SNAPSHOT</version>
<groupId>org.toop</groupId>
<artifactId>pism</artifactId>
<version>1.0-SNAPSHOT</version>
<properties>
<maven.compiler.source>24</maven.compiler.source>
<maven.compiler.target>24</maven.compiler.target>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
<lwjgl.version>3.3.6</lwjgl.version>
<lwjgl.natives>natives-windows</lwjgl.natives>
</properties>
<profiles>
<profile>
<id>lwjgl-natives-linux-amd64</id>
<activation>
<os>
<family>unix</family>
<name>linux</name>
<arch>amd64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-linux</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-macos-aarch64</id>
<activation>
<os>
<family>mac</family>
<arch>aarch64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-macos-arm64</lwjgl.natives>
</properties>
</profile>
<profile>
<id>lwjgl-natives-windows-amd64</id>
<activation>
<os>
<family>windows</family>
<arch>amd64</arch>
</os>
</activation>
<properties>
<lwjgl.natives>natives-windows</lwjgl.natives>
</properties>
</profile>
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-bom</artifactId>
<version>${lwjgl.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<properties>
<maven.compiler.source>24</maven.compiler.source>
<maven.compiler.target>24</maven.compiler.target>
<dependencies>
<!-- https://mvnrepository.com/artifact/com.google.guava/guava -->
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.4.8-jre</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-assimp</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-openal</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-stb</artifactId>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-assimp</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-glfw</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-openal</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-opengl</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-stb</artifactId>
<classifier>${lwjgl.natives}</classifier>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
</dependencies>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>
</project>
<main-class>org.toop.Main</main-class>
<lwjgl.version>3.3.6</lwjgl.version>
</properties>
<profiles>
<profile><id>lwjgl-natives-windows-amd64</id><activation><os><family>windows</family><arch>amd64</arch></os></activation><properties><lwjgl.natives>natives-windows</lwjgl.natives></properties></profile>
<profile><id>lwjgl-natives-linux-amd64</id><activation><os><family>unix</family><name>linux</name><arch>amd64</arch></os></activation><properties><lwjgl.natives>natives-linux</lwjgl.natives></properties></profile>
<profile><id>lwjgl-natives-macos-aarch64</id><activation><os><family>mac</family><arch>aarch64</arch></os></activation><properties><lwjgl.natives>natives-macos-arm64</lwjgl.natives></properties></profile>
</profiles>
<dependencyManagement>
<dependencies>
<dependency>
<groupId>org.lwjgl</groupId>
<artifactId>lwjgl-bom</artifactId>
<version>${lwjgl.version}</version>
<scope>import</scope>
<type>pom</type>
</dependency>
</dependencies>
</dependencyManagement>
<dependencies>
<dependency>
<groupId>com.google.guava</groupId>
<artifactId>guava</artifactId>
<version>33.4.8-jre</version>
</dependency>
<dependency>
<groupId>org.junit.jupiter</groupId>
<artifactId>junit-jupiter</artifactId>
<version>RELEASE</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-api</artifactId>
<version>2.25.1</version>
</dependency>
<dependency>
<groupId>org.apache.logging.log4j</groupId>
<artifactId>log4j-core</artifactId>
<version>2.25.1</version>
</dependency>
<dependency><groupId>org.lwjgl</groupId><artifactId>lwjgl</artifactId></dependency>
<dependency><groupId>org.lwjgl</groupId><artifactId>lwjgl-glfw</artifactId></dependency>
<dependency><groupId>org.lwjgl</groupId><artifactId>lwjgl-opengl</artifactId></dependency>
<dependency><groupId>org.lwjgl</groupId><artifactId>lwjgl-stb</artifactId></dependency>
<dependency><groupId>org.lwjgl</groupId><artifactId>lwjgl</artifactId><classifier>${lwjgl.natives}</classifier></dependency>
<dependency><groupId>org.lwjgl</groupId><artifactId>lwjgl-glfw</artifactId><classifier>${lwjgl.natives}</classifier></dependency>
<dependency><groupId>org.lwjgl</groupId><artifactId>lwjgl-opengl</artifactId><classifier>${lwjgl.natives}</classifier></dependency>
<dependency><groupId>org.lwjgl</groupId><artifactId>lwjgl-stb</artifactId><classifier>${lwjgl.natives}</classifier></dependency>
</dependencies>
<build>
<plugins>
<plugin>
<groupId>org.apache.maven.plugins</groupId>
<artifactId>maven-shade-plugin</artifactId>
<version>3.6.1</version>
<executions>
<execution>
<phase>package</phase>
<goals>
<goal>shade</goal>
</goals>
<configuration>
<transformers>
<transformer implementation="org.apache.maven.plugins.shade.resource.ManifestResourceTransformer">
<mainClass>${main-class}</mainClass>
</transformer>
</transformers>
</configuration>
</execution>
</executions>
</plugin>
</plugins>
</build>
</project>

View File

@@ -1,5 +0,0 @@
package org.toop;
public class Client {
}

View File

@@ -1,54 +1,55 @@
package org.toop;
import org.toop.eventbus.Events;
import org.toop.eventbus.GlobalEventBus;
import org.toop.server.Server;
import org.toop.server.backend.Testsss;
import org.toop.server.backend.TcpServer;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
import org.toop.core.*;
import org.toop.eventbus.*;
import org.toop.graphics.*;
public class Main {
private static final Logger logger = LogManager.getLogger(Main.class);
private static boolean running = false;
public static void main(String[] args) {
public static void main(String[] args) {
registerEvents();
if (!initEvents()) {
throw new RuntimeException("A event could not be initialized");
}
Window window = Window.setup(Window.API.GLFW, "Test", new Window.Size(1280, 720));
Renderer renderer = Renderer.setup(Renderer.API.OPENGL);
TcpServer server = new TcpServer(5001);
Thread serverThread = new Thread(server);
serverThread.start();
Server.start("127.0.0.1", "5001");
// Testsss.start(""); // Used for testing server.
Window.start("");
Shader shader = Shader.create(
"src/main/resources/shaders/gui_vertex.glsl",
"src/main/resources/shaders/gui_fragment.glsl");
}
running = window != null && renderer != null && shader != null;
/**
* Returns false if any event could not be initialized.
*/
private static boolean initEvents() {
try {
while (running) {
window.update();
renderer.clear();
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerIp.class,
event ->
logger.info("Changing server ip to {}", event.ip())
);
shader.start();
renderer.render();
}
GlobalEventBus.subscribeAndRegister(Events.ServerEvents.OnChangingServerPort.class,
event ->
logger.info("Changing server port to {}", event.port())
);
if (shader != null) shader.cleanup();
if (renderer != null) renderer.cleanup();
if (window != null) window.cleanup();
}
return true;
}
catch (Exception err) {
logger.info("{}", err.getMessage());
return false;
}
}
private static void registerEvents() {
GlobalEventBus.subscribeAndRegister(Events.WindowEvents.OnQuitRequested.class, event -> {
quit();
});
}
GlobalEventBus.subscribeAndRegister(Events.WindowEvents.OnMouseMove.class, event -> {
});
}
private static void quit() {
running = false;
}
public static boolean isRunning() {
return running;
}
public static void setRunning(boolean running) {
Main.running = running;
}
}

View File

@@ -1,164 +0,0 @@
package org.toop;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import java.nio.*;
import static org.lwjgl.glfw.Callbacks.*;
import static org.lwjgl.glfw.GLFW.*;
import static org.lwjgl.opengl.GL11.*;
import static org.lwjgl.system.MemoryStack.*;
import static org.lwjgl.system.MemoryUtil.*;
import static org.lwjgl.stb.STBImage.*;
public class Window extends Thread {
private static final Logger logger = LogManager.getLogger(Main.class);
// The window handle
private long window;
public void run() {
init();
loop();
// Free the window callbacks and destroy the window
glfwFreeCallbacks(window);
glfwDestroyWindow(window);
// Terminate GLFW and free the error callback
glfwTerminate();
glfwSetErrorCallback(null).free();
}
private void init() {
// Set up an error callback. The default implementation
// will print the error message in System.err.
GLFWErrorCallback.createPrint(System.err).set();
// Initialize GLFW. Most GLFW functions will not work before doing this.
if ( !glfwInit() ){
throw new IllegalStateException("Unable to initialize 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
// Create the window
window = glfwCreateWindow(1920, 1080, "ISY Game Selector", NULL, NULL);
if ( window == NULL )
throw new RuntimeException("Failed to create the GLFW window");
// Set up 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
}
});
// 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
glfwGetWindowSize(window, pWidth, pHeight);
// Get the resolution of the primary monitor
GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor());
// Center the window
assert vidmode != null;
glfwSetWindowPos(
window,
(vidmode.width() - pWidth.get(0)) / 2,
(vidmode.height() - pHeight.get(0)) / 2
);
// Prepare buffers to receive image data
IntBuffer width = stack.mallocInt(1);
IntBuffer height = stack.mallocInt(1);
IntBuffer channels = stack.mallocInt(1);
// Load the image
String imagePath = "img/icon.png";
ByteBuffer image = stbi_load(imagePath, width, height, channels, 4); // Force RGBA (4 channels)
if (image == null) {
throw new RuntimeException("Failed to load image: " + stbi_failure_reason());
}
// Create GLFWImage
GLFWImage icon = GLFWImage.malloc(stack);
icon.set(width.get(0), height.get(0), image);
// Create a buffer with the icon(s) — can be multiple icons for different sizes
GLFWImage.Buffer icons = GLFWImage.malloc(1, stack);
icons.put(0, icon);
// Set the window icon
glfwSetWindowIcon(window, icons);
// Free the image data
stbi_image_free(image);
} // the stack frame is popped automatically
// Make the OpenGL context current
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(0.5f, 0.5f, 0.5f, 0.0f);
// 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); // swap the color buffers
// Poll for window events. The key callback above will only be
// invoked during this call.
glfwPollEvents();
}
}
//public static void main(String[] args) {
//new Window().run();
//}
/**
* TODO: Is putting the window on a second thread, safe?
* Can't overwrite start(), so a overload is needed.
*
* @param ignoredKeepEmpty Just input "" an empty string.
*/
public static void start(String ignoredKeepEmpty){
new Window().start();
}
}

View File

@@ -0,0 +1,34 @@
package org.toop.core;
import java.io.*;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class FileSystem {
public record File(String path, CharSequence buffer) {};
private static final Logger logger = LogManager.getLogger(FileSystem.class);
public static File read(String path) {
File file;
try (BufferedReader reader = new BufferedReader(new FileReader(path))) {
StringBuilder buffer = new StringBuilder();
String line = reader.readLine();
while (line != null) {
buffer.append(line);
buffer.append(System.lineSeparator());
line = reader.readLine();
}
file = new File(path, buffer);
} catch (IOException e) {
logger.error("{}", e.getMessage());
return null;
}
return file;
}
}

View File

@@ -0,0 +1,5 @@
package org.toop.core;
public interface ICallable<T> {
public T call();
}

View File

@@ -0,0 +1,51 @@
package org.toop.core;
import org.toop.platform.core.glfw.*;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public abstract class Window {
public enum API {
NONE,
GLFW,
}
public record Size(int width, int height) {}
protected static final Logger logger = LogManager.getLogger(Window.class);
private static API api = API.NONE;
private static Window instance = null;
public static Window setup(API api, String title, Size size) {
if (instance != null) {
logger.warn("Window is already setup.");
return instance;
}
switch (api) {
case GLFW:
instance = new GlfwWindow(title, size);
break;
default:
logger.fatal("No valid window api chosen");
return null;
}
Window.api = api;
return instance;
}
public static API getApi() {
return api;
}
public void cleanup() {
instance = null;
logger.info("Window cleanup.");
}
public abstract void update();
}

View File

@@ -38,4 +38,4 @@ public class EventMeta<T> {
", ready=" + ready +
'}';
}
}
}

View File

@@ -123,4 +123,4 @@ public class EventRegistry {
'}';
}
}
}
}

View File

@@ -1,6 +1,7 @@
package org.toop.eventbus;
import org.toop.server.Server;
import org.toop.core.*;
import java.lang.reflect.Constructor;
import java.util.Arrays;
@@ -116,7 +117,30 @@ public class Events implements IEvents {
}
public static class WindowEvents {
/**
* Triggers when the window wants to quit.
*/
public record OnQuitRequested() {}
/**
* Triggers when the window is resized.
*/
public record OnResize(Window.Size size) {}
/**
* Triggers when the mouse is moved within the window.
*/
public record OnMouseMove(int x, int y) {}
/**
* Triggers when the mouse is clicked within the window.
*/
public record OnMouseClick(int button) {}
/**
* Triggers when the mouse is released within the window.
*/
public record OnMouseRelease(int button) {}
}
public static class TttEvents {

View File

@@ -109,4 +109,4 @@ public enum GlobalEventBus {
GlobalEventBus.INSTANCE.get().post(event);
}
}
}

View File

@@ -5,6 +5,7 @@ public class TTT extends GameBase {
public TTT(String player1, String player2) {
super(9);
players = new Player[2];
players[0] = new Player(player1, 'X');
players[1] = new Player(player2, 'O');

View File

@@ -0,0 +1,50 @@
package org.toop.graphics;
import org.toop.platform.graphics.opengl.*;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public abstract class Renderer {
public enum API {
NONE,
OPENGL,
};
protected static final Logger logger = LogManager.getLogger(Renderer.class);
private static API api = API.NONE;
private static Renderer instance = null;
public static Renderer setup(API api) {
if (instance != null) {
logger.warn("Renderer is already setup.");
return instance;
}
switch (api) {
case OPENGL:
instance = new OpenglRenderer();
break;
default:
logger.fatal("No valid renderer api chosen");
return null;
}
Renderer.api = api;
return instance;
}
public static API getApi() {
return api;
}
public void cleanup() {
instance = null;
logger.info("Renderer cleanup.");
}
public abstract void clear();
public abstract void render();
}

View File

@@ -0,0 +1,26 @@
package org.toop.graphics;
import org.toop.platform.graphics.opengl.*;
public abstract class Shader {
public static Shader create(String vertexPath, String fragmentPath) {
Shader shader = null;
switch (Renderer.getApi()) {
case OPENGL:
shader = new OpenglShader(vertexPath, fragmentPath);
break;
case NONE:
default:
break;
}
return shader;
}
public abstract void cleanup();
public abstract void start();
public abstract void stop();
}

View File

@@ -0,0 +1,26 @@
package org.toop.graphics.node;
import org.toop.core.*;
import org.toop.math.*;
public class Button extends Node {
ICallable<Boolean> onHover;
ICallable<Boolean> onClick;
public Button(int x, int y, int width, int height, Color color, ICallable<Boolean> onHover, ICallable<Boolean> onClick) {
super(x, y, width, height, color);
this.onHover = onHover;
this.onClick = onClick;
}
@Override
public void hover() {
onHover.call();
}
@Override
public void click() {
onClick.call();
}
}

View File

@@ -0,0 +1,20 @@
package org.toop.graphics.node;
import org.toop.math.*;
public abstract class Node {
protected Bounds bounds;
protected Color color;
public Node(int x, int y, int width, int height, Color color) {
bounds = new Bounds(x, y, width, height);
this.color = color;
}
public boolean check(int x, int y) {
return bounds.check(x, y);
}
public void hover() {}
public void click() {}
}

View File

@@ -0,0 +1,66 @@
package org.toop.graphics.node;
import org.toop.eventbus.*;
import org.toop.graphics.*;
import java.util.*;
import org.apache.logging.log4j.Logger;
import org.apache.logging.log4j.LogManager;
public class NodeManager {
private static final Logger logger = LogManager.getLogger(NodeManager.class);
private static NodeManager instance = null;
public static NodeManager setup() {
if (instance != null) {
logger.warn("NodeManager is already setup.");
return instance;
}
instance = new NodeManager();
return instance;
}
private Shader shader;
private ArrayList<Node> nodes;
private Node active;
private NodeManager() {
shader = Shader.create(
"src/main/resources/shaders/gui_vertex.glsl",
"src/main/resources/shaders/gui_fragment.glsl");
nodes = new ArrayList<Node>();
GlobalEventBus.subscribeAndRegister(Events.WindowEvents.OnMouseMove.class, event -> {
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
if (node.check(event.x(), event.y())) {
active = node;
node.hover();
break;
}
}
});
GlobalEventBus.subscribeAndRegister(Events.WindowEvents.OnMouseClick.class, event -> {
if (active != null) {
active.click();
}
});
}
public void cleanup() {
}
public void add(Node node) {
nodes.add(node);
}
public void render() {
}
}

View File

@@ -0,0 +1,49 @@
package org.toop.graphics.node;
import org.toop.math.*;
import java.util.*;
public class Widget {
Bounds bounds;
private ArrayList<Node> nodes;
public Widget(Bounds bounds) {
this.bounds = bounds;
nodes = new ArrayList<Node>();
}
public boolean check(int x, int y) {
return bounds.check(x, y);
}
public void add(Node node) {
nodes.add(node);
}
public boolean hover(int x, int y) {
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
if (node.check(x, y)) {
node.hover();
return true;
}
}
return false;
}
public boolean click(int x, int y) {
for (int i = 0; i < nodes.size(); i++) {
Node node = nodes.get(i);
if (node.check(x, y)) {
node.click();
return true;
}
}
return false;
}
}

View File

@@ -0,0 +1,30 @@
package org.toop.math;
public class Bounds {
private int x;
private int y;
private int width;
private int height;
public Bounds(int x, int y, int width, int height) {
set(x, y, width, height);
}
public void set(int x, int y, int width, int height) {
this.x = x;
this.y = y;
this.width = width;
this.height = height;
}
public int getX() { return x; }
public int getY() { return y; }
public int getWidth() { return width; }
public int getHeight() { return height; }
public boolean check(int x, int y) {
return
x >= this.x && x <= this.x + this.width &&
y >= this.y && y <= this.y + this.height;
}
}

View File

@@ -0,0 +1,17 @@
package org.toop.math;
public class Color {
private float r;
private float g;
private float b;
public Color(float r, float g, float b) {
this.r = r;
this.g = g;
this.b = b;
}
public float r() { return r; }
public float g() { return g; }
public float b() { return b; }
}

View File

@@ -0,0 +1,95 @@
package org.toop.platform.core.glfw;
import org.toop.core.*;
import org.toop.eventbus.*;
import org.lwjgl.glfw.*;
import org.lwjgl.system.*;
public class GlfwWindow extends Window {
private long window;
public GlfwWindow(String title, Size size) {
if (!GLFW.glfwInit()) {
logger.fatal("Failed to initialize glfw");
return;
}
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE);
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE);
GLFWVidMode videoMode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
int width = size.width();
int height = size.height();
if (width <= 0 || height <= 0 || width > videoMode.width() || height > videoMode.height()) {
width = videoMode.width();
height = videoMode.height();
GLFW.glfwWindowHint(GLFW.GLFW_MAXIMIZED, GLFW.GLFW_TRUE);
}
long window = GLFW.glfwCreateWindow(width, height, title, MemoryUtil.NULL, MemoryUtil.NULL);
if (window == MemoryUtil.NULL) {
GLFW.glfwTerminate();
logger.fatal("Failed to create glfw window");
return;
}
int[] widthBuffer = new int[1];
int[] heightBuffer = new int[1];
GLFW.glfwGetWindowSize(window, widthBuffer, heightBuffer);
GLFW.glfwMakeContextCurrent(window);
GLFW.glfwSwapInterval(1);
GLFW.glfwSetWindowCloseCallback(window, (lwindow) -> {
GlobalEventBus.post(new Events.WindowEvents.OnQuitRequested());
});
GLFW.glfwSetFramebufferSizeCallback(window, (lwindow, lwidth, lheight) -> {
GlobalEventBus.post(new Events.WindowEvents.OnResize(new Size(lwidth, lheight)));
});
GLFW.glfwSetCursorPosCallback(window, (lwindow, lx, ly) -> {
GlobalEventBus.post(new Events.WindowEvents.OnMouseMove((int)lx, (int)ly));
});
GLFW.glfwSetMouseButtonCallback(window, (lwindow, lbutton, laction, lmods) -> {
switch (laction) {
case GLFW.GLFW_PRESS:
GlobalEventBus.post(new Events.WindowEvents.OnMouseClick(lbutton));
break;
case GLFW.GLFW_RELEASE:
GlobalEventBus.post(new Events.WindowEvents.OnMouseRelease(lbutton));
break;
default: break;
}
});
this.window = window;
GLFW.glfwShowWindow(window);
logger.info("Glfw window setup. Title: {}. Width: {}. Height: {}.", title, size.width(), size.height());
}
@Override
public void cleanup() {
GLFW.glfwDestroyWindow(window);
GLFW.glfwTerminate();
super.cleanup();
}
@Override
public void update() {
GLFW.glfwSwapBuffers(window);
GLFW.glfwPollEvents();
}
}

View File

@@ -0,0 +1,75 @@
package org.toop.platform.graphics.opengl;
import org.toop.eventbus.*;
import org.toop.graphics.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
public class OpenglRenderer extends Renderer {
private Shader shader;
private int vao;
public OpenglRenderer() {
GL.createCapabilities();
GL45.glClearColor(0.65f, 0.9f, 0.65f, 1f);
GlobalEventBus.subscribeAndRegister(Events.WindowEvents.OnResize.class, event -> {
GL45.glViewport(0, 0, event.size().width(), event.size().height());
});
logger.info("Opengl renderer setup.");
// Form here on, everything is temporary
float vertices[] = {
-0.5f, 0.5f, 1.0f, 0.0f, 0.0f,
-0.5f, -0.5f, 0.0f, 1.0f, 0.0f,
0.5f, -0.5f, 0.0f, 0.0f, 1.0f,
0.5f, 0.5f, 1.0f, 1.0f, 0.0f,
};
int indicies[] = {
0, 1, 2,
2, 3, 0,
};
vao = GL45.glCreateVertexArrays();
GL45.glBindVertexArray(vao);
int vbo = GL45.glCreateBuffers();
GL45.glBindBuffer(GL45.GL_ARRAY_BUFFER, vbo);
GL45.glBufferData(GL45.GL_ARRAY_BUFFER, vertices, GL45.GL_STATIC_DRAW);
GL45.glVertexAttribPointer(0, 2, GL45.GL_FLOAT, false, 5 * 4, 0);
GL45.glVertexAttribPointer(1, 3, GL45.GL_FLOAT, false, 5 * 4, 2 * 4);
GL45.glEnableVertexAttribArray(0);
GL45.glEnableVertexAttribArray(1);
int ib = GL45.glCreateBuffers();
GL45.glBindBuffer(GL45.GL_ELEMENT_ARRAY_BUFFER, ib);
GL45.glBufferData(GL45.GL_ELEMENT_ARRAY_BUFFER, indicies, GL45.GL_STATIC_DRAW);
shader = Shader.create(
"src/main/resources/shaders/gui_vertex.glsl",
"src/main/resources/shaders/gui_fragment.glsl");
}
@Override
public void cleanup() {
super.cleanup();
}
@Override
public void clear() {
GL45.glClear(GL45.GL_COLOR_BUFFER_BIT);
}
@Override
public void render() {
// temporary
// shader.start();
GL45.glBindVertexArray(vao);
GL45.glDrawElements(GL45.GL_TRIANGLES, 6, GL45.GL_UNSIGNED_INT, MemoryUtil.NULL);
}
}

View File

@@ -0,0 +1,58 @@
package org.toop.platform.graphics.opengl;
import org.toop.core.*;
import org.toop.graphics.*;
import org.lwjgl.opengl.*;
public class OpenglShader extends Shader {
private int programID;
public OpenglShader(String vertexPath, String fragmentPath) {
FileSystem.File vertexSource = FileSystem.read(vertexPath);
FileSystem.File fragmentSource = FileSystem.read(fragmentPath);
if (vertexSource == null || fragmentPath == null) {
return;
}
programID = GL45.glCreateProgram();
int vertexShader = GL45.glCreateShader(GL45.GL_VERTEX_SHADER);
int fragmentShader = GL45.glCreateShader(GL45.GL_FRAGMENT_SHADER);
GL45.glShaderSource(vertexShader, vertexSource.buffer());
GL45.glShaderSource(fragmentShader, fragmentSource.buffer());
GL45.glCompileShader(vertexShader);
GL45.glCompileShader(fragmentShader);
GL45.glAttachShader(programID, vertexShader);
GL45.glAttachShader(programID, fragmentShader);
GL45.glLinkProgram(programID);
GL45.glValidateProgram(programID);
GL45.glDetachShader(programID, vertexShader);
GL45.glDetachShader(programID, fragmentShader);
GL45.glDeleteShader(vertexShader);
GL45.glDeleteShader(fragmentShader);
}
@Override
public void cleanup() {
stop();
GL45.glDeleteProgram(programID);
}
@Override
public void start() {
GL45.glUseProgram(programID);
}
@Override
public void stop() {
GL45.glUseProgram(0);
}
}

View File

@@ -1,13 +1,13 @@
<?xml version="1.0" encoding="UTF-8"?>
<Configuration status="debug" name="AppConfig">
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>
<Appenders>
<Console name="Console" target="SYSTEM_OUT">
<PatternLayout pattern="%d{yyyy-MM-dd HH:mm:ss} [%t] %-5level %logger{36} - %msg%n"/>
</Console>
</Appenders>
<Loggers>
<Root level="info">
<AppenderRef ref="Console"/>
</Root>
</Loggers>
</Configuration>

View File

@@ -0,0 +1,9 @@
#version 450 core
in vec3 pass_color;
out vec4 out_color;
void main(void) {
out_color = vec4(pass_color, 1.0);
}

View File

@@ -0,0 +1,11 @@
#version 450 core
layout(location = 0) in vec2 in_position;
layout(location = 1) in vec3 in_color;
out vec3 pass_color;
void main(void) {
gl_Position = vec4(in_position, 0.0f, 1.0f);
pass_color = in_color;
}

View File

@@ -111,4 +111,4 @@ public class GlobalEventBusTest {
// TestEvent storedEvent = storedEntry.getEvent();
// assertEquals(event, storedEvent);
// }
}
}