Java-Gaming.org    
Featured games (81)
games approved by the League of Dukes
Games in Showcase (480)
Games in Android Showcase (110)
games submitted by our members
Games in WIP (547)
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 2014-06-22 03:22:01, viewed 51 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  
package me.CopyableCougar4.opengl;

import static org.lwjgl.opengl.GL11.*;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

import javax.imageio.ImageIO;

import org.lwjgl.BufferUtils;
import org.lwjgl.opengl.GL12;

public class Texture {
   
   /** The texture id */
   private int textureID;
   /** The width */
   private int width;
   /** The height */
   private int height;
   /** The buffered image */
   private BufferedImage image;
   /** Source for the image */
   private static String source;
   
   /**
    * Load a texture from a file
    */
   public static Texture loadTexture(File file){
      
      source = file.getAbsolutePath();
      try {
         BufferedImage bImage = ImageIO.read(file);
         return new Texture(bImage);
      } catch (IOException e) {
         System.out.println("Image couldn't be read");
         e.printStackTrace();
         return null;
      }
      
   }
   
   /**
    * Load a texture from the resource hunter
    */
   public static Texture loadTexture(String filename){
      
      source = filename;
      File textureFile = new File(filename);//ResourceHunter.findUserFile(ResourceType.RESOURCES, filename);
      return loadTexture(textureFile);
      
   }

   /**
    * Set the size of the texture by a scale
    */
   public void scaleActual(float scale){
      
      int height_ = Math.round(height * scale);
      int width_ = Math.round(width * scale);
      setSize(width_, height_);
      
   }
   
   /**
    * Get the source stored
    */
   public String getSource(){
      
      return source;
      
   }
   
   /**
    * Scale the texture
    * @throws CloneNotSupportedException if the texture can't be cloned
    */
   public Texture scale(float scale) throws CloneNotSupportedException{
      
      Texture texture = (Texture) clone();
      int height_ = Math.round(height * scale);
      int width_ = Math.round(width * scale);
      texture.setSize(width_, height_);
      return texture;
      
   }
   
   /**
    * Set a texture's size
    */
   public void setSize(int width, int height){
      
      this.width = width;
      this.height = height;
      
   }
   
   /**
    * Construct a texture
    */
   public Texture(BufferedImage image){

      this.image = image;
      IntBuffer idBuffer = BufferUtils.createIntBuffer(1);
      glGenTextures(idBuffer);
      textureID = idBuffer.get(0);
      loadTexture(image);
      
   }
   
   /**
    * Construct the texture with an alpha component max
    */
   public Texture(BufferedImage image, float alphaMax){
      
      this.image = image;
      IntBuffer idBuffer = BufferUtils.createIntBuffer(1);
      glGenTextures(idBuffer);
      textureID = idBuffer.get(0);
      loadTexture(image, alphaMax);
      
   }
   
   /**
    * Bind the texture
    */
   public void bind(){
      
      glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, textureID);
      
   }
   
   /**
    * Unbind the texture
    */
   public static void unbind(){
      
      glDisable(GL_TEXTURE_2D);
      glEnable(GL_TEXTURE_2D);
      
   }
   
   /**
    * Get the height
    */
   public int getHeight(){
      
      return height;
      
   }
   
   /**
    * Get the width
    */
   public int getWidth(){
      
      return width;
      
   }
   
   /**
    * Get the buffered image
    */
   public BufferedImage getImage(){
      
      return image;
      
   }
   
   /**
    * Load a texture with a set alpha factor
    * @param image
    *       buffered image for the texture
    */
   public void loadTexture(BufferedImage image, float alphaFactor){
      
      glEnable(GL_TEXTURE_2D);
      width = image.getWidth();
      height = image.getHeight();
         int[] pixels = new int[image.getWidth() * image.getHeight()];
           image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

           ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); //4 for RGBA, 3 for RGB
           
           for(int y = 0; y < image.getHeight(); y++){
               for(int x = 0; x < image.getWidth(); x++){
                   int pixel = pixels[y * image.getWidth() + x];
                   buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
                   buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
                   buffer.put((byte) (pixel & 0xFF));               // Blue component
                   buffer.put((byte) ((Math.round(pixel * alphaFactor) >> 24) & 0xFF));    // Alpha component. Only for RGBA
               }
           }

           buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS

           // You now have a ByteBuffer filled with the color data of each pixel.
           // Now just create a texture ID and bind it. Then you can load it using 
           // whatever OpenGL method you want, for example:

           glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
           
           //Setup wrap mode
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

           //Setup texture scaling filtering
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
           
           //Send texel data to OpenGL
           glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
           
   }
   
   /**
    * Load a texture
    * @param image
    *       buffered image for the texture
    */
   public void loadTexture(BufferedImage image){
         
      glEnable(GL_TEXTURE_2D);
      width = image.getWidth();
      height = image.getHeight();
         int[] pixels = new int[image.getWidth() * image.getHeight()];
           image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());

           ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); //4 for RGBA, 3 for RGB
           
           for(int y = 0; y < image.getHeight(); y++){
               for(int x = 0; x < image.getWidth(); x++){
                   int pixel = pixels[y * image.getWidth() + x];
                   buffer.put((byte) ((pixel >> 16) & 0xFF));     // Red component
                   buffer.put((byte) ((pixel >> 8) & 0xFF));      // Green component
                   buffer.put((byte) (pixel & 0xFF));               // Blue component
                   buffer.put((byte) ((pixel >> 24) & 0xFF));    // Alpha component. Only for RGBA
               }
           }

           buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS

           // You now have a ByteBuffer filled with the color data of each pixel.
           // Now just create a texture ID and bind it. Then you can load it using 
           // whatever OpenGL method you want, for example:

           glBindTexture(GL_TEXTURE_2D, textureID); //Bind texture ID
           
           //Setup wrap mode
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL12.GL_CLAMP_TO_EDGE);
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL12.GL_CLAMP_TO_EDGE);

           //Setup texture scaling filtering
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
           glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
           
           //Send texel data to OpenGL
           glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA8, image.getWidth(), image.getHeight(), 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
           
      }

}


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  
package me.CopyableCougar4.widget;

import static org.lwjgl.opengl.GL11.*;

import java.awt.Dimension;
import java.awt.Toolkit;

import me.CopyableCougar4.opengl.Texture;

public class IImage {

   /** The texture */
   private Texture texture;
   
   /**
    * Construct an IImage
    */
   public IImage(Texture texture){
      
      this.texture = texture;
      
   }
   
   /**
    * Get the texture
    */
   public Texture getTexture(){
      
      return texture;
      
   }
   
   /**
    * Construct an IImage
    */
   public IImage(String src){
      
      this(Texture.loadTexture(src));
      
   }
   
   /**
    * Get a scale
    */
   public float getScale(float width){
      
      return (width / texture.getWidth());
      
   }
   
   /**
    * Draw the IImage at a given x and y, and with a given scale
    */
   public void draw(float x, float y, float scale){
      
      float width = texture.getWidth() * scale;
      float height = texture.getHeight() * scale;
      
      texture.bind();
      glBegin(GL_QUADS);
         
         glTexCoord2f(0, 0);
         glVertex2f(x, y);
         
         glTexCoord2f(1, 0);
         glVertex2f(x + width, y);
         
         glTexCoord2f(1, 1);
         glVertex2f(x + width, y + height);
         
         glTexCoord2f(0, 1);
         glVertex2f(x, y + height);
         
      glEnd();
         
      
   }
   
   /**
    * Draw stretched to the screen
    */
   public void drawStretchedToScreen(){
      
      Dimension screenSize = Toolkit.getDefaultToolkit().getScreenSize();
      
      texture.bind();
      glBegin(GL_QUADS);
         
         glTexCoord2f(0, 0);
         glVertex2f(0.0f, 0.0f);
         
         glTexCoord2f(1, 0);
         glVertex2f(screenSize.width, 0.0f);
         
         glTexCoord2f(1, 1);
         glVertex2f(screenSize.width, screenSize.height);
         
         glTexCoord2f(0, 1);
         glVertex2f(0.0f, screenSize.height);
         
      glEnd();
      
   }
   
   /**
    * Draw centered but full screen
    */
   public void drawFullscreen(){
      
      float width = (float) Toolkit.getDefaultToolkit().getScreenSize().getWidth();
      float imageWidth = getWidth();
      float scale = width / imageWidth;
      draw(0.0f, 0.0f, scale);
      
   }
   
   /**
    * Draw at the center
    */
   public void drawCentered(float x, float y, float scale){
      
      float width = texture.getWidth() * scale;
      float height = texture.getHeight() * scale;
      
      texture.bind();
      glBegin(GL_QUADS);
         
         glTexCoord2f(0, 0);
         glVertex2f(x - (width / 2), y - (height / 2));
         
         glTexCoord2f(1, 0);
         glVertex2f(x + (width / 2), y - (height / 2));
         
         glTexCoord2f(1, 1);
         glVertex2f(x + (width / 2), y + (height / 2));
         
         glTexCoord2f(0, 1);
         glVertex2f(x - (width / 2), y + (height / 2));
         
      glEnd();
      
   }
   
   /**
    * Draw the image centered to the screen
    */
   public void drawCenteredToScreen(float scale){
      
      float x = Toolkit.getDefaultToolkit().getScreenSize().width / 2;
      float y = Toolkit.getDefaultToolkit().getScreenSize().height / 2;
      drawCentered(x, y, scale);
      
   }
   
   /**
    * Draw the IImage at a given x and y
    */
   public void draw(float x, float y){
      
      draw(x, y, 1.0f);
      
   }
   
   /**
    * Get the width
    */
   public float getWidth(){
      
      return texture.getWidth();
      
   }
   
   /**
    * Get the height
    */
   public float getHeight(){
      
      return texture.getHeight();
      
   }
   
}





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.

atombrot (24 views)
2014-08-19 09:29:53

Tekkerue (24 views)
2014-08-16 06:45:27

Tekkerue (23 views)
2014-08-16 06:22:17

Tekkerue (13 views)
2014-08-16 06:20:21

Tekkerue (20 views)
2014-08-16 06:12:11

Rayexar (58 views)
2014-08-11 02:49:23

BurntPizza (38 views)
2014-08-09 21:09:32

BurntPizza (30 views)
2014-08-08 02:01:56

Norakomi (37 views)
2014-08-06 19:49:38

BurntPizza (67 views)
2014-08-03 02:57:17
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06

List of Learning Resources
by SilverTiger
2014-07-31 11:54:12

HotSpot Options
by dleskov
2014-07-08 01:59: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!