Created
April 18, 2015 18:45
-
-
Save codepainkiller/a55d744a77448f1adaa9 to your computer and use it in GitHub Desktop.
Game Loop - Java 2D
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| package me.martincruz.main; | |
| import java.awt.BorderLayout; | |
| import java.awt.Canvas; | |
| import java.awt.Dimension; | |
| import java.awt.Graphics; | |
| import java.util.logging.Level; | |
| import java.util.logging.Logger; | |
| import javax.swing.JFrame; | |
| /** | |
| * | |
| * @author Martin Cruz | |
| */ | |
| public class Game extends Canvas implements Runnable { | |
| public static final int WIDTH = 160; | |
| public static final int HEIGHT = WIDTH / 12 * 9; | |
| public static final int SCALE = 3; | |
| public static final String NAME = "Game 2D"; | |
| private static final long serialVersionUID = 1L; | |
| private JFrame frame; | |
| public boolean running = false; | |
| public int tickCount = 0; | |
| public Game() { | |
| setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE)); | |
| setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE)); | |
| setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE)); | |
| frame = new JFrame(NAME); | |
| frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
| frame.setLayout(new BorderLayout()); | |
| frame.add(this, BorderLayout.CENTER); | |
| frame.pack(); | |
| frame.setResizable(false); | |
| frame.setLocationRelativeTo(null); | |
| frame.setVisible(true); | |
| } | |
| public synchronized void start() { | |
| new Thread(this).start(); | |
| running = true; | |
| } | |
| public synchronized void stop() { | |
| running = false; | |
| } | |
| @Override | |
| public void run() { | |
| long lastTime = System.nanoTime(); | |
| double nsPerTick = 1000000000D / 64; | |
| int ticks = 0; | |
| int frames = 0; | |
| long lastTimer = System.currentTimeMillis(); | |
| double delta = 0; | |
| while (running) { | |
| long now = System.nanoTime(); | |
| delta += (now - lastTime) / nsPerTick; | |
| lastTime = now; | |
| boolean shouldRender = true; | |
| while (delta >= 1) { | |
| ticks++; | |
| tick(); | |
| delta -= 1; | |
| shouldRender = true; | |
| } | |
| try { | |
| Thread.sleep(2); | |
| } catch (InterruptedException ex) { | |
| ex.printStackTrace(); | |
| } | |
| if (shouldRender) { | |
| frames++; | |
| render(); | |
| } | |
| if (System.currentTimeMillis() - lastTimer >= 1000) { | |
| lastTimer += 1000; | |
| System.out.println(ticks + " ticks, " + frames + " frames"); | |
| frames = 0; | |
| ticks = 0; | |
| } | |
| } | |
| } | |
| public void tick() { | |
| tickCount++; | |
| } | |
| public void render() { | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
entendi nada