-
Notifications
You must be signed in to change notification settings - Fork 0
/
Screen.java
88 lines (71 loc) · 2.38 KB
/
Screen.java
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
import com.badlogic.gdx.Gdx;
import com.badlogic.gdx.graphics.Camera;
import com.badlogic.gdx.graphics.Color;
import com.badlogic.gdx.graphics.GL10;
import com.badlogic.gdx.graphics.OrthographicCamera;
import com.badlogic.gdx.graphics.g2d.SpriteBatch;
import com.badlogic.gdx.graphics.glutils.ShapeRenderer;
import com.badlogic.gdx.math.Vector2;
import com.badlogic.gdx.scenes.scene2d.Stage;
import com.badlogic.gdx.utils.Scaling;
/**
* General screen stuff that can be shared
*/
public abstract class Screen implements com.badlogic.gdx.Screen{
protected int width, height;
protected OrthographicCamera cam;
protected Vector2 center;
protected SpriteBatch batch;
protected ShapeRenderer debug;
protected abstract void gameStep(float delta);
protected float scale = 1;
public Screen(int width, int height) {
this.width = width;
this.height = height;
this.center = new Vector2(0, 0);
}
@Override
public void render(float delta) {
Gdx.gl.glClearColor(0, 0, 0, 1);
Gdx.gl.glClear(GL10.GL_COLOR_BUFFER_BIT);
gameStep(delta);
cam.update();
batch.setProjectionMatrix(cam.combined);
debug.setProjectionMatrix(cam.combined);
}
@Override
public void resize(int width, int height) {
Vector2 size = Scaling.fit.apply(this.width, this.height, width, height);
int viewportX = (int) (width - size.x) / 2;
int viewportY = (int) (height - size.y) / 2;
int viewportWidth = (int) size.x;
int viewportHeight = (int) size.y;
float zoom = width < viewportWidth ?
(float) this.height / viewportHeight :
(float) this.width / viewportWidth;
Gdx.gl.glViewport(viewportX, viewportY, viewportWidth, viewportHeight);
cam.position.set(center.x, center.y, 0);
cam.zoom = zoom;
cam.viewportWidth = viewportWidth;
cam.viewportHeight = viewportHeight;
scale = (float) viewportHeight / (float) this.height;
}
@Override
public void show() {
this.cam = new OrthographicCamera(width, height);
this.batch = new SpriteBatch();
this.debug = new ShapeRenderer();
}
@Override
public void hide() {
}
@Override
public void pause() {
}
@Override
public void resume() {
}
@Override
public void dispose() {
}
}