ulfben

DebugTextRenderer (android game dev)

Mar 18th, 2018
184
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. package com.ulfben.agp18.platformer;
  2. //Created by Ulf Benjaminsson (ulfben) on 2018-03-10.
  3. import android.graphics.Canvas;
  4. import android.graphics.Color;
  5. import android.graphics.Paint;
  6. import android.graphics.PointF;
  7. import java.util.Locale;
  8.  
  9. /* Add to Viewport.java:
  10.  @Override
  11.     public String toString(){
  12.         return String.format(Locale.getDefault(),"Viewport [%dpx, %dpx / %.1fm, %.1fm]", mScreenWidth, mScreenHeight, mMetersToShowX, mMetersToShowY);
  13.     }
  14. */
  15. /*
  16. add to Game.java, in update()
  17.        DebugTextRenderer.CAMERA_INFO = mCamera.toString();
  18.         DebugTextRenderer.PLAYER_POSITION.x = mLevel.mPlayer.x;
  19.         DebugTextRenderer.PLAYER_POSITION.y = mLevel.mPlayer.y;
  20.         DebugTextRenderer.PLAYER_VEL.x = mLevel.mPlayer.mVelocity.x;
  21.         DebugTextRenderer.PLAYER_VEL.y = mLevel.mPlayer.mVelocity.y;
  22.         DebugTextRenderer.TOTAL_OBJECT_COUNT = mLevel.mEntities.size();
  23.         DebugTextRenderer.VISIBLE_OBJECTS = mVisibleEntities.size();
  24.         DebugTextRenderer.FRAMERATE = 0;
  25. */
  26.  
  27.  
  28. class DebugTextRenderer {
  29.     private DebugTextRenderer(){super();}
  30.     //Updated from the GameEngine
  31.     static int VISIBLE_OBJECTS = 0;
  32.     static int TOTAL_OBJECT_COUNT = 0;
  33.     static final PointF PLAYER_POSITION = new PointF(0f,0f);
  34.     static final PointF PLAYER_VEL = new PointF(0f,0f);
  35.     static int FRAMERATE = 0;
  36.     static String CAMERA_INFO = "";
  37.  
  38.     private final static String[] DBG_STRINGS = new String[4];
  39.     //[0] = CAMERA_INFO
  40.     private final static String DBG_OBJ_RENDER_COUNT =  "Objects rendered: %d / %d"; //[1] rendering stats
  41.     private final static String DBG_PLAYER_INFO =  "Player: [%.2f, %.2f / %.2f, %.2f]"; //[2] player position and velocity
  42.     private final static String DBG_UPS = "FPS: %d"; //[3] frames per second
  43.     private final static Locale LOCALE = Locale.getDefault();
  44.  
  45.     private static void updateDebugStrings(){
  46.         DBG_STRINGS[0] = CAMERA_INFO;
  47.         DBG_STRINGS[1] = String.format(LOCALE, DBG_OBJ_RENDER_COUNT, VISIBLE_OBJECTS,  TOTAL_OBJECT_COUNT);
  48.         DBG_STRINGS[2] = String.format(LOCALE, DBG_PLAYER_INFO,  PLAYER_POSITION.x,  PLAYER_POSITION.y, PLAYER_VEL.x, PLAYER_VEL.y);
  49.         DBG_STRINGS[3] = String.format(LOCALE, DBG_UPS, FRAMERATE);
  50.     }
  51.  
  52.     static void render(final Canvas canvas, final Viewport camera, final Paint paint){
  53.         final int textSize = 20;
  54.         final int margin = 10;
  55.         int y = camera.getScreenHeight()-margin;
  56.         paint.setTextSize(textSize);
  57.         paint.setTextAlign(Paint.Align.LEFT);
  58.         paint.setColor(Color.WHITE);
  59.         updateDebugStrings();
  60.         for(final String s : DBG_STRINGS){
  61.             canvas.drawText(s, margin, y, paint);
  62.             y -= textSize;
  63.         }
  64.     }
  65. }
RAW Paste Data

Adblocker detected! Please consider disabling it...

We've detected AdBlock Plus or some other adblocking software preventing Pastebin.com from fully loading.

We don't have any obnoxious sound, or popup ads, we actively block these annoying types of ads!

Please add Pastebin.com to your ad blocker whitelist or disable your adblocking software.

×