Example #1
0
 /**
  * Loads a PolygonRegion from a PSH (Polygon SHape) file. The PSH file format defines the polygon
  * vertices before triangulation:
  *
  * <p>s 200.0, 100.0, ...
  *
  * <p>Lines not prefixed with "s" are ignored. PSH files can be created with external tools, eg:
  * <br>
  * https://code.google.com/p/libgdx-polygoneditor/ <br>
  * http://www.codeandweb.com/physicseditor/
  *
  * @param file file handle to the shape definition file
  */
 public PolygonRegion load(TextureRegion textureRegion, FileHandle file) {
   BufferedReader reader = file.reader(256);
   try {
     while (true) {
       String line = reader.readLine();
       if (line == null) break;
       if (line.startsWith("s")) {
         // Read shape.
         String[] polygonStrings = line.substring(1).trim().split(",");
         float[] vertices = new float[polygonStrings.length];
         for (int i = 0, n = vertices.length; i < n; i++)
           vertices[i] = Float.parseFloat(polygonStrings[i]);
         // It would probably be better if PSH stored the vertices and triangles, then we don't
         // have to triangulate here.
         return new PolygonRegion(
             textureRegion, vertices, triangulator.computeTriangles(vertices).toArray());
       }
     }
   } catch (IOException ex) {
     throw new GdxRuntimeException("Error reading polygon shape file: " + file, ex);
   } finally {
     StreamUtils.closeQuietly(reader);
   }
   throw new GdxRuntimeException("Polygon shape not found: " + file);
 }
Example #2
0
    public Sound(OpenALAudio audio, FileHandle file) {
      super(audio);
      if (audio.noDevice) return;

      WavInputStream input = null;
      try {
        input = new WavInputStream(file);
        setup(
            StreamUtils.copyStreamToByteArray(input, input.dataRemaining),
            input.channels,
            input.sampleRate);
      } catch (IOException ex) {
        throw new GdxRuntimeException("Error reading WAV file: " + file, ex);
      } finally {
        StreamUtils.closeQuietly(input);
      }
    }
Example #3
0
 @Override
 public LuaValue load(
     AssetManager assetManager, String fileName, FileHandle file, LuaFileParameter parameter) {
   Globals globals = parameter.mGlobals;
   LuaValue environment = parameter.mEnvironment;
   InputStream scriptStream = file.read();
   try {
     return globals.load(scriptStream, fileName, "t", environment).call();
   } finally {
     StreamUtils.closeQuietly(scriptStream);
   }
 }
Example #4
0
 @Override
 public byte[] getResult() {
   try {
     int contentLength = connection.getContentLength();
     ByteArrayOutputStream buffer;
     if (contentLength > 0) buffer = new OptimizedByteArrayOutputStream(contentLength);
     else buffer = new OptimizedByteArrayOutputStream();
     StreamUtils.copyStream(inputStream, buffer);
     return buffer.toByteArray();
   } catch (IOException e) {
     return StreamUtils.EMPTY_BYTES;
   }
 }
Example #5
0
 @Override
 public String getResultAsString() {
   BufferedReader reader = new BufferedReader(new InputStreamReader(inputStream));
   try {
     int approxStringLength = connection.getContentLength();
     StringBuilder b;
     if (approxStringLength > 0) b = new StringBuilder(approxStringLength);
     else b = new StringBuilder();
     String line;
     while ((line = reader.readLine()) != null) b.append(line);
     return b.toString();
   } catch (IOException e) {
     return "";
   } finally {
     StreamUtils.closeQuietly(reader);
   }
 }
Example #6
0
    WavInputStream(FileHandle file) {
      super(file.read());
      try {
        if (read() != 'R' || read() != 'I' || read() != 'F' || read() != 'F')
          throw new GdxRuntimeException("RIFF header not found: " + file);

        skipFully(4);

        if (read() != 'W' || read() != 'A' || read() != 'V' || read() != 'E')
          throw new GdxRuntimeException("Invalid wave file header: " + file);

        int fmtChunkLength = seekToChunk('f', 'm', 't', ' ');

        int type = read() & 0xff | (read() & 0xff) << 8;
        if (type != 1) throw new GdxRuntimeException("WAV files must be PCM: " + type);

        channels = read() & 0xff | (read() & 0xff) << 8;
        if (channels != 1 && channels != 2)
          throw new GdxRuntimeException("WAV files must have 1 or 2 channels: " + channels);

        sampleRate =
            read() & 0xff | (read() & 0xff) << 8 | (read() & 0xff) << 16 | (read() & 0xff) << 24;

        skipFully(6);

        int bitsPerSample = read() & 0xff | (read() & 0xff) << 8;
        if (bitsPerSample != 16)
          throw new GdxRuntimeException("WAV files must have 16 bits per sample: " + bitsPerSample);

        skipFully(fmtChunkLength - 16);

        dataRemaining = seekToChunk('d', 'a', 't', 'a');
      } catch (Throwable ex) {
        StreamUtils.closeQuietly(this);
        throw new GdxRuntimeException("Error reading WAV file: " + file, ex);
      }
    }
Example #7
0
 public void reset() {
   StreamUtils.closeQuietly(input);
   input = null;
 }