generated from sigonasr2/JavaProjectTemplate
Render loop now paints properly
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
010ac996e6
commit
ca082c3f2c
@ -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) {
|
||||
|
@ -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 + "]";
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
|
@ -51,6 +51,9 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
public HashMap<Integer,Boolean> 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<Math.PI*2;theta+=((Math.PI*2)/CIRCLE_PRECISION)) {
|
||||
@ -212,11 +242,11 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
//System.out.println("X:"+(Math.sin(theta)*r+center_x)+" Y:"+(Math.cos(theta)*r+center_y));
|
||||
points[counter++]=new Point((int)(Math.round(Math.sin(theta)*r+center_x)),(int)(Math.round(Math.cos(theta)*r+center_y)));
|
||||
}
|
||||
FillPolygon(p,col,0,0,points);
|
||||
FillPolygon(col,0,0,points);
|
||||
}
|
||||
|
||||
|
||||
public void FillOval(byte[] p,byte col,double center_x,double center_y,double w,double h) {
|
||||
public void FillOval(Color col,double center_x,double center_y,double w,double h) {
|
||||
int counter=0;
|
||||
Point[] points = new Point[CIRCLE_PRECISION];
|
||||
double r = Math.max(w,h);
|
||||
@ -234,10 +264,10 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
newP.y+=center_y;
|
||||
points[counter++]=newP;
|
||||
}
|
||||
FillPolygon(p,col,0,0,points);
|
||||
FillPolygon(col,0,0,points);
|
||||
}
|
||||
|
||||
public void FillPolygon(byte[] p,byte col,double x_offset,double y_offset,Point...points) {
|
||||
public void FillPolygon(Color col,double x_offset,double y_offset,Point...points) {
|
||||
Edge[] edges = new Edge[points.length];
|
||||
List<Edge> edges_sorted = new ArrayList<Edge>();
|
||||
for (int i=0;i<points.length;i++) {
|
||||
@ -272,8 +302,8 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
//System.out.println("Drawing from "+((int)Math.round(e1.x_of_min_y))+" to "+e2.x_of_min_y+" on line "+scanLine);
|
||||
for (int x=(int)Math.round(e1.x_of_min_y);x<=e2.x_of_min_y;x++) {
|
||||
int index = (scanLine+(int)y_offset)*getWidth()+x+(int)x_offset;
|
||||
if (index<p.length&&index>=0) {
|
||||
Draw(p,index,col);
|
||||
if (index<pixel.length&&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 (f!=null) {
|
||||
Graphics2D g2 = (Graphics2D)f.getGraphics();
|
||||
if (g2!=null) {
|
||||
g2.setRenderingHints(RENDERHINTS);
|
||||
try {
|
||||
paintComponent(g2);
|
||||
repaint();
|
||||
} finally {
|
||||
g2.dispose();
|
||||
}
|
||||
} else {
|
||||
System.out.println("Invalid frame!");
|
||||
}
|
||||
}
|
||||
} while (f.getBufferStrategy().contentsRestored());
|
||||
f.getBufferStrategy().show();
|
||||
Toolkit.getDefaultToolkit().sync();
|
||||
} while (f.getBufferStrategy().contentsLost());
|
||||
} 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<xe) {
|
||||
x=x+1;
|
||||
if (px<0) {
|
||||
@ -462,7 +489,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
}
|
||||
px=px+2*(dy1-dx1);
|
||||
}
|
||||
Draw(canvas,y*JavaProjectTemplate.WINDOW_WIDTH+x,col);
|
||||
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
||||
}
|
||||
} else {
|
||||
if (dy>=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<ye) {
|
||||
y=y+1;
|
||||
if (py<=0) {
|
||||
@ -483,17 +510,17 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
}
|
||||
py=py+2*(dx1-dy1);
|
||||
}
|
||||
Draw(canvas,y*JavaProjectTemplate.WINDOW_WIDTH+x,col);
|
||||
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Fill_Rect(byte[] p,byte col,double x,double y,double w,double h) {
|
||||
public void Fill_Rect(Color col,double x,double y,double w,double h) {
|
||||
for (int xx=0;xx<w;xx++) {
|
||||
for (int yy=0;yy<h;yy++) {
|
||||
if (x+xx>=0&&y+yy>=0&&x+xx<JavaProjectTemplate.WINDOW_WIDTH&&y+yy<JavaProjectTemplate.WINDOW_HEIGHT) {
|
||||
int index = ((int)y+yy)*JavaProjectTemplate.WINDOW_WIDTH+(int)x+xx;
|
||||
Draw(p,index,col);
|
||||
Draw(index,col.getColor());
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -550,7 +577,7 @@ public class Panel extends JPanel implements Runnable,KeyListener {
|
||||
if (((sprite.getImg().getRGB(X,Y)>>>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<JavaProjectTemplate.WINDOW_HEIGHT;y++) {
|
||||
for (int x=0;x<JavaProjectTemplate.WINDOW_WIDTH;x++) {
|
||||
Draw(pixel,y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
||||
Draw(y*JavaProjectTemplate.WINDOW_WIDTH+x,col.getColor());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
public void Draw(int[] canvas,int index, int col) {
|
||||
canvas[index]=col;
|
||||
}
|
||||
}
|
Loading…
x
Reference in New Issue
Block a user