diff --git a/src/diveengine2d/Engine.java b/src/diveengine2d/Engine.java index b479215..a77d697 100644 --- a/src/diveengine2d/Engine.java +++ b/src/diveengine2d/Engine.java @@ -21,10 +21,6 @@ public class Engine { public static String startScene = null; public static String name = null; - // no fucking clue. - private GLFWErrorCallback errorCallback; - private GLFWKeyCallback keyCallback; - //literally a byte address private long window; @@ -160,11 +156,8 @@ public class Engine { glfwWindowHint(GLFW_VISIBLE, GLFW_FALSE); // the window will stay hidden after creation glfwWindowHint(GLFW_RESIZABLE, GLFW_TRUE); // the window will be resizable - int WIDTH = 300; - int HEIGHT = 300; - // Create the window - window = glfwCreateWindow(WIDTH, HEIGHT, "Hello World!", NULL, NULL); + window = glfwCreateWindow(WIDTH, HEIGHT, name, NULL, NULL); if ( window == NULL ) throw new RuntimeException("Failed to create the GLFW window"); @@ -184,6 +177,28 @@ public class Engine { // Make the window visible glfwShowWindow(window); + + GL.createCapabilities(); + + //glViewport(0, 0, WIDTH, HEIGHT); + //glMatrixMode(GL_PROJECTION); + //glLoadIdentity(); + glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); + //glMatrixMode(GL_MODELVIEW); + //glLoadIdentity(); + /* + glMatrixMode(GL_PROJECTION); + glLoadIdentity(); + glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); + glMatrixMode(GL_MODELVIEW); + */ + + //ratio = WIDTH / (float) HEIGHT; + + + // Set the clear color + glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + } private void loop() { @@ -192,42 +207,19 @@ public class Engine { // LWJGL detects the context that is current in the current thread, // creates the GLCapabilities instance and makes the OpenGL // bindings available for use. - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(0, WIDTH, HEIGHT, 0, 1, -1); - glMatrixMode(GL_MODELVIEW); - - GL.createCapabilities(); - - // Set the clear color - glClearColor(0.0f, 0.0f, 0.0f, 0.0f); + + + // Run the rendering loop until the user has attempted to close // the window or has pressed the ESCAPE key. while ( glfwWindowShouldClose(window) == GLFW_FALSE ) { - //glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); // clear the framebuffer - float ratio; - //glfwGetFramebufferSize(window, &width, &height); - - - ratio = WIDTH / (float) HEIGHT; - glViewport(0, 0, WIDTH, HEIGHT); - glClear(GL_COLOR_BUFFER_BIT); - glMatrixMode(GL_PROJECTION); - glLoadIdentity(); - glOrtho(-ratio, ratio, -1.f, 1.f, 1.f, -1.f); - glMatrixMode(GL_MODELVIEW); - glLoadIdentity(); - glRotatef((float) glfwGetTime() * 50.f, 0.f, 0.f, 1.f); - glBegin(GL_TRIANGLES); - glColor3f(1.f, 0.f, 0.f); - glVertex3f(-0.6f, -0.4f, 0.f); - glColor3f(0.f, 1.f, 0.f); - glVertex3f(0.6f, -0.4f, 0.f); - glColor3f(0.f, 0.f, 1.f); - glVertex3f(0.f, 0.6f, 0.f); - glEnd(); + //glRotatef(50.f, 0.f, 0.f, 1.f); + + + glfwSwapBuffers(window); glfwPollEvents(); } diff --git a/src/diveengine2d/Window.java b/src/diveengine2d/Window.java new file mode 100644 index 0000000..49d724c --- /dev/null +++ b/src/diveengine2d/Window.java @@ -0,0 +1,173 @@ +package diveengine2d; + +import static org.lwjgl.glfw.GLFW.*; +import static org.lwjgl.opengl.GL11.*; +import static org.lwjgl.system.MemoryUtil.NULL; + +import org.lwjgl.glfw.GLFWCursorPosCallback; +import org.lwjgl.glfw.GLFWErrorCallback; +import org.lwjgl.glfw.GLFWFramebufferSizeCallback; +import org.lwjgl.glfw.GLFWKeyCallback; +import org.lwjgl.glfw.GLFWMouseButtonCallback; +import org.lwjgl.glfw.GLFWWindowSizeCallback; +import org.lwjgl.glfw.GLFWVidMode; +import org.lwjgl.opengl.GL; + +public class Window { + + private GLFWErrorCallback errorCallback; + private GLFWKeyCallback keyCallback; + private GLFWMouseButtonCallback mouseButtonCallback; + private GLFWCursorPosCallback cursorPosCallback; + private GLFWFramebufferSizeCallback frameBufferCallback; + private GLFWWindowSizeCallback windowSizeCallback; + + private long window; + + private String title; + private int width, height; + private boolean vsync, fullscreen, visible, resizable; + + public Window(String title, int width, int height, boolean vsync, boolean fullscreen, boolean visible, boolean resizable) { + this.title = title; + this.width = width; + this.height = height; + this.vsync = vsync; + this.fullscreen = fullscreen; + this.visible = visible; + this.resizable = resizable; + + init(); + } + + private void init() { + if (glfwInit() != GL_TRUE) { + throw new IllegalStateException("Failed to initialize GLFW."); + } + + glfwDefaultWindowHints(); + glfwWindowHint(GLFW_VISIBLE, visible ? GL_TRUE : GL_FALSE); + glfwWindowHint(GLFW_RESIZABLE, resizable ? GL_TRUE : GL_FALSE); + + window = glfwCreateWindow(width, height, title, fullscreen ? glfwGetPrimaryMonitor() : NULL, NULL); + if (window == NULL) { + throw new RuntimeException("Failed to create the GLFW window."); + } + + GLFWVidMode vidmode = glfwGetVideoMode(glfwGetPrimaryMonitor()); + glfwSetWindowPos(window, (vidmode.width() - width) / 2, (vidmode.height() - height) / 2); + + glfwMakeContextCurrent(window); + + errorCallback = GLFWErrorCallback.createPrint(); + errorCallback.set(); + + frameBufferCallback = new GLFWFramebufferSizeCallback() { + @Override + public void invoke(long window, int width, int height) { + glViewport(0, 0, width, height); + } + }; + frameBufferCallback.set(window); + + windowSizeCallback = new GLFWWindowSizeCallback() { + @Override + public void invoke(long window, int width, int height) { + Window.this.width = width; + Window.this.height = height; + } + }; + windowSizeCallback.set(window); + + glfwSwapInterval(vsync ? 1 : 0); + + GL.createCapabilities(); + } + + public void clear() { + glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT); + } + + public void update() { + glfwSwapBuffers(window); + glfwPollEvents(); + } + + public void dispose() { + keyCallback.release(); + mouseButtonCallback.release(); + cursorPosCallback.release(); + frameBufferCallback.release(); + windowSizeCallback.release(); + glfwTerminate(); + errorCallback.release(); + } + + public void close() { + glfwSetWindowShouldClose(window, GL_TRUE); + } + + public boolean shouldClose() { + return glfwWindowShouldClose(window) == GL_TRUE; + } + + public String getTitle() { + return title; + } + + public void setTitle(String title) { + this.title = title; + } + + public int getWidth() { + return width; + } + + public void setWidth(int width) { + this.width = width; + } + + public int getHeight() { + return height; + } + + public void setHeight(int height) { + this.height = height; + } + + public boolean vsync() { + return vsync; + } + + public void setVsync(boolean vsync) { + this.vsync = vsync; + glfwSwapInterval(vsync ? 1 : 0); + } + + public boolean fullscreen() { + return fullscreen; + } + + public void setFullscreen(boolean fullscreen) { + // TODO + this.fullscreen = fullscreen; + } + + public boolean visible() { + return visible; + } + + public void setVisible(boolean visible) { + this.visible = visible; + if (visible) { + glfwShowWindow(window); + } else { + glfwHideWindow(window); + } + } + + public boolean resizable() { + return resizable; + } + +} \ No newline at end of file diff --git a/src/diveengine2d/components/RigidBody.java b/src/diveengine2d/components/RigidBody.java index c9bc3e4..7276829 100644 --- a/src/diveengine2d/components/RigidBody.java +++ b/src/diveengine2d/components/RigidBody.java @@ -7,6 +7,8 @@ import diveengine2d.Time; import java.awt.Color; import java.awt.Graphics2D; +import static org.lwjgl.opengl.GL11.*; + public class RigidBody extends DiveScript { private static Color xAxisColor = new Color(244, 67, 54); //A500 red @@ -35,6 +37,13 @@ public class RigidBody extends DiveScript { g.drawLine((int)entity.x, (int)entity.y, (int)entity.x, (int)entity.y+(int)(dy*20)); g.setColor(debugColor); g.drawLine((int)entity.x, (int)entity.y, (int)entity.x+(int)(dx*15), (int)entity.y+(int)(dy*15)); + + glLineWidth(2.5f); + glColor3f(1.0f, 0.0f, 0.0f); + glBegin(GL_LINES); + glVertex3f(0.0f, 0.0f, 0.0f); + glVertex3f(15f, 0f, 0f); + glEnd(); } } }