【问题标题】:Is my screensaver overloading the Mac OSX Catalina Screensaver Engine?我的屏幕保护程序是否使 Mac OSX Catalina 屏幕保护程序引擎过载?
【发布时间】:2020-10-04 01:31:04
【问题描述】:

我开发了一个我认为是处理中的简洁小程序。它设置了一些 rng,然后绘制每个 rng 的每个输出的计数。这是处理代码。我认为这方面没有任何问题,因为它在处理过程中运行良好:

int [] randomCounts;
int [] gaussianCounts;
int [] monteCarloCounts;
int [] invMonteCarloCounts;
void setup(){
  size(1000,700);
  randomCounts = new int[500];
  gaussianCounts = new int[500];
  monteCarloCounts = new int[500];
  invMonteCarloCounts = new int[500];
}
float invMonteCarlo(){
  while (true){
    float r1 = random(1);
    float r2 = random(1);
    if (r2>r1){
      return r1;
    }
  }
}
float monteCarlo(){
  while (true){
    float r1 = random(1);
    float r2 = random(1);
    if (r2<r1){
      return r1;
    }
  }
}
void draw(){
  background(255);
  int randIndex = int(random(randomCounts.length));
  int gaussIndex = int(randomGaussian()*20+250);
  int monteIndex = int(monteCarlo()*monteCarloCounts.length);
  int invMonteIndex = int(invMonteCarlo()*invMonteCarloCounts.length);
  randomCounts[randIndex]++;
  gaussianCounts[gaussIndex]++;
  monteCarloCounts[monteIndex]++;
  invMonteCarloCounts[invMonteIndex]++;
  
  noStroke();
  int w = width/randomCounts.length;
  fill(0,255,0,150);
  for (int x = 0; x < gaussianCounts.length;x++){
    rect(x*w,height-gaussianCounts[x],w-1,gaussianCounts[x]);
  }
  fill(0,0,255,150);
  for (int x = 0;x < monteCarloCounts.length;x++){
    rect(x*w,height-monteCarloCounts[x],w-1,monteCarloCounts[x]);
  }
  fill(255,255,0,150);
  for (int x = 0;x < invMonteCarloCounts.length;x++){
    rect(x*w,height-invMonteCarloCounts[x],w-1,invMonteCarloCounts[x]);
  }
  fill(255,0,0,150);
  for (int x = 0; x < randomCounts.length; x++){
    rect(x*w,height-randomCounts[x],w-1,randomCounts[x]);
  }
}
void mousePressed(){
  for (int x = 0; x < randomCounts.length; x++){
    randomCounts[x] = 0;
    gaussianCounts[x] = 0;
    monteCarloCounts[x] = 0;
    invMonteCarloCounts[x] = 0;
  }
}

此代码再次运行良好。我试图在 swift 中做基本相同的事情(这有点疯狂),XCode 没有指出任何错误。但是,当我在系统偏好设置中安装保护程序时,预览只是黑色并停止运行,即使是其他屏幕保护程序,也无法实际运行屏幕保护程序。它似乎卡在某个地方,从而阻止它在引擎中实际运行。 这是我的快速代码:

import Foundation
import ScreenSaver
import GameplayKit

class RandomGraphView: ScreenSaverView {
    private var randomCounts: [Int] = [500]
    private var gaussianCounts: [Int] = [500]
    private var shuffledCounts: [Int] = [500]
    private var monteCarloCounts: [Int] = [500]
    private var invMonteCarloCounts: [Int] = [500]
    
    private func MonteCarlo() -> Int{
        while true {
            let r1 = Int.random(in: 1...360)
            let r2 = Int.random(in: 1...360)
            if r2<r1 {
                return r1
            }
        }
    }

    private func invMonteCarlo() -> Int{
        while true {
            let r1 = Int.random(in: 1...360)
            let r2 = Int.random(in: 1...360)
            if r2>r1 {
                return r1
            }
        }
    }

    private func gaussian() -> Int{
        let gaussianD6 = GKGaussianDistribution(lowestValue: 1, highestValue: 360)
        return gaussianD6.nextInt()
    }

    private func shuffled() -> Int{
        let shuffledD6 = GKShuffledDistribution(lowestValue: 1, highestValue: 360)
        return shuffledD6.nextInt()
    }

    private func pullNumbers(){
        let randIndex = Int.random(in: 1...360)
        let gaussIndex = gaussian()
        let shuffIndex = shuffled()
        let monteCarloIndex = MonteCarlo()
        let invMonteCarloIndex = invMonteCarlo()
        randomCounts[randIndex]+=1
        gaussianCounts[gaussIndex]+=1
        shuffledCounts[shuffIndex]+=1
        monteCarloCounts[monteCarloIndex]+=1
        invMonteCarloCounts[invMonteCarloIndex]+=1
    }

    private func drawRandomBars(){
        for n in 1...360{
            let countBarRect = NSRect(x:(n-1)*4,y: 900, width:4, height: randomCounts[n])
            let countBar = NSBezierPath(rect: countBarRect)
            NSColor.red.setFill()
            countBar.fill()
        }
    }
    
    private func drawGaussianBars(){
        for n in 1...360{
            let countBarRect = NSRect(x: (n-1)*4, y: 900, width: 4, height: gaussianCounts[n])
            let countBar = NSBezierPath(rect: countBarRect)
            NSColor.green.setFill()
            countBar.fill()
        }
    }
    
    private func drawShuffledBars(){
        for n in 1...360{
            let countBarRect = NSRect(x: (n-1)*4, y: 900, width: 4, height: shuffledCounts[n])
            let countBar = NSBezierPath(rect: countBarRect)
            NSColor.systemBrown.setFill()
            countBar.fill()
        }
    }
    
    private func drawMonteCarloBars(){
        for n in 1...360{
            let countBarRect = NSRect(x: (n-1)*4, y: 900, width: 4, height: monteCarloCounts[n])
            let countBar = NSBezierPath(rect: countBarRect)
            NSColor.magenta.setFill()
            countBar.fill()
        }
    }
    
    private func drawInverseMonteCarloBars(){
        for n in 1...360{
            let countBarRect = NSRect(x: (n-1)*4, y: 900, width: 4, height: invMonteCarloCounts[n])
            let countBar = NSBezierPath(rect: countBarRect)
            NSColor.systemIndigo.setFill()
            countBar.fill()
        }
    }
    private func drawBackground(_ color:NSColor){
        let background = NSBezierPath(rect: bounds)
        color.setFill()
        background.fill()
    }

    override init?(frame: NSRect, isPreview: Bool){
        super.init(frame: frame, isPreview: isPreview)
    }

    @available(*, unavailable)
    required init?(coder decoder: NSCoder){
        fatalError("init(coder:) has not been implemented")
    }

    override func draw(_ rect: NSRect){
        drawBackground(.white)
        pullNumbers()
        drawRandomBars()
        drawGaussianBars()
        drawShuffledBars()
        drawMonteCarloBars()
        drawInverseMonteCarloBars()
    }
    

    override func animateOneFrame(){
        super.animateOneFrame()
    }

}

抱歉,代码太长了,我真的不知道我做错了什么。我从this tutorial 复制了大部分脚本,效果很好。

【问题讨论】:

    标签: swift macos screensaver


    【解决方案1】:

    你必须在 animateOneFrame 中设置 needsDisplay,像这样:

    override func animateOneFrame(){
        needsDisplay = true
    }
    

    【讨论】:

      【解决方案2】:

      这一行

      private var randomCounts: [Int] = [500]
      

      不等于

      randomCounts = new int[500];
      

      前者创建一个包含单个数字 500 的数组,而后者创建一个包含 500 个整数的数组(我不是处理/Java 专家,但我假设它们被初始化为 0)。所以当randIndex、gaussIndex等大于0时你的程序会崩溃,因为索引会越界。

      您可能想使用init(repeating:count:): private var randomCounts: [Int] = Array(repeating: 0, count: 500)。

      为了避免这种问题,你可以改进你的代码,不要到处写1...360,而是使用randomCounts.count来表示数组的长度。同样,不要使用for n in 1...360,而是使用for (n, height) in monteCarloCounts.enumerated() 之类的东西。

      【讨论】:

      • 感谢您的帮助。在我看到你的答案之前,我已经弄清楚了数组的问题,所以事情不再崩溃了,但它仍然没有渲染。
      • 您应该编辑您的问题以包含您更新的代码以及更多关于您尝试调试的内容,否则很难用很少的信息提供帮助。
      猜你喜欢
      • 2018-02-04
      • 1970-01-01
      • 1970-01-01
      • 2011-03-01
      • 2012-01-11
      • 1970-01-01
      • 2020-11-29
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多