Book a Demo!
CoCalc Logo Icon
StoreFeaturesDocsShareSupportNewsAboutPoliciesSign UpSign In
epoxy
GitHub Repository: epoxy/proj11
Path: blob/master/SLICK_HOME/src/org/newdawn/slick/opengl/SlickCallable.java
1461 views
1
package org.newdawn.slick.opengl;
2
3
import org.lwjgl.opengl.GL11;
4
import org.newdawn.slick.SlickException;
5
import org.newdawn.slick.opengl.renderer.Renderer;
6
7
/**
8
* A utility to allow performing GL operations without contaminating the
9
* Slick OpenGL state. Note this will not protect you from OpenGL programming errors
10
* like a glBegin() without a glEnd(), or glPush() without glPop() etc.
11
*
12
* Expected usage:
13
*
14
* <code>
15
* SlickCallable callable = new SlickCallable() {
16
* public performGLOperations() throws SlickException {
17
* GL.glTranslate(0,0,1);
18
* glBegin(GL.GL_POLYGONS);
19
* glVertex(..);
20
* ...
21
* glEnd();
22
* }
23
* }
24
* callable.call();
25
* </code>
26
*
27
* Alternatively you can use the static methods directly
28
*
29
* <code>
30
* SlickCallable.enterSafeBlock();
31
*
32
* GL.glTranslate(0,0,1);
33
* glBegin(GL.GL_POLYGONS);
34
* glVertex(..);
35
* ...
36
* glEnd();
37
*
38
* SlickCallable.leaveSafeBlock();
39
* </code>
40
*
41
* @author kevin
42
*/
43
public abstract class SlickCallable {
44
/** The last texture used */
45
private static Texture lastUsed;
46
/** True if we're in a safe block */
47
private static boolean inSafe = false;
48
49
50
/**
51
* Enter a safe block ensuring that all the OpenGL state that slick
52
* uses is safe before touching the GL state directly.
53
*/
54
public static void enterSafeBlock()
55
{
56
if (inSafe) {
57
return;
58
}
59
60
Renderer.get().flush();
61
lastUsed = TextureImpl.getLastBind();
62
TextureImpl.bindNone();
63
GL11.glPushAttrib(GL11.GL_ALL_ATTRIB_BITS);
64
GL11.glPushClientAttrib(GL11.GL_ALL_CLIENT_ATTRIB_BITS);
65
GL11.glMatrixMode(GL11.GL_MODELVIEW);
66
GL11.glPushMatrix();
67
GL11.glMatrixMode(GL11.GL_PROJECTION);
68
GL11.glPushMatrix();
69
GL11.glMatrixMode(GL11.GL_MODELVIEW);
70
71
inSafe = true;
72
}
73
74
/**
75
* Leave a safe block ensuring that all of Slick's OpenGL state is
76
* restored since the last enter.
77
*/
78
public static void leaveSafeBlock()
79
{
80
if (!inSafe) {
81
return;
82
}
83
84
GL11.glMatrixMode(GL11.GL_PROJECTION);
85
GL11.glPopMatrix();
86
GL11.glMatrixMode(GL11.GL_MODELVIEW);
87
GL11.glPopMatrix();
88
GL11.glPopClientAttrib();
89
GL11.glPopAttrib();
90
91
if (lastUsed != null) {
92
lastUsed.bind();
93
} else {
94
TextureImpl.bindNone();
95
}
96
97
inSafe = false;
98
}
99
100
/**
101
* Cause this callable to perform it's GL operations (@see performGLOperations()). This
102
* method will block until the GL operations have been performed.
103
*
104
* @throws SlickException Indicates a failure while performing the GL operations or
105
* maintaing SlickState
106
*/
107
public final void call() throws SlickException {
108
enterSafeBlock();
109
110
performGLOperations();
111
112
leaveSafeBlock();
113
}
114
115
/**
116
* Perform the GL operations that this callable is intended to. This operations should
117
* not effect the slick OpenGL state.
118
*
119
* @throws SlickException Indicates a failure of some sort. This is user exception
120
*/
121
protected abstract void performGLOperations() throws SlickException;
122
}
123
124