Advertisement
Guest User

Untitled

a guest
Nov 19th, 2012
459
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.34 KB | None | 0 0
  1. package net.anyspieceofinter.unnamedgameproject;
  2.  
  3. import java.awt.Canvas;
  4. import java.awt.Color;
  5. import java.awt.Graphics;
  6. import java.awt.Dimension;
  7. import javax.swing.JFrame;
  8. import java.awt.image.BufferedImage;
  9. import java.awt.image.DataBufferInt;
  10. import java.awt.image.BufferStrategy;
  11. import net.anyspieceofinter.unnamedgameproject.input.Keyboard;
  12. import net.anyspieceofinter.unnamedgameproject.graphics.Screen;
  13.  
  14. public class Game extends Canvas implements Runnable {
  15.    
  16.     public static int aspectratioWidth = 7;
  17.     public static int aspectratioHeight = 5;
  18.     public static int scale = 2;
  19.     public static int width = 280;
  20.     public static int height = width / aspectratioWidth * aspectratioHeight;
  21.    
  22.     //0 - 29,99 | 30,000 pixels
  23.    
  24.     public static int bufferLayers = 3;
  25.     private JFrame frame;
  26.    
  27.     private Keyboard key;
  28.    
  29.     private boolean running = false;
  30.    
  31.     private Screen screen;
  32.    
  33.     private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  34.     private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  35.    
  36.     public Game() {
  37.        
  38.         Dimension size = new Dimension(width*scale, height*scale);
  39.         setPreferredSize(size);
  40.        
  41.         screen = new Screen(width, height);
  42.         frame = new JFrame();
  43.         key = new Keyboard();
  44.        
  45.         addKeyListener(key);
  46.  
  47.        
  48.     }
  49.    
  50.     private Thread thread;
  51.     public synchronized void start() {
  52.        
  53.         running = true;
  54.        
  55.         thread = new Thread(this, "Game");
  56.         thread.start();
  57.        
  58.     }
  59.    
  60.     public synchronized void stop() {
  61.        
  62.         running = false;
  63.        
  64.         try {
  65.            
  66.             thread.join();
  67.        
  68.         } catch (InterruptedException e) {
  69.            
  70.             e.printStackTrace();
  71.            
  72.         }
  73.        
  74.     }
  75.    
  76.     public void run() {
  77.        
  78.         long lastTime = System.nanoTime();
  79.         long timer = System.currentTimeMillis();
  80.         final double nanoSeconds = 1000000000.0 / 30.0;
  81.         double delta = 0;
  82.        
  83.         int updates = 0;
  84.         int frames = 0;
  85.        
  86.        
  87.         while(running) {
  88.            
  89.             long now = System.nanoTime();
  90.             delta += (now - lastTime) / nanoSeconds;
  91.             lastTime = now;
  92.            
  93.             while (delta >= 1) {
  94.                
  95.                 update();
  96.                
  97.                 updates++;
  98.                 delta--;
  99.                
  100.             }
  101.            
  102.             render();
  103.             frames++;
  104.            
  105.             if(System.currentTimeMillis() - timer > 1000) {
  106.                
  107.                 timer += 1000;
  108.                
  109.                 frame.setTitle("unname game project [ups: " + updates + ", fps:  " + frames + "]");
  110.                
  111.                 updates = 0;
  112.                 frames = 0;
  113.                
  114.             }
  115.            
  116.         }
  117.        
  118.         stop();
  119.        
  120.     }
  121.    
  122.     public void update() {
  123.        
  124.         key.update();
  125.        
  126.     }
  127.    
  128.     public void render() {
  129.        
  130.         BufferStrategy buffer = getBufferStrategy();
  131.        
  132.         if(buffer == null) {
  133.            
  134.             createBufferStrategy(Game.bufferLayers);
  135.             return;
  136.            
  137.         }
  138.        
  139.         screen.clear();
  140.         screen.render();
  141.        
  142.         for(int i = 0; i < pixels.length; i++) {
  143.            
  144.             pixels[i] = screen.pixels[i];
  145.         }
  146.        
  147.         Graphics g = buffer.getDrawGraphics();
  148.         g.setColor(Color.BLACK);
  149.         g.fillRect(0, 0, getWidth(), getHeight());
  150.        
  151.         g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  152.        
  153.         g.dispose();
  154.         buffer.show();
  155.        
  156.     }
  157.    
  158.     public static void main(String[] ags) {
  159.        
  160.         Game game = new Game();
  161.        
  162.         game.frame.setResizable(false);
  163.         game.frame.setTitle("unnamed game project");
  164.         game.frame.add(game);
  165.         game.frame.pack();
  166.         game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  167.         game.frame.setLocationRelativeTo(null);
  168.         game.frame.setVisible(true);
  169.        
  170.         game.start();
  171.        
  172.     }
  173.    
  174. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement