我已经编写了下面的代码作为我正在编写的游戏引擎的水平渲染部分。我非常高兴它的工作方式,但由于它是由单个块组成的背景/水平图形,它需要快速。
它的外观是从其他地方加载的资源中获取图形,如果它在查看窗口中,则将其缩放到块上。
Blocksize是块的大小,可以增加缩小,并用于缩放块,使缩放效果。
如何从速度和缩短代码的速度来提高代码的可读性?
public void render(Graphics g){
//Ensure player co-ords updated
camerax=playerx-(gamewidth/2);
cameray=playery-(gameheight/2);
int blockwidth=blocksize-2;
//Draw coloured blocks
int screenx=-(int)camerax-blocksize;
for (int x=0;x<sizex;x++){
screenx+=blocksize;
if (screenx>-blocksize && screenx<gamewidth){
int screeny=-(int)cameray-blocksize;
for (int y=0;y<sizey;y++){
screeny+=blocksize;
if (screeny>-blocksize && screeny<gameheight){
if (tiles[x][y][0]>0){
g.drawImage(levelgraphics, screenx,screeny, screenx+blockwidth,screeny+blockwidth, graphicsize,0,graphicsize*2,graphicsize, null);
} else {
g.setColor(new Color( tiles[x][y][1]));
g.fillRect(screenx,screeny,blockwidth,blockwidth);
g.setColor(Color.WHITE);
g.drawString(String.valueOf(x+y*sizex), screenx+5,screeny+20);
}
}
}
}
}
//world - camera = screen, algebra that world = screen + camera
//Add camerax to mouse screen co-ords to convert to world co-ords.
int cursorx_world=(int)camerax+(int)GameInput.mousex;
int cursorx_grid=(int)cursorx_world/blocksize; // World Co-ords / gridsize give grid co-ords
int cursorx_screen=-(int)camerax+(cursorx_grid*blocksize);
//Add cameray to mouse screen co-ords to convert to world co-ords.
int cursory_world=(int)cameray+(int)GameInput.mousey;
int cursory_grid=(int)cursory_world/blocksize;
int cursory_screen=-(int)cameray+(cursory_grid*blocksize);
//Draw on screen cursor
if (cursorx_grid>=0 && cursory_grid>=0 && cursorx_grid<sizex && cursory_grid<sizey){
//if (detect(cursorx_grid,cursory_grid)){
//if (tiles[cursorx_grid][cursory_grid][0]>1){
g.setColor(Color.yellow);
g.drawRect(cursorx_screen,cursory_screen,blockwidth,blockwidth);
//}
}
//Draw on screen map
for (int x=0;x<sizex;x++){
for (int y=0;y<sizey;y++){
g.setColor(new Color((int) tiles[x][y][1]));
g.fillRect(mapx+x*mapsize, mapy+y*mapsize, mapsize, mapsize);
}
}
//Outline
g.drawRect(mapx,mapy,mapsize*sizex ,mapsize*sizey );
//Map location box
g.setColor(Color.RED);
g.drawRect(mapx+mapsize*((int)camerax/blocksize),mapy+mapsize*((int)cameray/blocksize),(gamewidth/blocksize)*mapsize ,(gameheight/blocksize)*mapsize );
}最后一段代码将地图添加到屏幕上,并在其上弹出一个缩放框,这样玩家就可以看到他们正在查看的地图的哪一部分。
发布于 2014-03-26 17:39:15
您的代码很难阅读,因为所有的操作符都挤在一起。
一个遗漏的分支预测会迫使您的CPU放弃它所做的所有推测执行。虽然现代CPU非常擅长分支预测,但在可能的情况下避免分支是一个很好的实践。在您的例子中,它将减少循环中的迭代。
int screenx = -(int)camerax - blocksize;
for (int x = 0; x < sizex; x++){
screenx += blocksize;
if (screenx > -blocksize && screenx < gamewidth){应:
int screenx = -(int)camerax - blocksize;
int xstart = Math.max(0, -(blocksize + screenx) / blocksize);
int xend = Math.min(sizex, (gamewidth - screenx) / blocksize);
screenx += blocksize * xstart; // Edit: We need to keep this value updated too.
for (int x = xstart; x < xend; ++x){
screenx += blocksize;内环也是如此。取决于相机大小和级别大小的比率(如果我正确地解释了您的代码),这可能会带来很大的加速。
提示:y方向迭代开始值和结束值在x方向上独立于当前迭代,因此在进入第一个循环之前计算yend和ystart。
注意:由于我不知道所有变量是什么,上面的xstart和xend可能是一个接一个的,请根据口味进行调整。
编辑: Oops刚刚意识到这就是Neil在他回答的第一点中提到的。但我会把这个放在这里,因为这是他说的一个例子。
Edit2:在进入循环之前错过了screenx的增量。
发布于 2014-03-26 09:49:36
我注意到三件事:
我希望这能帮上忙。
https://codereview.stackexchange.com/questions/45381
复制相似问题