Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1051 chris 1
package com.gebauz.bauzoid.graphics;
2
 
3
import com.badlogic.gdx.Application.ApplicationType;
4
import com.badlogic.gdx.Gdx;
5
import com.badlogic.gdx.graphics.GL20;
6
import com.gebauz.bauzoid.app.BauzoidException;
7
import com.gebauz.bauzoid.game.Game;
8
import com.gebauz.bauzoid.game.GameObject;
9
import com.gebauz.bauzoid.graphics.model.IndexStream;
10
import com.gebauz.bauzoid.graphics.model.VertexStream;
11
import com.gebauz.bauzoid.graphics.renderstates.RenderStates;
12
import com.gebauz.bauzoid.graphics.shader.ShaderProgram;
13
import com.gebauz.bauzoid.graphics.sprite.SpriteShader;
14
import com.gebauz.bauzoid.graphics.sprite.TileBatch;
15
 
16
/** Graphics object.
17
 * Root of all graphics objects.
18
 * Stores references to objects that need to be globally available.
19
 *
20
 */
21
public class Graphics extends GameObject
22
{
23
        private int mWidth = 0;
24
        private int mHeight = 0;
25
 
26
        public RenderStates renderStates = new RenderStates();
27
 
28
        public TileBatch mBatch = null;
29
 
30
        /** Global font instance. */
31
        // private Font ...
32
        /** Global sprite shader instance. */
33
        private SpriteShader mSpriteShader;
34
 
35
        /** Constructor. */
36
        public Graphics(Game game)
37
        {
38
                super(game);
39
 
40
                try
41
                {
42
                        renderStates.initialize();
43
                }
44
                catch (BauzoidException ex)
45
                {
46
                        ex.printStackTrace();                  
47
                }
48
        }
49
 
50
        /** Initialize globally available objects. */
51
        public void init()
52
        {
53
                mSpriteShader = new SpriteShader(this);
54
 
55
                mBatch = new TileBatch(this);
56
        }
57
 
58
        /** Destroy globally available objects. */
59
        public void exit()
60
        {
61
                if (mBatch != null)
62
                {
63
                        mBatch.dispose();
64
                        mBatch = null;
65
                }
66
 
67
                if (mSpriteShader != null)
68
                {
69
                        mSpriteShader.dispose();
70
                        mSpriteShader = null;
71
                }
72
        }
73
 
74
        /** Clear render surface (color and depth buffer). */
75
        public void clear(float r, float g, float b, float a)
76
        {
77
                clear(r, g, b, a, true, true);
78
        }
79
 
80
        /** Same as clear(r, g, b, a, true, clearDepth). */
81
        public void clear(float r, float g, float b, float a, boolean clearDepth)
82
        {
83
                clear(r, g, b, a, true, clearDepth);
84
        }
85
 
86
        /** Clear render surface. */
87
        public void clear(float r, float g, float b, float a, boolean clearColor, boolean clearDepth)
88
        {
89
                int bits = 0;
90
                if (clearColor)
91
                        bits |= GL20.GL_COLOR_BUFFER_BIT;
92
                if (clearDepth)
93
                        bits |= GL20.GL_DEPTH_BUFFER_BIT;
94
 
95
                Gdx.gl20.glClearColor(r, g, b, a);
96
                Gdx.gl20.glClear(bits);
97
        }
98
 
99
        public void onSurfaceLost()
100
        {
101
                // destroy resources
102
        }
103
 
104
        public void onSurfaceRecreated()
105
        {
106
                // Update viewport
107
                Gdx.gl20.glViewport(0, 0, mWidth, mHeight);
108
 
109
                // recreate resources
110
                try
111
                {
112
                        renderStates.initialize();
113
                }
114
                catch (BauzoidException ex)
115
                {
116
                        ex.printStackTrace();                  
117
                }
118
                VertexStream.reloadManagedStreams();
119
                IndexStream.reloadManagedStreams();
120
                ShaderProgram.reloadManagedShaders();
121
        }
122
 
123
        /** Update the GL Surface's dimensions. */
124
        public void updateSurfaceDimensions(int w, int h)
125
        {
126
                mWidth = w;
127
                mHeight = h;
128
        }
129
 
130
        /** Get the GL Surface's width in pixels. */
131
        public final int getWidth()
132
        {
133
                return mWidth;
134
        }
135
 
136
        /** Get the GL Surface's height in pixels. */
137
        public final int getHeight()
138
        {
139
                return mHeight;
140
        }
141
 
142
        /** Get a reference to the sprite shader. */
143
        public final SpriteShader getSpriteShader()
144
        {
145
                return mSpriteShader;
146
        }
147
 
148
        /** Get the aspect ratio. */
149
        public final float getAspect()
150
        {
151
                return ((float)mWidth / (float)mHeight);
152
        }
153
 
154
        /** Get the scale from DIP to pixels. */
155
        public final float getDipToPixelScale()
156
        {
157
                if ((Gdx.app.getType() == ApplicationType.Desktop) ||
158
                        (Gdx.app.getType() == ApplicationType.Applet))
159
                        return 1.0f;
160
 
161
                return Gdx.graphics.getDensity();
162
        }
163
 
164
        public final TileBatch getBatch()
165
        {
166
                return mBatch;
167
        }
168
 
169
}
170
 
171