-
-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathPSurfaceNone.java
More file actions
371 lines (274 loc) · 9.75 KB
/
PSurfaceNone.java
File metadata and controls
371 lines (274 loc) · 9.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
/* -*- mode: java; c-basic-offset: 2; indent-tabs-mode: nil -*- */
/*
Part of the Processing project - http://processing.org
Copyright (c) 2015 The Processing Foundation
This library is free software; you can redistribute it and/or
modify it under the terms of the GNU Lesser General Public
License as published by the Free Software Foundation, version 2.1.
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General
Public License along with this library; if not, write to the
Free Software Foundation, Inc., 59 Temple Place, Suite 330,
Boston, MA 02111-1307 USA
*/
package processing.core;
/**
* Surface that's not really visible. Used for PDF and friends, or as a base
* class for other drawing surfaces. It includes the standard rendering loop.
*/
public class PSurfaceNone implements PSurface {
protected PApplet sketch;
protected PGraphics graphics;
protected Thread thread;
protected boolean paused;
protected Object pauseObject = new Object();
protected float frameRateTarget = 60;
protected long frameRatePeriod = 1000000000L / 60L;
public PSurfaceNone(PGraphics graphics) {
this.graphics = graphics;
}
@Override
public void initOffscreen(PApplet sketch) {
this.sketch = sketch;
setSize(sketch.sketchWidth(), sketch.sketchHeight());
}
// public Component initComponent(PApplet sketch) {
// return null;
// }
@Override
public void initFrame(PApplet sketch) {
throw new IllegalStateException("initFrame() not available with " +
getClass().getSimpleName());
}
public Object getNative() {
return null;
}
/** Set the window (and dock, or whatever necessary) title. */
@Override
public void setTitle(String title) {
// You're in a utopian PSurface implementation where titles don't exist.
}
@Override
public void setIcon(PImage image) {
// I ain't visible, man.
}
/** Show or hide the window. */
@Override
public void setVisible(boolean visible) {
// I'm always invisible. You can't catch me.
}
/** Set true if we want to resize things (default is not resizable) */
@Override
public void setResizable(boolean resizable) {
// I don't need size to know my worth.
}
@Override
public void placeWindow(int[] location, int[] editorLocation) { }
@Override
public void placePresent(int stopColor) { }
@Override
public void setupExternalMessages() { }
@Override
public void setAlwaysOnTop(boolean always) { }
//
@Override
public void setLocation(int x, int y) {
// I'm everywhere, because I'm nowhere.
}
@Override
public void setSize(int wide, int high) {
if (PApplet.DEBUG) {
//System.out.format("frame visible %b, setSize(%d, %d) %n", frame.isVisible(), wide, high);
new Exception(String.format("setSize(%d, %d)", wide, high)).printStackTrace(System.out);
}
//if (wide == sketchWidth && high == sketchHeight) { // doesn't work on launch
if (wide == sketch.width && high == sketch.height) {
if (PApplet.DEBUG) {
new Exception("w/h unchanged " + wide + " " + high).printStackTrace(System.out);
}
return; // unchanged, don't rebuild everything
}
//throw new RuntimeException("implement me, see readme.md");
sketch.width = wide;
sketch.height = high;
// set PGraphics variables for width/height/pixelWidth/pixelHeight
graphics.setSize(wide, high);
}
// public void initImage(PGraphics graphics) {
// // TODO Auto-generated method stub
//
// }
// public Component getComponent() {
// return null;
// }
// public void setSmooth(int level) {
// // TODO Auto-generated method stub
// }
// void requestFocus() {
// }
// public void blit() {
// // TODO Auto-generated method stub
// }
public void setCursor(int kind) { }
public void setCursor(PImage image, int hotspotX, int hotspotY) { }
public void showCursor() { }
public void hideCursor() { }
//
public Thread createThread() {
return new AnimationThread();
}
public void startThread() {
if (thread == null) {
thread = createThread();
thread.start();
} else {
throw new IllegalStateException("Thread already started in " +
getClass().getSimpleName());
}
}
public boolean stopThread() {
if (thread == null) {
return false;
}
thread = null;
return true;
}
public boolean isStopped() {
return thread == null || !thread.isAlive();
}
// sets a flag to pause the thread when ready
public void pauseThread() {
PApplet.debug("PApplet.run() paused, calling object wait...");
paused = true;
}
// halts the animation thread if the pause flag is set
protected void checkPause() {
if (paused) {
synchronized (pauseObject) {
try {
pauseObject.wait();
// PApplet.debug("out of wait");
} catch (InterruptedException e) {
// waiting for this interrupt on a start() (resume) call
}
}
}
// PApplet.debug("done with pause");
}
public void resumeThread() {
paused = false;
synchronized (pauseObject) {
pauseObject.notifyAll(); // wake up the animation thread
}
}
public void setFrameRate(float fps) {
frameRateTarget = fps;
frameRatePeriod = (long) (1000000000.0 / frameRateTarget);
//g.setFrameRate(fps);
}
public class AnimationThread extends Thread {
public AnimationThread() {
super("Animation Thread");
}
// broken out so it can be overridden by Danger et al
public void callDraw() {
sketch.handleDraw();
}
/**
* Main method for the primary animation thread.
* <A HREF="http://java.sun.com/products/jfc/tsc/articles/painting/">Painting in AWT and Swing</A>
*/
@Override
public void run() { // not good to make this synchronized, locks things up
long beforeTime = System.nanoTime();
long overSleepTime = 0L;
int noDelays = 0;
// Number of frames with a delay of 0 ms before the
// animation thread yields to other running threads.
final int NO_DELAYS_PER_YIELD = 15;
/*
// If size un-initialized, might be a Canvas. Call setSize() here since
// we now have a parent object that this Canvas can use as a peer.
if (graphics.image == null) {
// System.out.format("it's null, sketchW/H already set to %d %d%n", sketchWidth, sketchHeight);
try {
EventQueue.invokeAndWait(new Runnable() {
public void run() {
setSize(sketchWidth, sketchHeight);
}
});
} catch (InterruptedException ie) {
ie.printStackTrace();
} catch (InvocationTargetException ite) {
ite.printStackTrace();
}
// System.out.format(" but now, sketchW/H changed to %d %d%n", sketchWidth, sketchHeight);
}
*/
// un-pause the sketch and get rolling
sketch.start();
while ((Thread.currentThread() == thread) && !sketch.finished) {
checkPause();
// Don't resize the renderer from the EDT (i.e. from a ComponentEvent),
// otherwise it may attempt a resize mid-render.
// Dimension currentSize = canvas.getSize();
// if (currentSize.width != sketchWidth || currentSize.height != sketchHeight) {
// System.err.format("need to resize from %s to %d, %d%n", currentSize, sketchWidth, sketchHeight);
// }
// render a single frame
// try {
// EventQueue.invokeAndWait(new Runnable() {
// public void run() {
// System.out.println("calling draw, finished = " + sketch.finished);
//System.out.println("calling draw, looping = " + sketch.looping + ", frameCount = " + sketch.frameCount);
callDraw();
// EventQueue.invokeLater(new Runnable() {
// public void run() {
// if (sketch.frameCount == 1) {
// requestFocus();
// }
// }
// });
// }
// });
// } catch (InterruptedException ie) {
// ie.printStackTrace();
// } catch (InvocationTargetException ite) {
// ite.getTargetException().printStackTrace();
// }
// wait for update & paint to happen before drawing next frame
// this is necessary since the drawing is sometimes in a
// separate thread, meaning that the next frame will start
// before the update/paint is completed
long afterTime = System.nanoTime();
long timeDiff = afterTime - beforeTime;
//System.out.println("time diff is " + timeDiff);
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(); // call to shutdown libs?
// If the user called the exit() function, the window should close,
// rather than the sketch just halting.
if (sketch.exitCalled) {
sketch.exitActual();
}
}
}
}