More work on space game and fixed more issues descoverd in the engine:
[alterverse.git] / src / org / alterverse / game / GameEngine.java
blobcf4366c2b0944c9d917f3fee46aa73aa841744a6
1 /***************************************************************************
2 * Copyright (C) 2010 by the Alterverse team *
3 * email: rynkruger@gmail.com *
4 * *
5 * This program is free software; you can redistribute it and/or modify *
6 * it under the terms of the GNU General Public License as published by *
7 * the Free Software Foundation; either version 3 of the License, or *
8 * (at your option) any later version. *
9 * *
10 * This program is distributed in the hope that it will be useful, *
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of *
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
13 * GNU General Public License for more details. *
14 * *
15 * You should have received a copy of the GNU General Public License *
16 * along with this program; if not, see: *
17 * <http://www.gnu.org/licenses/>. *
18 ***************************************************************************/
20 package org.alterverse.game;
22 import org.lwjgl.input.Keyboard;
23 import org.lwjgl.opengl.Display;
24 import org.alterverse.control.*;
25 import org.alterverse.speech.*;
27 public class GameEngine {
28 KeyboardHandler kh;
29 GameContext gc;
30 boolean running;
31 int fps;
32 SpeechEngine tts;
33 int [] defaultKeys = {Keyboard.CHAR_NONE,
34 Keyboard.KEY_0,
35 Keyboard.KEY_1,
36 Keyboard.KEY_2,
37 Keyboard.KEY_3,
38 Keyboard.KEY_4,
39 Keyboard.KEY_5,
40 Keyboard.KEY_6,
41 Keyboard.KEY_7,
42 Keyboard.KEY_8,
43 Keyboard.KEY_9,
44 Keyboard.KEY_A,
45 Keyboard.KEY_ADD,
46 Keyboard.KEY_APOSTROPHE,
47 Keyboard.KEY_APPS,
48 Keyboard.KEY_AT,
49 Keyboard.KEY_AX,
50 Keyboard.KEY_B,
51 Keyboard.KEY_BACK,
52 Keyboard.KEY_BACKSLASH,
53 Keyboard.KEY_C,
54 Keyboard.KEY_CAPITAL,
55 Keyboard.KEY_CIRCUMFLEX,
56 Keyboard.KEY_COLON,
57 Keyboard.KEY_COMMA,
58 Keyboard.KEY_CONVERT,
59 Keyboard.KEY_D,
60 Keyboard.KEY_DECIMAL,
61 Keyboard.KEY_DELETE,
62 Keyboard.KEY_DIVIDE,
63 Keyboard.KEY_DOWN,
64 Keyboard.KEY_E,
65 Keyboard.KEY_END,
66 Keyboard.KEY_EQUALS,
67 Keyboard.KEY_ESCAPE,
68 Keyboard.KEY_F,
69 Keyboard.KEY_F1,
70 Keyboard.KEY_F10,
71 Keyboard.KEY_F11,
72 Keyboard.KEY_F12,
73 Keyboard.KEY_F13,
74 Keyboard.KEY_F14,
75 Keyboard.KEY_F15,
76 Keyboard.KEY_F2,
77 Keyboard.KEY_F3,
78 Keyboard.KEY_F4,
79 Keyboard.KEY_F5,
80 Keyboard.KEY_F6,
81 Keyboard.KEY_F7,
82 Keyboard.KEY_F8,
83 Keyboard.KEY_F9,
84 Keyboard.KEY_G,
85 Keyboard.KEY_GRAVE,
86 Keyboard.KEY_H,
87 Keyboard.KEY_HOME,
88 Keyboard.KEY_I,
89 Keyboard.KEY_INSERT,
90 Keyboard.KEY_J,
91 Keyboard.KEY_K,
92 Keyboard.KEY_KANA,
93 Keyboard.KEY_KANJI,
94 Keyboard.KEY_L,
95 Keyboard.KEY_LBRACKET,
96 Keyboard.KEY_LCONTROL,
97 Keyboard.KEY_LEFT,
98 Keyboard.KEY_LMENU,
99 Keyboard.KEY_LMETA,
100 Keyboard.KEY_LSHIFT,
101 Keyboard.KEY_M,
102 Keyboard.KEY_MINUS,
103 Keyboard.KEY_MULTIPLY,
104 Keyboard.KEY_N,
105 Keyboard.KEY_NEXT,
106 Keyboard.KEY_NOCONVERT,
107 Keyboard.KEY_NONE,
108 Keyboard.KEY_NUMLOCK,
109 Keyboard.KEY_NUMPAD0,
110 Keyboard.KEY_NUMPAD1,
111 Keyboard.KEY_NUMPAD2,
112 Keyboard.KEY_NUMPAD3,
113 Keyboard.KEY_NUMPAD4,
114 Keyboard.KEY_NUMPAD5,
115 Keyboard.KEY_NUMPAD6,
116 Keyboard.KEY_NUMPAD7,
117 Keyboard.KEY_NUMPAD8,
118 Keyboard.KEY_NUMPAD9,
119 Keyboard.KEY_NUMPADCOMMA,
120 Keyboard.KEY_NUMPADENTER,
121 Keyboard.KEY_NUMPADEQUALS,
122 Keyboard.KEY_O,
123 Keyboard.KEY_P,
124 Keyboard.KEY_PAUSE,
125 Keyboard.KEY_PERIOD,
126 Keyboard.KEY_POWER,
127 Keyboard.KEY_PRIOR,
128 Keyboard.KEY_Q,
129 Keyboard.KEY_R,
130 Keyboard.KEY_RBRACKET,
131 Keyboard.KEY_RCONTROL,
132 Keyboard.KEY_RETURN,
133 Keyboard.KEY_RIGHT,
134 Keyboard.KEY_RMENU,
135 Keyboard.KEY_RMETA,
136 Keyboard.KEY_RSHIFT,
137 Keyboard.KEY_S,
138 Keyboard.KEY_SCROLL,
139 Keyboard.KEY_SEMICOLON,
140 Keyboard.KEY_SLASH,
141 Keyboard.KEY_SLEEP,
142 Keyboard.KEY_SPACE,
143 Keyboard.KEY_STOP,
144 Keyboard.KEY_SUBTRACT,
145 Keyboard.KEY_SYSRQ,
146 Keyboard.KEY_T,
147 Keyboard.KEY_TAB,
148 Keyboard.KEY_U,
149 Keyboard.KEY_UNDERLINE,
150 Keyboard.KEY_UNLABELED,
151 Keyboard.KEY_UP,
152 Keyboard.KEY_V,
153 Keyboard.KEY_W,
154 Keyboard.KEY_X,
155 Keyboard.KEY_Y,
156 Keyboard.KEY_YEN,
157 Keyboard.KEY_Z};
159 public GameEngine(GameContext gc) {
160 this(gc,32);
162 public GameEngine(GameContext context, int fps) {
163 gc=context;
164 this.fps=fps;
165 running = false;
166 tts=SpeechSystem.getEngine();
167 gc.tts=tts;
168 try {
169 Display.create();
170 Display.setFullscreen(true);
171 Keyboard.create();
172 } catch(Exception x) {
173 x.printStackTrace();
175 kh = new KeyboardHandler(defaultKeys, gc);
176 // int [] lk = {Keyboard.KEY_LEFT, Keyboard.KEY_RIGHT,Keyboard.KEY_SPACE};
177 // kh=new KeyboardHandler(lk,gc);
178 System.out.println(kh.getKeyboardListener());
181 public void run() {
182 // The main loop is here
183 running = true;
184 while (!Display.isCloseRequested()&&running) {
185 long cycleTime = System.currentTimeMillis();
186 // do our stuff
187 kh.listen();
188 gc.process();
189 // pad our frame
190 int remaning = (int)cycleTime+1000/fps-(int)System.currentTimeMillis();
191 try {
192 Thread.sleep(Math.max(remaning,5));
193 Display.processMessages();
194 } catch(Exception x) {
195 x.printStackTrace();
198 // our work is done
199 gc.destroy();
200 kh.destroy();
201 tts.destroy();
202 try {
203 Display.destroy();
204 } catch(Exception x) {
205 // nothing will really help now
209 public void setContext(GameContext context) {
210 gc=context;
211 gc.tts=tts;
214 public GameContext getContext() {
215 return gc;