private static void initNativeObject(final Bitmap bitmap) {
    final byte[] pixels = Cocos2dxBitmap.getPixels(bitmap);
    if (pixels == null) {
      return;
    }

    Cocos2dxBitmap.nativeInitBitmapDC(bitmap.getWidth(), bitmap.getHeight(), pixels);
  }
Esempio n. 2
0
  public static void init(final Activity activity) {
    final ApplicationInfo applicationInfo = activity.getApplicationInfo();

    initListener();

    try {
      // Get the lib_name from AndroidManifest.xml metadata
      ActivityInfo ai =
          activity
              .getPackageManager()
              .getActivityInfo(activity.getIntent().getComponent(), PackageManager.GET_META_DATA);
      if (null != ai.metaData) {
        String lib_name = ai.metaData.getString(META_DATA_LIB_NAME);
        if (null != lib_name) {
          System.loadLibrary(lib_name);
        } else {
          System.loadLibrary(DEFAULT_LIB_NAME);
        }
      }
    } catch (PackageManager.NameNotFoundException e) {
      throw new RuntimeException("Error getting activity info", e);
    }

    Cocos2dxHelper.sPackageName = applicationInfo.packageName;
    Cocos2dxHelper.sFileDirectory = activity.getFilesDir().getAbsolutePath();
    // Cocos2dxHelper.nativeSetApkPath(applicationInfo.sourceDir);

    Cocos2dxHelper.sCocos2dMusic = new Cocos2dxMusic(activity);
    Cocos2dxHelper.sCocos2dSound = new Cocos2dxSound(activity);
    Cocos2dxHelper.sAssetManager = activity.getAssets();

    // Cocos2dxHelper.nativeSetAssetManager(sAssetManager);
    Cocos2dxBitmap.setContext(activity);
    sActivity = activity;
  }
  /*
   * If maxWidth or maxHeight is not 0, split the string to fix the maxWidth
   * and maxHeight.
   */
  private static String[] splitString(
      final String string, final int maxWidth, final int maxHeight, final Paint paint) {
    final String[] lines = string.split("\\n");
    String[] ret = null;
    final FontMetricsInt fm = paint.getFontMetricsInt();
    final int heightPerLine = (int) Math.ceil(fm.bottom - fm.top);
    final int maxLines = maxHeight / heightPerLine;

    if (maxWidth != 0) {
      final LinkedList<String> strList = new LinkedList<String>();
      for (final String line : lines) {
        /*
         * The width of line is exceed maxWidth, should divide it into
         * two or more lines.
         */
        final int lineWidth = (int) Math.ceil(paint.measureText(line));
        if (lineWidth > maxWidth) {
          strList.addAll(Cocos2dxBitmap.divideStringWithMaxWidth(line, maxWidth, paint));
        } else {
          strList.add(line);
        }

        // Should not exceed the max height.
        if (maxLines > 0 && strList.size() >= maxLines) {
          break;
        }
      }

      // Remove exceeding lines.
      if (maxLines > 0 && strList.size() > maxLines) {
        while (strList.size() > maxLines) {
          strList.removeLast();
        }
      }

      ret = new String[strList.size()];
      strList.toArray(ret);
    } else if (maxHeight != 0 && lines.length > maxLines) {
      /* Remove exceeding lines. */
      final LinkedList<String> strList = new LinkedList<String>();
      for (int i = 0; i < maxLines; i++) {
        strList.add(lines[i]);
      }
      ret = new String[strList.size()];
      strList.toArray(ret);
    } else {
      ret = lines;
    }

    return ret;
  }
Esempio n. 4
0
  public static void init(
      final Context pContext, final Cocos2dxHelperListener pCocos2dxHelperListener) {
    final ApplicationInfo applicationInfo = pContext.getApplicationInfo();

    Cocos2dxHelper.sContext = pContext;
    Cocos2dxHelper.sCocos2dxHelperListener = pCocos2dxHelperListener;

    Cocos2dxHelper.sPackageName = applicationInfo.packageName;
    Cocos2dxHelper.sFileDirectory = pContext.getFilesDir().getAbsolutePath();
    Cocos2dxHelper.nativeSetApkPath(applicationInfo.sourceDir);

    Cocos2dxHelper.sCocos2dxAccelerometer = new Cocos2dxAccelerometer(pContext);
    Cocos2dxHelper.sCocos2dMusic = new Cocos2dxMusic(pContext);
    Cocos2dxHelper.sCocos2dSound = new Cocos2dxSound(pContext);
    Cocos2dxHelper.sAssetManager = pContext.getAssets();
    Cocos2dxBitmap.setContext(pContext);
    Cocos2dxETCLoader.setContext(pContext);
  }
  public static void init(final Activity activity) {
    if (!sInited) {
      final ApplicationInfo applicationInfo = activity.getApplicationInfo();

      Cocos2dxHelper.sCocos2dxHelperListener = (Cocos2dxHelperListener) activity;

      Cocos2dxHelper.sPackageName = applicationInfo.packageName;
      Cocos2dxHelper.sFileDirectory = activity.getFilesDir().getAbsolutePath();
      Cocos2dxHelper.nativeSetApkPath(applicationInfo.sourceDir);

      Cocos2dxHelper.sCocos2dxAccelerometer = new Cocos2dxAccelerometer(activity);
      Cocos2dxHelper.sCocos2dMusic = new Cocos2dxMusic(activity);
      Cocos2dxHelper.sCocos2dSound = new Cocos2dxSound(activity);
      Cocos2dxHelper.sAssetManager = activity.getAssets();
      Cocos2dxHelper.nativeSetContext((Context) activity, Cocos2dxHelper.sAssetManager);

      Cocos2dxBitmap.setContext(activity);
      sActivity = activity;

      sInited = true;
    }
  }
  private static TextProperty computeTextProperty(
      final String string, final int width, final int height, final Paint paint) {
    final FontMetricsInt fm = paint.getFontMetricsInt();
    final int h = (int) Math.ceil(fm.bottom - fm.top);
    int maxContentWidth = 0;

    final String[] lines = Cocos2dxBitmap.splitString(string, width, height, paint);

    if (width != 0) {
      maxContentWidth = width;
    } else {
      // Compute the max width.
      int temp = 0;
      for (final String line : lines) {
        temp = (int) Math.ceil(paint.measureText(line, 0, line.length()));
        if (temp > maxContentWidth) {
          maxContentWidth = temp;
        }
      }
    }

    return new TextProperty(maxContentWidth, h, lines);
  }
  /**
   * @param pWidth the width to draw, it can be 0
   * @param pHeight the height to draw, it can be 0
   */
  public static void createTextBitmap(
      String pString,
      final String pFontName,
      final int pFontSize,
      final int pAlignment,
      final int pWidth,
      final int pHeight) {
    final int horizontalAlignment = pAlignment & 0x0F;
    final int verticalAlignment = (pAlignment >> 4) & 0x0F;

    pString = Cocos2dxBitmap.refactorString(pString);
    final Paint paint = Cocos2dxBitmap.newPaint(pFontName, pFontSize, horizontalAlignment);

    final TextProperty textProperty =
        Cocos2dxBitmap.computeTextProperty(pString, pWidth, pHeight, paint);

    final int bitmapTotalHeight = (pHeight == 0 ? textProperty.mTotalHeight : pHeight);

    final Bitmap bitmap =
        Bitmap.createBitmap(textProperty.mMaxWidth, bitmapTotalHeight, Bitmap.Config.ARGB_8888);
    final Canvas canvas = new Canvas(bitmap);

    /* Draw string. */
    final FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
    int x = 0;
    int y =
        Cocos2dxBitmap.computeY(
            fontMetricsInt, pHeight, textProperty.mTotalHeight, verticalAlignment);
    final String[] lines = textProperty.mLines;
    for (final String line : lines) {
      x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment);
      canvas.drawText(line, x, y, paint);
      y += textProperty.mHeightPerLine;
    }

    Cocos2dxBitmap.initNativeObject(bitmap);
  }
  public static boolean createTextBitmapShadowStroke(
      byte[] bytes,
      final String fontName,
      int fontSize,
      int fontTintR,
      int fontTintG,
      int fontTintB,
      int fontTintA,
      int alignment,
      int width,
      int height,
      boolean shadow,
      float shadowDX,
      float shadowDY,
      float shadowBlur,
      float shadowOpacity,
      boolean stroke,
      int strokeR,
      int strokeG,
      int strokeB,
      int strokeA,
      float strokeSize) {

    String string;
    if (bytes == null || bytes.length == 0) {
      string = "";
    } else {
      string = new String(bytes);
    }

    final int horizontalAlignment = alignment & 0x0F;
    final int verticalAlignment = (alignment >> 4) & 0x0F;

    string = Cocos2dxBitmap.refactorString(string);
    final Paint paint = Cocos2dxBitmap.newPaint(fontName, fontSize, horizontalAlignment);

    // if the first word width less than designed width, it means no words to show
    if (0 != width) {
      final int firstWordWidth = (int) Math.ceil(paint.measureText(string, 0, 1));
      if (firstWordWidth > width) {
        Log.w(
            "createTextBitmapShadowStroke warning:",
            "the input width is less than the width of the pString's first word\n");
        return false;
      }
    }

    // set the paint color
    paint.setARGB(fontTintA, fontTintR, fontTintG, fontTintB);

    final TextProperty textProperty =
        Cocos2dxBitmap.computeTextProperty(string, width, height, paint);
    final int bitmapTotalHeight = (height == 0 ? textProperty.mTotalHeight : height);

    // padding needed when using shadows (not used otherwise)
    float bitmapPaddingX = 0.0f;
    float bitmapPaddingY = 0.0f;
    float renderTextDeltaX = 0.0f;
    float renderTextDeltaY = 0.0f;

    if (0 == textProperty.mMaxWidth || 0 == bitmapTotalHeight) {
      Log.w(
          "createTextBitmapShadowStroke warning:",
          "textProperty MaxWidth is 0 or bitMapTotalHeight is 0\n");
      return false;
    }

    final Bitmap bitmap =
        Bitmap.createBitmap(
            textProperty.mMaxWidth + (int) bitmapPaddingX,
            bitmapTotalHeight + (int) bitmapPaddingY,
            Bitmap.Config.ARGB_8888);

    final Canvas canvas = new Canvas(bitmap);

    // Draw string.
    final FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();

    // draw again with stroke on if needed
    if (stroke) {
      final Paint paintStroke = Cocos2dxBitmap.newPaint(fontName, fontSize, horizontalAlignment);
      paintStroke.setStyle(Paint.Style.STROKE);
      paintStroke.setStrokeWidth(strokeSize);
      paintStroke.setARGB(strokeA, strokeR, strokeG, strokeB);

      int x = 0;
      int y =
          Cocos2dxBitmap.computeY(
              fontMetricsInt, height, textProperty.mTotalHeight, verticalAlignment);
      final String[] lines2 = textProperty.mLines;

      for (final String line : lines2) {

        x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment);
        canvas.drawText(line, x + renderTextDeltaX, y + renderTextDeltaY, paintStroke);
        canvas.drawText(line, x + renderTextDeltaX, y + renderTextDeltaY, paint);
        y += textProperty.mHeightPerLine;
      }

    } else {
      int x = 0;
      int y =
          Cocos2dxBitmap.computeY(
              fontMetricsInt, height, textProperty.mTotalHeight, verticalAlignment);

      final String[] lines = textProperty.mLines;

      for (final String line : lines) {

        x = Cocos2dxBitmap.computeX(line, textProperty.mMaxWidth, horizontalAlignment);
        canvas.drawText(line, x + renderTextDeltaX, y + renderTextDeltaY, paint);
        y += textProperty.mHeightPerLine;
      }
    }

    Cocos2dxBitmap.initNativeObject(bitmap);

    return true;
  }