Advertisement
spacechase0

Dynamic Texture Creation

Apr 9th, 2013
118
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 2.15 KB | None | 0 0
  1. // In the client proxy, put these methods
  2. // Some of the indenting is messed up, sorry :(
  3.    
  4.     // Put a dummy method for this in CommonProxy as well
  5.     @Override
  6.     public int getTexture( String path )
  7.     {
  8.         Integer i = textures.get( path );
  9.         return ( i == null ? 0 : i );
  10.     }
  11.    
  12.     // You can probably take the Icon stuff out, it was for my purposes
  13.     private void makeTextureFor( String str, Icon icon )
  14.     {
  15.         System.out.println( "Generating texture " + str + "..." );
  16.         try
  17.         {
  18.             Minecraft mc = FMLClientHandler.instance().getClient();
  19.             RenderEngine re = mc.renderEngine;
  20.            
  21.             String texPath = iconToPath( re, icon );
  22.             ITexturePack pack = mc.texturePackList.getSelectedTexturePack();
  23.             BufferedImage other = ImageIO.read( pack.getResourceAsStream( texPath ) );
  24.            
  25.             BufferedImage image = new BufferedImage( 64, 32, BufferedImage.TYPE_INT_ARGB );
  26.             // Generate image here
  27.            
  28.             int id = re.allocateAndSetupTexture( image );
  29.             textures.put( str, id );
  30.         }
  31.         catch ( Exception exception )
  32.         {
  33.             exception.printStackTrace();
  34.         }
  35.     }
  36.    
  37.     private String iconToPath( RenderEngine re, Icon icon )
  38.     {
  39.         final String basePath = re.textureMapBlocks.basePath;
  40.         final String textureExt = re.textureMapBlocks.textureExt;
  41.        
  42.         String name = icon.getIconName();
  43.         String path;
  44.         if (name.indexOf(':') == -1)
  45.         {
  46.             path = basePath + name + textureExt;
  47.         }
  48.         else
  49.         {
  50.             String domain = name.substring(0, name.indexOf(':'));
  51.             String file = name.substring(name.indexOf(':') + 1);
  52.             path = "mods/" + domain +"/" + basePath + file + textureExt;
  53.         }
  54.        
  55.         return "/" + path;
  56.     }
  57.    
  58.     private Map< String, Integer > textures;
  59.  
  60. // Binding the texture is something like this:
  61.         String path = "/mod/someMod/texture/mytexture.png";
  62. GL11.glBindTexture( GL11.GL_TEXTURE_2D, MOD.instance.proxy.getTexture( path ) );
  63. // And make SURE to do this:
  64.         tileEntityRenderer.renderEngine.resetBoundTexture();
  65. // Otherwise you get something like this: http://spacechase0.com/wp-content/uploads/2013/04/1365528599_21425.png
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement