Package sk.doomlike

Source Code of sk.doomlike.Display

/*
* Doomlike - Display
* Shizuka Kamishima - 2013-01-27
*/
package sk.doomlike;

import java.awt.Canvas;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import org.apache.log4j.*;
import sk.doomlike.graphics.*;

public class Display extends Canvas implements Runnable{
 
  static Logger log = Logger.getLogger(Display.class.getName());
 
  private static final long serialVersionUID = 1L;
 
  public static final int WIN_WIDTH  = 800;
  public static final int WIN_HEIGHT = 600;
  public static final String WIN_TITLE = "Doomlike v0.0.1";
 
  private Thread thread;
  private boolean running = false;
  private Render render;
  private Screen screen;
  private BufferedImage img;
  private int[] pixels;
 
  public static void main(String[] args) {
    BasicConfigurator.configure(); //init logs
    /*
     * 2 [main] INFO sk.doomlike.Display  - Started...
     * ^ milliseconds since program start
     *   ^ thread that sent log
     *          ^ level
     *               ^ this class
     *                                      ^ message
     */
   
    // TODO add log4j.properties to give us file logging as well
   
    Display game = new Display(); //drawable area
    JFrame frame = new JFrame()//window
    frame.add(game); //add area to window
    frame.pack(); //fit window to contents
    frame.setTitle(WIN_TITLE);
    frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); //die on close
    frame.setSize(WIN_WIDTH, WIN_HEIGHT);
    frame.setLocationRelativeTo(null); //center on screen, use after size
    frame.setResizable(false);
    frame.setVisible(true);
   
    log.debug("Started");
    game.start();
  }
 
  public Display() {
    screen = new Screen(WIN_WIDTH, WIN_HEIGHT);
    img = new BufferedImage(WIN_WIDTH, WIN_HEIGHT, BufferedImage.TYPE_INT_RGB);
    pixels = ((DataBufferInt)img.getRaster().getDataBuffer()).getData();
  }
 
  @Override
  public void run() {
    while (running) {     
      tick();
      render();
    }
  }
 
  private void start() {
    if (running) {
      return;
    }
    running = true;
    thread = new Thread(this);
    thread.start();
  }
 
  private void stop() {
    if (!running) {
      return;
    }
    running = false;
    try {   
      thread.join();
    } catch (Exception e) {
      log.fatal(e);
      System.exit(0);
    }
  }

  private void tick() {
    // TODO tick logic
  }

  private void render() {
    BufferStrategy bs = this.getBufferStrategy();
    if (bs == null) {
      createBufferStrategy(3);
      return;
    }
   
    screen.render();
    System.arraycopy(screen.pixels, 0, pixels, 0, WIN_WIDTH * WIN_HEIGHT);
    /*
     * Replaces this block
     * for (int i = 0; i < WIN_WIDTH * WIN_HEIGHT; i++) {
     *   pixels[i] = screen.pixels[i];
     * }
     * it's all black magic to me
     */
   
    Graphics g = bs.getDrawGraphics();
    g.drawImage(img, 0, 0, WIN_WIDTH, WIN_HEIGHT, null);
    g.dispose();
    bs.show();
  }
}
TOP

Related Classes of sk.doomlike.Display

TOP
Copyright © 2018 www.massapi.com. All rights reserved.
All source code are property of their respective owners. Java is a trademark of Sun Microsystems, Inc and owned by ORACLE Inc. Contact coftware#gmail.com.