-
Notifications
You must be signed in to change notification settings - Fork 0
/
AndroidGame.java
159 lines (127 loc) · 4.34 KB
/
AndroidGame.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
package com.jahndis.whalebot.framework.implementation;
import com.jahndis.whalebot.framework.Audio;
import com.jahndis.whalebot.framework.FileIO;
import com.jahndis.whalebot.framework.Game;
import com.jahndis.whalebot.framework.Graphics;
import com.jahndis.whalebot.framework.Input;
import com.jahndis.whalebot.framework.Screen;
import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Bitmap.Config;
import android.graphics.Point;
import android.graphics.PointF;
import android.os.Build.VERSION;
import android.os.Bundle;
import android.view.Window;
import android.view.WindowManager;
public abstract class AndroidGame extends Activity implements Game {
AndroidFastRenderView renderView;
Graphics graphics;
Audio audio;
Input input;
FileIO fileIO;
Screen screen;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
// Set window to have no title bar and be full screen
requestWindowFeature(Window.FEATURE_NO_TITLE);
getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN);
// Determine dimensions and scaling of frame buffer
Point frameBufferDimensions = getFrameBufferDimensions();
PointF frameBufferScale = getFrameBufferScale(frameBufferDimensions);
// Create the frame buffer
Bitmap frameBuffer = Bitmap.createBitmap(frameBufferDimensions.x, frameBufferDimensions.y, Config.RGB_565);
// Create all of the components for the game
renderView = new AndroidFastRenderView(this, frameBuffer);
graphics = new AndroidGraphics(getAssets(), frameBuffer);
fileIO = new AndroidFileIO(this);
audio = new AndroidAudio(this);
input = new AndroidInput(this, renderView, frameBufferScale.x, frameBufferScale.y);
screen = getInitScreen();
setContentView(renderView);
// Keep the screen on while the application has focus
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
}
@Override
public void onResume() {
super.onResume();
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
screen.resume();
renderView.resume();
}
@Override
public void onPause() {
super.onPause();
getWindow().clearFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);
renderView.pause();
screen.pause();
if (isFinishing()) {
screen.dispose();
}
}
@Override
public void onBackPressed() {
getCurrentScreen().backButton();
}
@Override
public Input getInput() {
return input;
}
@Override
public FileIO getFileIO() {
return fileIO;
}
@Override
public Graphics getGraphics() {
return graphics;
}
@Override
public Audio getAudio() {
return audio;
}
@Override
public void setScreen(Screen screen) {
if (screen == null) {
throw new IllegalArgumentException("Screen must not be null");
}
this.screen.pause();
this.screen.dispose();
screen.resume();
screen.update(0);
this.screen = screen;
}
@Override
public Screen getCurrentScreen() {
return screen;
}
@Override
public void exit() {
finish();
}
/* Private Methods */
private Point getFrameBufferDimensions() {
Point frameBufferDimensions = new Point();
boolean isPortrait = getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT;
frameBufferDimensions.x = isPortrait ? 800: 1280;
frameBufferDimensions.y = isPortrait ? 1200: 800;
return frameBufferDimensions;
}
@SuppressLint("NewApi")
@SuppressWarnings("deprecation")
private PointF getFrameBufferScale(Point frameBufferDimensions) {
PointF frameBufferScale = new PointF();
if (VERSION.SDK_INT < 13) {
frameBufferScale.x = (float) frameBufferDimensions.x / getWindowManager().getDefaultDisplay().getWidth();
frameBufferScale.y = (float) frameBufferDimensions.y / getWindowManager().getDefaultDisplay().getHeight();
} else {
Point size = new Point();
getWindowManager().getDefaultDisplay().getSize(size);
frameBufferScale.x = (float) frameBufferDimensions.x / size.x;
frameBufferScale.y = (float) frameBufferDimensions.y / size.y;
}
return frameBufferScale;
}
}