Home | History | Annotate | Download | only in replicaisland
      1 /*
      2  * Copyright (C) 2010 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.replica.replicaisland;
     18 
     19 /**
     20  * Implements rendering of a drawable object for a game object.  If a drawable is set on this
     21  * component it will be passed to the renderer and drawn on the screen every frame.  Drawable
     22  * objects may be set to be "camera-relative" (meaning their screen position is relative to the
     23  * location of the camera focus in the scene) or not (meaning their screen position is relative to
     24  * the origin at the lower-left corner of the display).
     25  */
     26 public class RenderComponent extends GameComponent {
     27     private DrawableObject mDrawable;
     28     private int mPriority;
     29     private boolean mCameraRelative;
     30     private Vector2 mPositionWorkspace;
     31     private Vector2 mScreenLocation;
     32     private Vector2 mDrawOffset;
     33 
     34     public RenderComponent() {
     35         super();
     36         setPhase(ComponentPhases.DRAW.ordinal());
     37 
     38         mPositionWorkspace = new Vector2();
     39         mScreenLocation = new Vector2();
     40         mDrawOffset = new Vector2();
     41         reset();
     42     }
     43 
     44     @Override
     45     public void reset() {
     46         mPriority = 0;
     47         mCameraRelative = true;
     48         mDrawable = null;
     49         mDrawOffset.zero();
     50     }
     51 
     52     public void update(float timeDelta, BaseObject parent) {
     53         if (mDrawable != null) {
     54             RenderSystem system = sSystemRegistry.renderSystem;
     55             if (system != null) {
     56                 mPositionWorkspace.set(((GameObject)parent).getPosition());
     57                 mPositionWorkspace.add(mDrawOffset);
     58                 if (mCameraRelative) {
     59                     CameraSystem camera = sSystemRegistry.cameraSystem;
     60                     ContextParameters params = sSystemRegistry.contextParameters;
     61                     mScreenLocation.x = (mPositionWorkspace.x - camera.getFocusPositionX()
     62                                     + (params.gameWidth / 2));
     63                     mScreenLocation.y = (mPositionWorkspace.y - camera.getFocusPositionY()
     64                                     + (params.gameHeight / 2));
     65                 }
     66                 // It might be better not to do culling here, as doing it in the render thread
     67                 // would allow us to have multiple views into the same scene and things like that.
     68                 // But at the moment significant CPU is being spent on sorting the list of objects
     69                 // to draw per frame, so I'm going to go ahead and cull early.
     70                 if (mDrawable.visibleAtPosition(mScreenLocation)) {
     71                     system.scheduleForDraw(mDrawable, mPositionWorkspace, mPriority, mCameraRelative);
     72                 } else if (mDrawable.getParentPool() != null) {
     73                     // Normally the render system releases drawable objects back to the factory
     74                     // pool, but in this case we're short-circuiting the render system, so we
     75                     // need to release the object manually.
     76                     sSystemRegistry.drawableFactory.release(mDrawable);
     77                     mDrawable = null;
     78                 }
     79             }
     80         }
     81     }
     82 
     83     public DrawableObject getDrawable() {
     84         return mDrawable;
     85     }
     86 
     87     public void setDrawable(DrawableObject drawable) {
     88         mDrawable = drawable;
     89     }
     90 
     91     public void setPriority(int priority) {
     92         mPriority = priority;
     93     }
     94 
     95     public int getPriority() {
     96         return mPriority;
     97     }
     98 
     99     public void setCameraRelative(boolean relative) {
    100         mCameraRelative = relative;
    101     }
    102 
    103     public void setDrawOffset(float x, float y) {
    104         mDrawOffset.set(x, y);
    105     }
    106 
    107 }
    108