Advertisement
Collzi

WorldRenderer.java

Oct 4th, 2013
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.00 KB | None | 0 0
  1. package com.collzi.pong.view;
  2.  
  3. import com.badlogic.gdx.Gdx;
  4. import com.badlogic.gdx.graphics.Color;
  5. import com.badlogic.gdx.graphics.FPSLogger;
  6. import com.badlogic.gdx.graphics.GL10;
  7. import com.badlogic.gdx.graphics.OrthographicCamera;
  8. import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
  9. import com.badlogic.gdx.graphics.glutils.ShapeRenderer.ShapeType;
  10. import com.badlogic.gdx.math.Rectangle;
  11.  
  12. import com.collzi.pong.model.Ball;
  13. import com.collzi.pong.model.Player;
  14. import com.collzi.pong.model.World;
  15.  
  16. public class WorldRenderer {
  17.    
  18.     private static final float CAMERA_WIDTH = 10f;
  19.     private static final float CAMERA_HEIGHT = 7f;
  20.    
  21.     private World world;
  22.     private OrthographicCamera cam;
  23.    
  24.     ShapeRenderer shapeRenderer = new ShapeRenderer();
  25.    
  26.     Rectangle playerShape = new Rectangle();
  27.     Rectangle ballShape = new Rectangle();
  28.     Rectangle topBounds = new Rectangle();
  29.     Rectangle bottomBounds = new Rectangle();
  30.    
  31.     private int width, height;
  32.     //private float ppuX, ppuY;
  33.    
  34.     private FPSLogger fpsLogger;
  35.    
  36.     public void setSize(int w, int h) {
  37.         this.width = w;
  38.         this.height = h;
  39.         //ppuX = (float)width / CAMERA_WIDTH;
  40.         //ppuY = (float)height / CAMERA_HEIGHT;
  41.     }
  42.    
  43.     public WorldRenderer(World world) {
  44.         this.world = world;
  45.         this.cam = new OrthographicCamera(CAMERA_WIDTH, CAMERA_HEIGHT);
  46.         this.cam.position.set(CAMERA_WIDTH / 2f, CAMERA_HEIGHT / 2f, 0);
  47.         this.cam.update();
  48.         fpsLogger = new FPSLogger();
  49.     }
  50.    
  51.     public void render() {
  52.         Gdx.gl.glClearColor(0.1f, 0.1f, 0.1f, 1);
  53.         Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
  54.         drawBall();
  55.         drawPlayer();
  56.         drawBot();
  57.         drawWorld();
  58.         fpsLogger.log();
  59.     }
  60.    
  61.     private void drawWorld() {
  62.         topBounds = world.getTopBounds();
  63.         bottomBounds = world.getBottomBounds();
  64.         shapeRenderer.begin(ShapeType.Filled);
  65.         shapeRenderer.setColor(new Color(1, 1, 1, 1));
  66.         shapeRenderer.rect(topBounds.x, topBounds.y, topBounds.width, topBounds.height);
  67.         shapeRenderer.rect(bottomBounds.x, bottomBounds.y, bottomBounds.width, bottomBounds.height);
  68.         shapeRenderer.end();
  69.     }
  70.    
  71.     private void drawPlayer() {
  72.         Player player = world.getPlayer();
  73.         playerShape = player.getBounds();
  74.         shapeRenderer.begin(ShapeType.Filled);
  75.         shapeRenderer.setColor(new Color(1, 1, 1, 1));
  76.         shapeRenderer.rect(playerShape.x, playerShape.y, playerShape.width, playerShape.height);
  77.         shapeRenderer.end();
  78.     }
  79.    
  80.     private void drawBot() {
  81.         Player bot = world.getBot();
  82.         playerShape = bot.getBounds();
  83.         shapeRenderer.begin(ShapeType.Filled);
  84.         shapeRenderer.setColor(new Color(1, 1, 1, 1));
  85.         shapeRenderer.rect(playerShape.x, playerShape.y, playerShape.width, playerShape.height);
  86.         shapeRenderer.end();
  87.     }
  88.    
  89.     private void drawBall() {
  90.         Ball ball = world.getBall();
  91.         ballShape = ball.getBounds();
  92.         shapeRenderer.begin(ShapeType.Filled);
  93.         shapeRenderer.setColor(new Color(1, 1, 1, 1));
  94.         shapeRenderer.rect(ballShape.x, ballShape.y, ballShape.width, ballShape.height);
  95.         shapeRenderer.end();
  96.     }
  97.    
  98.     public void dispose() {
  99.         shapeRenderer.dispose();
  100.     }
  101. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement