diff --git a/src/sig/JavaProjectTemplate.java b/src/sig/JavaProjectTemplate.java index 23f3e52..94d2287 100644 --- a/src/sig/JavaProjectTemplate.java +++ b/src/sig/JavaProjectTemplate.java @@ -1,10 +1,7 @@ package sig; -import javax.swing.JFrame; - import sig.engine.Color; import sig.engine.Panel; -import java.awt.RenderingHints; public class JavaProjectTemplate { public static final String PROGRAM_NAME="Sig's Java Project Template"; @@ -12,10 +9,14 @@ public class JavaProjectTemplate { public static int WINDOW_HEIGHT=720; public static Panel game; + public static void updateGame() { + } + public static void drawGame() { game.Clear(Color.BRIGHT_BLUE); - - game.Draw_Line(null, WINDOW_HEIGHT, WINDOW_HEIGHT, WINDOW_WIDTH, WINDOW_HEIGHT, 0); + game.Draw(100,20,Color.BLACK); + game.Draw_Line(10,10,35,35,Color.BLACK); + game.FillCircle(Color.MAGENTA,200,200,50); } public static void main(String[] args) { diff --git a/src/sig/engine/Color.java b/src/sig/engine/Color.java index 3a6bde7..4512024 100644 --- a/src/sig/engine/Color.java +++ b/src/sig/engine/Color.java @@ -35,4 +35,11 @@ public class Color { public int getColor() { return (a<<24)+(r<<16)+(g<<8)+b; } + + @Override + public java.lang.String toString() { + return "Color [r=" + r + ", g=" + g + ", b=" + b + ", a=" + a + "]"; + } + + } diff --git a/src/sig/engine/Panel.java b/src/sig/engine/Panel.java index 758fc8c..0c71ef6 100644 --- a/src/sig/engine/Panel.java +++ b/src/sig/engine/Panel.java @@ -51,6 +51,9 @@ public class Panel extends JPanel implements Runnable,KeyListener { public HashMap MOUSE = new HashMap<>(); private Point mousePosition = new Point(0,0); private MouseScrollValue scrollWheel=null; + public static final int UPDATE_LOOP_FRAMERATE = 244; + public static final long UPDATE_LOOP_NANOTIME = (long)((1d/UPDATE_LOOP_FRAMERATE)*1000000000l); + public static final double UPDATE_MULT = 1d / UPDATE_LOOP_FRAMERATE; public static RenderingHints RENDERHINTS = new RenderingHints(RenderingHints.KEY_ANTIALIASING,RenderingHints.VALUE_ANTIALIAS_OFF); @@ -85,9 +88,37 @@ public class Panel extends JPanel implements Runnable,KeyListener { (int) ((Toolkit.getDefaultToolkit().getScreenSize().getHeight() - f.getHeight()) / 2)); f.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); f.setVisible(true); - f.createBufferStrategy(2); p.render(); + + long lastGameTime = System.nanoTime(); + long dt = 0; + while (true) { + dt += System.nanoTime() - lastGameTime; + lastGameTime = System.nanoTime(); + while (dt >= UPDATE_LOOP_NANOTIME) { + JavaProjectTemplate.updateGame(); + dt -= UPDATE_LOOP_NANOTIME; + TIME += UPDATE_LOOP_NANOTIME; + } + gameUpdateLoopStabilizer(dt); //This is hackish. Removing this slows down the game by about 30%. The timer runs slower. ??? + } + } + + private static void gameUpdateLoopStabilizer(long dt) { + if (dt < UPDATE_LOOP_NANOTIME) { + lastReportedTime = System.currentTimeMillis(); + } else { + if (System.currentTimeMillis() - lastReportedTime > 5000) { + System.out.println("WARNING! Game is lagging behind! Frames Behind: " + (dt / UPDATE_LOOP_NANOTIME)); + lastReportedTime = System.currentTimeMillis(); + } + } + try { + Thread.sleep(4); + } catch (InterruptedException e) { + e.printStackTrace(); + } } public Panel(JFrame f) { @@ -200,11 +231,10 @@ public class Panel extends JPanel implements Runnable,KeyListener { public /* abstract */ void render(){ - //a=h/w JavaProjectTemplate.drawGame(); } - public void FillCircle(byte[] p,byte col,double center_x,double center_y,double r) { + public void FillCircle(Color col,double center_x,double center_y,double r) { int counter=0; Point[] points = new Point[CIRCLE_PRECISION]; for (double theta=0;theta edges_sorted = new ArrayList(); for (int i=0;i=0) { - Draw(p,index,col); + if (index=0) { + Draw(index,col.getColor()); } } } @@ -335,36 +365,34 @@ public class Panel extends JPanel implements Runnable,KeyListener { } } - public void Draw(int index, byte col) { + public void Draw(int x, int y, Color col) { + Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor()); + } + + void Draw(int index, int col) { pixel[index]=col; } @Override public void run() { - if (mImageProducer==null) return; while (true) { // request a JPanel re-drawing - //repaint(); render(); mImageProducer.newPixels(); - if (f!=null&&f.getBufferStrategy()!=null) { - do { - do { - if (f.getBufferStrategy()!=null) { - Graphics2D g2 = (Graphics2D)f.getBufferStrategy().getDrawGraphics(); - g2.setRenderingHints(RENDERHINTS); - if (g2!=null) { - try { - paintComponent(g2); - } finally { - g2.dispose(); - } - } - } - } while (f.getBufferStrategy().contentsRestored()); - f.getBufferStrategy().show(); - Toolkit.getDefaultToolkit().sync(); - } while (f.getBufferStrategy().contentsLost()); + if (f!=null) { + Graphics2D g2 = (Graphics2D)f.getGraphics(); + if (g2!=null) { + g2.setRenderingHints(RENDERHINTS); + try { + repaint(); + } finally { + g2.dispose(); + } + } else { + System.out.println("Invalid frame!"); + } + } else { + System.out.println("Invalid frame!"); } updateFPSCounter(); //System.out.println("Repaint "+frameCount++); @@ -438,7 +466,7 @@ public class Panel extends JPanel implements Runnable,KeyListener { } } - public void Draw_Line(byte[] canvas,int x1,int y1,int x2,int y2,byte col) { + public void Draw_Line(int x1,int y1,int x2,int y2,Color col) { int x,y,dx,dy,dx1,dy1,px,py,xe,ye; dx=x2-x1;dy=y2-y1; dx1=Math.abs(dx);dy1=Math.abs(dy); @@ -449,7 +477,6 @@ public class Panel extends JPanel implements Runnable,KeyListener { } else { x=x2-1;y=y2-1;xe=x1; } - Draw(canvas,y*JavaProjectTemplate.WINDOW_WIDTH+x,col); while (x=0) { @@ -470,7 +497,7 @@ public class Panel extends JPanel implements Runnable,KeyListener { } else { x=x2-1;y=y2-1;ye=y1; } - Draw(canvas,y*JavaProjectTemplate.WINDOW_WIDTH+x,col); + Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor()); while (y=0&&y+yy>=0&&x+xx>>24)&0xFF)==0||index<0||index>=pixel.length) { continue; } else { - Draw(pixel,index,(col==Color.WHITE)?sprite.getImg().getRGB(X,Y):col.getColor()); + Draw(index,(col==Color.WHITE)?sprite.getImg().getRGB(X,Y):col.getColor()); } } } @@ -560,12 +587,8 @@ public class Panel extends JPanel implements Runnable,KeyListener { public void Clear(Color col){ for (int y=0;y