Java-Gaming.org Hi !
Featured games (90)
games approved by the League of Dukes
Games in Showcase (741)
Games in Android Showcase (225)
games submitted by our members
Games in WIP (823)
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: trollwarrior1 (posted 2014-04-04 12:06:45, viewed 473 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  
public class SpriteBatch {

   private static int size=8192;
   
   private FloatBuffer verticesBuffer = BufferUtils.createFloatBuffer(size);
   private FloatBuffer texturesBuffer = BufferUtils.createFloatBuffer(size);
   private FloatBuffer colorBuffer = BufferUtils.createFloatBuffer(size);

   
   private int amount = 0;
   
   public ShaderProgram shader = null;
   
   public SpriteBatch() {
   }

   public void draw(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float z, float px, float py, float rot, Sprite sprite, Color color00, Color color10, Color color11, Color color01) {
      if (amount >= 1023)
         flush();

      x1 -= px;
      y1 -= py;

      x2 -= px;
      y2 -= py;

      x3 -= px;
      y3 -= py;

      x4 -= px;
      y4 -= py;

      float[] data = new float[4 * 3];
      float[] colors = new float[4 * 4];
      float[] textures = new float[4 * 2];

      float xx1 = x1;
      float yy1 = y1;
      float xx2 = x2;
      float yy2 = y2;
      float xx3 = x3;
      float yy3 = y3;
      float xx4 = x4;
      float yy4 = y4;

      if (rot != 0) {
         final float sin = (float) Math.sin(Math.toRadians(rot));
         final float cos = (float) Math.cos(Math.toRadians(rot));

         x1 = cos * xx1 - sin * yy1;
         y1 = sin * xx1 + cos * yy1;

         x2 = cos * xx2 - sin * yy2;
         y2 = sin * xx2 + cos * yy2;

         x3 = cos * xx3 - sin * yy3;
         y3 = sin * xx3 + cos * yy3;

         x4 = cos * xx4 - sin * yy4;
         y4 = sin * xx4 + cos * yy4;
      } else {

         x1 = xx1;
         y1 = yy1;

         x2 = xx2;
         y2 = yy2;

         x3 = xx3;
         y3 = yy3;

         x4 = xx4;
         y4 = yy4;

      }

      x1 += px;
      y1 += py;

      x2 += px;
      y2 += py;

      x3 += px;
      y3 += py;

      x4 += px;
      y4 += py;

      int ii = 0;
      colors[ii++] = color00.r;
      colors[ii++] = color00.g;
      colors[ii++] = color00.b;
      colors[ii++] = color00.a;
      colors[ii++] = color10.r;
      colors[ii++] = color10.g;
      colors[ii++] = color10.b;
      colors[ii++] = color10.a;
      colors[ii++] = color11.r;
      colors[ii++] = color11.g;
      colors[ii++] = color11.b;
      colors[ii++] = color11.a;
      colors[ii++] = color01.r;
      colors[ii++] = color01.g;
      colors[ii++] = color01.b;
      colors[ii++] = color01.a;

      data[0] = x1;
      data[1] = y1;
      data[2] = z;
      textures[0] = sprite.x0;
      textures[1] = sprite.y0;

      data[3] = x2;
      data[4] = y2;
      data[5] = z;
      textures[2] = sprite.x1;
      textures[3] = sprite.y0;

      data[6] = x3;
      data[7] = y3;
      data[8] = z;
      textures[4] = sprite.x1;
      textures[5] = sprite.y1;

      data[9] = x4;
      data[10] = y4;
      data[11] = z;
      textures[6] = sprite.x0;
      textures[7] = sprite.y1;

      verticesBuffer.put(data);
      texturesBuffer.put(textures);
      colorBuffer.put(colors);

      amount += 4;
   }

   public void draw(float x1, float y1, float x2, float y2, float x3, float y3, float x4, float y4, float z, float px, float py, float rot, Sprite sprite, Color color) {
      this.draw(x1, y1, x2, y2, x3, y3, x4, y4, z, px, py, rot, sprite, color, color, color, color);
   }
   
   public void draw(float x, float y, float width, float height,float z, float px, float py, float rot, Sprite sprite, Color color) {
      this.draw(x, y, x + width, y, x + width, y + height, x, y + height, z, px, py, rot, sprite, color);
   }

   public void draw(float x, float y, float width, float height, float z, float rot, Sprite sprite, Color color) {
      this.draw(x, y, width, height, x + width / 2, y + height / 2, z, rot, sprite, color);
   }

   public void draw(float x, float y, float width, float height, float z, Sprite sprite) {
      this.draw(x, y, width, height, z, 0, sprite, Color.WHITE);
   }

   public void flush() {
      
      verticesBuffer.flip();
      texturesBuffer.flip();
      colorBuffer.flip();

      int handle = 0;

      if (shader != null)
         handle = shader.handle;

      
      int loc = glGetAttribLocation(handle, "a_position");
      glVertexAttribPointer(loc, 3, false, 0, verticesBuffer);

      loc = glGetAttribLocation(handle, "a_texCoord");
      glVertexAttribPointer(loc, 2, false, 0, texturesBuffer);

      loc = glGetAttribLocation(handle, "a_color");
      glVertexAttribPointer(2, 4, false, 0, colorBuffer);

      glDrawArrays(GL_QUADS, 0, amount);

      verticesBuffer.clear();
      texturesBuffer.clear();
      colorBuffer.clear();
      amount = 0;

   }
   
   public void setShader(ShaderProgram shader) {
      this.shader = shader;
      if(shader == null) {
         ShaderProgram.useDefault();
         return;
      }
      shader.use();
   }
   
}





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
 
Ecumene (110 views)
2017-09-30 02:57:34

theagentd (144 views)
2017-09-26 18:23:31

cybrmynd (245 views)
2017-08-02 12:28:51

cybrmynd (241 views)
2017-08-02 12:19:43

cybrmynd (240 views)
2017-08-02 12:18:09

Sralse (254 views)
2017-07-25 17:13:48

Archive (872 views)
2017-04-27 17:45:51

buddyBro (1016 views)
2017-04-05 03:38:00

CopyableCougar4 (1573 views)
2017-03-24 15:39:42

theagentd (1373 views)
2017-03-24 15:32:08
List of Learning Resources
by elect
2017-03-13 14:05:44

List of Learning Resources
by elect
2017-03-13 14:04:45

SF/X Libraries
by philfrei
2017-03-02 08:45:19

SF/X Libraries
by philfrei
2017-03-02 08:44:05

SF/X Libraries
by SkyAphid
2017-03-02 06:38:56

SF/X Libraries
by SkyAphid
2017-03-02 06:38:32

SF/X Libraries
by SkyAphid
2017-03-02 06:38:05

SF/X Libraries
by SkyAphid
2017-03-02 06:37:51
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!