From 07c4572ce3faedeeb4539a4a3bd0f8d3343e805f Mon Sep 17 00:00:00 2001 From: Marcus Gosselin Date: Tue, 23 Feb 2016 23:10:45 -0500 Subject: [PATCH] vectors, time, new rendering methods, all o dat yo --- src/diveengine2d/DebugSettings.java | 32 +++++++++++++++ src/diveengine2d/DiveMath.java | 32 +++++++++++++++ src/diveengine2d/Engine.java | 63 ++++++++++++++++++++++------- src/diveengine2d/Entity.java | 6 +++ src/diveengine2d/Input.java | 48 +++++++++++++++++++++- src/diveengine2d/RectRenderer.java | 15 +++++-- src/diveengine2d/RigidBody.java | 29 ++++++++++--- src/diveengine2d/SceneManager.java | 5 +++ src/diveengine2d/Time.java | 1 + src/diveengine2d/Vector2.java | 21 ++++++++++ src/diveengine2d/Vector3.java | 10 +++++ 11 files changed, 239 insertions(+), 23 deletions(-) create mode 100644 src/diveengine2d/DebugSettings.java create mode 100644 src/diveengine2d/DiveMath.java create mode 100644 src/diveengine2d/Vector2.java create mode 100644 src/diveengine2d/Vector3.java diff --git a/src/diveengine2d/DebugSettings.java b/src/diveengine2d/DebugSettings.java new file mode 100644 index 0000000..78a3b52 --- /dev/null +++ b/src/diveengine2d/DebugSettings.java @@ -0,0 +1,32 @@ +package diveengine2d; + +import java.awt.Graphics2D; + +import java.awt.Color; + +public class DebugSettings { + public static int debugLevel = 0; + public static Color backgroundColor = new Color(250, 250, 250); + public static Color foregroundColor = new Color(25, 118, 210); + + public static void render(Graphics2D g) { + if(debugLevel == 0) return; + g.setColor(backgroundColor); + + int y = 20; + int x = 20; + g.fillRect(x, y, 140, debugLevel == 1 ? 72 : 100); + g.setColor(foregroundColor); + + x += 10; + y += 20; + int yOff = 20; + g.drawString("" + Time.FPS, x, y); + y += yOff; + g.drawString("" + Time.timedFramesCurrent, x, y); + y += yOff; + g.drawString("" + SceneManager.entityCount(), x, y); + y += yOff; + + } +} diff --git a/src/diveengine2d/DiveMath.java b/src/diveengine2d/DiveMath.java new file mode 100644 index 0000000..4524f96 --- /dev/null +++ b/src/diveengine2d/DiveMath.java @@ -0,0 +1,32 @@ +package diveengine2d; + +public class DiveMath { + + public static boolean inTriangle(Vector2 a, Vector2 b, Vector2 c, Vector2 p) { + return sameSide(p, a, b, c) && sameSide(p, b, c, a) && sameSide(p, c, a, b); + } + + public static boolean sameSide(Vector2 p1, Vector2 p2, Vector2 a, Vector2 b) { + Vector2 bma = b.subtract(a); + Vector2 p1ma = p1.subtract(a); + Vector2 p2ma = p2.subtract(a); + + float cp1 = bma.x*p1ma.y-bma.y*p1ma.x; + float cp2 = bma.x*p2ma.y-bma.y*p2ma.x; + + return cp1/cp2 >= 0; + } + + /* + function SameSide(p1,p2, a,b) + cp1 = CrossProduct(b-a, p1-a) + cp2 = CrossProduct(b-a, p2-a) + if DotProduct(cp1, cp2) >= 0 then return true + else return false + +function PointInTriangle(p, a,b,c) + if SameSide(p,a, b,c) and SameSide(p,b, a,c) + and SameSide(p,c, a,b) then return true + else return false + */ +} diff --git a/src/diveengine2d/Engine.java b/src/diveengine2d/Engine.java index fb6ce23..1c74324 100644 --- a/src/diveengine2d/Engine.java +++ b/src/diveengine2d/Engine.java @@ -3,8 +3,8 @@ package diveengine2d; import java.awt.Canvas; import java.awt.Color; import java.awt.Dimension; -import java.awt.Graphics; import java.awt.Graphics2D; +import java.awt.RenderingHints; import java.awt.image.BufferStrategy; import java.io.File; import java.nio.charset.Charset; @@ -19,7 +19,7 @@ public class Engine extends Canvas { public static int WIDTH, HEIGHT; public static String startScene = null; public static String name = null; - public static BufferStrategy bs; + public static BufferStrategy bs = null; public Engine(String gameFolder) { @@ -60,26 +60,62 @@ public class Engine extends Canvas { frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); this.requestFocus(); this.addKeyListener(new Input()); + this.addMouseMotionListener(new Input()); + this.addMouseListener(new Input()); createBufferStrategy(2); bs = getBufferStrategy(); + Time.nanos = System.nanoTime(); + + while(true) { - long startTime = System.currentTimeMillis(); - updateScene(); - Graphics2D g = (Graphics2D)bs.getDrawGraphics(); - render(g); - bs.show(); - int elapsed = (int)(System.currentTimeMillis() - startTime); - try{ - Thread.sleep(17 - elapsed); - }catch(Exception e) { - + + Time.startTime = System.currentTimeMillis(); + if (System.currentTimeMillis() > Time.nextSecond) { + Time.nextSecond += 1000; + Time.FPS = Time.framesInCurrentSecond; + Time.framesInCurrentSecond = 0; + System.out.println("Timed Frames: " + Time.timedFramesCurrent); + System.out.println("Calculated Frames: " + Time.FPS); + Time.timedFramesCurrent = 0; } + Time.framesInCurrentSecond++; + + render(); + updateScene(); + Time.tickTime = (System.nanoTime() - Time.nanos)/16640000d; + Time.deltaTime = Time.tickTime * Time.timeScale; + Time.nanos = System.nanoTime(); +// System.out.println("dTime: " + Time.deltaTime); + + Time.timedFramesCurrent += Time.deltaTime; + } } + public void render() { + Graphics2D g = (Graphics2D)bs.getDrawGraphics(); + g.setRenderingHint(RenderingHints.KEY_ANTIALIASING, RenderingHints.VALUE_ANTIALIAS_ON); + g.setRenderingHint(RenderingHints.KEY_TEXT_ANTIALIASING, RenderingHints.VALUE_TEXT_ANTIALIAS_ON); + render(g); + DebugSettings.render(g); + + Vector2 a = new Vector2(512, 100); + Vector2 b = new Vector2(256, 200); + Vector2 c = new Vector2(768, 400); + Vector2 mouse = new Vector2(Input.mouseX, Input.mouseY); + + g.setColor(DiveMath.inTriangle(a, b, c, mouse) ? Color.GREEN : Color.RED); + + g.fillOval((int)a.x, (int)a.y, 10, 10); + g.fillOval((int)b.x, (int)b.y, 10, 10); + g.fillOval((int)c.x, (int)c.y, 10, 10); + + bs.show(); + } + private void updateScene() { SceneManager.updateAll(); } @@ -112,7 +148,7 @@ public class Engine extends Canvas { parts[1] = parts[1].trim(); if (parts[0].equals("StartScene")) { - this.startScene = parts[1]; + Engine.startScene = parts[1]; } else if (parts[0].equals("Resolution")) { String[] resparts = parts[1].split("x"); @@ -143,7 +179,6 @@ public class Engine extends Canvas { System.out.println("Loaded Config File..."); } - private void render(Graphics2D g) { SceneManager.render(g); g.setColor(Color.BLACK); diff --git a/src/diveengine2d/Entity.java b/src/diveengine2d/Entity.java index 15b8ddf..aa6fcc0 100644 --- a/src/diveengine2d/Entity.java +++ b/src/diveengine2d/Entity.java @@ -1,4 +1,5 @@ package diveengine2d; +import java.awt.Graphics2D; import java.util.ArrayList; import java.util.List; @@ -10,6 +11,7 @@ public class Entity { public List components = new ArrayList(); + @SuppressWarnings("unchecked") //HACK because it is checked, java is just duuuumb public T getComponent(Class componentType) { for(DiveScript script : components) { if(script.getClass().isAssignableFrom(componentType)) { @@ -36,4 +38,8 @@ public class Entity { public void addComponent(DiveScript component) { components.add(component); } + + public void render(Graphics2D g) { + if(DebugSettings.debugLevel > 0) g.drawString("" + x + ", " + y, x, y - 2); + } } diff --git a/src/diveengine2d/Input.java b/src/diveengine2d/Input.java index c8721ea..b25c7ca 100644 --- a/src/diveengine2d/Input.java +++ b/src/diveengine2d/Input.java @@ -2,13 +2,17 @@ package diveengine2d; import java.awt.event.KeyEvent; import java.awt.event.KeyListener; +import java.awt.event.MouseEvent; +import java.awt.event.MouseListener; +import java.awt.event.MouseMotionListener; import java.util.ArrayList; import java.util.List; -public class Input implements KeyListener{ +public class Input implements KeyListener, MouseListener, MouseMotionListener{ private static boolean[] keys = new boolean[512]; private static List listeners = new ArrayList(); + public static int mouseX, mouseY; public static void addKeyListener(KeyListener listener) { listeners.add(listener); @@ -36,4 +40,46 @@ public class Input implements KeyListener{ keys[e.getKeyCode()] = false; for(KeyListener l : listeners) l.keyReleased(e); } + + @Override + public void mouseClicked(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseEntered(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseExited(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mousePressed(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseReleased(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseDragged(MouseEvent e) { + // TODO Auto-generated method stub + + } + + @Override + public void mouseMoved(MouseEvent e) { + mouseX = e.getX(); + mouseY = e.getY(); + } } diff --git a/src/diveengine2d/RectRenderer.java b/src/diveengine2d/RectRenderer.java index 51d59bf..8dd67d0 100644 --- a/src/diveengine2d/RectRenderer.java +++ b/src/diveengine2d/RectRenderer.java @@ -2,6 +2,7 @@ package diveengine2d; import java.awt.Color; import java.awt.Graphics2D; +import java.awt.Polygon; public class RectRenderer extends DiveScript{ @@ -12,10 +13,18 @@ public class RectRenderer extends DiveScript{ name = "Rectangle Renderer"; } + float i = 0; public void render(Graphics2D g) { + //g.rotate(Math.toRadians(i+=Time.deltaTime)); g.setColor(color); - g.fillRect((int)entity.x, (int)entity.y, width, height); - //System.out.println("" + entity.x + " " + entity.y); - g.drawString("" + Time.deltaTime, 100, 100); + Polygon p = new Polygon(new int[]{0, width, width, 0}, new int[]{0, 0, height, height}, 4); + for(int i = 0; i < p.npoints; i ++) { + + } + p.translate((int)entity.x, (int)entity.y); + g.fillPolygon(p); + + //g.fillRect((int)entity.x, (int)entity.y, width, height); + //g.rotate(Math.toRadians(-i)); } } diff --git a/src/diveengine2d/RigidBody.java b/src/diveengine2d/RigidBody.java index 435c54b..f3eb7ce 100644 --- a/src/diveengine2d/RigidBody.java +++ b/src/diveengine2d/RigidBody.java @@ -1,17 +1,36 @@ package diveengine2d; +import java.awt.Color; +import java.awt.Graphics2D; + public class RigidBody extends DiveScript{ - + + private static Color xAxisColor = new Color(244, 67, 54); //A500 red + private static Color yAxisColor = new Color(33, 150, 243); //A500 blue + private static Color debugColor = new Color(76, 175, 80); //A500 green public double dx, dy, drot; public double friction; + public boolean physics = false; + //public bool public void update() { + dx += (dx*friction - dx) * Time.deltaTime; + dy += (dy*friction - dy) * Time.deltaTime; + drot += (drot*friction - drot) * Time.deltaTime; + entity.x += dx * Time.deltaTime; entity.y += dy * Time.deltaTime; entity.rotation += drot * Time.deltaTime; - double friction = 1 - ((1 - this.friction) * Time.deltaTime); - dx *= friction; - dy *= friction; - drot *= friction; + } + + public void render(Graphics2D g) { + if(DebugSettings.debugLevel > 0) { + g.setColor(xAxisColor); + g.drawLine((int)entity.x, (int)entity.y, (int)entity.x+(int)(dx*20), (int)entity.y); + g.setColor(yAxisColor); + 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)); + } } } diff --git a/src/diveengine2d/SceneManager.java b/src/diveengine2d/SceneManager.java index 248b883..f0272c6 100644 --- a/src/diveengine2d/SceneManager.java +++ b/src/diveengine2d/SceneManager.java @@ -188,6 +188,7 @@ public class SceneManager { for(DiveScript script : e.components) { script.render(g); } + e.render(g); } } @@ -198,4 +199,8 @@ public class SceneManager { } } } + + public static int entityCount() { + return entities.size(); + } } diff --git a/src/diveengine2d/Time.java b/src/diveengine2d/Time.java index 27132ec..4dfabe1 100644 --- a/src/diveengine2d/Time.java +++ b/src/diveengine2d/Time.java @@ -9,5 +9,6 @@ public class Time { public static double timeScale = 1; public static double deltaTime = 0; public static long nextSecond = System.currentTimeMillis() + 1000, startTime = 0; + public static double timedFramesCurrent; } diff --git a/src/diveengine2d/Vector2.java b/src/diveengine2d/Vector2.java new file mode 100644 index 0000000..7dd18f7 --- /dev/null +++ b/src/diveengine2d/Vector2.java @@ -0,0 +1,21 @@ +package diveengine2d; + +public class Vector2 { + public float x, y; + public Vector2(float x, float y) { + this.x = x; + this.y = y; + } + public Vector2 subtract(Vector2 b) { + Vector2 _return = new Vector2(x, y); + _return.x -= b.x; + _return.y -= b.y; + return _return; + } + + public Vector2 rectToPolar(Vector2 b) { + Vector2 _return = new Vector2(x, y); + + return _return; + } +} diff --git a/src/diveengine2d/Vector3.java b/src/diveengine2d/Vector3.java new file mode 100644 index 0000000..8b1e774 --- /dev/null +++ b/src/diveengine2d/Vector3.java @@ -0,0 +1,10 @@ +package diveengine2d; + +public class Vector3 { + public float x, y, z; + public Vector3(float x, float y, float z) { + this.x = x; + this.y = y; + this.z = z; + } +}