Path: blob/master/SLICK_HOME/src/org/newdawn/slick/CachedRender.java
1456 views
package org.newdawn.slick;12import org.newdawn.slick.opengl.SlickCallable;3import org.newdawn.slick.opengl.renderer.Renderer;4import org.newdawn.slick.opengl.renderer.SGL;56/**7* A set of rendering that is cached onto the graphics card and hopefully8* is quicker to render. Note that there are some things that can't be done9* in lists and that all dependent operations must be container. For instance,10* any colour configuration can not be assumed from outside the cache.11*12* Note: The destroy method needs to be used to tidy up. This is pretty important13* in this case since there are limited number of underlying resources.14*15* @author kevin16*/17public class CachedRender {18/** The renderer to use for all GL operations */19protected static SGL GL = Renderer.get();2021/** The operations to cache */22private Runnable runnable;23/** The display list cached to */24private int list = -1;2526/**27* Create a new cached render that will build the specified28* operations on to a video card resource29*30* @param runnable The operations to cache31*/32public CachedRender(Runnable runnable) {33this.runnable = runnable;34build();35}3637/**38* Build the display list39*/40private void build() {41if (list == -1) {42list = GL.glGenLists(1);4344SlickCallable.enterSafeBlock();45GL.glNewList(list, SGL.GL_COMPILE);46runnable.run();47GL.glEndList();48SlickCallable.leaveSafeBlock();49} else {50throw new RuntimeException("Attempt to build the display list more than once in CachedRender");51}52}5354/**55* Render the cached operations. Note that this doesn't call the operations, but56* rather calls the cached version57*/58public void render() {59if (list == -1) {60throw new RuntimeException("Attempt to render cached operations that have been destroyed");61}6263SlickCallable.enterSafeBlock();64GL.glCallList(list);65SlickCallable.leaveSafeBlock();66}6768/**69* Destroy this cached render70*/71public void destroy() {72GL.glDeleteLists(list,1);73list = -1;74}75}767778