aboutsummaryrefslogtreecommitdiffstats
path: root/src/ru/olamedia/game/GameFrame.java
blob: 6227a155de4c664393a2766a141796d5ddbe20d6 (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
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
package ru.olamedia.game;

import java.awt.Dimension;
import java.awt.Frame;
import java.awt.Image;
import java.awt.Toolkit;
import java.awt.event.KeyListener;
import java.awt.im.InputContext;
import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

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 ru.olamedia.olacraft.game.Game;

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;

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); // hardware swap
			caps.setBackgroundOpaque(true);
			caps.setSampleBuffers(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.setAutoSwapBufferMode(false);
			glWindow.setPointerVisible(true);
			glWindow.confinePointer(false);
			glWindow.addWindowListener(new QuitAdapter());
			animator = new Animator(glWindow);
			// animator.setUpdateFPSFrames(200, System.out);
			// 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);
					}
				}

				@Override
				public void keyPressed(KeyEvent e) {
					super.keyPressed(e);
					System.out.println(e.toString());
				}
			});
			//animator.setUpdateFPSFrames(1000, 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 getGLWidth() {
		return Game.Display.getWidth();
	}

	public static int getGLHeight() {
		return Game.Display.getHeight();
	}

	public static int getHeight() {
		if (null == glWindow) {
			return jFrame.getHeight();
		}
		return glWindow.getHeight();
	}

	public GameFrame() {
		instance = this;
		// en.selectInputMethod(Locale.ENGLISH);
		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();
	}
}