From 3ede7e2a14fdc3ba0927240ca1b50c1bd7927e44 Mon Sep 17 00:00:00 2001 From: Lee Date: Sat, 12 Jan 2013 17:25:49 -0500 Subject: [PATCH] Added Renderer, Screen Also, yes, Eclipse. --- .gitignore | 24 ++++++----- src/Renderer.java | 120 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ src/Screen.java | 33 +++++++++++++++ 3 files changed, 167 insertions(+), 10 deletions(-) create mode 100644 src/Renderer.java create mode 100644 src/Screen.java diff --git a/.gitignore b/.gitignore index 3ded774..f42c311 100644 --- a/.gitignore +++ b/.gitignore @@ -1,10 +1,14 @@ -*.class - -bin/* - -.ant_targets - -# Package Files # -*.jar -*.war -*.ear +*.class + +bin/* + +.ant_targets + +# Package Files # +*.jar +*.war +*.ear + +# Eclipse # +.classpath +.project diff --git a/src/Renderer.java b/src/Renderer.java new file mode 100644 index 0000000..65c0d4c --- /dev/null +++ b/src/Renderer.java @@ -0,0 +1,120 @@ + +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; + +public class Renderer extends Canvas implements Runnable { + private static final long serialVersionUID = 1L; + + public static int width = 320; + public static int height = 240; + public static int scale = 2; + + public String title = "Title"; + + private Thread thread; + private JFrame frame; + private boolean running = false; + + private Screen screen; + + private BufferedImage image = new BufferedImage(width, height, BufferedImage.TYPE_INT_RGB); + private int[] pixels = ((DataBufferInt) image.getRaster().getDataBuffer()).getData(); + + public Renderer(int width, int height) { + System.out.println(width); + } + + public Renderer() { + Dimension size = new Dimension(width*scale, height*scale); + setPreferredSize(size); + screen = new Screen(width, height); + + frame = new JFrame(); + } + + public synchronized void start() { + running = true; + thread = new Thread(this, "Display"); + thread.start(); + } + + public synchronized void stop() { + running = false; + try { + thread.join(); + } catch (InterruptedException e) { + e.printStackTrace(); + } + } + + public void run() { + long lastTime = System.nanoTime(); + long timer = System.currentTimeMillis(); + double delta = 0; + int frames = 0; + int updates = 0; + long now; + while (running) { + now = System.nanoTime(); + delta += (now - lastTime) * 6.0 / 100000000.0; + lastTime = now; + while (delta >= 1) { + update(); + updates++; + delta--; + render(); + frames++; + } + if (System.currentTimeMillis() - timer > 1000) { + timer += 1000; + frame.setTitle(title + " | updates: " + updates + " | frames: "+ frames); + frames = 0; + updates = 0; + } + } + stop(); + } + + public void update() { + } + + public void render() { + BufferStrategy bs = getBufferStrategy(); + if (bs == null) { + createBufferStrategy(3); + return; + } + + screen.clear(); + screen.render(); + + for (int i = 0; i < pixels.length; i++) { + pixels[i] = screen.pixels[i]; + } + + Graphics g = bs.getDrawGraphics(); + g.drawImage(image, 0, 0, getWidth(), getHeight(), null); + g.dispose(); + bs.show(); + } + + public static void main(String[] args) { + Renderer render = new Renderer(); + render.frame.setResizable(false); + render.frame.setTitle(render.title); + render.frame.add(render); + render.frame.pack(); + render.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); + render.frame.setLocationRelativeTo(null); + render.frame.setVisible(true); + + render.start(); + render.screen.draw(100,100,0xFFFFFF); + } +} diff --git a/src/Screen.java b/src/Screen.java new file mode 100644 index 0000000..1e00d30 --- /dev/null +++ b/src/Screen.java @@ -0,0 +1,33 @@ + +public class Screen { + + private int width, height; + public int[] pixels; + private int[] _grid; + + public Screen(int width, int height) { + this.width = width; + this.height = height; + + pixels = new int[width * height]; + _grid = new int[width * height]; + } + + public void clear() { + for (int x = 0; x < _grid.length; x++) { + pixels[x] = 0; + } + } + + public void render() { + for (int x = 0; x < width; x++) { + for (int y = 0; y < height; y++) { + pixels[x + y * width] = _grid[x + y * width]; + } + } + } + + public void draw(int x, int y, int color) { + _grid[x + y * width] = color; + } +}