File tree Expand file tree Collapse file tree 4 files changed +13
-18
lines changed
out/artifacts/JavaGameEngine_jar Expand file tree Collapse file tree 4 files changed +13
-18
lines changed Original file line number Diff line number Diff line change @@ -23,6 +23,8 @@ public class GameWorld extends JPanel{
2323 */
2424 public static LinkedList <Component > layerList = new LinkedList <>();
2525 public static String fps = "0" ;
26+ private long last ;
27+
2628 public GameWorld () {
2729 /*
2830 Key keyboard inputs
@@ -76,16 +78,21 @@ public void mouseMoved(MouseEvent e) {
7678 a .start ();
7779 }
7880 }
79-
81+ float fpsecund = 0 ;
8082
8183 /**
8284 * Here is the main drawing function
8385 * */
8486 @ Override
8587 protected void paintComponent (Graphics g ) {
8688 super .paintComponent (g );
87-
8889 drawComponents (g );
90+ if (System .nanoTime ()-last >1000000000 ){
91+ fps = Float .toString (fpsecund );
92+ fpsecund = 0 ;
93+ last = System .nanoTime ();
94+ }
95+ fpsecund +=1 ;
8996 }
9097 private void drawUi (Graphics g ){
9198
Original file line number Diff line number Diff line change @@ -49,6 +49,8 @@ public static void start(JFrame frame) {
4949 public static float deltaTime = 0f ;
5050
5151 private static int fps = 0 ;
52+ static float last =0 ;
53+
5254
5355 private static void startGame (){
5456 frame .setVisible (true );
@@ -73,17 +75,8 @@ public void run() {
7375 // Debug.log(totalElapsed);
7476 GAMEWORLD .repaint ();
7577 Toolkit .getDefaultToolkit ().sync (); // so it does not lag on linux
76- float current = System .nanoTime ();
77- deltaTime = current - previous ;
78- previous = current ;
79- totalElapsed += deltaTime ;
80-
81- if ((totalElapsed /1000000000 )>1 ){
82- GameWorld .fps = String .valueOf (fps );
83- totalElapsed = 0 ;
84- fps = 0 ;
85- }
86- fps ++;
78+ //System.out.println(System.nanoTime()-last);
79+
8780 }
8881 }, DELAY ,DELAY );
8982
Original file line number Diff line number Diff line change @@ -33,11 +33,6 @@ public static void main(String[] args){
3333
3434 ComponentHandler .addObject (new T ());
3535
36- for (int i = 0 ;i <10 ;i ++){
37- Coin coin = new Coin ();
38- coin .setPosition (new Vector2 (i *100 ,420 ));
39- ComponentHandler .addObject (coin );
40- }
4136
4237
4338 start ();
You can’t perform that action at this time.
0 commit comments