Esempio n. 1
0
  /**
   * Returns a Texture2D object given an CGImageRef image If the image was not previously loaded, it
   * will create a new CCTexture2D object and it will return it. Otherwise it will return a
   * reference of a previously loaded image The "key" parameter will be used as the "key" for the
   * cache. If "key" is nil, then a new texture will be created each time.
   *
   * <p>BE AWARE OF the fact that copy of image is stored in memory, use assets method if you can.
   *
   * @since v0.8
   */
  public CCTexture2D addImage(Bitmap image, String key) {
    assert (image != null) : "TextureCache: image must not be null";
    CCTexture2D tex = null;

    if (key != null && (tex = textures.get(key)) != null) {
      return tex;
    }

    final Bitmap copy = image.copy(image.getConfig(), false);

    if (copy != null) {
      final CCTexture2D texNew = new CCTexture2D();
      texNew.setLoader(
          new GLResourceHelper.GLResourceLoader() {
            @Override
            public void load() {
              Bitmap initImage = copy.copy(copy.getConfig(), false);
              texNew.initWithImage(initImage);
            }
          });
      if (key != null) {
        textures.put(key, texNew);
      }

      return texNew;
    } else {
      ccMacros.CCLOG("cocos2d", "Couldn't add Bitmap in CCTextureCache");
      return null;
    }
  }
Esempio n. 2
0
 /**
  * Purges the dictionary of loaded textures. Call this method if you receive the "Memory Warning"
  * In the short term: it will free some resources preventing your app from being killed In the
  * medium term: it will allocate more resources In the long term: it will be the same
  */
 public void removeAllTextures() {
   /* Do nothing, or do all.*/
   for (CCTexture2D tex : textures.values()) {
     tex.releaseTexture(CCDirector.gl);
   }
   // textures.clear();
 }
Esempio n. 3
0
  /** sets a new display frame to the CCSprite. */
  public void setDisplayFrame(CCSpriteFrame frame) {
    unflippedOffsetPositionFromCenter_.set(frame.offset_);

    CCTexture2D newTexture = frame.getTexture();
    // update texture before updating texture rect
    if (texture_ == null || newTexture.name() != texture_.name()) setTexture(newTexture);

    // update rect
    setTextureRect(frame.rect_, frame.originalSize_, frame.rotated_);
  }
Esempio n. 4
0
  /**
   * Initializes an sprite with a CGImageRef and a key The key is used by the CCTextureCache to know
   * if a texture was already created with this CGImage. For example, a valid key
   * is: @"sprite_frame_01". If key is nil, then a new texture will be created each time by the
   * CCTextureCache.
   *
   * @since v0.99.0
   */
  public CCSprite(Bitmap image, String key) {
    assert image != null : "Invalid CGImageRef for sprite";

    // XXX: possible bug. See issue #349. New API should be added
    CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage(image, key);

    CGSize size = texture.getContentSize();
    CGRect rect = CGRect.make(0, 0, size.width, size.height);

    init(texture, rect);
  }
Esempio n. 5
0
  /**
   * Initializes an sprite with an image filepath. The rect used will be the size of the image. The
   * offset will be (0,0).
   */
  public CCSprite(String filepath) {
    assert filepath != null : "Invalid filename for sprite";

    CCTexture2D texture = CCTextureCache.sharedTextureCache().addImage(filepath);
    if (texture != null) {
      CGRect rect = CGRect.make(0, 0, 0, 0);
      rect.size = texture.getContentSize();
      init(texture, rect);
    } else {
      ccMacros.CCLOGERROR("CCSprite", "Unable to load texture from file: " + filepath);
    }
  }
Esempio n. 6
0
  /**
   * Returns a Texture2D object given an file image If the file image was not previously loaded, it
   * will create a new CCTexture2D object and it will return it. It will use the filename as a key.
   * Otherwise it will return a reference of a previosly loaded image. Supported image extensions:
   * .png, .bmp, .tiff, .jpeg, .pvr, .gif
   */
  public CCTexture2D addImage(String path) {
    assert path != null : "TextureMgr: path must not be null";

    CCTexture2D tex = textures.get(path);
    // removing texture if it is empty, bug fix
    if (tex != null && tex.name() == 0) {
      textures.remove(path);
      tex = null;
    }

    if (tex == null) {
      tex = createTextureFromFilePath(path);
      textures.put(path, tex);
    }
    return tex;
  }
Esempio n. 7
0
  private static CCTexture2D createTextureFromFilePathExternal(final String path) {

    final CCTexture2D tex = new CCTexture2D();
    tex.setLoader(
        new GLResourceHelper.GLResourceLoader() {

          @Override
          public void load() {
            try {
              InputStream is = new FileInputStream(path);
              Bitmap bmp = BitmapFactory.decodeStream(is);
              is.close();
              tex.initWithImage(bmp);
            } catch (IOException e) {
              // TODO Auto-generated catch block
              e.printStackTrace();
            }
          }
        });

    return tex;
  }
Esempio n. 8
0
  public void draw(GL10 gl) {
    assert !usesSpriteSheet_
        : "If CCSprite is being rendered by CCSpriteSheet, CCSprite#draw SHOULD NOT be called";

    // Default GL states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Needed states: GL_TEXTURE_2D, GL_VERTEX_ARRAY, GL_COLOR_ARRAY, GL_TEXTURE_COORD_ARRAY
    // Unneeded states: -

    boolean newBlend = false;
    if (blendFunc_.src != ccConfig.CC_BLEND_SRC || blendFunc_.dst != ccConfig.CC_BLEND_DST) {
      newBlend = true;
      gl.glBlendFunc(blendFunc_.src, blendFunc_.dst);
    }

    //        ((EGL10) gl).eglWaitNative(EGL10.EGL_NATIVE_RENDERABLE, null);
    //        // bug fix in case texture name = 0
    //        texture_.checkName();
    // #define kQuadSize sizeof(quad_.bl)
    gl.glBindTexture(GL10.GL_TEXTURE_2D, texture_.name());

    // int offset = (int)&quad_;

    // vertex
    // int diff = offsetof( ccV3F_C4B_T2F, vertices);
    gl.glVertexPointer(3, GL10.GL_FLOAT, 0, vertexes);

    // color
    // diff = offsetof( ccV3F_C4B_T2F, colors);
    gl.glColorPointer(4, GL10.GL_FLOAT, 0, colors);

    // tex coords
    // diff = offsetof( ccV3F_C4B_T2F, texCoords);
    gl.glTexCoordPointer(2, GL10.GL_FLOAT, 0, texCoords);

    gl.glDrawArrays(GL10.GL_TRIANGLE_STRIP, 0, 4);

    if (newBlend) gl.glBlendFunc(ccConfig.CC_BLEND_SRC, ccConfig.CC_BLEND_DST);

    /*
    if (ccConfig.CC_SPRITE_DEBUG_DRAW) {
        CGSize s = this.contentSize();
        CGPoint vertices[]= new CGPoint [] {
            CGPoint.ccp(0,0),   CGPoint.ccp(s.width,0),
            CGPoint.ccp(s.width,s.height),  CGPoint.ccp(0,s.height)
        };
        ccDrawingPrimitives.ccDrawPoly(vertices, 4, true);
    } // CC_TEXTURENODE_DEBUG_DRAW
    */
  }
Esempio n. 9
0
  private void updateBlendFunc() {
    assert !usesSpriteSheet_
        : "CCSprite: updateBlendFunc doesn't work when the sprite is rendered using a CCSpriteSheet";

    // it's possible to have an untextured sprite
    if (texture_ == null || !texture_.hasPremultipliedAlpha()) {
      blendFunc_.src = GL10.GL_SRC_ALPHA;
      blendFunc_.dst = GL10.GL_ONE_MINUS_SRC_ALPHA;
      setOpacityModifyRGB(false);
    } else {
      blendFunc_.src = ccConfig.CC_BLEND_SRC;
      blendFunc_.dst = ccConfig.CC_BLEND_DST;
      setOpacityModifyRGB(true);
    }
  }
Esempio n. 10
0
 /*
  * Add a texture to the cache so it gets managed
  */
 public void addTexture(CCTexture2D tex) {
   if (tex == null) return;
   textures.put(String.valueOf(tex.hashCode()), tex);
 }
Esempio n. 11
0
  private void updateTextureCoords(CGRect rect) {
    float atlasWidth = 1;
    float atlasHeight = 1;

    if (texture_ != null) {
      atlasWidth = texture_.pixelsWide();
      atlasHeight = texture_.pixelsHigh();
    }

    if (rectRotated_) {
      float left = (2 * rect.origin.x + 1) / (2 * atlasWidth);
      float right = left + (rect.size.height * 2 - 2) / (2 * atlasWidth);
      float top = (2 * rect.origin.y + 1) / (2 * atlasHeight);
      float bottom = top + (rect.size.width * 2 - 2) / (2 * atlasHeight);

      if (flipX_) {
        float tmp = top;
        top = bottom;
        bottom = tmp;
      }

      if (flipY_) {
        float tmp = left;
        left = right;
        right = tmp;
      }

      tmpV[0] = right;
      tmpV[1] = top; // tl v
      tmpV[2] = left; // bl u
      tmpV[3] = top; // bl v
      tmpV[4] = right; // tr u
      tmpV[5] = bottom; // tr v
      tmpV[6] = left; // br u
      tmpV[7] = bottom; // br v

      BufferUtils.copyFloats(tmpV, 0, texCoords, 8);

      //	        texCoords.put(0, right); // tl u
      //	        texCoords.put(1, top); // tl v
      //	        texCoords.put(2, left); // bl u
      //	        texCoords.put(3, top); // bl v
      //	        texCoords.put(4, right); // tr u
      //	        texCoords.put(5, bottom); // tr v
      //	        texCoords.put(6, left); // br u
      //	        texCoords.put(7, bottom); // br v
    } else {
      float left = (2 * rect.origin.x + 1) / (2 * atlasWidth);
      float right = left + (rect.size.width * 2 - 2) / (2 * atlasWidth);
      float top = (2 * rect.origin.y + 1) / (2 * atlasHeight);
      float bottom = top + (rect.size.height * 2 - 2) / (2 * atlasHeight);

      if (flipX_) {
        float tmp = left;
        left = right;
        right = tmp;
      }

      if (flipY_) {
        float tmp = top;
        top = bottom;
        bottom = tmp;
      }

      tmpV[0] = left; // tl u
      tmpV[1] = top; // tl v
      tmpV[2] = left; // bl u
      tmpV[3] = bottom; // bl v
      tmpV[4] = right; // tr u
      tmpV[5] = top; // tr v
      tmpV[6] = right; // br u
      tmpV[7] = bottom; // br v
      BufferUtils.copyFloats(tmpV, 0, texCoords, 8);

      //	        texCoords.put(0, left); // tl u
      //	        texCoords.put(1, top); // tl v
      //	        texCoords.put(2, left); // bl u
      //	        texCoords.put(3, bottom); // bl v
      //	        texCoords.put(4, right); // tr u
      //	        texCoords.put(5, top); // tr v
      //	        texCoords.put(6, right); // br u
      //	        texCoords.put(7, bottom); // br v
    }

    texCoords.position(0);

    if (usesSpriteSheet_) textureAtlas_.putTexCoords(texCoords, atlasIndex);
  }
Esempio n. 12
0
 /**
  * Initializes an sprite with a texture. The rect used will be the size of the texture. The offset
  * will be (0,0).
  */
 public CCSprite(CCTexture2D texture) {
   CGSize size = texture.getContentSize();
   CGRect rect = CGRect.make(0, 0, size.width, size.height);
   init(texture, rect);
 }