Friday, April 25, 2014

Eclipse: Screen Class & Rendering Pixels

Today we worked through two more video tutorials by The Cherno.  We created a new Screen class and then began work on rendering pixels.

We watched Tutorial 8: https://www.youtube.com/watch?v=e61rMGZi8V4&list=ELshNxV9QFUOo

And then moved on to Tutorial 9:

I'm not going to be sharing code throughout this tutorial, but since we are just getting our foundations built at this point, I'm going to post the current state of our programs:

The Game class:  (Remember that package names may be different for you)
package com.version001.rain;

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.version001.rain.graphics.Screen;

public class Game extends Canvas implements Runnable {
 private static final long serialVersionUID = 1L;

 public static int width = 300;
 public static int height = width / 16 * 9;
 public static int scale = 3;

 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 Game() {
  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 (final InterruptedException e) {
   e.printStackTrace();
  }
 }

 @Override
 public void run() {
  while (running) {
   // System.out.println("Running...");
   update();
   render();
  }
 }

 public void update() {
 }

 public void render() {
  BufferStrategy bs = getBufferStrategy();
  if (bs == null) {
   createBufferStrategy(3); // Triple Buffering
   return;
  }

  screen.render();

  for (int i = 0; i < pixels.length; i++) {

   pixels[i] = screen.pixels[i];

  }

  Graphics g = bs.getDrawGraphics(); // graphics = buffer strategy :
  // linking two together

  // All graphics displayed between here and g.dispose();
  // g.setColor(new Color(0, 0, 0)); // Sets the color with RGB ** removed
  // for updated code
  // g.fillRect(0, 0, getWidth(), getHeight()); // Fills a rectangle the
  // size of the screen ** removed for updated code
  g.drawImage(image, 0, 0, getWidth(), getHeight(), null);
  g.dispose(); // Removes graphics at end of each frame
  bs.show(); // Display buffers available to release memory or it will
  // crash
 }

 public static void main(String[] args) {
  Game game = new Game();
  game.frame.setResizable(false); // Do not allow user to resize window
  game.frame.setTitle("Rain");
  game.frame.add(game);
  game.frame.pack();
  game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE); // Close when you hit X button
  game.frame.setLocationRelativeTo(null); // Centers our window
  game.frame.setVisible(true); // Makes window visible.

  game.start(); // Starts the game
 }
}
The Screen class:
package com.version001.rain.graphics;

public class Screen {

 private int width, height;
 public int[] pixels;

 public Screen(int width, int height) {
  this.width = width;
  this.height = height;
  pixels = new int[width * height];
 }

 public void render() {
  // This loop draws a pixel across every column (X) and the continues down for every row (y)
  for (int y = 0; y < height; y++) { 
   for (int x = 0; x < width; x++) {
    pixels[x + y * width] = 0xff00ff;
   }
  }
 }

}

No comments:

Post a Comment