Isolate the draw loop

Co-authored-by: r3cp3ct <45179536+r3cp3ct@users.noreply.github.com>
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
main
sigonasr2 3 years ago
parent cedd064da6
commit 121dc6acb6
  1. 28
      src/sig/DrawLoop.java
  2. 18
      src/sig/engine/Panel.java

@ -0,0 +1,28 @@
package sig;
import sig.engine.Panel;
import sig.engine.Sprite;
public class DrawLoop {
public static Panel panel;
public static void drawGame(Panel panel) {
DrawLoop.panel=panel;
int[] p = panel.pixel;
for (int y=0;y<panel.getHeight();y++) {
for (int x=0;x<panel.getWidth();x++) {
p[y*panel.getWidth()+x]=(0<<16)+(0<<8)+0;//RGB
}
}
Draw_Sprite(panel.nanaX,panel.nanaY,Sprite.NANA);
}
public static void Draw_Sprite(double x, double y, Sprite sprite){
int[] p = panel.pixel;
for(int X=0;X<sprite.getHeight();X++){
for(int Y=0;Y<sprite.getWidth();Y++){
p[(Y+(int)y)*panel.getWidth()+X+(int)x] = sprite.getBi_array()[Y*sprite.getWidth()+X];
}
}
}
}

@ -19,6 +19,7 @@ import javax.swing.JPanel;
import javax.swing.event.MouseInputListener; import javax.swing.event.MouseInputListener;
import java.awt.event.KeyListener; import java.awt.event.KeyListener;
import sig.DrawLoop;
import sig.RabiClone; import sig.RabiClone;
public class Panel extends JPanel implements Runnable,ComponentListener,KeyListener { public class Panel extends JPanel implements Runnable,ComponentListener,KeyListener {
@ -158,13 +159,7 @@ public class Panel extends JPanel implements Runnable,ComponentListener,KeyListe
resizeUpdate(); resizeUpdate();
int[] p = pixel; // this avoid crash when resizing int[] p = pixel; // this avoid crash when resizing
//a=h/w //a=h/w
DrawLoop.drawGame(this);
for (int y=0;y<getHeight();y++) {
for (int x=0;x<getWidth();x++) {
p[y*getWidth()+x]=(0<<16)+(0<<8)+0;//RGB
}
}
Draw_Sprite(nanaX,nanaY,Sprite.NANA);
} }
private void resizeUpdate() { private void resizeUpdate() {
@ -344,15 +339,6 @@ public class Panel extends JPanel implements Runnable,ComponentListener,KeyListe
} }
} }
public void Draw_Sprite(double x, double y, Sprite sprite){
int[] p = pixel;
for(int X=0;X<sprite.height;X++){
for(int Y=0;Y<sprite.width;Y++){
p[(Y+(int)y)*getWidth()+X+(int)x] = sprite.bi_array[Y*sprite.width+X];
}
}
}
@Override @Override
public void run() { public void run() {
while (true) { while (true) {

Loading…
Cancel
Save