Results 1 to 1 of 1
  1. #1
    Heloic is offline Member
    Join Date
    Jan 2013
    Posts
    1
    Rep Power
    0

    Default Pixels show on map instead of Tiles

    Hey guys, im makeing a game, and i should get tile maps. But i get pixel maps. Here is a image of what happens [Moderator edit: link removed]. Can anybody help?



    Java Code:
    //Code for level to render the map. 
    
    package com.cmstudios.rain.level;
    
    import com.cmstudios.rain.graphics.Screen;
    import com.cmstudios.rain.level.tile.Tile;
    
    public class Level {
    	
    	protected int width, height;
    	protected int[] tiles;
    	
    	public Level(int width, int height){
    		this.width = width;
    		this.height = height;
    		tiles = new int[width* height];
    		generateLevel();
    		
    	}
    	
    	public Level(String path){
    		loadLevel(path);
    	}
    
    	protected void generateLevel() {
    	}
    	
    	private void loadLevel(String path){
    	}
    	
    	public void update(){
    		
    	}
    	
    	private void time() {
    	}
    	
    	public void render(int xScroll, int yScroll, Screen screen){
    		screen.setOffset(xScroll, yScroll);
    		int x0 = xScroll >> 4;
    		int x1 = (xScroll + screen.width) >> 4;
    		int y0 = yScroll >> 4;
    		int y1 = (yScroll + screen.height) >>4;		
    		
    		for(int y = y0; y < y1; y++){
    			for(int x = x0; x < x1; x++){
    				getTile(x, y).render(x, y, screen);				
    			}
    		}
    	}
    	
    	public Tile getTile(int x, int y){
    		if(tiles[x + y * width] == 0) return Tile.grass;
    		return Tile.voidTile;
    	}
    }
    Java Code:
    //Code that sets the tile to be tile not pixel
    //grasstile
    
    package com.cmstudios.rain.level.tile;
    
    import com.cmstudios.rain.graphics.Screen;
    import com.cmstudios.rain.graphics.Sprite;
    
    public class GrassTile extends Tile {
    
    	public GrassTile(Sprite sprite) {
    		super(sprite);
    	}
    	
    	public void render(int x , int y, Screen screen){
    		screen.renderTile(x << 4 , y << 4 , this);
    		
    	}
    
    }
    Java Code:
    //screen 
    package com.cmstudios.rain.graphics;
    
    import java.util.Random;
    
    import com.cmstudios.rain.level.tile.Tile;
    
    public class Screen {
    	
    	public int width, height;
    	public int[] pixels;
    	public final int MAP_SIZE = 64;
    	public final int MAP_SIZE_MASK = MAP_SIZE - 1;
    	
    	public int xOffset, yOffset;
    	
    	public int[] tiles = new int[MAP_SIZE * MAP_SIZE];
    	private Random random = new Random();
    	
    	public Screen(int width, int height){
    		this.width = width;
    		this.height = height;
    		pixels = new int[width * height];
    		
    		for(int i = 0; i < MAP_SIZE * MAP_SIZE; i++) {
    			tiles[i] = random.nextInt(0xffffff);
    			tiles[0] = 0;
    		}
    	}
    	
    	public void clear(){
    		for(int i = 0; i < pixels.length; i++){
    			pixels[i] = 0;
    		}
    	}
    	
    	
    	
    	public void renderTile(int xp, int yp, Tile tile){
    		xp -= xOffset;
    		yp -= yOffset;
    		for(int y = 0; y < tile.sprite.SIZE; y++){
    			int ya = y + yp;
    			for(int x = 0; x < tile.sprite.SIZE; x++){
    				int xa = y + xp;
    				if(xa < 0 || xa >= width || ya < 0 || ya >= width)break;
    				pixels[xa+ya*width] = tile.sprite.pixels[x+y*tile.sprite.SIZE];
    			}
    		}
    	}
    	
    	public void setOffset(int xOffset, int yOffset){
    		this.xOffset = xOffset;
    		this.yOffset = yOffset;
    	}
    }
    Java Code:
    //game class
    
    package com.cmstudios.rain;
    
    import java.awt.Canvas;
    import java.awt.Color;
    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.cmstudios.rain.graphics.Screen;
    import com.cmstudios.rain.input.Keyboard;
    import com.cmstudios.rain.level.Level;
    import com.cmstudios.rain.level.RandomLevel;
    
    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;
    	
    	public static String title = "Rain";
    	
    	private Thread thread;
    	private JFrame frame;
    	private Keyboard key;
    	private Level level;
    	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();
    		key = new Keyboard();
    		level = new RandomLevel(64, 64);
    		
    		addKeyListener(key);
    		
    		
    	}
    	
    	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();
    		final double ns = 1000000000.0 / 60.0;
    		double delta = 0;
    		int frames = 0;
    		int updates = 0;
    		requestFocus();
    		while (running) {
    			long now = System.nanoTime();
    			delta += (now - lastTime) / ns;
    			lastTime = now;
    			while(delta >= 1){
    				update();
    				updates++;
    				delta--;
    			}
    			render();
    			frames++;
    			
    			if(System.currentTimeMillis()- timer > 1000){
    				timer += 1000;
    				System.out.println(updates + "UPS," + frames + "FPS, ");
    				frame.setTitle(title + "  |  " + updates + "UPS," + frames + "FPS, " );
    				updates = 0;
    				frames = 0;
    			}
    		}
    	}
    	
    	int x = 0, y = 0;
    	
    	
    	public void update(){
    		key.update();
    		if(key.up) y--;
    		if(key.down) y++;
    		if(key.left) x--;
    		if(key.right) x++;
    	}
    	
    	public void render(){
    		BufferStrategy bs = getBufferStrategy();
    		if(bs == null){
    			createBufferStrategy(3);
    			return;
    		}
    		
    		screen.clear();
    		level.render(x, y, screen);
    		
    		
    		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){
    		Game game = new Game();
    		game.frame.setResizable(false);
    		game.frame.setTitle(Game.title);
    		game.frame.add(game);
    		game.frame.pack();
    		game.frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
    		game.frame.setLocationRelativeTo(null);
    		game.frame.setVisible(true);
    		
    		game.start();
    	}
    }
    Last edited by DarrylBurke; 01-09-2013 at 09:13 AM. Reason: Removed link

Similar Threads

  1. tiles problem
    By rahulseven in forum Web Frameworks
    Replies: 1
    Last Post: 09-01-2010, 03:04 PM
  2. Replies: 3
    Last Post: 05-27-2010, 11:54 AM
  3. struts tiles
    By sasikumarm in forum Web Frameworks
    Replies: 0
    Last Post: 03-27-2008, 03:36 PM
  4. netbeans 6.0 not show commpunent or show blank page
    By fahimaamir in forum NetBeans
    Replies: 1
    Last Post: 01-26-2008, 07:20 AM
  5. Tiles in JSF
    By Heather in forum SWT / JFace
    Replies: 2
    Last Post: 06-30-2007, 04:20 PM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •