/** * Render the game (part of the main loop, no allocations anywhere in here). * * @param gl openGL context reference */ public void render(GL10 gl) { GLGfx.clearScreen(gl, true, CLEAR_COLOR, true); mBack.draw(gl); GLGfx.drawLine( gl, RAIL_COLOR, 0, BANNER_HEIGHT * mDensity, getWidth(), BANNER_HEIGHT * mDensity); mPaddles[0].draw(gl); mPaddles[1].draw(gl); mBall.draw(gl); }
/** * Load game assets and initialize * * @param gl openGL context reference */ public void loadAssets(GL10 gl) { GLGfx.initGfx(40); // Get the density for banner size calcs. DisplayMetrics metrics = new DisplayMetrics(); mActivity.getWindowManager().getDefaultDisplay().getMetrics(metrics); mDensity = metrics.density; // initialize the only texture try { AssetManager assetMgr = mActivity.getAssets(); mTex = Texture.createTextureFromStream((GL11) gl, true, assetMgr.open("assets_tex.png")); } catch (Exception e) { throw new RuntimeException(e); } // setup the sprites mPaddles = new Sprite[2]; mPaddles[0] = new Sprite9Patch(PADDLE_TEXRECT, PADDLE_SIZE); mPaddles[1] = new Sprite9Patch(PADDLE_TEXRECT, PADDLE_SIZE); mBall = new Sprite(BALL_TEXRECT); mBack = new Sprite(BACK_TEXRECT); // position the background image so it is centered in the game area drawn at it's native size if // it fits // if it doesn't fit scale it so it draws as large as possible float gameAreaHeight = getHeight() - BANNER_HEIGHT * mDensity; float scale = (BACK_SIZE.y > gameAreaHeight) ? (gameAreaHeight / BACK_SIZE.y) : 1.0f; float bgX = (getWidth() - (scale * BACK_SIZE.x)) / 2.0f; float bgY = (BANNER_HEIGHT * mDensity) + (gameAreaHeight - (scale * BACK_SIZE.y)) / 2.0f; mBack.setPosAndSize(bgX, bgY, scale * BACK_SIZE.x, scale * BACK_SIZE.y); // Initialize the game mGameLogic.setupRect(new RectF(0.0f, BANNER_HEIGHT * mDensity, getWidth(), getHeight())); mGameLogic.initRound(); }