Browse Source

Rendering and gameloop added.

master
JeasonFire 8 years ago
parent
commit
d9db66497f
6 changed files with 331 additions and 0 deletions
  1. +1
    -0
      .gitignore
  2. +50
    -0
      src/com/jeasonfire/engineer/Engineer.java
  3. +126
    -0
      src/com/jeasonfire/engineer/Game.java
  4. +66
    -0
      src/com/jeasonfire/engineer/Input.java
  5. +33
    -0
      src/com/jeasonfire/engineer/graphics/screens/IntroScreen.java
  6. +55
    -0
      src/com/jeasonfire/engineer/graphics/screens/Screen.java

+ 1
- 0
.gitignore View File

@ -10,3 +10,4 @@
# virtual machine crash logs, see http://www.java.com/en/download/help/error_hotspot.xml
hs_err_pid*
/bin

+ 50
- 0
src/com/jeasonfire/engineer/Engineer.java View File

@ -0,0 +1,50 @@
package com.jeasonfire.engineer;
import java.awt.Dimension;
import javax.swing.JApplet;
import javax.swing.JFrame;
public class Engineer extends JApplet {
private static final long serialVersionUID = 1L;
private static int GWIDTH = 160, GHEIGHT = 90, SCALE = 4;
private static Game game;
private static JFrame frame;
public void init() {
game = new Game(GWIDTH, GHEIGHT);
add(game);
Dimension size = new Dimension(GWIDTH * SCALE, GHEIGHT * SCALE);
setMinimumSize(size);
setPreferredSize(size);
setMaximumSize(size);
game.requestFocus();
}
public void start() {
game.start();
}
public void stop() {
game.stop();
}
public static void main(String[] args) {
frame = new JFrame();
game = new Game(GWIDTH, GHEIGHT);
Dimension size = new Dimension(GWIDTH * SCALE, GHEIGHT * SCALE);
game.setMinimumSize(size);
game.setPreferredSize(size);
game.setMaximumSize(size);
frame.add(game);
frame.pack();
frame.setResizable(true);
frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
frame.setTitle("Engineer");
frame.setLocationRelativeTo(null);
frame.setVisible(true);
game.requestFocus();
game.start();
}
}

+ 126
- 0
src/com/jeasonfire/engineer/Game.java View File

@ -0,0 +1,126 @@
package com.jeasonfire.engineer;
import java.awt.Canvas;
import java.awt.Color;
import java.awt.Graphics;
import java.awt.image.BufferStrategy;
import java.awt.image.BufferedImage;
import java.awt.image.DataBufferInt;
import com.jeasonfire.engineer.graphics.screens.IntroScreen;
import com.jeasonfire.engineer.graphics.screens.Screen;
public class Game extends Canvas implements Runnable {
private static final long serialVersionUID = 1L;
private int width, height;
private int[] pixels;
private BufferedImage screenImage;
private boolean running;
private Thread thread;
private Input input;
private Screen screen;
public Game(int width, int height) {
this.width = width;
this.height = height;
screenImage = new BufferedImage(width, height,
BufferedImage.TYPE_INT_RGB);
pixels = ((DataBufferInt) screenImage.getRaster().getDataBuffer())
.getData();
input = new Input(this);
addKeyListener(input);
addMouseListener(input);
addMouseMotionListener(input);
screen = new IntroScreen(this);
}
public void update(float delta) {
screen.update(delta);
}
public void clear() {
screen.clear();
}
public void draw() {
screen.draw();
for (int i = 0; i < pixels.length; i++) {
pixels[i] = screen.getPixel(i % width, i / width);
}
}
public void render() {
BufferStrategy bs = getBufferStrategy();
if (bs == null) {
createBufferStrategy(3);
return;
}
clear();
draw();
Graphics g = bs.getDrawGraphics();
g.setColor(Color.BLACK);
g.fillRect(0, 0, getWidth(), getHeight());
if (getWidth() / getHeight() > width / height) {
g.drawImage(screenImage,
(getWidth() - getHeight() * width / height) / 2, 0,
getHeight() * width / height, getHeight(), null);
} else {
g.drawImage(screenImage, 0, (getHeight() - getWidth() / width
* height) / 2, getWidth(), getWidth() / width * height,
null);
}
g.dispose();
bs.show();
}
public int getGameWidth() {
return width;
}
public int getGameHeight() {
return height;
}
public synchronized void start() {
running = true;
thread = new Thread(this, "Game Thread");
thread.start();
}
public synchronized void stop() {
running = false;
new Thread() {
public void run() {
try {
thread.join();
} catch (InterruptedException ex) {
ex.printStackTrace();
}
}
}.start();
System.exit(0);
}
public void run() {
long timer = System.currentTimeMillis();
float delta = 0;
int frames = 0;
long nowTime, lastTime = System.nanoTime();
while (running) {
nowTime = System.nanoTime();
delta = (nowTime - lastTime) / 1000000000.0f;
update(delta);
render();
frames++;
lastTime = nowTime;
if (System.currentTimeMillis() - timer > 1000) {
System.out.println("FPS: " + frames);
frames = 0;
timer = System.currentTimeMillis();
}
}
}
}

+ 66
- 0
src/com/jeasonfire/engineer/Input.java View File

@ -0,0 +1,66 @@
package com.jeasonfire.engineer;
import java.awt.Point;
import java.awt.event.KeyEvent;
import java.awt.event.KeyListener;
import java.awt.event.MouseEvent;
import java.awt.event.MouseListener;
import java.awt.event.MouseMotionListener;
public class Input implements KeyListener, MouseListener, MouseMotionListener {
public static boolean[] keys = new boolean[1024];
public static boolean mouseDown = false;
public static Point msp = new Point(0, 0);
public static int lastKey = -1;
private Game game;
public Input(Game game) {
this.game = game;
}
public void mouseDragged(MouseEvent e) {
msp.x = e.getX() / (game.getWidth() / game.getGameWidth());
msp.y = e.getY() / (game.getHeight() / game.getGameHeight());
}
public void mouseMoved(MouseEvent e) {
msp.x = e.getX() / (game.getWidth() / game.getGameWidth());
msp.y = e.getY() / (game.getHeight() / game.getGameHeight());
}
public void mousePressed(MouseEvent e) {
mouseDown = true;
}
public void mouseReleased(MouseEvent e) {
mouseDown = false;
}
public void keyPressed(KeyEvent e) {
keys[e.getKeyCode()] = true;
lastKey = e.getKeyCode();
}
public void keyReleased(KeyEvent e) {
keys[e.getKeyCode()] = false;
if (e.getKeyCode() == lastKey) {
lastKey = -1;
}
}
/**
* Unused
*/
public void keyTyped(KeyEvent e) {
}
public void mouseClicked(MouseEvent e) {
}
public void mouseEntered(MouseEvent e) {
}
public void mouseExited(MouseEvent e) {
}
}

+ 33
- 0
src/com/jeasonfire/engineer/graphics/screens/IntroScreen.java View File

@ -0,0 +1,33 @@
package com.jeasonfire.engineer.graphics.screens;
import java.awt.event.KeyEvent;
import com.jeasonfire.engineer.Game;
import com.jeasonfire.engineer.Input;
public class IntroScreen extends Screen {
private float x = 0, y = 0;
public IntroScreen(Game game) {
super(game);
}
public void draw() {
drawShadedRectangle(0xCC00, 0xAA00, 0x8800, (int) x, (int) y, 40, 40);
}
public void update(float delta) {
if (Input.keys[KeyEvent.VK_W]) {
y -= delta * 100.0f;
}
if (Input.keys[KeyEvent.VK_A]) {
x -= delta * 100.0f;
}
if (Input.keys[KeyEvent.VK_S]) {
y += delta * 100.0f;
}
if (Input.keys[KeyEvent.VK_D]) {
x += delta * 100.0f;
}
}
}

+ 55
- 0
src/com/jeasonfire/engineer/graphics/screens/Screen.java View File

@ -0,0 +1,55 @@
package com.jeasonfire.engineer.graphics.screens;
import com.jeasonfire.engineer.Game;
public abstract class Screen {
protected int width, height;
private int[] pixels;
protected final Game game;
public Screen(Game game) {
this.game = game;
this.width = game.getGameWidth();
this.height = game.getGameHeight();
this.pixels = new int[width * height];
}
public abstract void draw();
public abstract void update(float delta);
public void clear() {
for (int i = 0; i < pixels.length; i++) {
pixels[i] = 0;
}
}
public void drawShadedRectangle(int brightColor, int midColor, int shadowColor, int x, int y, int w, int h) {
drawRectangle(brightColor, x, y, w - 1, 1);
drawRectangle(brightColor, x, y + 1, 1, h - 1);
drawRectangle(midColor, x + 1, y + 1, w - 2, h - 2);
drawRectangle(shadowColor, x + w - 1, y, 1, h);
drawRectangle(shadowColor, x + 1, y + h - 1, w - 1, 1);
}
public void drawRectangle(int color, int x, int y, int w, int h) {
for (int yp = 0; yp < h; yp++) {
int yy = yp + y;
for (int xp = 0; xp < w; xp++) {
int xx = xp + x;
setPixel(color, xx, yy);
}
}
}
public void setPixel(int color, int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height)
return;
pixels[x + y * width] = color;
}
public int getPixel(int x, int y) {
if (x < 0 || x >= width || y < 0 || y >= height)
return -1;
return pixels[x + y * width];
}
}

Loading…
Cancel
Save