g_spawn_command_line_sync used for spawning the game now.
[crack-attack.git] / src / MetaState.cxx
blob9899395f5759cec9abd76d10a586d0c1307bb46d
1 /*
2 * MetaState.cxx
3 * Daniel Nelson - 10/22/0
5 * Copyright (C) 2000 Daniel Nelson
6 * Copyright (C) 2004 Andrew Sayman
7 *
8 * This program is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU General Public License
10 * as published by the Free Software Foundation; either version 2
11 * of the License, or (at your option) any later version.
13 * This program is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License
19 * along with this program; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
22 * Daniel Nelson - aluminumangel.org
23 * 174 W. 18th Ave.
24 * Columbus, OH 43210
26 * Handles the program's state and transfers between them.
29 #include <GL/glut.h>
30 #include <cstring>
32 #include "glext.h"
34 using namespace std;
36 #include "Game.h"
37 #include "MetaState.h"
38 #include "Displayer.h"
39 #include "CelebrationManager.h"
40 #include "Controller.h"
41 #include "Communicator.h"
42 #include "WinRecord.h"
43 #include "MessageManager.h"
45 int MetaState::state;
46 int MetaState::mode = 0;
47 int MetaState::final_time_step;
48 char MetaState::player_name[GC_PLAYER_NAME_LENGTH];
50 void MetaState::programStart ( int _mode,
51 char _player_name[GC_PLAYER_NAME_LENGTH],
52 int width,
53 int height)
55 state = MS_BOTH_KEY_WAIT;
56 mode |= _mode;
57 strncpy(player_name, _player_name, GC_PLAYER_NAME_LENGTH);
59 Game::initialize();
60 Displayer::initialize(width, height);
62 MessageManager::mode = MM_NORMAL;
63 MessageManager::readyMessage(MS_ANYKEY);
65 glutKeyboardFunc(Controller::keyboardMeta);
66 glutSpecialFunc(Controller::specialMeta);
67 glutKeyboardUpFunc(null);
68 glutSpecialUpFunc(null);
69 glutEntryFunc(Controller::entry);
70 glutDisplayFunc(Displayer::displayMeta);
71 glutReshapeFunc(Displayer::reshape);
72 glutIdleFunc(Game::idleMeta);
74 if (!(mode & CM_SOLO))
75 Communicator::barrier();
77 atexit(programEnd);
79 Game::go();
82 void MetaState::programEnd ( )
84 Game::cleanUp();
85 Displayer::cleanUp();
86 Communicator::cleanUp();
89 void MetaState::gameStart ( )
91 state = MS_GAME_PLAY;
93 MessageManager::freeMessage();
95 if (!(mode & CM_SOLO))
96 Communicator::gameStart();
97 Game::gameStart();
98 Displayer::gameStart();
100 glutKeyboardFunc(Controller::keyboardPlay);
101 glutSpecialFunc(Controller::specialPlay);
102 glutKeyboardUpFunc(Controller::keyboardUpPlay);
103 glutSpecialUpFunc(Controller::specialUpPlay);
104 glutDisplayFunc(Displayer::displayPlay);
105 glutIdleFunc(Game::idlePlay);
107 if (!(mode & CM_SOLO))
108 Communicator::barrier();
110 Game::go();
113 void MetaState::gameWon ( )
115 DOT(3);
116 WinRecord::gameWon();
117 DOT(3);
118 gameFinish();
121 void MetaState::gameLoss ( )
123 WinRecord::gameLoss();
124 gameFinish();
127 void MetaState::gameFinish ( )
129 final_time_step = Game::time_step;
131 DOT(3);
132 if (state & MS_CONCESSION)
133 WinRecord::matchConceded();
135 DOT(3);
136 if (!(mode & CM_SOLO))
137 Communicator::gameFinish();
138 Game::gameFinish();
139 Displayer::gameFinish();
141 if (WinRecord::isMatchFinished()) {
142 if (!(mode & CM_SOLO))
143 Communicator::cleanUp();
144 state = MS_GAME_OVER_KEY_WAIT;
146 } else
147 state = MS_CELEBRATION_WAIT | MS_BOTH_KEY_WAIT;
149 glutKeyboardFunc(Controller::keyboardMeta);
150 glutSpecialFunc(Controller::specialMeta);
151 glutDisplayFunc(Displayer::displayMeta);
152 glutIdleFunc(Game::idleMeta);
154 // GLUT 3.7 is seg faulting if glutKeyboardUpFunc is unset and
155 // glutSpecialUpFunc is set. This is the only use of
156 // Controller::keyboardUpMeta() .
157 // glutKeyboardUpFunc(null); // removed
158 if (mode & CM_SOLO || mode & CM_AI) {
159 glutSpecialUpFunc(Controller::specialUpMeta);
160 glutKeyboardUpFunc(Controller::keyboardUpMeta); // workaround
161 } else {
162 glutSpecialUpFunc(null);
163 glutKeyboardUpFunc(null); // workaround
166 if (!(mode & CM_SOLO) && !WinRecord::isMatchFinished())
167 Communicator::barrier();
169 Game::go();
172 void MetaState::celebrationComplete ( )
174 // allow the player to end the celebration
175 state &= ~MS_CELEBRATION_WAIT;
178 void MetaState::localKeyPressed ( bool esc )
180 if (state & MS_CELEBRATION_WAIT) return;
182 switch (state) {
183 case MS_BOTH_KEY_WAIT:
184 if (WinRecord::current_game == -1)
185 MessageManager::freeMessage();
186 else
187 CelebrationManager::celebrationFinish();
188 MessageManager::readyMessage(MS_WAITING);
189 if (!(mode & CM_SOLO))
190 state = MS_REMOTE_KEY_WAIT;
191 else {
192 state = MS_READY_GAME_START;
193 remoteReady();
195 return;
197 case MS_LOCAL_KEY_WAIT:
198 if (WinRecord::current_game == -1)
199 MessageManager::freeMessage();
200 else
201 CelebrationManager::celebrationFinish();
202 MessageManager::readyMessage(MS_WAITING);
203 state = MS_READY_GAME_START;
204 return;
206 case MS_GAME_OVER_KEY_WAIT:
207 if (!esc) return;
208 exit(0);
209 return;
213 void MetaState::remoteKeyPressed ( )
215 switch (state) {
216 case MS_BOTH_KEY_WAIT:
217 state = MS_LOCAL_KEY_WAIT;
218 return;
220 case MS_REMOTE_KEY_WAIT:
221 state = MS_READY_GAME_START;
222 return;
226 void MetaState::remoteReady ( )
228 switch (state) {
229 case MS_READY_GAME_START:
230 gameStart();
231 return;
233 case MS_REMOTE_KEY_WAIT:
234 state = MS_READY_GAME_START;
235 return;
237 case MS_BOTH_KEY_WAIT:
238 state = MS_LOCAL_KEY_WAIT;
239 return;