Java-Gaming.org Hi !
Featured games (83)
games approved by the League of Dukes
Games in Showcase (522)
Games in Android Showcase (127)
games submitted by our members
Games in WIP (590)
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-08-01 19:37:19, viewed 120 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  
package warlord.opengl;

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

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

import javax.imageio.ImageIO;

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

import filesystem.Accessor;

import warlord.ingame.ObjectLoading;

public class Texture {
   
   private int textureID;
   private int width;
   private int height;
   private BufferedImage image;
//   private static String source;
   
   public static Texture loadTexture(File file){
//      source = file.getAbsolutePath();
      try {
         BufferedImage bImage = ImageIO.read(file);
         return new Texture(bImage);
      } catch (IOException e) {
         if(Game.isDebug()){
            Logger.print("Image couldn't be read");
            Logger.print(e);
         }
         return null;
      }
   }
   
   public int getTextureID(){
      return textureID;
   }
   
   public static Texture loadTexture(String filename){
//      source = filename;
      if(TextureLoader.getTexture(filename) != null){
         return TextureLoader.getTexture(filename);
      }
      if(filename.toLowerCase().contains(".resource".toLowerCase())){
         // Serialized buffer
         try {
            SerializableBufferWrapper buffer = (SerializableBufferWrapper) ObjectLoading.loadObject(filename);
            return loadTexture(buffer, filename);
         } catch (ClassNotFoundException | IOException e) {
            File textureFile = null;
            try {
               textureFile = new File(Accessor.class.getResource(filename).toURI());
            } catch (URISyntaxException e1) {
               if(Game.isDebug()){
                  Logger.print(e1);
               }
            }
            return loadTexture(textureFile);
         }
      } else {
         File textureFile = null;
         try {
            textureFile = new File(Accessor.class.getResource(filename).toURI());
         } catch (URISyntaxException e) {
            if(Game.isDebug()){
               Logger.print(e);
            }
         }
         return loadTexture(textureFile);
      }
   }
   
   public static Texture loadTexture(SerializableBufferWrapper buffer, String filename){
//      source = filename;
      return new Texture(buffer);
   }

   public void scaleActual(float scale){
      int height_ = Math.round(height * scale);
      int width_ = Math.round(width * scale);
      setSize(width_, height_);
   }
   
//   public String getSource(){
//      return source;
//   }
   
   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;
   }
   
   public void setSize(int width, int height){
      this.width = width;
      this.height = height;
   }
   
   public Texture(BufferedImage image){
      this.image = image;
      IntBuffer idBuffer = BufferUtils.createIntBuffer(1);
      glGenTextures(idBuffer);
      textureID = idBuffer.get(0);
      loadTexture(image);
//      source = "";
   }
   
   public Texture(SerializableBufferWrapper buffer){
      IntBuffer idBuffer = BufferUtils.createIntBuffer(1);
      glGenTextures(idBuffer);
      textureID = idBuffer.get(0);
   //   Logger.print(textureID);
      loadTexture(buffer.buffer, buffer.width, buffer.height);
//      source = "";
   }
   
   public void bind(){
      glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, textureID);
   }
   
   public static void unbind(){
      glDisable(GL_TEXTURE_2D);
      glEnable(GL_TEXTURE_2D);
      glBindTexture(GL_TEXTURE_2D, 0);
   }
   
   public int getHeight(){
      return height;
   }
   
   public int getWidth(){
      return width;
   }
   
   public BufferedImage getImage(){
      return image;
   }
   
   public void loadTexture(byte[] buffer_, int width, int height){
      glEnable(GL_TEXTURE_2D);
      this.width = width;
      this.height = height;
      ByteBuffer buffer = BufferUtils.createByteBuffer(buffer_.length).put(buffer_); // Clone the buffer
      buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
        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, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, buffer);
       glBindTexture(GL_TEXTURE_2D, 0); // Clear the texture
       glDisable(GL_TEXTURE_2D);
   }
   
   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); 
           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));
                   buffer.put((byte) ((pixel >> 8) & 0xFF));
                   buffer.put((byte) (pixel & 0xFF));
                   buffer.put((byte) ((pixel >> 24) & 0xFF));
               }
           }
           buffer.flip(); //FOR THE LOVE OF GOD DO NOT FORGET THIS
            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);
           glBindTexture(GL_TEXTURE_2D, 0); // Clear the texture
           glDisable(GL_TEXTURE_2D);
   }
   
   public void render(float x, float y, float width, float height){
      glEnable(GL_TEXTURE_2D);
      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();
   }

}


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  
package warlord.ingame;

import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.util.Properties;

import warlord.ingame.world.World;
import warlord.opengl.Game;
import warlord.opengl.Logger;

public class ObjectLoading {
   
   public static void unloadObject(File target, Object object) throws IOException{
      if(!target.exists()){
         target.createNewFile();
      }
      FileOutputStream fout = new FileOutputStream(target);
      ObjectOutputStream oos = new ObjectOutputStream(fout);
      oos.writeObject(object);
      oos.close();
   }
   public static void unloadProperties(File target, Properties properties) throws IOException{
      if(!target.exists()){
         target.createNewFile();
      }
      FileOutputStream fout = new FileOutputStream(target);
      ObjectOutputStream oos = new ObjectOutputStream(fout);
      oos.writeObject(properties);
      oos.close();
   }
   public static void unloadObject(String filename, Object object) throws IOException{
      File target = Game.getFile(filename);
      if(!target.exists()){
         target.createNewFile();
      }
      FileOutputStream fout = new FileOutputStream(target);
      ObjectOutputStream oos = new ObjectOutputStream(fout);
      oos.writeObject(object);
      oos.close();
   }
   
   public static Object loadObject(String filename) throws IOException, ClassNotFoundException{
      File target = Game.getFile(filename);
      if(!target.exists()){
         target.createNewFile();
      }
      FileInputStream fin = new FileInputStream(target);
      ObjectInputStream ois = new ObjectInputStream(fin);
      Object object = ois.readObject();
      ois.close();
      return object;
   }
   public static Object loadObject(File target) throws IOException, ClassNotFoundException{
      if(!target.exists()){
         target.createNewFile();
      }
      FileInputStream fin = new FileInputStream(target);
      ObjectInputStream ois = new ObjectInputStream(fin);
      Object object = ois.readObject();
      ois.close();
      return object;
   }
   
   public static World loadWorld(String filename){
      try {
         return (World) loadObject(filename);
      } catch (ClassNotFoundException | IOException e) {
         if(Game.isDebug()){
            Logger.print(e);
         }
         return null;
      }
   }

}


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  
package warlord.opengl;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.io.Serializable;
import javax.imageio.ImageIO;

import warlord.ingame.ObjectLoading;

public class SerializableBufferWrapper implements Serializable {

   private static final long serialVersionUID = -4472500424115053448L;
   public int width, height;
   public byte[] buffer;
   
   public SerializableBufferWrapper(){
   }
   
   public byte[] getBuffer(){
      return buffer;
   }
   
   public static void main(String[] args) throws IOException{
      args = new String[]{"computers-splash.png", "computers-splash.resource"};
      if(args.length < 2){
         return;
      }
      File input = new File(new File(System.getenv("APPDATA"), ".QuestOfTheWarlord"), args[0]);
      BufferedImage image = ImageIO.read(input);
      int width_ = image.getWidth();
      int height_ = image.getHeight();
       int[] pixels = new int[image.getWidth() * image.getHeight()];
        image.getRGB(0, 0, image.getWidth(), image.getHeight(), pixels, 0, image.getWidth());
        byte[] pixelData = new byte[image.getWidth() * image.getHeight() * 4];
        int index = 0;
        //   ByteBuffer buffer = BufferUtils.createByteBuffer(image.getWidth() * image.getHeight() * 4); 
        for(int y = 0; y < image.getHeight(); y++){
            for(int x = 0; x < image.getWidth(); x++){
                int pixel = pixels[y * image.getWidth() + x];
                pixelData[index++] = (byte) ((pixel >> 16) & 0xFF);
                pixelData[index++] = (byte) ((pixel >> 8) & 0xFF);
                pixelData[index++] = (byte) ((pixel >> 0) & 0xFF);
                pixelData[index++] = (byte) ((pixel >> 24) & 0xFF);
           //     buffer.put((byte) ((pixel >> 16) & 0xFF));
           //     buffer.put((byte) ((pixel >> 8) & 0xFF));
           //     buffer.put((byte) (pixel & 0xFF));
           //     buffer.put((byte) ((pixel >> 24) & 0xFF));
            }
        }
        SerializableBufferWrapper instance = new SerializableBufferWrapper();
        instance.buffer = pixelData;
        instance.width = width_;
        instance.height = height_;
        ObjectLoading.unloadObject(new File(new File(System.getenv("APPDATA"), ".QuestOfTheWarlord"), args[1]), instance);
   }
   
}





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.

trollwarrior1 (33 views)
2014-11-22 12:13:56

xFryIx (73 views)
2014-11-13 12:34:49

digdugdiggy (52 views)
2014-11-12 21:11:50

digdugdiggy (46 views)
2014-11-12 21:10:15

digdugdiggy (40 views)
2014-11-12 21:09:33

kovacsa (65 views)
2014-11-07 19:57:14

TehJavaDev (69 views)
2014-11-03 22:04:50

BurntPizza (67 views)
2014-11-03 18:54:52

moogie (82 views)
2014-11-03 06:22:04

CopyableCougar4 (82 views)
2014-11-01 23:36:41
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

List of Learning Resources
by SilverTiger
2014-07-31 16:26:06
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!