Advertisement
Guest User

Main

a guest
Apr 28th, 2012
154
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.23 KB | None | 0 0
  1. package com.ottdev.game;
  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. import java.io.IOException;
  11.  
  12. import javax.imageio.ImageIO;
  13. import javax.swing.JFrame;
  14.  
  15. import com.ottdev.game.gfx.Screen;
  16. import com.ottdev.game.gfx.SpriteSheet;
  17.  
  18. public class Game extends Canvas implements Runnable {
  19.  
  20.     public static final int HEIGHT = 240;
  21.     public static final int WIDTH = HEIGHT * 16 / 9;
  22.     private Thread thread;
  23.  
  24.     private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  25.     private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
  26.     private boolean running = false;
  27.     private int tickCount;
  28.     private Screen screen;
  29.  
  30.     public void start() {
  31.         if (running)
  32.             return;
  33.         running = true;
  34.         new Thread(this).start();
  35.     }
  36.  
  37.     public void stop() {
  38.         running = false;
  39.     }
  40.  
  41.     private void init() {
  42.         try {
  43.             screen = new Screen(WIDTH, HEIGHT, new SpriteSheet(ImageIO.read(Game.class.getResourceAsStream("/icons.png"))));
  44.         } catch (IOException e) {
  45.             // TODO Auto-generated catch block
  46.             e.printStackTrace();
  47.         }
  48.     }
  49.  
  50.     public void run() {
  51.         long lastTime = System.nanoTime();
  52.         double unprocessed = 0;
  53.         double nsPerTick = 1000000000.0 / 60;
  54.         int frames = 0;
  55.         int ticks = 0;
  56.         long lastTimer1 = System.currentTimeMillis();
  57.  
  58.         init();
  59.  
  60.         while (running) {
  61.             long now = System.nanoTime();
  62.             unprocessed += (now - lastTime) / nsPerTick;
  63.             lastTime = now;
  64.             boolean shouldRender = true;
  65.             while (unprocessed >= 1) {
  66.                 ticks++;
  67.                 tick();
  68.                 unprocessed -= 1;
  69.                 shouldRender = true;
  70.             }
  71.  
  72.             try {
  73.                 Thread.sleep(2);
  74.             } catch (InterruptedException e) {
  75.                 e.printStackTrace();
  76.             }
  77.  
  78.             if (shouldRender) {
  79.                 frames++;
  80.                 render();
  81.             }
  82.  
  83.             if (System.currentTimeMillis() - lastTimer1 > 1000) {
  84.                 lastTimer1 += 1000;
  85.                 System.out.println(ticks + " ticks, " + frames + " fps");
  86.                 frames = 0;
  87.                 ticks = 0;
  88.             }
  89.         }
  90.     }
  91.  
  92.     public void tick() {
  93.         tickCount++;
  94.         screen.xScroll++;
  95.         screen.yScroll++;
  96.  
  97.     }
  98.  
  99.     public void render() {
  100.         BufferStrategy bs = getBufferStrategy();
  101.         if (bs == null) {
  102.             createBufferStrategy(3);
  103.             return;
  104.         }
  105.         screen.render(pixels, 0, WIDTH);
  106.  
  107.         Graphics g = bs.getDrawGraphics();
  108.         g.fillRect(0, 0, getWidth(), getHeight());
  109.        
  110.        
  111.         int ww = WIDTH * 3;    
  112.         int hh = WIDTH * 3;
  113.         int xo = (getHeight()-ww)/2;
  114.         int yo = (getWidth()-hh)/2;
  115.        
  116.         g.drawImage(image, xo, yo, ww, hh, null);
  117.         g.dispose();
  118.         bs.show();
  119.     }
  120.  
  121.     public static void main(String[] args) {
  122.         Game game = new Game();
  123.         game.setMinimumSize(new Dimension(WIDTH * 2, HEIGHT * 2));
  124.         game.setMaximumSize(new Dimension(WIDTH * 2, HEIGHT * 2));
  125.         game.setPreferredSize(new Dimension(WIDTH * 2, HEIGHT * 2));
  126.  
  127.         JFrame frame = new JFrame();
  128.         frame.setLayout(new BorderLayout());
  129.         frame.add(game, BorderLayout.CENTER);
  130.         frame.pack();
  131.         frame.setLocationRelativeTo(null);
  132.         frame.setTitle("The Game With No Name: Indev!");
  133.         frame.setResizable(false);
  134.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  135.         frame.setVisible(true);
  136.  
  137.         game.start();
  138.     }
  139. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement