处理cpu限制

时间:2013-11-20 09:37:02

标签: limit cpu processing

我正在制作带有Processing的2D游戏,当我添加一个灯光时,fps从30减少到20~,如果我再添加一个,它会下降更多。但是处理只使用%c的cpu而不管是什么,我怎么能增加它,所以我可以看到30 fps?

void castLight(int mapNum){
  color c = color(0, 0, 0);
  if(mapNum == 1){
    for(int x = 0; x < width; x++){
      for(int y = 0; y < height; y++){
        if(improvedLights){
          putShadow = improvedLights();//not working
        } else {
          putShadow = true;
        }
        for(int i = 0; i < torchHolder1.size(); i++){
          if(dist(x, y, torchHolderX1.get(i), torchHolderY1.get(i)) <= maxDist){
            if(improvedLights){
              putShadow = improvedLights();
            } else {
              putShadow = false;
            }
          }
        }
        if(putShadow){
          int loc = x+y*width;
          float r,g,b;
          r = red (pixels[loc]);
          g = green (pixels[loc]);
          b = blue (pixels[loc]);
          r -= 100;
          g -= 100;
          b -= 100;
          r = constrain(r, 0, 255);
          g = constrain(g, 0, 255);
          b = constrain(b, 0, 255);
          if(torchAnimCounter % 31 < 16){
            c = color(r, g, b);
        } else {
            c = color(r+10, g+10, b+10);
          }
          pixels[y*width + x] = c;
        }
      }
    }
    for(int i = 0; i < torchHolderX1.size(); i++){
      for(int x = torchHolderX1.get(i) - maxDist; x < torchHolderX1.get(i) + maxDist; x++){
        if(x - maxDist < torchHolderX1.get(i) && x + maxDist > torchHolderX1.get(i)){
          torchCrossX = true;
        } else {
          torchCrossX = false;
        }
        for(int y = torchHolderY1.get(i) - maxDist; y < torchHolderY1.get(i) + maxDist; y++){
          if(y - maxDist < torchHolderY1.get(i) && y + maxDist > torchHolderY1.get(i)){
            torchCrossY = true;
          } else {
            torchCrossY = false;
          }
          float d = dist(x, y, torchHolderX1.get(i), torchHolderY1.get(i));
          if(torchCrossX && torchCrossY){
            if(dist(x, y, torchHolderX1.get(i), torchHolderY1.get(i)) <= maxDist){
              if(x > 0 && x < width && y > 0 && y < height){
                int loc = x+y*width;
                float r,g,b;
                r = red (pixels[loc]);
                g = green (pixels[loc]);
                b = blue (pixels[loc]);
                float adjustBrightness = maxDist-d-100;
                r += adjustBrightness;
                g += adjustBrightness;
                b += adjustBrightness;
                r = constrain(r, 0, 255);
                g = constrain(g, 0, 255);
                b = constrain(b, 0, 255);
                if(torchAnimCounter % 31 < 16){
                  c = color(r, g, b);
                } else {
                  c = color(r+10, g+10, b+10);
                }
                pixels[y*width + x] = c;
              } } } } } } 
  }//and same things goes for the second map

1 个答案:

答案 0 :(得分:0)

为什么不使用OPENGL的内置功能来点亮模型?看起来你正在进行大量的像素数据手动处理以调整亮度,大概是为了给出逼真的阴影等等,但放置在带有PointLight(或其他)的场景中的3D模型将完成所有这些并且它将需要完整GPU的优势。