Posted 27 February 2013 - 02:23 PM
The problem is that at line 72 in Display.java the division problem outputs "0.0" instead of between "0.001"-"1". So can anyone please help? All the classes are down there.
Classes
Display.java
Render3D.java
Render.java
Screen.java
Game.java
Classes
Display.java
Spoiler
package com.cube.cubefront;
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.cube.cubefront.graphix.Render;
import com.cube.cubefront.graphix.Screen;
public class Display extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
public static final int WIDTH = 800;
public static final int HEIGHT = 600;
public static String TITLE = "Cubefront Pre-Alpha 0.02";
private Thread thread;
private Screen screen;
private Game game;
private BufferedImage img;
private boolean running = false;
private int[] pixels;
public Display() {
Dimension size = new Dimension(WIDTH, HEIGHT);
setPreferredSize(size);
setMinimumSize(size);
setMaximumSize(size);
screen = new Screen(WIDTH, HEIGHT);
game = new Game();
img = new BufferedImage(WIDTH, HEIGHT, BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) img.getRaster().getDataBuffer()).getData();
}
private void start() {
if (running)
return;
running = true;
thread = new Thread(this);
thread.start();
}
public void stop() {
if (!running)
return;
running = false;
try {
thread.join();
} catch (InterruptedException e) {
e.printStackTrace();
}
}
public void run() {
int frames = 0;
double unprocessedSeconds = 0;
long previousTime = System.nanoTime();
double secondsPerTick = 1 / 60.0;
int tickCount = 0;
boolean ticked = false;
while (running) {
long currentTime = System.nanoTime();
long passedTime = currentTime - previousTime;
previousTime = currentTime;
System.out.println(passedTime+ " " + unprocessedSeconds);
unprocessedSeconds += passedTime / 1000000000;
while(unprocessedSeconds > secondsPerTick){
tick();
unprocessedSeconds -= secondsPerTick;
ticked = true;
tickCount++;
if (tickCount % 60 == 0) {
System.out.println(frames + " fps");
previousTime += 1000;
}
}
if (ticked) {
render();
frames++;
}
render();
frames++;
}
}
private void tick() {
game.tick();
}
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
screen.render(game);
for (int i = 0; i < WIDTH * HEIGHT; i++) {
pixels[i] = screen.pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(img, 0, 0, WIDTH + 10, HEIGHT + 10, null);
g.dispose();
bs.show();
}
public static void main(String[] args) {
// Game Display
Display game = new Display();
JFrame frame = new JFrame();
frame.add(game);
frame.pack();
frame.setTitle(TITLE);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setLocationRelativeTo(null);
frame.setResizable(false);
frame.setVisible(true);
game.start();
}
}
Render3D.java
Spoiler
package com.cube.cubefront.graphix;
import com.cube.cubefront.Game;
public class Render3D extends Render {
public Render3D(int width, int height) {
super(width, height);
}
public void floor(Game game) {
for (int y = 0; y < height; y++) {
double ceiling = (y - height / 2.0) / height;
double z = 8 / ceiling;
for (int x = 0; x < width; x++) {
double depth = (x - width / 2.0) / height;
depth *= z;
int xx = (int) (depth) & 15;
System.out.println(game.time);
int yy = (int) (z + game.time) & 15;
pixels[x + y * width] = (xx * 16) | (yy * 16) << 8;
}
}
}
}
Render.java
Spoiler
package com.cube.cubefront.graphix;
import com.cube.cubefront.Display;
public class Render {
public final int width;
public final int height;
public final int[] pixels;
public Render(int width, int height) {
this.width = width;
this.height = height;
pixels = new int[width * height];
}
public void draw(Render render, int xOffset, int yOffset) {
for (int y = 0; y < render.height; y++) {
int yPix = y + yOffset;
if (yPix < 0 || yPix >= height) {
continue;
}
for (int x = 0; x < render.width; x++) {
int xPix = x + xOffset;
if (xPix < 0 || xPix >= width) {
continue;
}
int alpha = render.pixels[x + y * render.width];
if (alpha > 0) {
pixels[xPix + yPix * width] = alpha;
}
}
}
}
}
Screen.java
Spoiler
package com.cube.cubefront.graphix;
import java.util.Random;
import com.cube.cubefront.Game;
public class Screen extends Render {
private Render test;
private Render3D render;
public Screen(int width, int height) {
super(width, height);
Random random = new Random();
render = new Render3D(width, height);
test = new Render(256, 256);
for (int i = 0; i < 256 * 256; i++) {
test.pixels[i] = random.nextInt() * (random.nextInt(5) / 4);
}
}
public void render(Game game) {
for (int i = 0; i < width * height; i++) {
pixels[i] = 0;
}
for (int i = 0; i < 50; i++) {
int anim = (int) (Math.sin((game.time + i * 2) % 1000.0 / 100) * 100);
int anim2 = (int) (Math.cos((game.time + i * 2) % 1000.0 / 100) * 100);
}
render.floor(game);
draw(render, 0, 0);
}
}
Game.java
Spoiler
package com.cube.cubefront;
public class Game {
public int time;
public void tick() {
time+=3;
}
}