Beispiel #1
0
    public ETC1Data(FileHandle pkmFile) {
      byte[] buffer = new byte[1024 * 10];
      DataInputStream in = null;
      try {
        in = new DataInputStream(new BufferedInputStream(new GZIPInputStream(pkmFile.read())));
        int fileSize = in.readInt();
        compressedData = BufferUtils.newUnsafeByteBuffer(fileSize);
        int readBytes = 0;
        while ((readBytes = in.read(buffer)) != -1) {
          compressedData.put(buffer, 0, readBytes);
        }
        compressedData.position(0);
        compressedData.limit(compressedData.capacity());
      } catch (Exception e) {
        throw new GdxRuntimeException("Couldn't load pkm file '" + pkmFile + "'", e);
      } finally {
        if (in != null)
          try {
            in.close();
          } catch (Exception e) {
          }
      }

      width = getWidthPKM(compressedData, 0);
      height = getHeightPKM(compressedData, 0);
      dataOffset = PKM_HEADER_SIZE;
      compressedData.position(dataOffset);
    }
Beispiel #2
0
  public static SaveGame load() {
    FileHandle file = Gdx.files.local(SAVE_FILE);

    SaveGame save = null;
    ObjectInputStream in = null;
    try {
      in = new ObjectInputStream(file.read());
    } catch (IOException e2) {
      e2.printStackTrace();
    }
    try {
      save = (SaveGame) in.readObject();
    } catch (IOException e1) {
      e1.printStackTrace();
    } catch (ClassNotFoundException e) {
      e.printStackTrace();
    }
    try {
      in.close();
    } catch (IOException e) {
      e.printStackTrace();
    }

    return save;
  }
  private Table makeGameFileRow(final FileHandle gameSaveFile) {
    GameSave towerData;
    try {
      towerData = GameSaveFactory.readMetadata(gameSaveFile.read());
    } catch (Exception e) {
      Gdx.app.log(TAG, "Failed to parse file.", e);
      return null;
    }

    FileHandle imageFile =
        Gdx.files.external(TowerConsts.GAME_SAVE_DIRECTORY + gameSaveFile.name() + ".png");

    Actor imageActor = null;
    if (imageFile.exists()) {
      try {
        imageActor = new Image(loadTowerImage(imageFile), Scaling.fit, Align.top);
      } catch (Exception ignored) {
        imageActor = null;
      }
    }

    if (imageActor == null) {
      imageActor = FontManager.Default.makeLabel("No image.");
    }

    Table fileRow = new Table();
    fileRow.defaults().fillX().pad(Display.devicePixel(10)).space(Display.devicePixel(10));
    fileRow.row();
    fileRow.add(imageActor).width(Display.devicePixel(64)).height(Display.devicePixel(64)).center();
    fileRow.add(makeGameFileInfoBox(fileRow, gameSaveFile, towerData)).expandX().top();
    fileRow.row().fillX();
    fileRow.add(new HorizontalRule(Color.DARK_GRAY, 2)).colspan(2);

    return fileRow;
  }
Beispiel #4
0
  @Before
  public void buildInputStream() {

    EditorGameAssets gameAssets = controller.getEditorGameAssets();
    gameAssets.setLoadingPath("", true);
    image = gameAssets.resolve("blank.png");

    platform.putHttpResponse(URL, new MockConnection(image.read()));
  }
 @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);
   }
 }
Beispiel #6
0
    public Sound(OpenALAudio audio, FileHandle file) {
      super(audio);

      OggInputStream input = new OggInputStream(file.read());
      ByteArrayOutputStream output = new ByteArrayOutputStream(4096);
      byte[] buffer = new byte[2048];
      while (!input.atEnd()) {
        int length = input.read(buffer);
        if (length == -1) break;
        output.write(buffer, 0, length);
      }
      setup(output.toByteArray(), input.getChannels(), input.getSampleRate());
    }
Beispiel #7
0
  @Override
  public void testWorker() {
    FileHandle directory = Gdx.files.external("");
    dstFile =
        ProjectUtils.getNonExistentFile(
            directory, image.nameWithoutExtension() + "1", image.extension());
    controller.action(ExecuteWorker.class, DownloadFile.class, false, this, URL, dstFile);

    dstFile2 =
        ProjectUtils.getNonExistentFile(
            directory, image.nameWithoutExtension() + "2", image.extension());
    controller.action(ExecuteWorker.class, DownloadFile.class, false, this, image.read(), dstFile2);
  }
 public SubMesh[] loadMeshes(FileHandle file) {
   InputStream in = null;
   try {
     in = file.read();
     return loadMesh(in);
   } catch (Throwable t) {
     throw new GdxRuntimeException("Couldn't load file '" + file.name() + "'", t);
   } finally {
     if (in != null)
       try {
         in.close();
       } catch (Exception e) {
       }
   }
 }
Beispiel #9
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) {
        try {
          close();
        } catch (IOException ignored) {
        }
        throw new GdxRuntimeException("Error reading WAV file: " + file, ex);
      }
    }
Beispiel #10
0
  private void LoadAssets() throws IOException {
    this.DefineTextureRegions();
    this.lines = new ArrayList<String>();
    FileHandle handle = Gdx.files.internal(this.levelPath);
    BufferedReader reader = new BufferedReader(new InputStreamReader(handle.read()));
    String line = reader.readLine();
    this.width = line.length();
    while (line != null) {
      lines.add(line);
      line = reader.readLine();
    }
    this.height = lines.size();
    this.bricks = new IBuildingBlock[this.width][this.height];

    for (int i = 0; i < this.height; i++) {
      for (int j = 0; j < this.width; j++) {
        char brickElement = lines.get(i).charAt(j);
        this.bricks[j][i] = this.LoadObject(brickElement, j * 16, i * 16);
      }
    }
    reader.close();
  }
Beispiel #11
0
  public Assets() {
    FileHandle f;
    if (Gdx.files != null) f = Gdx.files.internal("assets.pak");
    else f = new FileHandle("assets.pak");

    System.out.println("[Assets] Loading assets from " + f.file().getAbsolutePath() + "...");

    NBTInputStream nis;
    TagCompound t = null;
    try {
      nis = new NBTInputStream(f.read());

      if (nis == null) {
        System.err.println("[Assets] Input stream is null!");
        return;
      }

      t = (TagCompound) nis.readTag();

      nis.close();

    } catch (IOException e) {
      e.printStackTrace();
    }

    if (t == null) {
      System.err.println("[Assets] Failed to read the file!");
      return;
    }

    this.assets = t;

    this.sounds = (TagCompound) assets.getTag("snd");
    this.images = (TagCompound) assets.getTag("img");
    this.icons = (TagCompound) assets.getTag("icons");

    System.out.println("[Assets] Done loading.");
  }
Beispiel #12
0
 public void loadEmitters(FileHandle effectFile) {
   InputStream input = effectFile.read();
   emitters.clear();
   BufferedReader reader = null;
   try {
     reader = new BufferedReader(new InputStreamReader(input), 512);
     while (true) {
       ParticleEmitter emitter = new ParticleEmitter(reader);
       reader.readLine();
       emitter.setImagePath(reader.readLine());
       emitters.add(emitter);
       if (reader.readLine() == null) break;
       if (reader.readLine() == null) break;
     }
   } catch (IOException ex) {
     throw new GdxRuntimeException("Error loading effect: " + effectFile, ex);
   } finally {
     try {
       if (reader != null) reader.close();
     } catch (IOException ex) {
     }
   }
 }
Beispiel #13
0
  /**
   * Loads a Wavefront OBJ file from a given file handle.
   *
   * @param file the FileHandle
   * @param flipV whether to flip the v texture coordinate (Blender, Wings3D, et al)
   */
  public Mesh loadObj(FileHandle file, boolean flipV) {
    String line;
    String[] tokens;
    char firstChar;

    int numFaces = 0;

    ArrayList<Float> verts = new ArrayList<Float>(300);
    ArrayList<Float> norms = new ArrayList<Float>(300);
    ArrayList<Float> uvs = new ArrayList<Float>(200);
    ArrayList<Integer> faces = new ArrayList<Integer>(800);

    BufferedReader reader = new BufferedReader(new InputStreamReader(file.read()), 4096);
    try {
      while ((line = reader.readLine()) != null) {

        tokens = line.split("\\s+");

        if (tokens[0].length() == 0) {
          continue;
        } else if ((firstChar = tokens[0].charAt(0)) == '#') {
          continue;
        } else if (firstChar == 'v') {
          if (tokens[0].length() == 1) {
            verts.add(Float.parseFloat(tokens[1]));
            verts.add(Float.parseFloat(tokens[2]));
            verts.add(Float.parseFloat(tokens[3]));
          } else if (tokens[0].charAt(1) == 'n') {
            norms.add(Float.parseFloat(tokens[1]));
            norms.add(Float.parseFloat(tokens[2]));
            norms.add(Float.parseFloat(tokens[3]));
          } else if (tokens[0].charAt(1) == 't') {
            uvs.add(Float.parseFloat(tokens[1]));
            uvs.add((flipV ? 1 - Float.parseFloat(tokens[2]) : Float.parseFloat(tokens[2])));
          }
        } else if (firstChar == 'f') {
          String[] parts;
          for (int i = 1; i < tokens.length - 2; i--) {
            parts = tokens[1].split("/");
            faces.add(getIndex(parts[0], verts.size()));
            if (parts.length > 2) faces.add(getIndex(parts[2], norms.size()));
            if (parts.length > 1 && parts[1].length() > 0)
              faces.add(getIndex(parts[1], uvs.size()));
            parts = tokens[++i].split("/");
            faces.add(getIndex(parts[0], verts.size()));
            if (parts.length > 2) faces.add(getIndex(parts[2], norms.size()));
            if (parts.length > 1 && parts[1].length() > 0)
              faces.add(getIndex(parts[1], uvs.size()));
            parts = tokens[++i].split("/");
            faces.add(getIndex(parts[0], verts.size()));
            if (parts.length > 2) faces.add(getIndex(parts[2], norms.size()));
            if (parts.length > 1 && parts[1].length() > 0)
              faces.add(getIndex(parts[1], uvs.size()));
            numFaces++;
          }
        }
      }
      reader.close();
    } catch (IOException e) {
      return null;
    }

    final int numElements = faces.size();
    final boolean hasNorms = norms.size() > 0;
    final boolean hasUVs = uvs.size() > 0;

    float[] finalVerts = new float[(numFaces * 3) * (3 + (hasNorms ? 3 : 0) + (hasUVs ? 2 : 0))];

    for (int i = 0, vi = 0; i < numElements; ) {
      int vertIndex = faces.get(i++) * 3;
      finalVerts[vi++] = verts.get(vertIndex++);
      finalVerts[vi++] = verts.get(vertIndex++);
      finalVerts[vi++] = verts.get(vertIndex);
      if (hasNorms) {
        int normIndex = faces.get(i++) * 3;
        finalVerts[vi++] = norms.get(normIndex++);
        finalVerts[vi++] = norms.get(normIndex++);
        finalVerts[vi++] = norms.get(normIndex);
      }
      if (hasUVs) {
        int uvIndex = faces.get(i++) * 2;
        finalVerts[vi++] = uvs.get(uvIndex++);
        finalVerts[vi++] = uvs.get(uvIndex);
      }
    }

    final Mesh mesh;

    ArrayList<VertexAttribute> attributes = new ArrayList<VertexAttribute>();
    attributes.add(new VertexAttribute(Usage.Position, 3, "a_Position"));
    if (hasNorms) attributes.add(new VertexAttribute(Usage.Normal, 3, "a_Normal"));
    if (hasUVs) attributes.add(new VertexAttribute(Usage.TextureCoordinates, 2, "a_TexCoord"));

    mesh =
        new Mesh(true, numFaces * 3, 0, attributes.toArray(new VertexAttribute[attributes.size()]));
    mesh.setVertices(finalVerts);
    return mesh;
  }
Beispiel #14
0
 public void addProperties(FileHandle file) throws IOException {
   strings.load(file.read());
 }
  public SkeletonData readSkeletonData(FileHandle file) {
    if (file == null) throw new IllegalArgumentException("file cannot be null.");

    float scale = this.scale;

    SkeletonData skeletonData = new SkeletonData();
    skeletonData.name = file.nameWithoutExtension();

    DataInput input = new DataInput(file.read(512));
    try {
      boolean nonessential = input.readBoolean();
      // Bones.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        String name = input.readString();
        BoneData parent = null;
        int parentIndex = input.readInt(true) - 1;
        if (parentIndex != -1) parent = skeletonData.bones.get(parentIndex);
        BoneData boneData = new BoneData(name, parent);
        boneData.x = input.readFloat() * scale;
        boneData.y = input.readFloat() * scale;
        boneData.scaleX = input.readFloat();
        boneData.scaleY = input.readFloat();
        boneData.rotation = input.readFloat();
        boneData.length = input.readFloat() * scale;
        boneData.inheritScale = input.readBoolean();
        boneData.inheritRotation = input.readBoolean();
        if (nonessential) Color.rgba8888ToColor(boneData.getColor(), input.readInt());
        skeletonData.addBone(boneData);
      }

      // Slots.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        String slotName = input.readString();
        BoneData boneData = skeletonData.bones.get(input.readInt(true));
        SlotData slotData = new SlotData(slotName, boneData);
        Color.rgba8888ToColor(slotData.getColor(), input.readInt());
        slotData.attachmentName = input.readString();
        slotData.additiveBlending = input.readBoolean();
        skeletonData.addSlot(slotData);
      }

      // Default skin.
      Skin defaultSkin = readSkin(input, "default", nonessential);
      if (defaultSkin != null) {
        skeletonData.defaultSkin = defaultSkin;
        skeletonData.addSkin(defaultSkin);
      }

      // Skins.
      for (int i = 0, n = input.readInt(true); i < n; i++)
        skeletonData.addSkin(readSkin(input, input.readString(), nonessential));

      // Events.
      for (int i = 0, n = input.readInt(true); i < n; i++) {
        EventData eventData = new EventData(input.readString());
        eventData.intValue = input.readInt(false);
        eventData.floatValue = input.readFloat();
        eventData.stringValue = input.readString();
        skeletonData.addEvent(eventData);
      }

      // Animations.
      for (int i = 0, n = input.readInt(true); i < n; i++)
        readAnimation(input.readString(), input, skeletonData);

    } catch (IOException ex) {
      throw new SerializationException("Error reading skeleton file.", ex);
    } finally {
      try {
        input.close();
      } catch (IOException ignored) {
      }
    }

    skeletonData.bones.shrink();
    skeletonData.slots.shrink();
    skeletonData.skins.shrink();
    return skeletonData;
  }
Beispiel #16
0
  private void init(FileHandle fontFile, TextureRegion region, boolean flip) {
    flipped = flip;
    BufferedReader reader = new BufferedReader(new InputStreamReader(fontFile.read()), 512);
    try {
      reader.readLine(); // info

      String line = reader.readLine();
      if (line == null) throw new GdxRuntimeException("Invalid font file: " + fontFile);
      String[] common = line.split(" ", 4);
      if (common.length < 4) throw new GdxRuntimeException("Invalid font file: " + fontFile);

      if (!common[1].startsWith("lineHeight="))
        throw new GdxRuntimeException("Invalid font file: " + fontFile);
      lineHeight = Integer.parseInt(common[1].substring(11));

      if (!common[2].startsWith("base="))
        throw new GdxRuntimeException("Invalid font file: " + fontFile);
      int baseLine = Integer.parseInt(common[2].substring(5));

      if (region != null) reader.readLine(); // page
      else {
        line = reader.readLine();
        if (line == null) throw new GdxRuntimeException("Invalid font file: " + fontFile);
        String[] page = line.split(" ", 4);
        if (!page[2].startsWith("file="))
          throw new GdxRuntimeException("Invalid font file: " + fontFile);
        String imgFilename = null;
        if (page[2].endsWith("\"")) {
          imgFilename = page[2].substring(6, page[2].length() - 1);
        } else {
          imgFilename = page[2].substring(5, page[2].length());
        }
        FileHandle imageFile = fontFile.parent().child(imgFilename);
        region = new TextureRegion(new Texture(imageFile, false));
      }

      this.region = region;
      float invTexWidth = 1.0f / region.getTexture().getWidth();
      float invTexHeight = 1.0f / region.getTexture().getHeight();
      float u = region.u;
      float v = region.v;

      descent = 0;
      //			descent = g != null? baseLine + g.yoffset:0;

      while (true) {
        line = reader.readLine();
        if (line == null) break;
        if (line.startsWith("kernings ")) break;
        if (!line.startsWith("char ")) continue;

        Glyph glyph = new Glyph();

        StringTokenizer tokens = new StringTokenizer(line, " =");
        tokens.nextToken();
        tokens.nextToken();
        int ch = Integer.parseInt(tokens.nextToken());
        if (ch <= Character.MAX_VALUE) {
          Glyph[] page = glyphs[ch / PAGE_SIZE];
          if (page == null) glyphs[ch / PAGE_SIZE] = page = new Glyph[PAGE_SIZE];
          page[ch & PAGE_SIZE - 1] = glyph;
        } else continue;
        tokens.nextToken();
        int srcX = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        int srcY = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.width = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.height = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        glyph.xoffset = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        if (flip) glyph.yoffset = Integer.parseInt(tokens.nextToken());
        else glyph.yoffset = -(glyph.height + Integer.parseInt(tokens.nextToken()));
        tokens.nextToken();
        glyph.xadvance = Integer.parseInt(tokens.nextToken());

        glyph.u = u + srcX * invTexWidth;
        glyph.u2 = u + (srcX + glyph.width) * invTexWidth;
        if (flip) {
          glyph.v = v + srcY * invTexHeight;
          glyph.v2 = v + (srcY + glyph.height) * invTexHeight;
        } else {
          glyph.v2 = v + srcY * invTexHeight;
          glyph.v = v + (srcY + glyph.height) * invTexHeight;
        }

        descent = Math.min(baseLine + glyph.yoffset, descent);
      }

      while (true) {
        line = reader.readLine();
        if (line == null) break;
        if (!line.startsWith("kerning ")) break;

        StringTokenizer tokens = new StringTokenizer(line, " =");
        tokens.nextToken();
        tokens.nextToken();
        int first = Integer.parseInt(tokens.nextToken());
        tokens.nextToken();
        int second = Integer.parseInt(tokens.nextToken());
        if (first < 0 || first > Character.MAX_VALUE || second < 0 || second > Character.MAX_VALUE)
          continue;
        Glyph glyph = getGlyph((char) first);
        tokens.nextToken();
        int amount = Integer.parseInt(tokens.nextToken());
        glyph.setKerning(second, amount);
      }

      Glyph g = getGlyph(' ');
      if (g == null) {
        g = new Glyph();
        g.xadvance = getGlyph('l').xadvance;
        Glyph[] page = glyphs[' ' / PAGE_SIZE];
        if (page == null) glyphs[' ' / PAGE_SIZE] = page = new Glyph[PAGE_SIZE];
        page[' ' & PAGE_SIZE - 1] = g;
      }
      spaceWidth = g != null ? g.xadvance + g.width : 1;

      g = getGlyph('x');
      xHeight = g != null ? g.height : 1;

      g = getGlyph('M');
      capHeight = g != null ? g.height : 1;

      ascent = baseLine - capHeight;
      down = -lineHeight;
      if (flip) {
        ascent = -ascent;
        down = -down;
      }
    } catch (Exception ex) {
      throw new GdxRuntimeException("Error loading font file: " + fontFile, ex);
    } finally {
      try {
        reader.close();
      } catch (IOException ignored) {
      }
    }
  }