Package com.kgrwhite.cyrene

Source Code of com.kgrwhite.cyrene.Game

package com.kgrwhite.cyrene;

import java.awt.*;
import java.awt.image.*;


import javax.swing.JFrame;

import com.kgrwhite.cyrene.graphics.Screen;

public class Game extends Canvas implements Runnable {
  private static final long serialVersionUID = 1L;
 
  public static int width = 300;
  public static int height = width / 16 * 9;
  public static int scale = 3;
  public static String title = "Cyrene";
 
  private Thread thread;
  private JFrame frame;
  private boolean running = false;
 
  private Screen screen;
 
  private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB);
  private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData();
 
  public Game(){
    Dimension size = new Dimension(width * scale, height * scale);
    setPreferredSize(size);
    screen = new Screen(width, height);
   
    frame = new JFrame();
   
  }
 
  public synchronized void start(){
    running = true;
    thread = new Thread(this, "Display");
    thread.start();
  }
 
  public synchronized void stop(){
    running = false;
    try{
      thread.join();
    } catch(InterruptedException e){
      e.printStackTrace();
    }
  }
 
  public void update(){
   
  }
 
  public void render(){
    BufferStrategy bs = getBufferStrategy();
    if (bs == null){
      createBufferStrategy(3);
      return;
    }
    screen.clear();
    screen.render();
   
    for (int i = 0; i < pixels.length; i++){
      pixels[i] = screen.pixels[i];
    }
   
    Graphics g = bs.getDrawGraphics();
    //Graphics here
    g.setColor(Color.BLACK);
    g.fillRect(0, 0, getWidth(), getHeight());
    g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
   
   
   
    //end graphics
    g.dispose();
    bs.show();
  }
 
  public void run(){
    long lastTime = System.nanoTime();
    long timer = System.currentTimeMillis();
    final double ns = 1000000000.0 / 60.0;
    double delta = 0;
    int frames = 0;
    int updates = 0;
    while (running){
      long now = System.nanoTime();
      delta += (now - lastTime) / ns;
      lastTime = now;
     
      while (delta >= 1){
        update();
        updates++;
        delta--;
      }
     
      render();
      frames++;
     
      if (System.currentTimeMillis() - timer > 1000){
        timer += 1000;
        //System.out.println(updates + " ups, " + frames + " fps");
        frame.setTitle(title + "   |   " +" UPS: " + updates + " FPS: " + frames);
        updates = 0;
        frames = 0;
      }
       
    }
    stop();
  }
 
  public static void main(String[] args){
    Game game = new Game();
    game.frame.setResizable(false);
   
    game.frame.setTitle(Game.title);
    game.frame.add(game);
    game.frame.pack();
    game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    game.frame.setLocationRelativeTo(null);
    game.frame.setVisible(true);
   
    game.start();
   
  }
 

}
TOP

Related Classes of com.kgrwhite.cyrene.Game

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.