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
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
|
package ru.olamedia.game;
import java.awt.event.KeyListener;
import java.util.Set;
import javax.media.opengl.GL;
import javax.media.opengl.GL2ES2;
import javax.media.opengl.GLAutoDrawable;
import javax.media.opengl.GLEventListener;
import ru.olamedia.olacraft.game.Game;
import ru.olamedia.olacraft.network.discovery.DiscoveryThread;
import ru.olamedia.olacraft.render.jogl.DefaultRenderer;
import ru.olamedia.olacraft.render.jogl.IRenderer;
import ru.olamedia.olacraft.world.blockRenderer.ChunkMeshGarbageCollector;
import ru.olamedia.olacraft.world.chunk.Chunk;
import ru.olamedia.olacraft.world.chunk.ChunkMeshBulder;
import ru.olamedia.olacraft.world.location.BlockLocation;
import ru.olamedia.tasks.TaskManager;
import com.jogamp.opengl.JoglVersion;
public class GameManager implements GLEventListener {
public static GameManager instance;
private GameFrame frame;
private ClientGame clientGame;
private ServerGame serverGame;
private IRenderer renderer;
private MainMenu menu;
public GameManager() {
instance = this;
tests();
}
private void assertBoolean(boolean val) {
if (!val) {
throw new RuntimeException("Assert failed");
}
}
private void tests() {
System.out.println("====TESTS====");
// Game.client.getWorldProvider().loadChunk(new ChunkLocation());
// Game.client.getWorldProvider().loadChunk(new ChunkLocation(-1,0,0));
// Game.client.getWorldProvider().loadChunk(new ChunkLocation(0,0,-1));
// Game.client.getWorldProvider().loadChunk(new ChunkLocation(-1,0,-1));
BlockLocation b;
b = new BlockLocation(0, 0, 0);
assertBoolean(b.getChunkLocation().x == 0);
assertBoolean(b.getChunkLocation().getBlockLocation().x == 0);
b = new BlockLocation(14, 0, 0);
assertBoolean(b.getChunkLocation().x == 0);
assertBoolean(b.getChunkLocation().getBlockLocation().x == 0);
b = new BlockLocation(15, 0, 0);
assertBoolean(b.getChunkLocation().x == 0);
assertBoolean(b.getChunkLocation().getBlockLocation().x == 0);
b = new BlockLocation(16, 0, 0);
assertBoolean(b.getChunkLocation().x == 1);
assertBoolean(b.getChunkX() == Chunk.v(b.x));
assertBoolean(b.getChunkY() == Chunk.v(b.y + 128));
assertBoolean(b.getChunkZ() == Chunk.v(b.z));
// failed assertBoolean(b.getChunkLocation().getBlockLocation().x == 0);
b = new BlockLocation(-1, 0, 0);
assertBoolean(b.getChunkLocation().x == -1);
assertBoolean(b.getChunkX() == Chunk.v(b.x));
assertBoolean(b.getChunkY() == Chunk.v(b.y + 128));
assertBoolean(b.getChunkZ() == Chunk.v(b.z));
// failed assertBoolean(b.getChunkLocation().getBlockLocation().x ==
// -1);
b = new BlockLocation(-14, 0, 0);
assertBoolean(b.getChunkLocation().x == -1);
assertBoolean(b.getChunkX() == Chunk.v(b.x));
assertBoolean(b.getChunkY() == Chunk.v(b.y + 128));
assertBoolean(b.getChunkZ() == Chunk.v(b.z));
b = new BlockLocation(-15, 0, 0);
assertBoolean(b.getChunkLocation().x == -1);
assertBoolean(b.getChunkX() == Chunk.v(b.x));
assertBoolean(b.getChunkY() == Chunk.v(b.y + 128));
assertBoolean(b.getChunkZ() == Chunk.v(b.z));
// failed assertBoolean(b.getChunkLocation().getBlockLocation().x ==
// -1);
b = new BlockLocation(-16, 0, 0);
assertBoolean(b.getChunkLocation().x == -1);
// failed assertBoolean(b.getChunkLocation().getBlockLocation().x ==
// -1);
assertBoolean(b.getByteX() == 0);
assertBoolean(b.getByteY() == 0);
assertBoolean(b.getByteZ() == 0);
assertBoolean(b.getChunkX() == Chunk.v(b.x));
assertBoolean(b.getChunkY() == Chunk.v(b.y + 128));
assertBoolean(b.getChunkZ() == Chunk.v(b.z));
b = new BlockLocation(-17, 0, 0);
assertBoolean(b.getChunkLocation().x == -2);
// assertBoolean(b.getChunkLocation().getBlockLocation().x == -17);
assertBoolean(b.getByteX() == 15);
assertBoolean(b.getByteY() == 0);
assertBoolean(b.getByteZ() == 0);
int id = Chunk.in(b.x) * 16 * 16 + Chunk.in(b.y) * 16 + Chunk.in(b.z);
System.out.print(b.getChunkX() + "/" + Chunk.v(b.x));
assertBoolean(b.getId() == id);
assertBoolean(b.getChunkX() == Chunk.v(b.x));
assertBoolean(b.getChunkY() == Chunk.v(b.y + 128));
assertBoolean(b.getChunkZ() == Chunk.v(b.z));
// ChunkData data = new ChunkData();
// data.setVoidLight(18, (byte) 6);
// assert data.getVoidLight(18) == 6;
}
private void createServerGame() {
if (null == serverGame) {
serverGame = new ServerGame(this);
}
}
private void createClientGame() {
if (null == clientGame) {
clientGame = new ClientGame(this);
}
}
public void startServerGame() {
createServerGame();
serverGame.start();
}
public void startClientGame() {
createClientGame();
clientGame.start();
}
public void resumeClientGame() {
createClientGame();
clientGame.resume();
}
public void finishClientGame() {
createClientGame();
clientGame.finish();
}
public void resumeServerGame() {
createServerGame();
serverGame.resume();
}
public void finishServerGame() {
createServerGame();
clientGame.finish();
serverGame.dispose();
}
private void init() {
this.frame = new GameFrame();
menu = new MainMenu();
this.renderer = new DefaultRenderer();
GameFrame.getFrame().getContentPane().add(menu);
GameFrame.getFrame().getContentPane().repaint();
GameFrame.getFrame().addKeyListener(new KeyListener() {
@Override
public void keyTyped(java.awt.event.KeyEvent e) {
System.out.println(e.toString());
}
@Override
public void keyReleased(java.awt.event.KeyEvent e) {
System.out.println(e.toString());
}
@Override
public void keyPressed(java.awt.event.KeyEvent e) {
System.out.println(e.toString());
}
});
// GameFrame.getFrame().getContentPane().validate();
// GameFrame.getFrame().validate();
// GameFrame.getFrame().getContentPane().paintComponents(GameFrame.getFrame().getContentPane().getGraphics());
// GameFrame.getFrame().validate();
// menu.setVisible(true);
// GameFrame.getFrame().setVisible(true);
}
private void glTest(GLAutoDrawable drawable) {
// m = new PMVMatrix(true);
// m.glGetMviMatrixf();
// m.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
// m.glLoadIdentity();
// m.gluPerspective(30, 1, 1, 100);
// m.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
// m.glLoadIdentity();
// m.glTranslatef(5, 6, 7);
// m.glRotatef(50, 0, 1, 0);
// m.glRotatef(30, 1, 0, 0);
// m.setDirty();
// m.update();
//
// GLUniformData pmvMatrixUniform = new GLUniformData("mgl_PMVMatrix",
// 4, 4, m.glGetPMvMatrixf());
//
// FloatBuffer pmv = m.glGetMviMatrixf();
// pmv = m.glGetPMatrixf();
// for (int i = 0; i < 16; i++) {
// System.out.println(pmv.get(i) + " == ");
// }
// GL2 gl = drawable.getGL().getGL2();
// gl.glMatrixMode(GLMatrixFunc.GL_PROJECTION);
// gl.glLoadIdentity();
// GLU glu = new GLU();
// glu.gluPerspective(30, 1, 1, 100);
// gl.glMatrixMode(GLMatrixFunc.GL_MODELVIEW);
// gl.glLoadIdentity();
// gl.glTranslatef(5, 6, 7);
// gl.glRotatef(50, 0, 1, 0);
// gl.glRotatef(30, 1, 0, 0);
//
// FloatBuffer glmv = FloatBuffer.allocate(16);
// gl.glGetFloatv(GLMatrixFunc.GL_MODELVIEW_MATRIX, glmv);
// for (int i = 0; i < 16; i++) {
// System.out.println(pmv.get(i) + " == " + glmv.get(i));
// }
// System.exit(0);
}
public void start() {
init();
while (!QuitAdapter.shouldQuit) {
try {
Thread.sleep(100);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
public void dispose() {
TaskManager.stopAll();
if (null != Game.server) {
Game.server.dispose();
}
if (null != Game.client) {
Game.client.dispose();
}
if (null != GameFrame.animator) {
if (GameFrame.animator.isStarted()) {
GameFrame.animator.stop();
}
}
frame.dispose();
if (ChunkMeshBulder.instance.isAlive()) {
ChunkMeshBulder.instance.interrupt();
}
if (GameLogicThread.instance.isAlive()) {
GameLogicThread.instance.interrupt();
}
if (ChunkMeshGarbageCollector.instance.isAlive()) {
ChunkMeshGarbageCollector.instance.interrupt();
}
// Get all threads
Set<Thread> threadSet = Thread.getAllStackTraces().keySet();
for (Thread t : threadSet) {
if (t instanceof DiscoveryThread) {
t.interrupt();
}
if (t instanceof ChunkMeshBulder) {
t.interrupt();
}
}
}
@Override
public void init(GLAutoDrawable drawable) {
// GLContext.getContext().getGL()
GL2ES2 gl = drawable.getGL().getGL2ES2();
drawable.setAutoSwapBufferMode(false);
gl.setSwapInterval(0);
// drawable.setGL(new DebugGL2ES2(gl));
System.err.println(JoglVersion.getGLInfo(drawable.getGL(), null));
System.err.println(Thread.currentThread() + " Chosen GLCapabilities: " + drawable.getChosenGLCapabilities());
System.err.println(Thread.currentThread() + " INIT GL IS: " + gl.getClass().getName());
System.err.println(Thread.currentThread() + " GL_VENDOR: " + gl.glGetString(GL.GL_VENDOR));
System.err.println(Thread.currentThread() + " GL_RENDERER: " + gl.glGetString(GL.GL_RENDERER));
System.err.println(Thread.currentThread() + " GL_VERSION: " + gl.glGetString(GL.GL_VERSION));
System.err.println(Thread.currentThread() + " GL Profile: " + gl.getGLProfile());
System.err.println(Thread.currentThread() + " GL:" + gl);
System.err.println(Thread.currentThread() + " GL_VERSION=" + gl.glGetString(GL.GL_VERSION));
renderer.init(drawable);
glTest(drawable);
}
@Override
public void dispose(GLAutoDrawable drawable) {
// TODO Auto-generated method stub
}
@Override
public void display(GLAutoDrawable drawable) {
// final GL gl = drawable.getGL();
// gl.glClear(GL.GL_COLOR_BUFFER_BIT | GL.GL_DEPTH_BUFFER_BIT);
// gl.glClearColor(0.2f, 0.2f, 0.2f, 1);
renderer.render(drawable);
// Different GL implementations buffer commands in several different
// locations, including network buffers and the graphics accelerator
// itself. glFlush empties all of these buffers, causing all issued
// commands to be executed as quickly as they are accepted by the actual
// rendering engine. Though this execution may not be completed in any
// particular time period, it does complete in finite time.
//gl.glF
//gl.glFlush();
//gl.glFinish();
//gl.glFlush();
drawable.swapBuffers();
}
@Override
public void reshape(GLAutoDrawable drawable, int x, int y, int width, int height) {
final GL gl = drawable.getGL();
gl.glViewport(0, 0, width, height);
renderer.reshape(drawable);
}
public void showMainMenu() {
menu.setVisible(true);
}
public void hideMainMenu() {
menu.setVisible(false);
}
}
|