画线算法和裁剪算法集成
关于画线(draw line) 和Clipping 的算法,请参考另外两篇文章, 其中有非常详细的介绍。
本节主要讨论如何集成。在Game.java, 增加了对应的画线和裁剪逻辑
private void render() {
BufferStrategy bs = this.getBufferStrategy();
if (bs == null) {
this.createBufferStrategy(3);
return;// skip one render
}
//populate the bitmap
Bitmap floors = ImageLoader.sprites;
this.screen.render(ticker,floors);
//draw the line
Line line = new Line(10, 10, 60, 80, 255<<8);
Clipping clip= new Clipping();
clip.setArea(0, 0, 64, 68);
clip.clipLine(line);
line.draw(this.screen);
//copy from the screen bitmap to screen buffer
int[] pixels =this.screen.getPixels();
for (int i = 0, len = pixels.length; i < len; i++) {
this.screenPixels[i] = pixels[i];
}
Graphics g = bs.getDrawGraphics();
g.drawImage(this.screenImage, 0, 0, WIDTH * SCALE, HEIGHT * SCALE, null);
g.dispose();
bs.show();
}
因为本节只是介绍如何使用, 因此非常的简单明了. 下一节我们将介绍更有意思的主题精灵(Spirit).