JavaとJavaEEプログラマのブログ

JavaEEを中心にしたをソフトウェア開発についてのブログ

複数桁の数字の描画

GraphicUtilsクラスに数字を描画するメソッドを追加。

	/**
	 * 指定した数字を、指定した桁数で表示する。
	 * @param x 表示位置のX座標
	 * @param y 表示位置のy座標
	 * @param width  数字の表示サイズ(幅)
	 * @param height 数字の表示サイズ(高さ)
	 * @param number 表示する数字
	 * @param figures 桁数
	 */
	public static void drawNumbers(GL10 gl, Context context, final float x, final float y,
			final float width, final float height,
			int textureId,
			final int number,final int figures,
			final int red, final int green, final int blue, final int alpha
			){

		float totalWidth = width * figures; //n文字分の横幅
		float rightX = x + totalWidth * 0.5f;
		float figlX = rightX - width * 0.5f;

		for(int i=0; i < figures; i++){
			float figNX = figlX - i * width;//数字の中心座標
			int numberToDraw = number / (int)Math.pow(10.0, (double)i) % 10; // i桁目の数字を取り出す。
			drawNumber(gl, context, figNX, y, width, height, textureId, numberToDraw, red, green, blue, alpha);
		}

	}


	/**
	 * 指定した数字のテクスチャを描画
	 */
	public static void drawNumber(GL10 gl, Context context, final float x, final float y,
			final float width, final float height,int textureId,
			final int number,
			final int red, final int green, final int blue, final int alpha
			){

		float u = (float)( number % 4 ) * 0.25f;
		float v = (float)( number / 4 ) * 0.25f;


		drawTextureRectangle(gl,context,x,y,width,height,textureId, u, v, 0.25f, 0.25f, red,green,blue,alpha);


	}

GLRendererクラスのonDrawFrameメソッドを修正。

		gl.glBlendFunc(GL10.GL_SRC_ALPHA, GL10.GL_ONE_MINUS_SRC_ALPHA);//アルファブレンディング
		gl.glEnable(GL10.GL_BLEND);

		drawTextureRectangle(gl,context,0.5f,0.7f,1.0f, 1.0f, textureId, 0, 0, 0, half/2 );
		drawTextureRectangle(gl,context,0.0f,0.0f,1.0f, 1.0f, textureId, one, one, one, half );

		drawNumber(gl, context,   0.5f,  0.5f, 1.0f, 1.0f, numberTextureId,0,one, one, one, half);
		drawNumber(gl, context,  -0.5f, -0.5f, 1.0f, 1.0f, numberTextureId,6,one, one, one, half);
		drawNumber(gl, context,   0.5f, -0.5f, 1.0f, 1.0f, numberTextureId,9,one, one, one, half);

		drawNumbers(gl, context,   0.0f, 0.0f,
				0.2f, 0.2f, numberTextureId,
				10925, 8,
				one, one, one, one);

		gl.glDisable(GL10.GL_BLEND);