Advertisement
Guest User

Untitled

a guest
Feb 23rd, 2019
65
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 5.71 KB | None | 0 0
  1. package game;
  2.  
  3. import graphics.Screen;
  4. import graphics.SpriteSheet;
  5.  
  6. import java.awt.BorderLayout;
  7. import java.awt.Canvas;
  8. import java.awt.Dimension;
  9. import java.awt.Graphics;
  10. import java.awt.image.BufferStrategy;
  11. import java.awt.image.BufferedImage;
  12. import java.awt.image.DataBufferInt;
  13.  
  14. import javax.swing.JFrame;
  15.  
  16. public class Game extends Canvas implements Runnable{
  17.  
  18.  
  19.     private static final long serialVersionUID = 1L;
  20.    
  21.     public static final int WIDTH = 160;
  22.     public static final int HEIGHT = WIDTH/12*9;
  23.     public static final int SCALE = 3;
  24.     public static final String NAME = "Forgotten Realm";
  25.    
  26.     private JFrame frame;
  27.    
  28.     public boolean running = false;
  29.     public int updateCount = 0;
  30.    
  31.     private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
  32.     private int[] pixels =((DataBufferInt)image.getRaster().getDataBuffer()).getData();
  33.    
  34.     private Screen screen;
  35.    
  36.     public Game(){
  37.         setMinimumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  38.         setMaximumSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  39.         setPreferredSize(new Dimension(WIDTH*SCALE,HEIGHT*SCALE));
  40.        
  41.         frame = new JFrame(NAME);
  42.    
  43.         frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
  44.         frame.setLayout(new BorderLayout());
  45.        
  46.         frame.add(this, BorderLayout.CENTER);
  47.         frame.pack();
  48.        
  49.         frame.setResizable(false);
  50.         frame.setLocationRelativeTo(null);
  51.         frame.setVisible(true);
  52.     }
  53.    
  54.     public void init()
  55.     {
  56.         screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
  57.     }
  58.    
  59.     public synchronized void start(){
  60.         running = true;
  61.         new Thread(this).start();
  62.     }
  63.     public synchronized void stop(){
  64.         running = false;
  65.     }
  66.     public void run() {
  67.         long lastTime = System.nanoTime();
  68.         double nanosecondsPerUpdate = 1000000000D/60D;
  69.        
  70.         int updates = 0;
  71.         int frames = 0;
  72.        
  73.         long lastTimer = System.currentTimeMillis();
  74.        
  75.         double delta = 0;
  76.        
  77.         init();
  78.        
  79.         while(running)
  80.         {
  81.             long now = System.nanoTime();
  82.             delta += (now - lastTime)/nanosecondsPerUpdate;
  83.             lastTime = now;
  84.             boolean shouldRender = true;
  85.            
  86.             while(delta >= 1)
  87.             {
  88.             updates++;
  89.             update();
  90.             delta -= 1;
  91.             shouldRender = true;
  92.             }
  93.            
  94.             try {
  95.                 Thread.sleep(2);
  96.             } catch (InterruptedException e) {
  97.                 e.printStackTrace();
  98.             }
  99.            
  100.             if(shouldRender)
  101.             {
  102.             frames++;
  103.             draw();
  104.             }  
  105.            
  106.             if(System.currentTimeMillis() - lastTimer >= 1000)
  107.             {
  108.                 lastTimer += 1000;
  109.                 System.out.println(updates + " updates, "+frames + " frames");
  110.                 frames = 0;
  111.                 updates = 0;
  112.             }
  113.         }
  114.     }
  115.    
  116.     public void update(){
  117.         updateCount++;
  118.         for(int i = 0; i < pixels.length;i++)
  119.         {
  120.             pixels[i] = i + updateCount;   
  121.         }
  122.     }
  123.     public void draw(){
  124.         BufferStrategy bs = getBufferStrategy();
  125.         if(bs == null)
  126.         {
  127.             createBufferStrategy(3);
  128.             return;
  129.         }
  130.        
  131.         screen.draw(pixels, 0, WIDTH);
  132.        
  133.         Graphics g = bs.getDrawGraphics();
  134.        
  135.         g.drawRect(0,0, getWidth(), getHeight());
  136.         g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  137.        
  138.         g.dispose();
  139.         bs.show();
  140.     }
  141.    
  142.     public static void main(String[] args){
  143.         new Game().start();
  144.     }
  145.  
  146. }
  147.  
  148.  
  149. package graphics;
  150.  
  151. import java.awt.image.BufferedImage;
  152. import java.io.IOException;
  153.  
  154. import javax.imageio.ImageIO;
  155.  
  156. public class SpriteSheet {
  157.     public String path;
  158.     public int width;
  159.     public int height;
  160.    
  161.     public int[] pixels;
  162.    
  163.     public SpriteSheet(String path)
  164.     {
  165.         BufferedImage image = null;
  166.        
  167.         try {
  168.             image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
  169.         } catch (IOException e) {
  170.             e.printStackTrace();
  171.         }
  172.        
  173.         if(image == null)
  174.         {
  175.             return;
  176.         }
  177.        
  178.         this.path = path;
  179.         this.width = image.getWidth();
  180.         this.height = image.getHeight();
  181.        
  182.         pixels = image.getRGB(0, 0, width, height, null, 0, width);
  183.        
  184.         for(int i = 0; i < pixels.length; i++)
  185.         {
  186.             //Removes alpha channels
  187.             //AARRGGBB
  188.             //256/4 = ~64
  189.             pixels[i] = (pixels[i]&0xff) / 64;  
  190.         }
  191.     }
  192. }
  193.  
  194.  
  195. package graphics;
  196.  
  197. public class Screen {
  198.    
  199.     public static final int MAP_WIDTH = 64;
  200.     public static final int MAP_WIDTH_MASK = MAP_WIDTH -1;
  201.    
  202.     public int[] tiles = new int[MAP_WIDTH * MAP_WIDTH];
  203.     public int[] colors = new int[MAP_WIDTH * MAP_WIDTH * 4];
  204.    
  205.     public int xOffset = 0;
  206.     public int yOffset = 0;
  207.    
  208.     public int width;
  209.     public int height;
  210.    
  211.     public SpriteSheet sheet;
  212.    
  213.     public Screen(int width, int height, SpriteSheet sheet)
  214.     {
  215.         this.width = width;
  216.         this.height = height;
  217.         this.sheet = sheet;
  218.        
  219.         for(int i = 0; i < MAP_WIDTH * MAP_WIDTH; i++)
  220.         {
  221.             //What they correspond to on the sprite_sheet
  222.             colors[i*4+0] = 0xff00ff; //Black
  223.             colors[i*4+1] = 0x00ffff; //Dark Color
  224.             colors[i*4+2] = 0xffff00; //Other Dark Color
  225.             colors[i*4+3] = 0xffffff; //White
  226.         }
  227.     }
  228.    
  229.     public void draw(int[] pixels, int offset, int row)
  230.     {
  231.         //Variables
  232.         int xTile = 0;
  233.         int yTile = 0;
  234.         int xMin = 0;
  235.         int xMax = 0;
  236.         int yMin = 0;
  237.         int yMax = 0;
  238.        
  239.         for(yTile = yOffset >> 3;yTile <= (yOffset + height) >>3;yTile++)
  240.         {
  241.             yMin = yTile * 8 - yOffset;
  242.             yMax = yMin + 8;
  243.             if(yMin < 0) yMin = 0;
  244.             if(yMax > height) yMax = height;
  245.         }
  246.         for(xTile = xOffset >> 3;xTile <= (xOffset + width) >>3;xTile++)
  247.         {
  248.             xMin = xTile * 8 - xOffset;
  249.             xMax = xMin + 8;
  250.             if(xMin < 0) xMin = 0;
  251.             if(xMax > width) xMax = width;
  252.         }
  253.         int tileIndex = (xTile & (MAP_WIDTH_MASK)) + (yTile &(MAP_WIDTH_MASK)) * MAP_WIDTH;
  254.        
  255.         for(int y = 0; y <yMax;y++)
  256.         {
  257.             //Goes through it and draws it.
  258.             int sheetPixel =(y+yOffset & 7) * sheet.width + ((xMin + xOffset) & 7);
  259.             int tilePixel = offset + xMin + y * row;
  260.            
  261.             for(int x = xMin; x < xMax; x++)
  262.             {
  263.                 int color = tileIndex * 4 + sheet.pixels[sheetPixel++];
  264.                 pixels[tilePixel++] = colors[color];
  265.             }
  266.         }
  267.     }
  268. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement