Skip to content

@Bevilacqua /MainGame.java
Created

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Stack Log When i run debug mode
package me.bevilacqua.game;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Dimension;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import javax.swing.JFrame;
import me.bevilacqua.game.gfx.Screen;
public class MainGame extends Canvas implements Runnable {
//SIDESCROLLER!!!
private static final long serialVersionUID = 1L;
private static final int WIDTH = 300;
private static final int HEIGHT = WIDTH / 16 * 9; // 16 * 9 maintains a 16 X 9 aspect ratio
private static final int SCALE = 2;
private static final String TITLE = "Ongoing";
JFrame frame;
public boolean running = false;
private Screen screen;
private BufferedImage image = new BufferedImage(WIDTH , HEIGHT , BufferedImage.TYPE_INT_RGB); //The image the game runs on but you cant edit it without a raster
private int[] pixels = ((DataBufferInt)image.getRaster().getDataBuffer()).getData(); //Converts the buffered image into an array of intigers to hold pixel data
private static Dimension size = new Dimension(WIDTH * SCALE , HEIGHT * SCALE);
public MainGame() {
setSize(size);
setPreferredSize(size);
screen = new Screen(WIDTH , HEIGHT);
frame = new JFrame();
frame.add(this);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setTitle(TITLE);
frame.setVisible(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
}
public static void main(String Args[]) {
new MainGame().start();
}
public synchronized void start() {
running = true;
new Thread(this).start();
}
public synchronized void stop() {
running = false;
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
while (running) {
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean ShouldRender = false;
while (delta >= 1) {
ticks++;
tick();
delta--;
ShouldRender = true;
}
try { Thread.sleep(5); } //Too Slow? make 0
catch (InterruptedException e) { e.printStackTrace(); }
if (ShouldRender) {
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer >= 1000) {
lastTimer += 1000;
frame.setTitle(TITLE + " |" + " Frames: " + frames + " Ticks: " + ticks);
System.out.println(TITLE + " |" + " Frames: " + frames + " Ticks: " + ticks);
frames = 0;
ticks = 0;
}
}
}
private void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(4); //Too Slow? make this 3
return;
}
//Optional clearing goes below
screen.clear();
//Rendering goes below:
screen.render();
for (int i = 0 ; i < pixels.length ; i++) { //Sets the pixels array in MainGame to the pixel array in the Screen class
pixels[i] = screen.pixels[i];
}
//Displaying goes below:
Graphics g = bs.getDrawGraphics();
g.drawImage(image, 0, 0, getWidth(), getHeight(), null); //Draws the buffered image (pixels[])
g.setColor(Color.blue);
g.dispose(); //Disposes the graphics
bs.show(); //Shows the next buffer
}
private void tick() {
// TODO Auto-generated method stub
}
}
package me.bevilacqua.game.gfx;
public class Screen {
private int width;
private int height;
public int[] pixels;
public Screen(int width , int height) {
this.width = width;
this.height = height;
pixels = new int[width * height];
}
public void clear() { //Not neaded but clears screen
for(int i = 0 ; i < pixels.length ; i++)
pixels[i] = 0;
}
public void render() {
for(int y = 0 ; y < height ; y++) {
for(int x = 0 ; x < width ; x++) {
pixels[x + y * width] = 0xff00ff;
}
}
}
}
Thread [main] (Suspended (exception FileNotFoundException))
FileInputStream.open(String) line: not available [native method]
FileInputStream.<init>(File) line: not available
Toolkit$1.run() line: not available
AccessController.doPrivileged(PrivilegedAction<T>) line: not available [native method]
Toolkit.initAssistiveTechnologies() line: not available
Toolkit.<clinit>() line: not available
Component.<clinit>() line: not available
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.