Advertisement
Guest User

Untitled

a guest
Aug 30th, 2014
253
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.02 KB | None | 0 0
  1. package Main;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Dimension;
  5. import java.awt.Graphics;
  6. import java.awt.Graphics2D;
  7. import java.awt.image.BufferedImage;
  8. import java.awt.image.DataBufferInt;
  9.  
  10. import javax.swing.JFrame;
  11.  
  12. import GameState.GameStateManager;
  13. import Graphics.Screen;
  14. import Level.Tile.Tile;
  15.  
  16. @SuppressWarnings("serial") public class Game extends Canvas implements Runnable{
  17.  
  18. public static final int WIDTH = 320;
  19. public static final int HEIGHT = 240;
  20. public static final int SCALE = 2;
  21. public static String TITLE = "Game";
  22.  
  23. private Thread thread;
  24. private JFrame frame;
  25. public static Screen screen;
  26. private boolean isRunning = false;
  27.  
  28.  
  29. private Graphics2D g;
  30.  
  31. public static GameStateManager gsm;
  32.  
  33. private static BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  34. public static int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  35.  
  36. public Game() {
  37. Dimension size = new Dimension(WIDTH * SCALE, HEIGHT * SCALE);
  38. setPreferredSize(size);
  39.  
  40. frame = new JFrame();
  41. screen = new Screen(WIDTH, HEIGHT);
  42.  
  43. addKeyListener(new Listener());
  44. }
  45.  
  46. public synchronized void start() {
  47. isRunning = true;
  48. thread = new Thread(this, "Display");
  49. thread.start();
  50. }
  51.  
  52. public synchronized void stop() {
  53. isRunning = false;
  54. try {
  55. thread.join();
  56. } catch (InterruptedException e) {
  57. e.printStackTrace();
  58. }
  59. }
  60.  
  61. private void init() {
  62.  
  63. g = (Graphics2D) image.getGraphics();
  64.  
  65. gsm = new GameStateManager();
  66. }
  67.  
  68. @Override
  69. public void run() {
  70.  
  71. init();
  72.  
  73. long lastTime = System.nanoTime();
  74. long timer = System.currentTimeMillis();
  75. final double ns = 1000000000.0 / 60.0;
  76. double delta = 0;
  77. int frames = 0;
  78. int updates = 0;
  79.  
  80. requestFocus();
  81.  
  82. while (isRunning) {
  83. long now = System.nanoTime();
  84. delta += (now - lastTime) / ns;
  85. lastTime = now;
  86.  
  87. while (delta >= 1) {
  88. update();
  89. updates++;
  90. delta--;
  91. }
  92.  
  93. draw();
  94. drawToScreen();
  95. frames++;
  96.  
  97. // one second || 1000 ns = 1 sec
  98.  
  99. if (System.currentTimeMillis() - timer > 1000) {
  100. timer += 1000;
  101. // System.out.println(updates + " ups, " + frames + " fps");
  102. frame.setTitle(TITLE + " | " + updates + " ups, " + frames + " fps");
  103. frames = 0;
  104. updates = 0;
  105. }
  106. }
  107. stop();
  108. }
  109.  
  110. public void update() {
  111. gsm.update();
  112. }
  113.  
  114. private void draw() {
  115. gsm.draw(g);
  116.  
  117. screen.clear();
  118.  
  119.  
  120. test();
  121. }
  122.  
  123. public void test() {
  124.  
  125. }
  126.  
  127. private void drawToScreen() {
  128. Graphics g2 = getGraphics();
  129. g2.drawImage(image, 0, 0, WIDTH * SCALE , HEIGHT * SCALE , null);
  130. g2.dispose();
  131. }
  132.  
  133. public static void main(String[] args) {
  134. Game game = new Game();
  135. game.frame.setResizable(false);
  136. game.frame.setTitle(TITLE);
  137. game.frame.add(game);
  138. game.frame.pack();
  139. game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  140. game.frame.setLocationRelativeTo(null);
  141. game.frame.setVisible(true);
  142.  
  143. game.start();
  144. }
  145.  
  146. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement