Advertisement
Guest User

Untitled

a guest
Feb 25th, 2018
53
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.65 KB | None | 0 0
  1. package gg.appletgame;
  2.  
  3. import java.awt.BorderLayout;
  4. import java.awt.Canvas;
  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. public class Engine extends Canvas implements Runnable {
  13.    
  14.     private static final long serialVersionUID = 1L;
  15.    
  16.     public static final int WIDTH = 160;
  17.     public static final int HEIGHT = WIDTH/12*9;
  18.     public static final int SCALE = 3;
  19.     public static final String Name ="Game";
  20.    
  21.     private JFrame frame;
  22.     public boolean running= false;
  23.     public int tickCount = 0;
  24.    
  25.     private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_BGR);
  26.     private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  27.    
  28.     public Engine() {
  29.         setMinimumSize(new Dimension(WIDTH* SCALE,HEIGHT* SCALE));
  30.         setMaximumSize(new Dimension(WIDTH* SCALE,HEIGHT* SCALE));
  31.         setPreferredSize(new Dimension(WIDTH* SCALE,HEIGHT* SCALE));
  32.         frame=new JFrame(Name);
  33.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  34.         frame.setLayout(new BorderLayout());
  35.         frame.add(this, BorderLayout.CENTER);
  36.         frame.pack();
  37.         frame.setResizable(true);
  38.         frame.setLocationRelativeTo(null);
  39.         frame.setVisible(true);
  40.     }
  41.    
  42.     public synchronized void start() {
  43.     new Thread(this).start();
  44.     running = true;
  45.     }
  46.    
  47.     public synchronized void stop() {
  48.         running = false;
  49.     }
  50.  
  51.         public void run() {
  52.         long lastTime = System.nanoTime();
  53.         double nsPerTick = 1000000000D/60D;
  54.  
  55.         int frames = 0;
  56.         int ticks = 0;
  57.  
  58.         long lastTimer = System.currentTimeMillis();
  59.         double delta = 0;
  60.        
  61.         while(running) {
  62.             long now = System.nanoTime();
  63.             delta += (now - lastTime) / nsPerTick;
  64.             lastTime = now;
  65.             boolean shouldRender = true;
  66.             while(delta >= 1)
  67.             {
  68.                 ticks++;
  69.                 tick();
  70.                 delta -=1;
  71.                 shouldRender = true;
  72.             }
  73.            
  74.             if (shouldRender)
  75.             {
  76.                 frames++;
  77.                 render();
  78.             }
  79.            
  80.             if(System.currentTimeMillis() - lastTimer >= 1000) {
  81.                 lastTimer += 1000;
  82.                 System.out.println(ticks + "ticks, " + frames + " frames");
  83.                 frames = 0;
  84.                 ticks = 0;
  85.                 }
  86.             }
  87.         }
  88.         public void tick() {
  89.             tickCount++;
  90.            
  91.             for (int i = 0; i< pixels.length; i++) {
  92.                 pixels[i] = i + tickCount;
  93.             }
  94.         }
  95.     public void render() {
  96.         BufferStrategy bs = getBufferStrategy();
  97.         if (bs == null) {
  98.             createBufferStrategy(3);
  99.             return;
  100.         }
  101.         Graphics g = bs.getDrawGraphics();
  102.        
  103.         g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  104.        
  105.        
  106.         g.dispose();
  107.         bs.show();
  108.     }
  109.     public static void main(String [] args) {
  110.         new Engine().start();
  111.     }  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement