Advertisement
Guest User

Automa.java

a guest
Jun 8th, 2013
38
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.32 KB | None | 0 0
  1. package tk.sketchistgames.Automa;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Dimension;
  6. import java.awt.Graphics;
  7. import java.awt.image.BufferStrategy;
  8. import java.awt.image.BufferedImage;
  9. import java.awt.image.DataBufferInt;
  10.  
  11. import javax.swing.JFrame;
  12.  
  13. import tk.sketchistgames.graphics.Screen;
  14. import tk.sketchistgames.input.Keyboard;
  15. import tk.sketchistgames.level.Level;
  16. import tk.sketchistgames.level.RandomLevel;
  17.  
  18. public class Automa extends Canvas implements Runnable {
  19.  
  20. private static final long serialVersionUID = 2664234447792702880L;
  21. public static int width = 300;
  22. public static int height = width / 16 * 9; // 168.5
  23. public static int scale = 3;
  24.  
  25. private Thread GameThread;
  26. private JFrame frame = new JFrame();
  27. private Keyboard key;
  28. private Level level;
  29. private boolean running = false;
  30.  
  31. private Screen screen;
  32.  
  33. private BufferedImage image = new BufferedImage(width, height,
  34. BufferedImage.TYPE_INT_RGB);
  35. private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  36.  
  37. public Automa() {
  38. Dimension size = new Dimension(width * scale, height * scale);
  39. setPreferredSize(size);
  40. screen = new Screen(width, height);
  41. frame = new JFrame();
  42. key = new Keyboard();
  43. level = new RandomLevel(32, 32);
  44. addKeyListener(key);
  45.  
  46. }
  47.  
  48. public synchronized void start() {
  49. running = true;
  50. GameThread = new Thread(this, "Display");
  51. GameThread.start();
  52.  
  53. }
  54.  
  55. public synchronized void stop() {
  56. running = false;
  57. try {
  58. GameThread.join();
  59. } catch (InterruptedException e) {
  60. e.printStackTrace();
  61. }
  62. }
  63.  
  64. public void run() {
  65. long lastTime = System.nanoTime();
  66. long timer = System.currentTimeMillis();
  67. final double ns = 1000000000.0 / 60.0;
  68. double delta = 0;
  69. int frames = 0;
  70. int updates = 0;
  71. requestFocus();
  72.  
  73. while (running) {
  74. long now = System.nanoTime();
  75. delta += (now - lastTime) / ns;
  76. lastTime = now;
  77. while(delta >= 1){
  78. update();
  79. updates++;
  80. delta--;
  81. }
  82. this.render();
  83. frames++;
  84.  
  85. if(System.currentTimeMillis() - timer > 1000){
  86. timer += 1000;
  87. frame.setTitle("Automa | " + "Chunk Updates: " + updates + ", " + "FPS: " + frames);
  88. updates = 0;
  89. frames = 0;
  90. }
  91. }
  92. stop();
  93. }
  94. int x=0,y=0;
  95. public void update() {
  96. key.update();
  97. if(key.up) y++;
  98. if(key.down) y--;
  99. if(key.left) x++;
  100. if(key.right) x--;
  101.  
  102. }
  103.  
  104.  
  105. public void render() {
  106. BufferStrategy bs = getBufferStrategy();
  107. if (bs == null) {
  108. createBufferStrategy(3);
  109. return;
  110. }
  111. screen.clear();
  112. level.render(x, y, screen);
  113.  
  114. for(int i = 0; i < pixels.length; i++){
  115. pixels[i] = screen.pixels[i];
  116.  
  117. }
  118.  
  119. Graphics g = bs.getDrawGraphics();
  120. /*
  121. * Graphics go here before g.dispose
  122. */
  123. g.setColor(Color.WHITE);
  124. g.fillRect(0, 0, getWidth(), getHeight());
  125. g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  126. g.dispose();
  127. bs.show();
  128. }
  129.  
  130. public static void main(String[] args) {
  131. Automa game = new Automa();
  132. game.frame.setResizable(false);
  133. game.frame.setTitle("Automa"); //TODO COME UP WITH BETTER NAME
  134. game.frame.add(game);
  135. game.frame.pack();
  136. game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  137. game.frame.setLocationRelativeTo(null);
  138. game.frame.setVisible(true);
  139.  
  140. game.start();
  141. }
  142. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement