[Example] Migrate example to the new module

This commit is contained in:
SpaiR committed 2021-01-24 22:20:37 +02:00
1 parent 46f74381aa
commit cf9e74b1d0
17 files changed
+489 -822

No files matched your search

@@ -12,7 +12,11 @@ package imgui.app;
* <li>{@link #configure(Configuration)} It's called before window creation, so only basic application setups are expected.</li>
* <li>{@link #initWindow(Configuration)} Method creates application window.</li>
* <li>{@link #initImGui(Configuration)} Method initializes Dear ImGui context. Could be used to do Dear ImGui setup as well.</li>
* <li>{@link #preRun()} Method called once, before application loop.</li>
* <li>{@link #preProcess()} Method called every frame, before {@link #process()}.</li>
* <li>{@link #process()} Method is meant to be overridden with user application logic.</li>
* <li>{@link #postProcess()} Method called every frame, after {@link #process()}.</li>
* <li>{@link #postRun()} Method called once, after application loop.</li>
* <li>{@link #disposeImGui()} Destroys Dear ImGui context.</li>
* <li>{@link #disposeWindow()} Destroys application window.</li>
* </ol>
@@ -27,17 +31,11 @@ package imgui.app;
* <pre>{@code
* import imgui.ImGui;
* import imgui.app.Application;
* import imgui.app.Configuration;
*
* public class Main extends Application {
* @Override
* protected void configure(final Configuration config) {
* config.setTitle("Example Application");
* }
*
* @Override
* public void process() {
* ImGui.text("Hello, World");
* ImGui.text("Hello, World!");
* }
*
* public static void main(final String[] args) {
@@ -55,13 +53,25 @@ package imgui.app;
*/
public abstract class Application extends Window {
/**
* Method is called before window creation. Could be used to provide basic window information, like title name etc.
* Method called before window creation. Could be used to provide basic window information, like title name etc.
*
* @param config configuration object with basic window information
*/
protected void configure(final Configuration config) {
}
/**
* Method called once, before application run loop.
*/
protected void preRun() {
}
/**
* Method called once, after application run loop.
*/
protected void postRun() {
}
/**
* Entry point of any ImGui application. Use it to start the application loop.
*
@@ -69,7 +79,9 @@ public abstract class Application extends Window {
*/
public static void launch(final Application app) {
initialize(app);
app.preRun();
app.run();
app.postRun();
app.dispose();
}
@@ -0,0 +1,62 @@
package imgui.app;
import lombok.Data;
import lombok.NoArgsConstructor;
/**
* Object for color data.
*/
@Data
@NoArgsConstructor
public class Color implements Cloneable {
private final float[] data = new float[4];
public Color(final float[] color) {
set(color);
}
public Color(final float red, final float green, final float blue, final float alpha) {
data[0] = red;
data[1] = green;
data[2] = blue;
data[3] = alpha;
}
public final void set(final float red, final float green, final float blue, final float alpha) {
data[0] = red;
data[1] = green;
data[2] = blue;
data[3] = alpha;
}
public final void set(final float[] color) {
System.arraycopy(color, 0, data, 0, 4);
}
public final float getRed() {
return data[0];
}
public final float getGreen() {
return data[1];
}
public final float getBlue() {
return data[2];
}
public final float getAlpha() {
return data[3];
}
/**
* Method to clone Color instance.
*
* @return cloned Color instance
*/
@Override
@SuppressWarnings("MethodDoesntCallSuperMethod")
public Color clone() {
return new Color(data);
}
}
@@ -6,7 +6,7 @@ import lombok.Data;
* Data class to provide basic information about the window. Like, the title name etc.
*/
@Data
public final class Configuration {
public class Configuration {
/**
* Application title.
*/
+32 -7
View File
@@ -13,7 +13,6 @@ import org.lwjgl.opengl.GL32;
import org.lwjgl.system.MemoryStack;
import org.lwjgl.system.MemoryUtil;
import java.awt.Color;
import java.nio.IntBuffer;
import java.util.Objects;
@@ -29,13 +28,14 @@ public abstract class Window {
private String glslVersion = null;
/**
* Pointer to native GLFW window.
* Pointer to the native GLFW window.
*/
protected long handle;
/**
* Background color of the window.
*/
protected Color colorBg = Color.GRAY;
protected final Color colorBg = new Color(.5f, .5f, .5f, 1);
/**
* Method to initialize application.
@@ -126,13 +126,27 @@ public abstract class Window {
ImGui.createContext();
}
/**
* Method called every frame, before calling {@link #process()} method.
*/
protected void preProcess() {
}
/**
* Method called every frame, after calling {@link #process()} method.
*/
protected void postProcess() {
}
/**
* Main application loop.
*/
protected final void run() {
while (!GLFW.glfwWindowShouldClose(handle)) {
startFrame();
preProcess();
process();
postProcess();
endFrame();
}
}
@@ -143,10 +157,7 @@ public abstract class Window {
public abstract void process();
private void startFrame() {
if (colorBg != null) {
GL32.glClearColor(colorBg.getRed() / 255f, colorBg.getGreen() / 255f, colorBg.getBlue() / 255f, colorBg.getAlpha() / 255f);
}
GL32.glClearColor(colorBg.getRed(), colorBg.getGreen(), colorBg.getBlue(), colorBg.getAlpha());
GL32.glClear(GL32.GL_COLOR_BUFFER_BIT | GL32.GL_DEPTH_BUFFER_BIT);
imGuiGlfw.newFrame();
ImGui.newFrame();
@@ -183,4 +194,18 @@ public abstract class Window {
GLFW.glfwTerminate();
Objects.requireNonNull(GLFW.glfwSetErrorCallback(null)).free();
}
/**
* @return pointer to the native GLFW window
*/
public final long getHandle() {
return handle;
}
/**
* @return {@link Color} instance, which represents background color for the window
*/
public final Color getColorBg() {
return colorBg;
}
}