Start using render engine stuff

part 1/?
master
Caroline Bell 2019-09-17 19:21:59 -07:00
parent d0142fd8d1
commit 9d12b6e8ca
5 changed files with 136 additions and 78 deletions

View File

@ -18,5 +18,6 @@
</attributes>
</classpathentry>
<classpathentry kind="lib" path="lib/bwyap-engine.jar" sourcepath="C:/Coding/Java/libraries/Bwyap&apos;s RenderEngine/src.zip"/>
<classpathentry kind="lib" path="lib/json-simple-1.1.1.jar"/>
<classpathentry kind="output" path="target/classes"/>
</classpath>

BIN
lib/json-simple-1.1.1.jar Normal file

Binary file not shown.

View File

@ -0,0 +1,104 @@
package com.github.halotroop.litecraft;
import java.nio.IntBuffer;
import org.joml.Vector2f;
import org.lwjgl.Version;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.MemoryStack;
import com.bwyap.engine.EngineInterface;
import com.bwyap.engine.input.InputHandler;
import com.bwyap.engine.window.WindowInterface;
public class LCWindow extends com.bwyap.engine.window.Window
{
private long windowLong;
public long getWindowLong()
{ return windowLong; }
public void setWindowLong(long window)
{ this.windowLong = window; }
public LCWindow(int width, int height)
{
super(width, height, "LiteCraft", true);
init();
start();
}
@Override
public boolean shouldClose()
{
return false;
}
@Override
public void processEvents()
{
}
@Override
public void swapDisplayBuffers()
{
}
@Override
public EngineInterface createEngine() throws Exception
{
return null;
}
@Override
public void init()
{
GLFW.glfwDefaultWindowHints();
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, 1);
// Create the window
windowLong = GLFW.glfwCreateWindow(getWidth(), getHeight(), getDefaultTitle(), 0, 0);
if (windowLong == 0) throw new RuntimeException("Failed to create the GLFW window");
}
@Override
public void start()
{
// Get the thread stack and push a new frame
try (MemoryStack stack = MemoryStack.stackPush())
{
IntBuffer pWidth = stack.mallocInt(1);
IntBuffer pHeight = stack.mallocInt(1);
// Get the windowLong size passed to glfwCreateWindow
GLFW.glfwGetWindowSize(windowLong, pWidth, pHeight);
// Get the resolution of the primary monitor
GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
// Center the window
GLFW.glfwSetWindowPos(windowLong, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2);
// Make the OpenGL context current
GLFW.glfwMakeContextCurrent(windowLong);
// Enable v-sync
GLFW.glfwSwapInterval(1);
// Make the window visible
GLFW.glfwShowWindow(windowLong);
}
}
@Override
public void dispose()
{
Callbacks.glfwFreeCallbacks(windowLong);
GLFW.glfwDestroyWindow(windowLong);
}
@Override
protected void setWindowTitle(String title)
{
super.setDefaultTitle(title);
}
}

View File

@ -4,38 +4,45 @@ import org.lwjgl.*;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.*;
import java.nio.*;
public class LiteCraftMain
{
Window win;
private void init()
{
// Setup 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 (!GLFW.glfwInit())
throw new IllegalStateException("Unable to initialize GLFW");
// Configure GLFW
GLFW.glfwDefaultWindowHints(); // optional, the current window hints are already the default
GLFW.glfwWindowHint(GLFW.GLFW_VISIBLE, GLFW.GLFW_FALSE); // the window will stay hidden after creation
GLFW.glfwWindowHint(GLFW.GLFW_RESIZABLE, GLFW.GLFW_TRUE); // the window will be resizable
win = new Window();
}
protected LCWindow window;
public static int width = 400, height = 300; // Don't change these values. They just initialize it in case we forget to set them later.
public void run()
{
System.out.println("LWJGL version: " + Version.getVersion());
init();
loop();
destroy();
}
private void init()
{
// Setup 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 (!GLFW.glfwInit()) throw new IllegalStateException("Unable to initialize GLFW");
// Configure GLFW
window = new LCWindow(width, height);
window.setDefaultTitle("LiteCraft " + "INSERT SPLASH TEXT HERE!");
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
GLFW.glfwSetKeyCallback(window.getWindowLong(), (window, key, scancode, action, mods) ->
{
if (key == GLFW.GLFW_KEY_ESCAPE && action == GLFW.GLFW_RELEASE)
GLFW.glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
});
}
private void destroy()
{
win.destroy();
window.dispose();
// Terminate GLFW and free the error callback
GLFW.glfwTerminate();
GLFW.glfwSetErrorCallback(null).free();
@ -48,15 +55,19 @@ public class LiteCraftMain
// Set the background color
GL11.glClearColor(0.0f, 0.0f, 0.0f, 1.0f);
// Run the rendering loop until the user has attempted to close the window or has pressed the ESCAPE key.
while (!GLFW.glfwWindowShouldClose(win.getWindow()))
while (!GLFW.glfwWindowShouldClose(window.getWindowLong()))
{
GL11.glClear(GL11.GL_COLOR_BUFFER_BIT | GL11.GL_DEPTH_BUFFER_BIT); // clear the framebuffer
GLFW.glfwSwapBuffers(win.getWindow()); // swap the color buffers
GLFW.glfwSwapBuffers(window.getWindowLong()); // swap the color buffers
// Poll for window events. The key callback above will only be invoked during this call.
GLFW.glfwPollEvents();
}
}
public static void main(String[] args)
{ new LiteCraftMain().run(); }
{
width = 1600;
height = 900;
new LiteCraftMain().run();
}
}

View File

@ -1,58 +0,0 @@
package com.github.halotroop.litecraft;
import java.nio.IntBuffer;
import org.lwjgl.Version;
import org.lwjgl.glfw.*;
import org.lwjgl.opengl.*;
import org.lwjgl.system.MemoryStack;
public class Window
{
private long window;
public long getWindow()
{ return window; }
public void setWindow(long window)
{ this.window = window; }
public Window()
{
// Create the window
window = GLFW.glfwCreateWindow(1600, 900, "Hello World!", 0, 0);
if (window == 0)
throw new RuntimeException("Failed to create the GLFW window");
// Setup a key callback. It will be called every time a key is pressed, repeated or released.
GLFW.glfwSetKeyCallback(window, (window, key, scancode, action, mods) ->
{
if (key == GLFW.GLFW_KEY_ESCAPE && action == GLFW.GLFW_RELEASE)
GLFW.glfwSetWindowShouldClose(window, true); // We will detect this in the rendering loop
});
// Get the thread stack and push a new frame
try (MemoryStack stack = MemoryStack.stackPush())
{
IntBuffer pWidth = stack.mallocInt(1); // int*
IntBuffer pHeight = stack.mallocInt(1); // int*
// Get the window size passed to glfwCreateWindow
GLFW.glfwGetWindowSize(window, pWidth, pHeight);
// Get the resolution of the primary monitor
GLFWVidMode vidmode = GLFW.glfwGetVideoMode(GLFW.glfwGetPrimaryMonitor());
// Center the window
GLFW.glfwSetWindowPos(window, (vidmode.width() - pWidth.get(0)) / 2, (vidmode.height() - pHeight.get(0)) / 2);
// Make the OpenGL context current
GLFW.glfwMakeContextCurrent(window);
// Enable v-sync
GLFW.glfwSwapInterval(1);
// Make the window visible
GLFW.glfwShowWindow(window);
}
}
public void destroy()
{
// Free the window callbacks and destroy the window
Callbacks.glfwFreeCallbacks(window);
GLFW.glfwDestroyWindow(window);
}
}