Basic window and rendering thread.
Co-authored-by: sigonasr2 <sigonasr2@gmail.com>
This commit is contained in:
parent
7a3cd28039
commit
1175c900a3
93
src/sig/Panel.java
Normal file
93
src/sig/Panel.java
Normal file
@ -0,0 +1,93 @@
|
|||||||
|
package sig;
|
||||||
|
|
||||||
|
import java.awt.Graphics;
|
||||||
|
import javax.swing.JPanel;
|
||||||
|
import java.awt.image.ColorModel;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.GraphicsConfiguration;
|
||||||
|
import java.awt.Image;
|
||||||
|
import java.awt.image.MemoryImageSource;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
|
||||||
|
public class Panel extends JPanel implements Runnable {
|
||||||
|
long startTime = System.nanoTime();
|
||||||
|
long endTime = System.nanoTime();
|
||||||
|
private ColorModel cm;
|
||||||
|
private Thread thread;
|
||||||
|
public int width=SigKeeper.SCREEN_WIDTH;
|
||||||
|
public int height=SigKeeper.SCREEN_HEIGHT;
|
||||||
|
private Image imageBuffer;
|
||||||
|
private MemoryImageSource mImageProducer;
|
||||||
|
public int pixel[];
|
||||||
|
|
||||||
|
public Panel() {
|
||||||
|
super(true);
|
||||||
|
thread = new Thread(this, "Panel Thread");
|
||||||
|
}
|
||||||
|
|
||||||
|
protected static ColorModel getCompatibleColorModel(){
|
||||||
|
GraphicsConfiguration gfx_config = GraphicsEnvironment.
|
||||||
|
getLocalGraphicsEnvironment().getDefaultScreenDevice().
|
||||||
|
getDefaultConfiguration();
|
||||||
|
return gfx_config.getColorModel();
|
||||||
|
}
|
||||||
|
|
||||||
|
public void init(){
|
||||||
|
cm = getCompatibleColorModel();
|
||||||
|
width = getWidth();
|
||||||
|
height = getHeight();
|
||||||
|
SigKeeper.SCREEN_WIDTH=getWidth();
|
||||||
|
SigKeeper.SCREEN_HEIGHT=getHeight();
|
||||||
|
int screenSize = width * height;
|
||||||
|
if(pixel == null || pixel.length < screenSize){
|
||||||
|
pixel = new int[screenSize];
|
||||||
|
}
|
||||||
|
mImageProducer = new MemoryImageSource(width, height, cm, pixel,0, width);
|
||||||
|
mImageProducer.setAnimated(true);
|
||||||
|
mImageProducer.setFullBufferUpdates(true);
|
||||||
|
imageBuffer = Toolkit.getDefaultToolkit().createImage(mImageProducer);
|
||||||
|
if(thread.isInterrupted() || !thread.isAlive()){
|
||||||
|
thread.start();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
public /* abstract */ void render(){
|
||||||
|
int[] p = pixel; // this avoid crash when resizing
|
||||||
|
if(p.length != width * height) return;
|
||||||
|
//a=h/w
|
||||||
|
}
|
||||||
|
|
||||||
|
public void repaint() {
|
||||||
|
super.repaint();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void paintComponent(Graphics g) {
|
||||||
|
startTime = System.nanoTime();
|
||||||
|
super.paintComponent(g);
|
||||||
|
// perform draws on pixels
|
||||||
|
render();
|
||||||
|
// ask ImageProducer to update image
|
||||||
|
mImageProducer.newPixels();
|
||||||
|
// draw it on panel
|
||||||
|
g.drawImage(this.imageBuffer, 0, 0, this);
|
||||||
|
endTime=System.nanoTime();
|
||||||
|
SigKeeper.DRAWLOOPTIME=(endTime-startTime)/1000000f;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Overrides ImageObserver.imageUpdate.
|
||||||
|
* Always return true, assuming that imageBuffer is ready to go when called
|
||||||
|
*/
|
||||||
|
@Override
|
||||||
|
public boolean imageUpdate(Image image, int a, int b, int c, int d, int e) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
@Override
|
||||||
|
public void run() {
|
||||||
|
while (true) {
|
||||||
|
// request a JPanel re-drawing
|
||||||
|
repaint();
|
||||||
|
try {Thread.sleep(5);} catch (InterruptedException e) {}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
@ -1,7 +1,163 @@
|
|||||||
package sig;
|
package sig;
|
||||||
|
|
||||||
class SigKeeper{
|
import javax.swing.JFrame;
|
||||||
|
import java.awt.Cursor;
|
||||||
|
import java.awt.event.MouseWheelListener;
|
||||||
|
import java.awt.event.WindowEvent;
|
||||||
|
import java.awt.event.WindowFocusListener;
|
||||||
|
import java.awt.event.MouseEvent;
|
||||||
|
import java.awt.event.MouseListener;
|
||||||
|
import java.awt.event.MouseMotionListener;
|
||||||
|
import java.awt.event.MouseWheelEvent;
|
||||||
|
import java.awt.event.KeyEvent;
|
||||||
|
import java.awt.event.KeyListener;
|
||||||
|
import java.awt.BorderLayout;
|
||||||
|
import java.awt.image.BufferedImage;
|
||||||
|
import java.awt.GraphicsDevice;
|
||||||
|
import java.awt.GraphicsEnvironment;
|
||||||
|
import java.awt.Point;
|
||||||
|
import java.awt.Toolkit;
|
||||||
|
|
||||||
|
public class SigKeeper implements WindowFocusListener,KeyListener,MouseListener,MouseMotionListener,MouseWheelListener{
|
||||||
|
JFrame frame;
|
||||||
|
Panel panel;
|
||||||
|
public static int SCREEN_WIDTH=1280;
|
||||||
|
public static int SCREEN_HEIGHT=720;
|
||||||
|
public final static long TIMEPERTICK = 16666667l;
|
||||||
|
public static float DRAWTIME=0;
|
||||||
|
public static float DRAWLOOPTIME=0;
|
||||||
|
|
||||||
|
public static Cursor invisibleCursor;
|
||||||
|
|
||||||
|
SigKeeper() {
|
||||||
|
frame = new JFrame("SigKeeper");
|
||||||
|
panel = new Panel();
|
||||||
|
|
||||||
|
frame.getContentPane().addMouseListener(this);
|
||||||
|
frame.getContentPane().addMouseMotionListener(this);
|
||||||
|
frame.addKeyListener(this);
|
||||||
|
frame.getContentPane().addMouseWheelListener(this);
|
||||||
|
frame.setSize(SCREEN_WIDTH,SCREEN_HEIGHT);
|
||||||
|
frame.add(panel,BorderLayout.CENTER);
|
||||||
|
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
|
||||||
|
|
||||||
|
invisibleCursor = frame.getToolkit().createCustomCursor(new BufferedImage(1,1,BufferedImage.TYPE_INT_ARGB),new Point(),null);
|
||||||
|
|
||||||
|
panel.setCursor(invisibleCursor);
|
||||||
|
frame.setVisible(true);
|
||||||
|
GraphicsDevice screen = GraphicsEnvironment.getLocalGraphicsEnvironment().getDefaultScreenDevice();
|
||||||
|
frame.setLocation((screen.getDisplayMode().getWidth()-SCREEN_WIDTH)/2,(screen.getDisplayMode().getHeight()-SCREEN_HEIGHT)/2);
|
||||||
|
panel.init();
|
||||||
|
|
||||||
|
new Thread() {
|
||||||
|
public void run(){
|
||||||
|
while (true) {
|
||||||
|
long startTime = System.nanoTime();
|
||||||
|
runGameLoop();
|
||||||
|
panel.repaint();
|
||||||
|
Toolkit.getDefaultToolkit().sync();
|
||||||
|
long endTime = System.nanoTime();
|
||||||
|
long diff = endTime-startTime;
|
||||||
|
try {
|
||||||
|
long sleepTime = TIMEPERTICK - diff;
|
||||||
|
long millis = (sleepTime)/1000000;
|
||||||
|
int nanos = (int)(sleepTime-(((sleepTime)/1000000)*1000000));
|
||||||
|
//System.out.println("FRAME DRAWING: Sleeping for ("+millis+"ms,"+nanos+"ns) - "+(diff)+"ns");
|
||||||
|
DRAWTIME = (float)diff/1000000;
|
||||||
|
frame.setTitle("Game Loop: "+DRAWTIME+"ms, Draw Loop: "+DRAWLOOPTIME+"ms");
|
||||||
|
if (sleepTime>0) {
|
||||||
|
Thread.sleep(millis,nanos);
|
||||||
|
}
|
||||||
|
} catch (InterruptedException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}.start();
|
||||||
|
}
|
||||||
|
|
||||||
|
protected void runGameLoop() {
|
||||||
|
}
|
||||||
|
|
||||||
public static void main(String[] args) {
|
public static void main(String[] args) {
|
||||||
System.out.println("Hello World!");
|
SigKeeper keeper = new SigKeeper();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseWheelMoved(MouseWheelEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseDragged(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseMoved(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseClicked(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 mouseEntered(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void mouseExited(MouseEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyTyped(KeyEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyPressed(KeyEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void keyReleased(KeyEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void windowGainedFocus(WindowEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void windowLostFocus(WindowEvent e) {
|
||||||
|
// TODO Auto-generated method stub
|
||||||
|
|
||||||
}
|
}
|
||||||
}
|
}
|
Loading…
x
Reference in New Issue
Block a user