Created
July 31, 2015 15:02
-
-
Save hoenn/01271f0afb9794f73fde to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.awt.BorderLayout; | |
import java.awt.Canvas; | |
import java.awt.Dimension; | |
import java.awt.Graphics; | |
import java.awt.Graphics2D; | |
import java.awt.image.BufferStrategy; | |
import java.awt.image.BufferedImage; | |
import java.awt.image.DataBufferInt; | |
import java.io.File; | |
import java.io.IOException; | |
import javax.imageio.ImageIO; | |
import javax.swing.JFrame; | |
public class Game extends Canvas implements Runnable | |
{ | |
private static final long serialVersionUID = 1L; | |
public static final String NAME = "Title"; | |
public static int HEIGHT; | |
public static int WIDTH; | |
//Can be dynamically determined at | |
//run time instead | |
public static int SCALE = 64; | |
private final String FILEPATH=""; | |
//Speed of the tick function call | |
public static final int FPS = 2; | |
public static boolean running = false; | |
private BufferedImage screenImage; | |
public static int[] screenPixels; | |
public static int[] boardPixels; | |
public static boolean reset; | |
public Game() | |
{ | |
new InputHandler(this); | |
init(); | |
} | |
public void start() | |
{ | |
running = true; | |
new Thread(this).start(); | |
} | |
public void stop() | |
{ | |
running = false; | |
} | |
public void init() | |
{ | |
BufferedImage temp = null; | |
try | |
{ | |
temp = ImageIO.read(new File(FILEPATH)); | |
} catch (IOException e) | |
{ | |
e.printStackTrace(); | |
} | |
//In pixels | |
HEIGHT= temp.getHeight(); | |
WIDTH = temp.getWidth(); | |
//Scale can be determined here based off the image parameters | |
//Rather than determined at compile time with a static | |
//variable at the top of this class | |
//SCALE=...; | |
//Create Buffered Image | |
BufferedImage boardImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); | |
//Capture graphics object of Buffered Image | |
Graphics2D g = boardImage.createGraphics(); | |
//Draw the loaded image @FILEPATH to graphics | |
g.drawImage(temp, 0, 0, null); | |
//Dispose after drawing to the BufferedImage by reference | |
g.dispose(); | |
//Create an array of pixels of the now loaded @FILEPATH image | |
boardPixels = ((DataBufferInt) boardImage.getRaster().getDataBuffer()).getData(); | |
//Create a new BufferedImage to use as the screen display | |
screenImage = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB); | |
//Create an array of pixels of the blank screen Image | |
screenPixels = ((DataBufferInt) screenImage.getRaster().getDataBuffer()).getData(); | |
for (int i = 0; i < screenPixels.length; i++) | |
{ | |
//Copy each pixels from @FILEPATH to the Screen | |
//This is optional depending on the type of game | |
//but is very common | |
screenPixels[i]=boardPixels[i]; | |
} | |
//From here you have the original image to reset with, and a manipulatable | |
//array of screen pixels to create a game with | |
reset=false; | |
} | |
public void tick() | |
{ | |
//This method is linked to the FPS | |
//Good place to animate a cursor | |
if (reset) | |
{ | |
init(); | |
} | |
} | |
public void render() | |
{ | |
//Magic for rendering | |
BufferStrategy bs = getBufferStrategy(); | |
if (bs == null) | |
{ | |
createBufferStrategy(3); | |
requestFocus(); | |
return; | |
} | |
Graphics g = bs.getDrawGraphics(); | |
g.fillRect(0, 0, getWidth(), getHeight()); | |
g.drawImage(screenImage, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null); | |
g.dispose(); | |
bs.show(); | |
} | |
public void run() | |
{ | |
long lastTime = System.nanoTime(); | |
double unprocessed = 0; | |
double nsPerTick = 1000000000.0 / FPS; | |
System.currentTimeMillis(); | |
while(running) | |
{ | |
long now = System.nanoTime(); | |
unprocessed += (now - lastTime) / nsPerTick; | |
lastTime = now; | |
while(unprocessed >= 1) | |
{ | |
//Updates @ var FPS | |
tick(); | |
unprocessed -= 1; | |
} | |
try | |
{ | |
Thread.sleep(20); | |
} catch (InterruptedException e) | |
{ | |
e.printStackTrace(); | |
} | |
//Renders as fast as possible | |
render(); | |
} | |
} | |
public static void main(String[] args) | |
{ | |
//Instantiate Game to initialize | |
Game game = new Game(); | |
//Uses variables from initialization | |
game.setPreferredSize(new Dimension(WIDTH * SCALE, HEIGHT * SCALE)); | |
//This JFrame is local to main and can't be accessed later | |
//This can be changed but JFrame class should be reconsidered if | |
//it needs to be changed frequently | |
JFrame frame= new JFrame(Game.NAME); | |
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); | |
frame.setLayout(new BorderLayout()); | |
frame.add(game, BorderLayout.CENTER); | |
frame.pack(); | |
frame.setResizable(false); | |
//Centers the JFrame to screen | |
frame.setLocationRelativeTo(null); | |
frame.setVisible(true); | |
//Begins game loops | |
game.start(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment