PageRenderTime 29ms CodeModel.GetById 14ms app.highlight 13ms RepoModel.GetById 1ms app.codeStats 0ms

/src/gosu/ImageDrawOp.java

http://jgosu.googlecode.com/
Java | 212 lines | 149 code | 42 blank | 21 comment | 19 complexity | d3feb6a8db09e4b57ea712af42ff26f8 MD5 | raw file
  1package gosu;
  2
  3import java.awt.geom.Point2D;
  4import com.sun.opengl.util.texture.Texture;
  5import com.sun.opengl.util.texture.TextureCoords;
  6import javax.media.opengl.GL;
  7import javax.media.opengl.glu.GLU;
  8
  9
 10public class ImageDrawOp extends DrawOp {
 11  private Image _image;
 12  private Point2D.Double[] _points;
 13  private double _z;
 14  private java.awt.Color[] _colors;
 15  private int _mode;
 16
 17  public ImageDrawOp(Image image, double x, double y, double z) {
 18    this(image, x, y, z, image.getWidth(), image.getHeight(), java.awt.Color.white, Image.DEFAULT);
 19  }
 20
 21
 22  public ImageDrawOp(Image image, double x, double y, double z, double factorX, double factorY) {
 23    this(image, x, y, z, (int) (image.getWidth() * factorX), (int) (image.getHeight() * factorY), java.awt.Color.white, Image.DEFAULT);
 24  }
 25
 26
 27  public ImageDrawOp(Image image, double x, double y, double z, double factorX, double factorY, Color color, int mode) {
 28    this(image, x, y, z, (int) (image.getWidth() * factorX), (int) (image.getHeight() * factorY), color.getAWTColor(), mode);
 29  }
 30
 31
 32  protected ImageDrawOp(Image image, double x, double y, double z, int width, int height, java.awt.Color color, int mode) {
 33    _image = image;
 34
 35    _points = new Point2D.Double[4];
 36    _points[0] = new Point2D.Double(x, y);
 37    _points[1] = new Point2D.Double(x + width, y);
 38    _points[2] = new Point2D.Double(x + width, y + height);
 39    _points[3] = new Point2D.Double(x, y + height);
 40
 41    _z = z;
 42    _mode = mode;
 43
 44    _colors = new java.awt.Color[] {color, color, color, color};
 45  }
 46
 47
 48  public ImageDrawOp(Image image, double x1, double y1, Color c1,
 49                                      double x2, double y2, Color c2,
 50                                      double x3, double y3, Color c3,
 51                                      double x4, double y4, Color c4, 
 52                                      double z, int mode) {
 53    _image = image;
 54    _points = new Point2D.Double[] {new Point2D.Double(x1, y1),
 55                                    new Point2D.Double(x2, y2),
 56                                    new Point2D.Double(x4, y4),
 57                                    new Point2D.Double(x3, y3)};
 58    _z = z;
 59    _mode = mode;
 60    _colors = new java.awt.Color[] {c1.getAWTColor(), c2.getAWTColor(),
 61                                    c4.getAWTColor(), c3.getAWTColor()};
 62  }
 63
 64
 65  /**
 66   * Draws a line.
 67   */
 68  public ImageDrawOp(double x1, double y1, Color c1, double x2, double y2, Color c2) {
 69    this(x1, y1, c1, x2, y2, c2, 0, Image.DEFAULT);
 70  }
 71
 72
 73  /**
 74   * Draws a line.
 75   */
 76  public ImageDrawOp(double x1, double y1, Color c1, double x2, double y2, Color c2, double z, int mode) {
 77    _points = new Point2D.Double[] {new Point2D.Double(x1, y1), new Point2D.Double(x2, y2)};
 78    _colors = new java.awt.Color[] {c1.getAWTColor(), c2.getAWTColor()};
 79    _z = z;
 80    _mode = mode;
 81  }
 82
 83
 84  /**
 85   * Draws a triangle.
 86   */
 87  public ImageDrawOp(double x1, double y1, Color c1,
 88                     double x2, double y2, Color c2,
 89                     double x3, double y3, Color c3) {
 90    this(x1, y1, c1, x2, y2, c2, x3, y3, c3, 0, Image.DEFAULT);
 91  }
 92
 93
 94  /**
 95   * Draws a triangle.
 96   */
 97  public ImageDrawOp(double x1, double y1, Color c1,
 98                     double x2, double y2, Color c2,
 99                     double x3, double y3, Color c3, double z, int mode) {
100    _points = new Point2D.Double[] {new Point2D.Double(x1, y1), 
101                                    new Point2D.Double(x2, y2),
102                                    new Point2D.Double(x3, y3)};
103    _colors = new java.awt.Color[] {c1.getAWTColor(), 
104                                    c2.getAWTColor(),
105                                    c3.getAWTColor()};
106    _z = z;
107    _mode = mode;
108  }
109
110
111  /**
112   * Draws a quad.
113   */
114  public ImageDrawOp(double x1, double y1, Color c1,
115                     double x2, double y2, Color c2,
116                     double x3, double y3, Color c3,
117                     double x4, double y4, Color c4) {
118    this(x1, y1, c1, x2, y2, c2, x3, y3, c3, x4, y4, c4, 0, Image.DEFAULT);
119  }
120
121
122  /**
123   * Draws a quad.
124   */
125  public ImageDrawOp(double x1, double y1, Color c1,
126                     double x2, double y2, Color c2,
127                     double x3, double y3, Color c3,
128                     double x4, double y4, Color c4, double z, int mode) {
129
130    _points = new Point2D.Double[] {new Point2D.Double(x1, y1), 
131                                    new Point2D.Double(x2, y2),
132                                    new Point2D.Double(x4, y4),
133                                    new Point2D.Double(x3, y3)};
134
135    _colors = new java.awt.Color[] {c1.getAWTColor(), 
136                                    c2.getAWTColor(),
137                                    c4.getAWTColor(),
138                                    c3.getAWTColor()};
139    _z = z;
140    _mode = mode;
141  }
142
143
144  public double getZ() { return _z; }
145
146
147  public void execute() {
148    GL gl = GLU.getCurrentGL();
149
150    if (_mode == Image.ADDITIVE) {
151      gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE);
152    } else {
153      gl.glBlendFunc(GL.GL_ONE, GL.GL_ONE_MINUS_SRC_ALPHA);
154    }
155
156    Texture texture = null;
157    TextureCoords coords = null;
158    if (_image != null) {
159      texture = _image.getTexture();
160      texture.enable();
161      texture.bind();
162      //texture.setTexParameteri(GL.GL_TEXTURE_MIN_FILTER, GL.GL_LINEAR);
163      //texture.setTexParameteri(GL.GL_TEXTURE_MAG_FILTER, GL.GL_LINEAR);
164      if (_image.hasSoftBorders()) {
165        coords = texture.getSubImageTexCoords(1, 1, _image.getWidth() + 1, _image.getHeight() + 1);
166      } else {
167        coords = texture.getImageTexCoords();
168      }
169    }
170
171    if (_points.length == 2) {
172        gl.glBegin(GL.GL_LINES);
173    } else if (_points.length == 3) {
174        gl.glBegin(GL.GL_TRIANGLES);
175    } else { // 4
176        gl.glBegin(GL.GL_QUADS);
177    }
178
179
180    for (int i = 0; i < _points.length; i++) {
181      // JOGL textures have premultiplied colors
182      float a = _colors[i].getAlpha() / 255.0f;
183      float r = a * _colors[i].getRed() / 255.0f;
184      float g = a * _colors[i].getGreen() / 255.0f;
185      float b = a * _colors[i].getBlue() / 255.0f;
186      gl.glColor4f(r, g, b, a);
187
188      if (texture != null) {
189        switch (i) {
190          case 0:
191            gl.glTexCoord2f(coords.left(), coords.top());
192            break;
193          case 1:
194            gl.glTexCoord2f(coords.right(), coords.top());
195            break;
196          case 2:
197            gl.glTexCoord2f(coords.right(), coords.bottom());
198            break;
199          case 3:
200            gl.glTexCoord2f(coords.left(), coords.bottom());
201            break;
202        }
203      }
204
205      gl.glVertex2d(_points[i].x, _points[i].y);
206    }
207
208    gl.glEnd();
209
210    if (texture != null) texture.disable();
211  }
212}