Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package Main;
- import java.awt.Canvas;
- import java.awt.Dimension;
- import java.awt.Graphics;
- import java.awt.Graphics2D;
- import java.awt.image.BufferedImage;
- import java.awt.image.DataBufferInt;
- import javax.swing.JFrame;
- import GameState.GameStateManager;
- import Graphics.Screen;
- import Level.Tile.Tile;
- @SuppressWarnings("serial") public class Game extends Canvas implements Runnable{
- public static final int WIDTH = 320;
- public static final int HEIGHT = 240;
- public static final int SCALE = 2;
- public static String TITLE = "Game";
- private Thread thread;
- private JFrame frame;
- public static Screen screen;
- private boolean isRunning = false;
- private Graphics2D g;
- public static GameStateManager gsm;
- private static BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
- public static int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
- public Game() {
- Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
- setPreferredSize(size);
- frame = new JFrame();
- screen = new Screen(WIDTH, HEIGHT);
- addKeyListener(new Listener());
- }
- public synchronized void start() {
- isRunning = true;
- thread = new Thread(this, "Display");
- thread.start();
- }
- public synchronized void stop() {
- isRunning = false;
- try {
- thread.join();
- } catch (InterruptedException e) {
- e.printStackTrace();
- }
- }
- private void init() {
- g = (Graphics2D) image.getGraphics();
- gsm = new GameStateManager();
- }
- @Override
- public void run() {
- init();
- long lastTime = System.nanoTime();
- long timer = System.currentTimeMillis();
- final double ns = 1000000000.0 / 60.0;
- double delta = 0;
- int frames = 0;
- int updates = 0;
- requestFocus();
- while (isRunning) {
- long now = System.nanoTime();
- delta += (now - lastTime) / ns;
- lastTime = now;
- while (delta >= 1) {
- update();
- updates++;
- delta--;
- }
- draw();
- drawToScreen();
- frames++;
- // one second || 1000 ns = 1 sec
- if (System.currentTimeMillis() - timer > 1000) {
- timer += 1000;
- // System.out.println(updates + " ups, " + frames + " fps");
- frame.setTitle(TITLE + " | " + updates + " ups, " + frames + " fps");
- frames = 0;
- updates = 0;
- }
- }
- stop();
- }
- public void update() {
- gsm.update();
- }
- private void draw() {
- gsm.draw(g);
- screen.clear();
- test();
- }
- public void test() {
- }
- private void drawToScreen() {
- Graphics g2 = getGraphics();
- g2.drawImage(image, 0, 0, WIDTH * SCALE , HEIGHT * SCALE , null);
- g2.dispose();
- }
- public static void main(String[] args) {
- Game game = new Game();
- game.frame.setResizable(false);
- game.frame.setTitle(TITLE);
- game.frame.add(game);
- game.frame.pack();
- game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
- game.frame.setLocationRelativeTo(null);
- game.frame.setVisible(true);
- game.start();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement