Equinox

Untitled

May 23rd, 2012
124
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 7.40 KB | None | 0 0
  1. package com.pi.graphics.device.opengl;
  2.  
  3. import java.awt.Color;
  4. import java.awt.Font;
  5. import java.awt.Rectangle;
  6. import java.awt.geom.Rectangle2D;
  7.  
  8. import javax.media.opengl.GL;
  9. import javax.media.opengl.GL2;
  10. import javax.media.opengl.GLAutoDrawable;
  11. import javax.media.opengl.GLEventListener;
  12. import javax.media.opengl.GLProfile;
  13. import javax.media.opengl.awt.GLCanvas;
  14.  
  15. import com.jogamp.opengl.util.FPSAnimator;
  16. import com.jogamp.opengl.util.awt.TextRenderer;
  17. import com.jogamp.opengl.util.texture.Texture;
  18. import com.pi.common.game.ObjectHeap;
  19. import com.pi.graphics.device.DisplayManager;
  20. import com.pi.graphics.device.GraphicsStorage;
  21. import com.pi.graphics.device.IGraphics;
  22.  
  23. public class GLGraphics extends IGraphics implements GLEventListener {
  24.     private final FPSAnimator animator;
  25.     private final TextRendererProvider txtRender;
  26.     private final TextureManager textureManager;
  27.     private Rectangle cliparea;
  28.  
  29.     public GLGraphics(DisplayManager mgr) {
  30.         super(mgr);
  31.         GLProfile.initSingleton(false);
  32.         canvas = new GLCanvas();
  33.         super.mgr.getSource().getContainer().add(canvas);
  34.         canvas.addGLEventListener(this);
  35.         canvas.setSize(super.mgr.getSource().getContainer().getSize());
  36.         cliparea = canvas.getBounds();
  37.         canvas.setLocation(0, 0);
  38.         animator = new FPSAnimator(mgr.maxFPS);
  39.         animator.add(canvas);
  40.         animator.start();
  41.         mgr.getSource().getLog().fine("Started graphics thread");
  42.         txtRender = new TextRendererProvider(mgr.getSource());
  43.         textureManager = new TextureManager(this, mgr.getSource());
  44.     }
  45.  
  46.     private GL2 gl;
  47.     private GLAutoDrawable glCore;
  48.     public final GLCanvas canvas;
  49.  
  50.     public GLAutoDrawable getCore() {
  51.         return glCore;
  52.     }
  53.  
  54.     @Override
  55.     public void drawLine(int x1, int y1, Color aC, int x2, int y2, Color bC) {
  56.         gl.glBegin(GL.GL_LINES);
  57.         gl.glColor4i(aC.getRed(), aC.getGreen(), aC.getBlue(), aC.getAlpha());
  58.         gl.glVertex2f(x1, y1);
  59.         gl.glColor4i(bC.getRed(), bC.getGreen(), bC.getBlue(), bC.getAlpha());
  60.         gl.glVertex2f(x2, y2);
  61.         gl.glEnd();
  62.     }
  63.  
  64.     @Override
  65.     public void drawLine(int x1, int y1, int x2, int y2) {
  66.         gl.glBegin(GL.GL_LINES);
  67.         gl.glVertex2f(x1, y1);
  68.         gl.glVertex2f(x2, y2);
  69.         gl.glEnd();
  70.     }
  71.  
  72.     @Override
  73.     public void drawRect(int x, int y, int width, int height) {
  74.         gl.glBegin(GL2.GL_LINE_LOOP);
  75.         gl.glVertex2f(x, y);
  76.         gl.glVertex2f(x + width, y);
  77.         gl.glVertex2f(x + width, (y + height));
  78.         gl.glVertex2f(x, (y + height));
  79.         gl.glEnd();
  80.     }
  81.  
  82.     @Override
  83.     public void fillRect(int x, int y, int width, int height) {
  84.         gl.glBegin(GL2.GL_QUADS);
  85.         gl.glVertex2f(x, y);
  86.         gl.glVertex2f(x + width, y);
  87.         gl.glVertex2f(x + width, (y + height));
  88.         gl.glVertex2f(x, (y + height));
  89.         gl.glEnd();
  90.     }
  91.  
  92.     @Override
  93.     public void drawImage(int texID, int dx, int dy, int dwidth, int dheight,
  94.             int sx, int sy, int swidth, int sheight) {
  95.         drawFilteredImage(texID, dx, dy, dwidth, dheight, sx, sy, swidth,
  96.                 sheight, 1f);
  97.     }
  98.  
  99.     @Override
  100.     public void drawFilteredImage(int texID, int dx, int dy, int dwidth,
  101.             int dheight, int sx, int sy, int swidth, int sheight, float opacity) {
  102.         Texture tex = textureManager.fetchTexture(texID);
  103.         if (tex != null) {
  104.             double width = tex.getWidth();
  105.             double height = tex.getHeight();
  106.             tex.enable();
  107.             tex.bind();
  108.             setColor(new Color(1f, 1f, 1f, opacity));
  109.             gl.glBegin(GL2.GL_QUADS);
  110.             gl.glTexCoord2d(sx / width, sy / height);
  111.             gl.glVertex2f(dx, dy);
  112.             gl.glTexCoord2d((sx + swidth) / width, sy / height);
  113.             gl.glVertex2f(dx + dwidth, dy);
  114.             gl.glTexCoord2d((sx + swidth) / width, (sy + sheight) / height);
  115.             gl.glVertex2f(dx + dwidth, (dy + dheight));
  116.             gl.glTexCoord2d(sx / width, (sy + sheight) / height);
  117.             gl.glVertex2f(dx, (dy + dheight));
  118.             gl.glEnd();
  119.             tex.disable();
  120.         }
  121.     }
  122.  
  123.     @Override
  124.     public void drawPoint(int x, int y) {
  125.         gl.glBegin(GL.GL_POINTS);
  126.         gl.glVertex2f(x, y);
  127.         gl.glEnd();
  128.     }
  129.  
  130.     @Override
  131.     public void setColor(Color color) {
  132.         gl.glColor4f(color.getRed() / 255f, color.getGreen() / 255f,
  133.                 color.getBlue() / 255f, color.getAlpha() / 255f);
  134.     }
  135.  
  136.     @Override
  137.     public void display(GLAutoDrawable arg0) {
  138.         if (!canvas.getSize().equals(
  139.                 super.mgr.getSource().getContainer().getSize())) {
  140.             canvas.setSize(super.mgr.getSource().getContainer().getSize());
  141.             cliparea = canvas.getBounds();
  142.             canvas.reshape(0, 0, super.mgr.getSource().getContainer()
  143.                     .getWidth(), super.mgr.getSource().getContainer()
  144.                     .getHeight());
  145.         }
  146.         this.gl = arg0.getGL().getGL2();
  147.         this.glCore = arg0;
  148.         gl.glMatrixMode(GL2.GL_PROJECTION);
  149.         gl.glLoadIdentity();
  150.         gl.glOrtho(0, canvas.getWidth(), canvas.getHeight(), 0, 1, -1);
  151.         gl.glMatrixMode(GL2.GL_MODELVIEW);
  152.         gl.glLoadIdentity();
  153.         gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
  154.         super.doRender();
  155.     }
  156.  
  157.     @Override
  158.     public void dispose(GLAutoDrawable arg0) {
  159.         this.gl = arg0.getGL().getGL2();
  160.         this.glCore = arg0;
  161.     }
  162.  
  163.     @Override
  164.     public void init(GLAutoDrawable arg0) {
  165.         this.gl = arg0.getGL().getGL2();
  166.         this.glCore = arg0;
  167.         this.gl.setSwapInterval(1);
  168.         this.gl.glEnable(GL.GL_BLEND);
  169.         this.gl.glBlendFunc(GL.GL_SRC_ALPHA, GL.GL_ONE_MINUS_SRC_ALPHA);
  170.     }
  171.  
  172.     @Override
  173.     public void reshape(GLAutoDrawable arg0, int arg1, int arg2, int arg3,
  174.             int arg4) {
  175.         this.gl = arg0.getGL().getGL2();
  176.         this.glCore = arg0;
  177.     }
  178.  
  179.     @Override
  180.     public void dispose() {
  181.         animator.stop();
  182.         mgr.getSource().getLog().fine("Killed OpenGL Graphics thread");
  183.         txtRender.dispose();
  184.         textureManager.dispose();
  185.         if (canvas != null)
  186.             canvas.destroy();
  187.     }
  188.  
  189.     @Override
  190.     public void drawText(String text, int x, int y, Font f, Color color) {
  191.         TextRenderer tRender = txtRender.fetchRenderer(f);
  192.         tRender.beginRendering(canvas.getWidth(), canvas.getHeight());
  193.         tRender.setColor(color);
  194.         tRender.draw(text, x,
  195.                 canvas.getHeight() - y
  196.                         - (int) tRender.getBounds(text).getHeight());
  197.         tRender.endRendering();
  198.     }
  199.  
  200.     @Override
  201.     public Rectangle2D getStringBounds(Font f, String s) {
  202.         return txtRender.fetchRenderer(f).getBounds(s);
  203.     }
  204.  
  205.     @Override
  206.     public int getFPS() {
  207.         long duration = animator.getDuration() / 1000;
  208.         if (duration != 0) {
  209.             return (int) (animator.getTotalFrames() / duration);
  210.         }
  211.         return 0;
  212.     }
  213.  
  214.     @Override
  215.     public int getImageWidth(int graphic) {
  216.         Texture tex = textureManager.fetchTexture(graphic);
  217.         if (tex != null)
  218.             return tex.getWidth();
  219.         return 0;
  220.     }
  221.  
  222.     @Override
  223.     public int getImageHeight(int graphic) {
  224.         Texture tex = textureManager.fetchTexture(graphic);
  225.         if (tex != null)
  226.             return tex.getHeight();
  227.         return 0;
  228.     }
  229.  
  230.     @Override
  231.     public void setClip(Rectangle r) {
  232.         if (r != null) {
  233.             gl.glClipPlane(GL2.GL_CLIP_PLANE0, new double[] { 1, 0, 0, -r.x },
  234.                     0);
  235.             gl.glClipPlane(GL2.GL_CLIP_PLANE1, new double[] { 0, 1, 0, -r.y },
  236.                     0);
  237.             gl.glClipPlane(GL2.GL_CLIP_PLANE2, new double[] { -1, 0, 0,
  238.                     r.x + r.width }, 0);
  239.             gl.glClipPlane(GL2.GL_CLIP_PLANE3, new double[] { 0, -1, 0,
  240.                     r.y + r.height }, 0);
  241.             gl.glEnable(GL2.GL_CLIP_PLANE0);
  242.             gl.glEnable(GL2.GL_CLIP_PLANE1);
  243.             gl.glEnable(GL2.GL_CLIP_PLANE2);
  244.             gl.glEnable(GL2.GL_CLIP_PLANE3);
  245.         } else {
  246.             gl.glDisable(GL2.GL_CLIP_PLANE0);
  247.             gl.glDisable(GL2.GL_CLIP_PLANE1);
  248.             gl.glDisable(GL2.GL_CLIP_PLANE2);
  249.             gl.glDisable(GL2.GL_CLIP_PLANE3);
  250.         }
  251.     }
  252.  
  253.     @Override
  254.     public Rectangle getClip() {
  255.         return cliparea;
  256.     }
  257.  
  258.     @Override
  259.     public ObjectHeap<? extends GraphicsStorage> loadedGraphics() {
  260.         return textureManager.loadedMap();
  261.     }
  262. }
Advertisement
Add Comment
Please, Sign In to add comment