Commits

yahiko  committed f1e70f8

initial import

  • Participants

Comments (0)

Files changed (5)

File res/icons.png

Added
New image

File src/be/devinity/minicraft/Game.java

+package be.devinity.minicraft;
+
+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 java.io.IOException;
+import javax.imageio.ImageIO;
+import javax.swing.JFrame;
+
+import be.devinity.minicraft.gfx.Screen;
+import be.devinity.minicraft.gfx.Sprite;
+import be.devinity.minicraft.gfx.SpriteSheet;
+
+public class Game extends Canvas implements Runnable
+{
+	private static final long serialVersionUID = 1L;
+
+	public static final int HEIGHT = 120;
+	public static final int WIDTH = 160;
+	public static final int SCALE = 3;
+	public static final String NAME = "Minicraft the game";
+
+	private BufferedImage image = new BufferedImage( Game.WIDTH, Game.HEIGHT, BufferedImage.TYPE_INT_RGB );
+	private int[] pixels = ( (DataBufferInt) image.getRaster().getDataBuffer() ).getData();
+	private boolean running = false;
+
+	private SpriteSheet spritesheet;
+	private Screen screen;
+
+	public void render()
+	{
+		//System.out.println( "render();" );
+		BufferStrategy bs = this.getBufferStrategy();
+
+		if (bs == null) {
+			this.createBufferStrategy( 3 );
+			this.requestFocus();
+			return;
+		}
+
+		
+		this.screen.drawRandomColorStatic();
+		this.spritesheet.FRAME_TOP_LEFT.draw(  this.screen, 8 , 8 );
+		this.spritesheet.FRAME_TOP.draw(  this.screen, 16, 8 );
+		this.spritesheet.FRAME_TOP.draw(  this.screen, 24, 8 );
+		this.spritesheet.FRAME_TOP.draw(  this.screen, 32, 8 );
+		this.spritesheet.FRAME_LEFT.draw(  this.screen, 8, 16 );
+		this.spritesheet.FRAME_TOP_LEFT.draw(  this.screen, 40 , 8 , Sprite.MIRROR_X );
+		this.spritesheet.FRAME_TOP_LEFT.draw(  this.screen, 8 , 24 , Sprite.MIRROR_Y  );
+		this.spritesheet.FRAME_TOP.draw(  this.screen, 16, 24 , Sprite.MIRROR_Y);
+		this.spritesheet.FRAME_TOP.draw(  this.screen, 24, 24 , Sprite.MIRROR_Y);
+		this.spritesheet.FRAME_TOP.draw(  this.screen, 32, 24 , Sprite.MIRROR_Y);
+		this.spritesheet.FRAME_TOP_LEFT.draw(  this.screen, 40 , 24 , Sprite.MIRROR_Y | Sprite.MIRROR_X );
+		this.spritesheet.FRAME_LEFT.draw(  this.screen, 40, 16 , Sprite.MIRROR_X );
+		
+		
+		
+		for( int y = 0 ; y < this.screen.getHeight() ; y++ ) {
+			for( int x = 0 ; x < this.screen.getWidth() ; x++ ) {
+				pixels[ x + y * this.screen.getWidth() ] = screen.pixels[ x + y * this.screen.getWidth() ];
+			}
+		}
+		
+		
+		
+		Graphics g = bs.getDrawGraphics();
+
+		g.fillRect( 0, 0, this.getWidth(), this.getHeight() );
+
+		g.drawImage( this.image, 0, 0, this.getWidth(), this.getHeight(), null );
+		g.dispose();
+
+		bs.show();
+	}
+
+	@Override
+	public void run()
+	{
+		this.init();
+
+		while (running) {
+			this.render();
+
+			try {
+				Thread.sleep( 50 );
+			} catch (InterruptedException e) {
+				// TODO Auto-generated catch block
+				e.printStackTrace();
+			}
+
+		}
+	}
+
+	public void start()
+	{
+		this.running = true;
+		new Thread( this ).start();
+	}
+
+	public void stop()
+	{
+		this.running = false;
+	}
+
+	private void init()
+	{
+		// initialize game and resources
+
+		try {
+			this.screen = new Screen( Game.WIDTH , Game.HEIGHT );
+			this.spritesheet = new SpriteSheet( ImageIO.read( Game.class.getResourceAsStream( "/icons.png" ) ) );
+		} catch (IOException e) {
+			// TODO Auto-generated catch block
+			e.printStackTrace();
+		}
+
+	}
+
+	/**
+	 * @param args
+	 */
+	public static void main( String[] args )
+	{
+		Game game = new Game();
+
+		game.setMinimumSize( new Dimension( WIDTH * SCALE, HEIGHT * SCALE ) );
+		game.setMaximumSize( new Dimension( WIDTH * SCALE, HEIGHT * SCALE ) );
+		game.setPreferredSize( new Dimension( WIDTH * SCALE, HEIGHT * SCALE ) );
+
+		JFrame frame = new JFrame( Game.NAME );
+
+		frame.setDefaultCloseOperation( JFrame.EXIT_ON_CLOSE );
+		frame.setLayout( new BorderLayout() );
+		frame.add( game, BorderLayout.CENTER );
+		frame.pack();
+
+		frame.setResizable( false );
+		frame.setLocationRelativeTo( null );
+		frame.setVisible( true );
+
+		game.start();
+	}
+}

File src/be/devinity/minicraft/gfx/Screen.java

+package be.devinity.minicraft.gfx;
+
+import java.util.Random;
+
+import be.devinity.minicraft.Game;
+
+public class Screen
+{
+	protected int width;
+	protected int height;
+	
+	public int[] pixels;
+	
+	public Screen( int width , int height )
+	{
+		this.width = width;
+		this.height = height;
+		
+		this.pixels = new int[ width * height ];
+	}
+
+	/**
+	 * @return the width
+	 */
+	public int getWidth()
+	{
+		return width;
+	}
+
+	/**
+	 * @param width the width to set
+	 */
+	public void setWidth( int width )
+	{
+		this.width = width;
+	}
+
+	/**
+	 * @return the height
+	 */
+	public int getHeight()
+	{
+		return height;
+	}
+
+	/**
+	 * @param height the height to set
+	 */
+	public void setHeight( int height )
+	{
+		this.height = height;
+	}
+	
+	
+	public void drawRandomColorStatic()
+	{
+		Random r = new Random();
+
+		for (int y = 0; y < Game.HEIGHT; y++) {
+			for (int x = 0; x < Game.WIDTH; x++) {
+				// int cc = screen.pixels[x + y * screen.w];
+				// if (cc < 255) pixels[x + y * WIDTH] = colors[cc];
+				int cy = r.nextInt( 50 ) + 205 / Game.HEIGHT * y;
+				int cx = r.nextInt( 50 ) + 205 / Game.WIDTH * x;
+				;
+				int cd = (int) ( ( Game.WIDTH * 1.0 / Game.HEIGHT ) * Math.abs( x - ( Game.WIDTH - y * ( Game.WIDTH * 1.0 / Game.HEIGHT ) ) ) );
+				cd += 10 - r.nextInt( 20 );
+				cd = Math.abs(  cd  );
+				
+				this.pixels[x + y * Game.WIDTH] = ( ( cd << 16 ) + ( cy << 8 ) + ( cx ) );
+				
+//				if( x < 10 && y < 10 ) {
+//					pixels[x + y * Game.WIDTH] = this.spritesheet.pixels[ x + y * this.spritesheet.width ];
+//				}
+				
+				//if( x % 3 == 2 ) pixels[ x + y * Game.WIDTH] = 0;
+				//if( y % 3 == 2 ) pixels[ x + y * Game.WIDTH] = 0;
+				
+			}
+		}
+	}
+}

File src/be/devinity/minicraft/gfx/Sprite.java

+package be.devinity.minicraft.gfx;
+
+public class Sprite
+{
+	public int x;
+	
+	public int y;
+	
+	public int width;
+	
+	public int height;
+
+	private SpriteSheet sheet;
+	
+	public static final int MIRROR_X = 0x01;
+	public static final int MIRROR_Y = 0x02;
+	
+	
+	public Sprite( SpriteSheet sheet , int x , int y , int width , int height )
+	{
+		this.sheet = sheet;
+		this.x = x;
+		this.y = y;
+		this.width = width;
+		this.height = height;
+	}
+	
+	public void draw( Screen screen , int x , int y ) {
+		this.draw( screen , x , y , 0 );
+	}
+	
+	public void draw( Screen screen , int x , int y , int bits )
+	{
+		int px , py;
+		
+		
+		boolean mirrorX = (bits & Sprite.MIRROR_X ) > 0;
+		boolean mirrorY = (bits & Sprite.MIRROR_Y ) > 0;
+		
+		for( int ix = 0 ; ix < this.width ; ix++ ) {
+			px = ix;
+			if( mirrorX ) px = this.width - ix - 1;
+			
+			for( int iy = 0 ; iy < this.height ; iy++ ) {
+				py = iy;
+				if( mirrorY ) py = this.height - iy - 1;
+				
+				int cc = sheet.pixels[ this.x + ix + ( this.y + iy ) * sheet.width ];
+				if( cc != 0 ) {
+					
+					cc = this._tryOverlay( screen.pixels[ px + py * screen.width ] , cc );
+					screen.pixels[ x + px + ( py + y ) * screen.width ] = cc; 
+				}
+				//screen.pixels[ i + j * screen.width ] = ( 255 << 16 ) + ( 255 << 8 ) + (255 );
+			}
+		}
+	}
+	
+	protected int _tryOverlay( int original_color , int new_color )
+	{
+		return ( ( (( new_color >> 16 & 255 ) + ( original_color >> 16 & 255) ) / 2 ) << 16 ) + // R
+		        ( ( (( new_color >> 8 & 255 ) + ( original_color >> 8 & 255 ) ) / 2 ) << 8 ) +   // G
+		        ( ( (( new_color >> 16 & 255 ) + ( original_color >> 16 & 255 ) ) / 2 ) );       // B
+	}
+	
+}

File src/be/devinity/minicraft/gfx/SpriteSheet.java

+package be.devinity.minicraft.gfx;
+
+import java.awt.image.BufferedImage;
+
+public class SpriteSheet
+{
+
+	public int width;
+
+	public int height;
+
+	public int[] pixels;
+
+	public Sprite FRAME_TOP_LEFT;
+
+	public Sprite FRAME_TOP;
+
+	public Sprite FRAME_LEFT;
+	
+
+
+	public SpriteSheet(BufferedImage image)
+	{
+		this.width = image.getWidth();
+		this.height = image.getHeight();
+		this.pixels = image.getRGB( 0, 0, this.width, this.height, null, 0, this.width );
+
+		for (int i = 0; i < pixels.length; i++) {
+			//this.pixels[i] = ( this.pixels[i] & 0xff ) / 64;
+			this.pixels[i] = ( this.pixels[i] & 0xffffff );
+		}
+		
+		this.createSprites();
+	}
+	
+	protected void createSprites()
+	{
+		this.FRAME_TOP_LEFT = new Sprite( this , 0 , 104 , 8 , 8 );
+		this.FRAME_TOP = new Sprite( this , 8 , 104 , 8 , 8 );
+		this.FRAME_LEFT = new Sprite( this , 16 , 104 , 8 , 8 );
+	}
+}