Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (539)
Games in Android Showcase (132)
games submitted by our members
Games in WIP (603)
games currently in development
News: Read the Java Gaming Resources, or peek at the official Java tutorials
 
    Home     Help   Search   Login   Register   
  JavaGaming.org - Pastebin



Author: davedes (posted 2012-05-21 13:59:23, viewed 219 times)

1   2   3   4   5   6   7   8   9   10   11   12   13   14   15   16   17   18   19   20   21   22   23   24   25   26   27   28   29   30   31   32   33   34   35   36   37   38   39   40   41   42   43   44   45   46   47   48   49   50   51   52   53   54   55   56   57   58   59   60   61   62   63   64   65   66   67   68   69   70   71   72   73   74   75   76   77   78   79   80   81   82   83   84   85   86   87   88   89   90   91   92   93   94   95   96   97   98   99   100   101   102   103   104   105   106   107   108   109   110   111   112   113   114   115   116   117   118   119   120   121   122   123   124   125   126   127   128   129   130   131   132   133   134   135   136   137   138   139   140   141   142   143   144   145   146   147   148   149   150   151   152   153   154   155   156   157   158   159   160   161   162   163   164   165   166   167   168   169   170   171   172   173   174   175   176   177   178   179   180   181   182   183   184   185   186   187   188   189   190   191   192   193   194   195   196   197   198   199   200   201   202   203   204   205   206   207   208   209   210   211   212   213   214   215   216   217   218   219   220   221   222   223   224   225   226   227   228   229   230   231   232   233   234   235   236   237   238   239   240   241   242   243   244   245   246   247   248   249   250   251   252   253   254   255   256   257   258   259   260   261   262   263   264   265   266   267   268   269   270   271   272   273   274   275   276   277   278   279   280   281   282   283   284   285   286   287   288   289   290   291   292   293   294   295   296   297   298   299   300   301   302   303   304   305   306   307   308   309   310   311   312   313   314   315   316   317   318   319   320   321   322   323   324   325   326   327   328   329   330   331   332   333   334   335   336   337   338   339   340   341   342   343   344   345   346   347   348   349   350   351   352   353   354   355   356   357   358   359   360   361   362   363   364   365   366   367   368   369   370   371   372  
package slim;

import java.nio.FloatBuffer;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL11;

import slim.texture.Texture2D;
import slim.util.FastTrig;


/**
 * 
 * @author Matt DesLauriers (davedes)
 */
public class SpriteBatchImage {

   public static final int ALIGN_LEFT = 0;
   public static final int ALIGN_CENTER = 1;
   public static final int ALIGN_RIGHT = 2;
    
   /**
    * Whether to send the image data as GL_TRIANGLES
    * or GL_QUADS. By default, GL_TRIANGLES is used.
    * 
    * @param b true to use triangle rendering
    */
   public static void setUseTriangles(boolean b) {
      mode = b ? GL11.GL_TRIANGLES : GL11.GL_QUADS;
   }
   
   /**
    * Returns whether to send the image data as GL_TRIANGLES
    * or GL_QUADS. By default, GL_TRIANGLES is used.
    * 
    * @return true if we are using triangle rendering
    */
   public static boolean isUseTriangles() {
      return mode==GL11.GL_TRIANGLES;
   }
   
   private static int mode = GL11.GL_TRIANGLES; 
   private final int TOLERANCE = 48; //we assume triangles is in use...
   
   private int idx = 0;
   private Texture2D texture;
   public int renderCalls = 0;
   
   private FloatBuffer vertices, colors, texcoords;
   private int maxVerts;
   private Color currentColor = new Color(Color.white);
   private Color sharedColor = new Color(Color.white);
   
   private float translateX, translateY;
   
   public SpriteBatchImage() {
      this(1000);
   }
   
   public SpriteBatchImage(int size) {
      if (size<=0)
         throw new IllegalArgumentException("batch size must be larger than 0");
      this.maxVerts = size;
      vertices = BufferUtils.createFloatBuffer(size * 2);
      colors = BufferUtils.createFloatBuffer(size * 4);
      texcoords = BufferUtils.createFloatBuffer(size * 2);
      
      GL11.glEnableClientState(GL11.GL_VERTEX_ARRAY);
        GL11.glEnableClientState(GL11.GL_TEXTURE_COORD_ARRAY);
        GL11.glEnableClientState(GL11.GL_COLOR_ARRAY);
   }
   
   /**
    * Returns the size of this ImageBatch as given in construction (default 1000).
    * The internal array will have a capacity of size * 8.
    * 
    * A large internal array will require less calls to render(), but will take up
    * more memory. 
    * For example, an ImageBatch with a size of 6 would be ideal if we are only
    * rendering a single image (made up of tris) within begin/end
    * (six vertices, 8 bytes per vertex -- 2 for XY, 2 for texture UV, 4 for RGBA).
    * 
    * However, it's usually better to create a single large-size ImageBatch instance
    * and re-use it throughout your game.
    * 
    * @return how many vertices to expect 
    */
   public int getSize() {
      return maxVerts;
   }
   
   public void setColor(Color color) {
      this.currentColor = color;
   }
   
   public void setColor(float r, float g, float b, float a) {
      sharedColor.r = r;
      sharedColor.g = g;
      sharedColor.b = b;
      sharedColor.a = a;
      setColor(sharedColor);
   }
   
   public Color getColor() {
      return currentColor;
   }
   
   public void resetTranslation() {
      translateX = translateY = 0;
   }
   
   /** */
   public void translate(float x, float y) {
      translateX += x;
      translateY += y;
   }
   
   public void flush() {
      if (idx>0) 
         render();
      idx = 0;
      texture = null;
      
      vertices.clear();
      texcoords.clear();
      colors.clear();
   }
   
   /**
    * Sends vertex, color and UV data to the GPU.
    */
   protected void render() {
      if (idx==0) 
         return;
      renderCalls++;
      //bind the last texture
      if (texture!=null) {
         Texture2D.enable(texture.getTarget());
         texture.bind();
      }
      vertices.flip();
      colors.flip();
      texcoords.flip();
      GL11.glVertexPointer(2, 0, vertices);
      GL11.glColorPointer(4, 0, colors);     
       GL11.glTexCoordPointer(2, 0, texcoords);
       GL11.glDrawArrays(mode, 0, idx);
       vertices.clear();
       colors.clear();
       texcoords.clear();
       idx = 0;
   }
   
   public void drawImageScaled(Image image, float x, float y, float scale) {
      drawImage(image, x, y, image.getWidth()*scale, image.getHeight()*scale);
   }
   
   public void drawImage(Image image) {
      drawImage(image, 0, 0);
   }
   
   public void drawImage(Image image, float x, float y) {
      drawImage(image, x, y, image.getWidth(), image.getHeight());
   }

   public void drawImage(Image image, float x, float y, float w, float h) {
      drawImage(image, x, y, w, h, null);
   }

   public void drawImage(Image image, float x, float y, float rotation) {
      drawImage(image, x, y, rotation, image.getWidth(), image.getHeight(), null);
   }
   
   public void drawImage(Image image, float x, float y, float rotation, float w, float h, Color[] corners) {
      if (rotation==0) {
         drawImage(image, x, y, w, h, corners);
         return;
      }
      
      checkRender(image);
      
      float scaleX = w/image.getWidth();
      float scaleY = h/image.getHeight();
      
      float cx = image.getCenterX()*scaleX;
      float cy = image.getCenterY()*scaleY;

      float p1x = -cx;
      float p1y = -cy;
      float p2x = w - cx;
      float p2y = -cy;
      float p3x = w - cx;
      float p3y = h - cy;
      float p4x = -cx;
      float p4y = h - cy;

      double rad = Math.toRadians(rotation);
      final float cos = (float) FastTrig.cos(rad);
      final float sin = (float) FastTrig.sin(rad);

      float tx = image.getNormalizedXOffset();
      float ty = image.getNormalizedYOffset();
      float tw = image.getNormalizedWidth();
      float th = image.getNormalizedHeight();

      float x1 = (cos * p1x - sin * p1y) + cx; // TOP LEFT
      float y1 = (sin * p1x + cos * p1y) + cy;
      float x2 = (cos * p2x - sin * p2y) + cx; // TOP RIGHT
      float y2 = (sin * p2x + cos * p2y) + cy;
      float x3 = (cos * p3x - sin * p3y) + cx; // BOTTOM RIGHT
      float y3 = (sin * p3x + cos * p3y) + cy;
      float x4 = (cos * p4x - sin * p4y) + cx; // BOTTOM LEFT
      float y4 = (sin * p4x + cos * p4y) + cy;
      drawQuadElement(x+x1, y+y1, tx, ty, corners!=null ? corners[0] : null,
                   x+x2, y+y2, tx+tw, ty, corners!=null ? corners[1] : null,
                   x+x3, y+y3, tx+tw, ty+th, corners!=null ? corners[2] : null,
                   x+x4, y+y4, tx, ty+th, corners!=null ? corners[3] : null);
   }
   
   public void drawImage(Image image, float x, float y, float w, float h, Color[] corners) {
      checkRender(image);
      float tx = image.getNormalizedXOffset();
      float ty = image.getNormalizedYOffset();
      float tw = image.getNormalizedWidth();
      float th = image.getNormalizedHeight();
      drawImage(image, x, y, w, h, tx, ty, tw, th, corners);
   }

   public void drawSubImage(Image image, float srcx, float srcy,
         float srcwidth, float srcheight, float x, float y) {
      drawSubImage(image, srcx, srcy, srcwidth, srcheight, x, y, srcwidth, srcheight);
   }
   
   public void drawSubImage(Image image, float srcx, float srcy,
         float srcwidth, float srcheight, float x, float y, float w, float h) {
      drawSubImage(image, srcx, srcy, srcwidth, srcheight, x, y, w, h, null);
   }

   public void drawSubImage(Image image, float srcx, float srcy,
         float srcwidth, float srcheight, float x, float y, float w,
         float h, Color[] corners) {
      checkRender(image);
      float iw = image.getWidth();
      float ih = image.getHeight();
      float tx = (srcx / iw * image.getNormalizedWidth()) + image.getNormalizedXOffset();
      float ty = (srcy / ih * image.getNormalizedHeight()) + image.getNormalizedYOffset();
      float tw = w / iw * image.getNormalizedWidth();
      float th = h / ih * image.getNormalizedHeight();
      drawQuadElement(x, y, tx, ty, corners != null ? corners[0] : null, x
            + w, y, tx + tw, ty, corners != null ? corners[1] : null,
            x + w, y + h, tx + tw, ty + th, corners != null ? corners[2]
                  : null, x, y + h, tx, ty + th,
            corners != null ? corners[3] : null);
   }
   
   public void drawImage(Image image, float x, float y, float width, float height, 
               float u, float v, float uWidth, float vHeight, Color[] corners) {
      checkRender(image);
      drawQuadElement(x, y, u, v, corners!=null ? corners[0] : null,
                   x+width, y, u+uWidth, v, corners!=null ? corners[1] : null,
                   x+width, y+height, u+uWidth, v+vHeight, corners!=null ? corners[2] : null,
                   x, y+height, u, v+vHeight, corners!=null ? corners[3] : null);
   }
   
   /**
    * 
    * @param image
    * @param x
    * @param y
    * @param points
    * @param texcoords a texcoord for each vertex (8 elements 
    * @param offset
    * @param corners
    */
   public void drawImage(Image image, float x, float y, float[] points, 
         float[] texcoords, int offset, int texcoordsOffset, Color[] corners) {
      checkRender(image);
      float x1 = points[offset++];
      float y1 = points[offset++];
      float x2 = points[offset++];
      float y2 = points[offset++];
      float x3 = points[offset++];
      float y3 = points[offset++];
      float x4 = points[offset++];
      float y4 = points[offset++];
      
      float u1 = texcoords[texcoordsOffset++];
      float v1 = texcoords[texcoordsOffset++];
      float u2 = texcoords[texcoordsOffset++];
      float v2 = texcoords[texcoordsOffset++];
      float u3 = texcoords[texcoordsOffset++];
      float v3 = texcoords[texcoordsOffset++];
      float u4 = texcoords[texcoordsOffset++];
      float v4 = texcoords[texcoordsOffset++];
      drawQuadElement(x+x1, y+y1, u1, v1, corners!=null ? corners[0] : null,
                   x+x2, y+y2, u2, v2, corners!=null ? corners[1] : null,
                   x+x3, y+y3, u3, v3, corners!=null ? corners[2] : null,
                   x+x4, y+y4, u4, v4, corners!=null ? corners[3] : null);
   }
   
   private void checkRender(Image image) {
      if (image==null || image.getTexture()==null)
         throw new NullPointerException("null texture");
      
      //we need to bind a different texture. this is
      //for convenience; ideally the user should order
      //their rendering wisely to minimize texture binds   
      if (image.getTexture()!=texture) {
         //apply the last texture
         render();
         texture = image.getTexture();
      } else if (idx >= maxVerts - 4) 
         render();
   }
   
   /**
    * Specifies vertex data.
    * 
    * @param x the x position
    * @param y the y position
    * @param u the U texcoord
    * @param v the V texcoord
    * @param color the color for this vertex
    */
   protected void vertex(float x, float y, float u, float v, Color color) {
      vertices.put(x);
      vertices.put(y);
      texcoords.put(u);
      texcoords.put(v);
      Color c = color!=null ? color : currentColor;
      colors.put(c.r);
      colors.put(c.g);
      colors.put(c.b);
      colors.put(c.a);
      idx++;
   }
   
   /**
    * Draws a quad-like element using either GL_QUADS or GL_TRIANGLES, depending
    * on this batch's configuration.
    */
   protected void drawQuadElement(
                  float x1, float y1, float u1, float v1, Color c1,   //TOP LEFT 
                  float x2, float y2, float u2, float v2, Color c2,   //TOP RIGHT
                  float x3, float y3, float u3, float v3, Color c3,   //BOTTOM RIGHT
                  float x4, float y4, float u4, float v4, Color c4) { //BOTTOM LEFT
      x1 += translateX;
      y1 += translateY;
      x2 += translateX;
      y2 += translateY;
      x3 += translateX;
      y3 += translateY;
      x4 += translateX;
      y4 += translateY;
      if (mode == GL11.GL_TRIANGLES) {
         //top left, top right, bottom left
         vertex(x1, y1, u1, v1, c1);
         vertex(x2, y2, u2, v2, c2);
         vertex(x4, y4, u4, v4, c4);
         //top right, bottom right, bottom left
         vertex(x2, y2, u2, v2, c2);
         vertex(x3, y3, u3, v3, c3);
         vertex(x4, y4, u4, v4, c4);
      } else {
         //quads: top left, top right, bottom right, bottom left
         vertex(x1, y1, u1, v1, c1);
         vertex(x2, y2, u2, v2, c2);
         vertex(x3, y3, u3, v3, c3);
         vertex(x4, y4, u4, v4, c4);
      }
   }
}





Dump your java code here :



Special syntax:
  • To highlight a line (yellow background), prefix it with '@@'
  • To indicate that a line should be removed (red background), prefix it with '-'
  • To indicate that a line should be added (green background), prefix it with '+'
  • To post multiple snippets, seperate them by '~~~~'
  EOF
 

Add your game by posting it in the WIP section,
or publish it in Showcase.

The first screenshot will be displayed as a thumbnail.

rwatson462 (37 views)
2014-12-15 09:26:44

Mr.CodeIt (30 views)
2014-12-14 19:50:38

BurntPizza (62 views)
2014-12-09 22:41:13

BurntPizza (99 views)
2014-12-08 04:46:31

JscottyBieshaar (59 views)
2014-12-05 12:39:02

SHC (74 views)
2014-12-03 16:27:13

CopyableCougar4 (77 views)
2014-11-29 21:32:03

toopeicgaming1999 (138 views)
2014-11-26 15:22:04

toopeicgaming1999 (127 views)
2014-11-26 15:20:36

toopeicgaming1999 (38 views)
2014-11-26 15:20:08
Resources for WIP games
by kpars
2014-12-18 10:26:14

Understanding relations between setOrigin, setScale and setPosition in libGdx
by mbabuskov
2014-10-09 22:35:00

Definite guide to supporting multiple device resolutions on Android (2014)
by mbabuskov
2014-10-02 22:36:02

List of Learning Resources
by Longor1996
2014-08-16 10:40:00

List of Learning Resources
by SilverTiger
2014-08-05 19:33:27

Resources for WIP games
by CogWheelz
2014-08-01 16:20:17

Resources for WIP games
by CogWheelz
2014-08-01 16:19:50

List of Learning Resources
by SilverTiger
2014-07-31 16:29:50
java-gaming.org is not responsible for the content posted by its members, including references to external websites, and other references that may or may not have a relation with our primarily gaming and game production oriented community. inquiries and complaints can be sent via email to the info‑account of the company managing the website of java‑gaming.org
Powered by MySQL Powered by PHP Powered by SMF 1.1.18 | SMF © 2013, Simple Machines | Managed by Enhanced Four Valid XHTML 1.0! Valid CSS!