public
anonymous / Game.java
Created

Cant find the error with this, used art 3 of your tutorial

  • Download Gist
Game.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132
package com.blazingkin.atrixEngine;
 
import java.awt.BorderLayout;
import java.awt.Canvas;
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 com.blazingkin.atrixEngine.gfx.Screen;
import com.blazingkin.atrixEngine.gfx.SpriteSheet;
 
public class Game extends Canvas implements Runnable {
 
private static final long serialVersionUID = 1L;
 
public static final int WIDTH = 160;
public static final int HEIGHT = WIDTH / 12 * 9;
public static final int SCALE = 3;
public static final String NAME = "Atrix";
public boolean running = false;
public int tickCount = 0;
private JFrame frame;
private BufferedImage image = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData();
 
private Screen screen;
public Game(){
setMinimumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
setMaximumSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
setPreferredSize(new Dimension(WIDTH*SCALE, HEIGHT*SCALE));
frame = new JFrame(NAME);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLayout(new BorderLayout());
frame.add(this, BorderLayout.CENTER);
frame.pack();
frame.setResizable(false);
frame.setLocationRelativeTo(null);
frame.setVisible(true);
}
public void init(){
screen = new Screen(WIDTH, HEIGHT, new SpriteSheet("/sprite_sheet.png"));
}
public void run() {
long lastTime = System.nanoTime();
double nsPerTick = 1000000000D / 60D;
int ticks = 0;
int frames = 0;
long lastTimer = System.currentTimeMillis();
double delta = 0;
init();
while (running){
long now = System.nanoTime();
delta += (now - lastTime) / nsPerTick;
lastTime = now;
boolean shouldRender = true;
while(delta >= 1){
ticks++;
delta--;
tick();
shouldRender = true;
}
try{
Thread.sleep(2);
}catch(Exception e){}
if (shouldRender){
frames++;
render();
}
if (System.currentTimeMillis() - lastTimer > 1000){
lastTimer+=1000;
System.out.println(frames+" frames : "+ticks + " ticks");
frames = 0;
ticks = 0;
}
}
}
public void tick(){
tickCount++;
 
}
public void render(){
BufferStrategy bs = getBufferStrategy();
if (bs == null){
createBufferStrategy(3);
return;
}
screen.render(pixels, 0, WIDTH);
Graphics g = bs.getDrawGraphics();
g.drawRect(0,0,getWidth(), getHeight());
g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
g.dispose();
bs.show();
}
private synchronized void start(){
running = true;
new Thread(this).start();
}
private synchronized void stop(){
running = false;
}
public static void main(String[] args){
new Game().start();
}
}
Screen.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58
package com.blazingkin.atrixEngine.gfx;
 
public class Screen {
 
public static final int MAP_WIDTH = 64;
public static final int MAP_WIDTH_MASK = MAP_WIDTH - 1;
public int[] tiles = new int[MAP_WIDTH * MAP_WIDTH];
public int[] colours = new int[MAP_WIDTH * MAP_WIDTH * 4];
public int xOffset = 0;
public int yOffset = 0;
public int width;
public int height;
public SpriteSheet sheet;
public Screen(int width, int height, SpriteSheet sheet){
this.width = width;
this.height = height;
this.sheet = sheet;
for (int i = 0; i < MAP_WIDTH * MAP_WIDTH; i++){
colours[i*4+0] = 0xff00ff;
colours[i*4+1] = 0x00ffff;
colours[i*4+2] = 0xffff00;
colours[i*4+3] = 0xffffff;
}
}
public void render(int[] pixels, int offset, int row){
for (int yTile = yOffset >> 3; yTile<= (yOffset + height) >> 3; yTile++){
int yMin = yTile * 8 - yOffset;
int yMax = yMin + 8;
if (yMin < 0){yMin = 0;}
if (yMax > height){yMax = height;}
for (int xTile = xOffset >> 3; xTile<= (xOffset + width) >> 3; xTile++){
int xMin = xTile * 8 - xOffset;
int xMax = xMin + 8;
if (xMin < 0){xMin = 0;}
if (xMax > width){xMax = width;}
int tileIndex = (xTile & MAP_WIDTH_MASK) + (yTile & MAP_WIDTH_MASK) * MAP_WIDTH;
for (int y = yMin; y < yMax; y++){
int sheetPixel = ((y + yOffset) & 7) * sheet.width + ((xMin + xOffset) &7);
int tilePixel = offset + xMin + y * row;
for (int x = xMin; x < xMax; x++){
int colour = tileIndex * 4 + sheet.pixels[sheetPixel];
pixels[tilePixel++] = colours[colour];
}
}
}
}
}
}
SpriteSheet.java
Java
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
package com.blazingkin.atrixEngine.gfx;
 
import java.awt.image.BufferedImage;
import java.io.IOException;
 
import javax.imageio.ImageIO;
 
public class SpriteSheet {
public String path;
public int width;
public int height;
public int[] pixels;
public SpriteSheet(String path){
BufferedImage image = null;
try {
image = ImageIO.read(SpriteSheet.class.getResourceAsStream(path));
} catch (IOException e) {
e.printStackTrace();
}
if (image == null){return;}
this.path = path;
this.width = image.getWidth();
this.height = image.getHeight();
pixels = image.getRGB(0, 0, width, height, null, 0, width);
for (int i = 0; i < pixels.length; i++){
pixels[i] = (pixels[i] & 0xff)/64;
}
for (int i = 0; i < 8; i++){
System.out.println(pixels[i]);
}
}
 
}

Please sign in to comment on this gist.

Something went wrong with that request. Please try again.