Java-Gaming.org Hi !
Featured games (91)
games approved by the League of Dukes
Games in Showcase (804)
Games in Android Showcase (239)
games submitted by our members
Games in WIP (868)
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: CopyableCougar4 (posted 2015-01-26 04:47:56, viewed 410 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  
package com.digiturtle.celestial;

import java.util.ArrayList;

// takes a texture and creates a ninepatch version, uploading to a vertex buffer with GLEngine
public class Ninepatch {
   
   private int TOP_LEFT = 0, TOP = 1, TOP_RIGHT = 2, LEFT = 3, CENTER = 4, RIGHT = 5, BOTTOM_LEFT = 6, BOTTOM = 7, BOTTOM_RIGHT = 8;
   
   public void loadBuffer(String source, String identifier, GLEngine engine, Vector targetSize) throws Exception {
      start = 0;
      Vector size = engine.getSize(source);
      // get minimum scaling
      while (size.getX() > targetSize.getX() || size.getY() > targetSize.getY()) {
         // scale it down by 50% until the size of each full texture is less than or equal to the target size
         size.set(size.getX() / 2, size.getY() / 2);
      }
      // split up the texture regions
      TextureRegion[] regions = new TextureRegion[9];
      regions[TOP_LEFT] = new TextureRegion(0, 0, size.getX() / 3, size.getY() / 3, 0, 0, (1 / 3f), (1 / 3f));
      regions[TOP] = new TextureRegion(size.getX() / 3, 0, 2 * size.getX() / 3, size.getY() / 3, (1 / 3f), 0, (2 / 3f), (1 / 3f));
      regions[TOP_RIGHT] = new TextureRegion(2 * size.getX() / 3, 0, size.getX(), size.getY() / 3, (2 / 3f), 0, 1, (1 / 3f));
      regions[LEFT] = new TextureRegion(0, size.getY() / 3, size.getX() / 3, 2 * size.getY() / 3, 0, (1 / 3f), (1 / 3f), (2 / 3f));
      regions[CENTER] = new TextureRegion(size.getX() / 3, size.getY() / 3, 2 * size.getX() / 3, 2 * size.getY() / 3, (1 / 3f), (1 / 3f), (2 / 3f), (2 / 3f));
      regions[RIGHT] = new TextureRegion(2 * size.getX() / 3, size.getY() / 3, size.getX(), 2 * size.getY() / 3, (2 / 3f), (1 / 3f), 1, (2 / 3f));
      regions[BOTTOM_LEFT] = new TextureRegion(0, 2 * size.getY() / 3, size.getX() / 3, size.getY(), 0, (2 / 3f), (1 / 3f), 1);
      regions[BOTTOM] = new TextureRegion(size.getX() / 3, 2 * size.getY() / 3, 2 * size.getX() / 3, size.getY(), (1 / 3f), (2 / 3f), (2 / 3f), 1);
      regions[BOTTOM_RIGHT] = new TextureRegion(2 * size.getX() / 3, 2 * size.getY() / 3, size.getX(), size.getY(), (2 / 3f), (2 / 3f), 1, 1);
      // create the vertices
      ArrayList<Vertex> vertices = new ArrayList<Vertex>();
      ArrayList<Byte> indices = new ArrayList<Byte>();
      // add the four corners
      vertices.addAll(regions[TOP_LEFT].getVertices(1, 1, 0, 0));
      indices.addAll(getIndices());
      vertices.addAll(regions[TOP_RIGHT].getVertices(1, 1, targetSize.getX() - regions[TOP_RIGHT].width, 0));
      indices.addAll(getIndices());
      vertices.addAll(regions[BOTTOM_LEFT].getVertices(1, 1, 0, targetSize.getY() - regions[BOTTOM_LEFT].height));
      indices.addAll(getIndices());
      vertices.addAll(regions[BOTTOM_RIGHT].getVertices(1, 1, targetSize.getX() - regions[TOP_RIGHT].width, targetSize.getY() - regions[BOTTOM_LEFT].height));
      indices.addAll(getIndices());
      // upload the top row
      float widthLeft = targetSize.getX() - regions[TOP_LEFT].width - regions[TOP_RIGHT].width;
      float offsetX = regions[TOP_LEFT].width;
      while (widthLeft >= regions[TOP].width) {
         vertices.addAll(regions[TOP].getVertices(1, 1, offsetX, 0));
         indices.addAll(getIndices());
         widthLeft -= regions[TOP].width;
         offsetX += regions[TOP].width;
      }
      vertices.addAll(regions[TOP].getVertices(widthLeft / regions[TOP].width, 1, offsetX, 0));
      indices.addAll(getIndices());
      // upload the left column
      float heightLeft = targetSize.getY() - regions[TOP_LEFT].height - regions[BOTTOM_LEFT].height;
      float offsetY = regions[TOP_LEFT].height;
      while (heightLeft >= regions[LEFT].height) {
         vertices.addAll(regions[LEFT].getVertices(1, 1, 0, offsetY));
         indices.addAll(getIndices());
         heightLeft -= regions[LEFT].height;
         offsetY += regions[LEFT].height;
      }
      vertices.addAll(regions[LEFT].getVertices(1, heightLeft / regions[LEFT].height, 0, offsetY));
      indices.addAll(getIndices());
      // upload the right column
      heightLeft = targetSize.getY() - regions[TOP_RIGHT].height - regions[BOTTOM_RIGHT].height;
      offsetY = regions[TOP_RIGHT].height;
      offsetX = targetSize.getX() - regions[TOP_RIGHT].width;
      while (heightLeft >= regions[RIGHT].height) {
         vertices.addAll(regions[RIGHT].getVertices(1, 1, offsetX, offsetY));
         indices.addAll(getIndices());
         heightLeft -= regions[RIGHT].height;
         offsetY += regions[RIGHT].height;
      }
      vertices.addAll(regions[RIGHT].getVertices(1, heightLeft / regions[RIGHT].height, offsetX, offsetY));
      indices.addAll(getIndices());
      // upload the bottom row
      widthLeft = targetSize.getX() - regions[BOTTOM_LEFT].width - regions[BOTTOM_RIGHT].width;
      offsetX = regions[BOTTOM_LEFT].width;
      offsetY = targetSize.getY() - regions[BOTTOM].height;
      while (widthLeft >= regions[BOTTOM].width) {
         vertices.addAll(regions[BOTTOM].getVertices(1, 1, offsetX, offsetY));
         indices.addAll(getIndices());
         widthLeft -= regions[BOTTOM].width;
         offsetX += regions[BOTTOM].width;
      }
      vertices.addAll(regions[BOTTOM].getVertices(widthLeft / regions[BOTTOM].width, 1, offsetX, offsetY));
      indices.addAll(getIndices());
      // upload the center cells
      widthLeft = targetSize.getX() - regions[BOTTOM_LEFT].width - regions[BOTTOM_RIGHT].width;
      heightLeft = targetSize.getY() - regions[TOP_LEFT].height - regions[BOTTOM_LEFT].height;
      offsetX = regions[LEFT].width;
      offsetY = regions[TOP].height;
      while (heightLeft >= (regions[BOTTOM].height)) {
         widthLeft = targetSize.getX() - regions[BOTTOM_LEFT].width - regions[BOTTOM_RIGHT].width;
         offsetX = regions[LEFT].width;
         while (widthLeft >= (regions[LEFT].width)) {
            vertices.addAll(regions[CENTER].getVertices(1, 1, offsetX, offsetY));
            indices.addAll(getIndices());
            offsetX += regions[CENTER].width;
            widthLeft -= regions[LEFT].width;
         }
         vertices.addAll(regions[CENTER].getVertices(widthLeft / regions[CENTER].width, 1, offsetX, offsetY));
         indices.addAll(getIndices());
         offsetY += regions[CENTER].height;
         heightLeft -= regions[BOTTOM].height;
      }
      widthLeft = targetSize.getX() - regions[BOTTOM_LEFT].width - regions[BOTTOM_RIGHT].width;
      offsetX = regions[LEFT].width;
      while (widthLeft >= (regions[LEFT].width)) {
         vertices.addAll(regions[CENTER].getVertices(1, heightLeft / regions[CENTER].height, offsetX, offsetY));
         indices.addAll(getIndices());
         offsetX += regions[CENTER].width;
         widthLeft -= regions[LEFT].width;
      }
      vertices.addAll(regions[CENTER].getVertices(widthLeft / regions[CENTER].width, heightLeft / regions[CENTER].height, offsetX, offsetY));
      indices.addAll(getIndices());
      // upload everything to the final buffer object
      byte[] indexArray = new byte[indices.size()];
      for (int index = 0, indexArraySize = indices.size(); index < indexArraySize; index++)
         indexArray[index] = indices.get(index);
//      System.out.println(Arrays.toString(indexArray));
      engine.createBuffer(source, identifier, identifier, indexArray, vertices);
   }
   
   private byte start = 0;
   
   private ArrayList<Byte> getIndices() {
      ArrayList<Byte> indices = new ArrayList<Byte>();
      indices.add(start);
      indices.add((byte) (start + 1));
      indices.add((byte) (start + 2));
      indices.add((byte) (start + 2));
      indices.add((byte) (start + 3));
      indices.add(start);
      start += 4;
      return indices;
   }
   
   private class TextureRegion {
      
      private float sx, sy, ex, ey, stx, sty, etx, ety, width, height;

      public TextureRegion(float sx, float sy, float ex, float ey, float stx, float sty, float etx, float ety) {
         this.sx = sx;
         this.sy = sy;
         this.ex = ex;
         this.ey = ey;
         this.stx = stx;
         this.sty = sty;
         this.etx = etx;
         this.ety = ety;
         width = ex - sx;
         height = ey - sy;
      }
      
      public ArrayList<Vertex> getVertices(float widthPercent, float heightPercent, float offsetx, float offsety) {
         ArrayList<Vertex> vertices = new ArrayList<Vertex>();
         float fragWidth = (ex - sx) * widthPercent, fragHeight = (ey - sy) * heightPercent;
         vertices.add(new Vertex().setPosition(new Vector(offsetx, offsety)).setColor(1, 1, 1).setTextureCoordinate(stx, sty));
         vertices.add(new Vertex().setPosition(new Vector(offsetx + fragWidth, offsety)).setColor(1, 1, 1).setTextureCoordinate(etx, sty));
         vertices.add(new Vertex().setPosition(new Vector(offsetx + fragWidth, offsety + fragHeight)).setColor(1, 1, 1).setTextureCoordinate(etx, ety));
         vertices.add(new Vertex().setPosition(new Vector(offsetx, offsety + fragHeight)).setColor(1, 1, 1).setTextureCoordinate(stx, ety));
         return vertices;
      }
      
   }

}





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
 
Riven (521 views)
2019-09-04 15:33:17

hadezbladez (5494 views)
2018-11-16 13:46:03

hadezbladez (2397 views)
2018-11-16 13:41:33

hadezbladez (5755 views)
2018-11-16 13:35:35

hadezbladez (1217 views)
2018-11-16 13:32:03

EgonOlsen (4658 views)
2018-06-10 19:43:48

EgonOlsen (5667 views)
2018-06-10 19:43:44

EgonOlsen (3193 views)
2018-06-10 19:43:20

DesertCoockie (4093 views)
2018-05-13 18:23:11

nelsongames (5048 views)
2018-04-24 18:15:36
A NON-ideal modular configuration for Eclipse with JavaFX
by philfrei
2019-12-19 19:35:12

Java Gaming Resources
by philfrei
2019-05-14 16:15:13

Deployment and Packaging
by philfrei
2019-05-08 15:15:36

Deployment and Packaging
by philfrei
2019-05-08 15:13:34

Deployment and Packaging
by philfrei
2019-02-17 20:25:53

Deployment and Packaging
by mudlee
2018-08-22 18:09:50

Java Gaming Resources
by gouessej
2018-08-22 08:19:41

Deployment and Packaging
by gouessej
2018-08-22 08:04:08
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!