blob: 973fcb81d1866eae2a4cc74f9a1cf79b1da769b0 (
plain)
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
|
package ru.olamedia.game;
import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.util.ArrayList;
import java.util.List;
import javax.media.opengl.GLCapabilities;
import javax.media.opengl.GLProfile;
import javax.swing.JFrame;
import ru.olamedia.asset.AssetManager;
import ru.olamedia.asset.AssetNotFoundException;
import ru.olamedia.input.Keyboard;
import ru.olamedia.input.MouseJail;
import ru.olamedia.olacraft.OlaCraft;
import jogamp.newt.awt.NewtFactoryAWT;
import com.jogamp.newt.Display;
import com.jogamp.newt.Screen;
import com.jogamp.newt.awt.NewtCanvasAWT;
import com.jogamp.newt.event.KeyAdapter;
import com.jogamp.newt.event.KeyEvent;
import com.jogamp.newt.opengl.GLWindow;
import com.jogamp.opengl.util.Animator;
import com.jogamp.opengl.util.FPSAnimator;
public class GameFrame {
// java.awt.SystemTray
// http://www.oracle.com/technetwork/articles/javase/systemtray-139788.html
public static GameFrame instance;
Display display;
Screen screen;
int screenId;
GLProfile glProfile;
GLCapabilities caps;
protected Frame awtFrame;
protected static GLWindow glWindow;
protected static JFrame jFrame;
int width = 854;
int height = 480;
public static Animator animator;
NewtCanvasAWT newtCanvasAWT;
private boolean glMode = false;
public void initGL() {
if (null == newtCanvasAWT) {
glProfile = GLProfile.get(GLProfile.GL2);// Default();
// ES2
caps = new GLCapabilities(glProfile);
caps.setHardwareAccelerated(true);
caps.setDoubleBuffered(true);
caps.setBackgroundOpaque(false);
display = NewtFactoryAWT.createDisplay(null);
screen = NewtFactoryAWT.createScreen(display, screenId);
glWindow = GLWindow.create(screen, caps);// GLWindow.create(screen,
// caps);
newtCanvasAWT = new NewtCanvasAWT(glWindow);
glWindow.setUndecorated(false);
glWindow.setPointerVisible(true);
glWindow.confinePointer(false);
glWindow.addWindowListener(new QuitAdapter());
animator = new Animator(glWindow);
//animator = new FPSAnimator(glWindow, 60);
//animator.setRunAsFastAsPossible(true); // By default there is a
// brief
// pause in the animation
// loop
animator.start();
glWindow.addMouseListener(MouseJail.instance);
glWindow.addKeyListener(Keyboard.instance);
glWindow.addKeyListener(new KeyAdapter() {
@Override
public void keyReleased(KeyEvent e) {
super.keyReleased(e);
if (e.getKeyCode() == KeyEvent.VK_ESCAPE) {
glWindow.confinePointer(false);
glWindow.setPointerVisible(true);
}
}
});
// animator.setUpdateFPSFrames(100, System.err);
jFrame.add(newtCanvasAWT);
glWindow.addGLEventListener(GameManager.instance);
}
}
public void setGLMode() {
if (!glMode) {
initGL();
glMode = true;
newtCanvasAWT.setVisible(true);
}
}
public void setUIMode() {
if (glMode) {
glMode = false;
newtCanvasAWT.setVisible(false);
}
}
public static int getX() {
return jFrame.getX();
}
public static int getY() {
return jFrame.getY();
}
public static int getWidth() {
if (null == glWindow) {
return jFrame.getWidth();
}
return glWindow.getWidth();
}
public static int getHeight() {
if (null == glWindow) {
return jFrame.getHeight();
}
return glWindow.getHeight();
}
public GameFrame() {
instance = this;
jFrame = new JFrame();
jFrame.setMinimumSize(new Dimension(200, 200));
jFrame.setSize(width, height);
jFrame.setDefaultCloseOperation(JFrame.DO_NOTHING_ON_CLOSE);
jFrame.setTitle("OlaCraft " + OlaCraft.version);
setIcons();
// glWindow.setLocation(100, 100);
jFrame.addWindowListener(new QuitAdapter());
jFrame.setVisible(true);
}
private void setIcons() {
List<Image> icons = new ArrayList<Image>();
try {
icons.add(getImage("icon16x16.png"));
icons.add(getImage("icon32x32.png"));
icons.add(getImage("icon64x64.png"));
icons.add(getImage("icon128x128.png"));
icons.add(getImage("icon256x256.png"));
} catch (AssetNotFoundException e1) {
e1.printStackTrace();
}
// if (!icons.isEmpty()) {
// awtFrame.setIconImage(getImage("icon32x32.png"));
jFrame.setIconImages(icons);
// }
}
private Image getImage(String filename) throws AssetNotFoundException {
String iconFile = AssetManager.getAsset("ru/olamedia/game/" + filename).getFile();
return Toolkit.getDefaultToolkit().createImage(iconFile);
}
public Animator getAnimator() {
return animator;
}
public static void confinePointer(boolean confine) {
if (glWindow != null) {
glWindow.confinePointer(confine);
}
}
public static void setPointerVisible(boolean visible) {
if (glWindow != null) {
glWindow.setPointerVisible(visible);
}
}
public static GLWindow getWindow() {
return glWindow;
}
public static JFrame getFrame() {
return jFrame;
}
public void dispose() {
// glWindow.destroy();
// newtCanvasAWT.destroy();
jFrame.dispose();
// System.err.close();
}
}
|