我在Processing中寻找void draw()
函数的源代码。
有什么建议吗?
谢谢
答案 0 :(得分:1)
您可以在GitHub上找到整个Processing库的源代码here。
Here是PApplet
类的直接链接,它定义了draw()
函数。搜索" void draw"找到这个:
public void draw() {
// if no draw method, then shut things down
//System.out.println("no draw method, goodbye");
finished = true;
}
它并不重要,因为您应该定义自己的draw()
函数覆盖这个函数。这就是Processing的工作原理。 您编写draw()
函数的代码。
修改:根据您的评论判断,我猜测您正在尝试了解Processing如何每秒调用draw()
函数60次。这有点复杂。
查看PSurfaceAWT
,这是处理中使用的默认渲染器。搜索" handleDraw"找到这段代码:
public Thread createThread() {
return new AnimationThread() {
@Override
public void callDraw() {
sketch.handleDraw();
render();
}
};
}
AnimationThread在PSurfaceNone
类中定义:
public class AnimationThread extends Thread {
public AnimationThread() {
super("Animation Thread");
}
public void callDraw() {
sketch.handleDraw();
}
@Override
public void run() {
long beforeTime = System.nanoTime();
long overSleepTime = 0L;
int noDelays = 0;
final int NO_DELAYS_PER_YIELD = 15;
sketch.start();
while ((Thread.currentThread() == thread) && !sketch.finished) {
checkPause();
callDraw();
long afterTime = System.nanoTime();
long timeDiff = afterTime - beforeTime;
long sleepTime = (frameRatePeriod - timeDiff) - overSleepTime;
if (sleepTime > 0) { // some time left in this cycle
try {
Thread.sleep(sleepTime / 1000000L, (int) (sleepTime % 1000000L));
noDelays = 0; // Got some sleep, not delaying anymore
} catch (InterruptedException ex) { }
overSleepTime = (System.nanoTime() - afterTime) - sleepTime;
} else { // sleepTime <= 0; the frame took longer than the period
overSleepTime = 0L;
noDelays++;
if (noDelays > NO_DELAYS_PER_YIELD) {
Thread.yield(); // give another thread a chance to run
noDelays = 0;
}
}
beforeTime = System.nanoTime();
}
sketch.dispose();
// If the user called the exit() function, the window should close,
// rather than the sketch just halting.
if (sketch.exitCalled) {
sketch.exitActual();
}
}
}
我删除了一些已注释掉的部分。这可能看起来像很多代码,但它所做的只是设置一个每秒触发60次的线程,通过测量已经过了多少时间并睡眠正确的数量。一个更基本的例子是这样的:
new Thread(){
public void run(){
while(true){
draw();
Thread.sleep(16);
}
}
}
这些是基础知识,但它可以得到更多参与。我推荐谷歌搜索&#34;动画线程&#34;或者&#34;游戏循环&#34;或者&#34;游戏动画线程&#34;并尝试一些事情。