Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/CachedRender.java
1456 views
1
package org.newdawn.slick;
2
3
import org.newdawn.slick.opengl.SlickCallable;
4
import org.newdawn.slick.opengl.renderer.Renderer;
5
import org.newdawn.slick.opengl.renderer.SGL;
6
7
/**
8
* A set of rendering that is cached onto the graphics card and hopefully
9
* is quicker to render. Note that there are some things that can't be done
10
* in lists and that all dependent operations must be container. For instance,
11
* any colour configuration can not be assumed from outside the cache.
12
*
13
* Note: The destroy method needs to be used to tidy up. This is pretty important
14
* in this case since there are limited number of underlying resources.
15
*
16
* @author kevin
17
*/
18
public class CachedRender {
19
/** The renderer to use for all GL operations */
20
protected static SGL GL = Renderer.get();
21
22
/** The operations to cache */
23
private Runnable runnable;
24
/** The display list cached to */
25
private int list = -1;
26
27
/**
28
* Create a new cached render that will build the specified
29
* operations on to a video card resource
30
*
31
* @param runnable The operations to cache
32
*/
33
public CachedRender(Runnable runnable) {
34
this.runnable = runnable;
35
build();
36
}
37
38
/**
39
* Build the display list
40
*/
41
private void build() {
42
if (list == -1) {
43
list = GL.glGenLists(1);
44
45
SlickCallable.enterSafeBlock();
46
GL.glNewList(list, SGL.GL_COMPILE);
47
runnable.run();
48
GL.glEndList();
49
SlickCallable.leaveSafeBlock();
50
} else {
51
throw new RuntimeException("Attempt to build the display list more than once in CachedRender");
52
}
53
}
54
55
/**
56
* Render the cached operations. Note that this doesn't call the operations, but
57
* rather calls the cached version
58
*/
59
public void render() {
60
if (list == -1) {
61
throw new RuntimeException("Attempt to render cached operations that have been destroyed");
62
}
63
64
SlickCallable.enterSafeBlock();
65
GL.glCallList(list);
66
SlickCallable.leaveSafeBlock();
67
}
68
69
/**
70
* Destroy this cached render
71
*/
72
public void destroy() {
73
GL.glDeleteLists(list,1);
74
list = -1;
75
}
76
}
77
78