Since many people still ask for how to do a small game loop I thought I would start a thread to compare how different people are doing there game loop.
Here is the source for minimal game loop. If any one has a better method feel free to post improvments.
This gives a jar size no manifest no extra compression of
jar -cvMf M.jar M.class
adding: M.class(in = 1084) (out= 690)(deflated 36%)
import java.awt.Graphics2D;
import java.awt.Color;
import java.awt.event.KeyEvent;
import java.awt.Event;
import java.awt.image.BufferStrategy;
import javax.swing.JFrame;
public class M extends JFrame
{
boolean[] k = new boolean[256];
public static void main(String args[])
{
new M();
}
public M()
{
// super("Title4k"); // optional
setSize(26*28, 20*28);
setResizable(false);
show(); // depricated but less bytes than setVisible(true);
createBufferStrategy(2);
BufferStrategy b = getBufferStrategy();
do
{
Graphics2D g = (Graphics2D)b.getDrawGraphics();
g.setColor(new Color(0xFF000000));
g.fillRect(0,0,26*28,20*28);
// do game here
b.show();
g.dispose();
try
{
Thread.sleep(10);
}catch(Exception e){}
} while(!k[KeyEvent.VK_ESCAPE] && isVisible());
System.exit(0);
}
public void processKeyEvent(KeyEvent e)
{
// & with 0xFF to avoid any overflow of k[]
k[e.getKeyCode()&0xFF] = e.getID() == 401;
}
}