Really low graphics patch thanks to Stephan Beyer
[crack-attack.git] / src / MetaState.cxx
blob020bf1433675667300bdd11f416ad25d21e7d0fa
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 #ifndef _WIN32
33 #else
34 # include <glext.h>
35 #endif
37 using namespace std;
39 #include "Game.h"
40 #include "MetaState.h"
41 #include "Displayer.h"
42 #include "CelebrationManager.h"
43 #include "Controller.h"
44 #include "Communicator.h"
45 #include "WinRecord.h"
46 #include "MessageManager.h"
48 int MetaState::state;
49 int MetaState::mode = 0;
50 int MetaState::final_time_step;
51 char MetaState::player_name[GC_PLAYER_NAME_LENGTH];
53 void MetaState::programStart ( int _mode,
54 char _player_name[GC_PLAYER_NAME_LENGTH],
55 int width,
56 int height)
58 state = MS_BOTH_KEY_WAIT;
59 mode |= _mode;
60 strncpy(player_name, _player_name, GC_PLAYER_NAME_LENGTH);
62 Game::initialize();
63 Displayer::initialize(width, height);
65 MessageManager::mode = MM_NORMAL;
66 MessageManager::readyMessage(MS_ANYKEY);
68 glutKeyboardFunc(Controller::keyboardMeta);
69 glutSpecialFunc(Controller::specialMeta);
70 glutKeyboardUpFunc(null);
71 glutSpecialUpFunc(null);
72 glutEntryFunc(Controller::entry);
73 glutDisplayFunc(Displayer::displayMeta);
74 glutReshapeFunc(Displayer::reshape);
75 glutIdleFunc(Game::idleMeta);
77 if (!(mode & CM_SOLO))
78 Communicator::barrier();
80 atexit(programEnd);
82 Game::go();
85 void MetaState::programEnd ( )
87 Game::cleanUp();
88 Displayer::cleanUp();
89 Communicator::cleanUp();
92 void MetaState::gameStart ( )
94 state = MS_GAME_PLAY;
96 MessageManager::freeMessage();
98 if (!(mode & CM_SOLO))
99 Communicator::gameStart();
100 Game::gameStart();
101 Displayer::gameStart();
103 glutKeyboardFunc(Controller::keyboardPlay);
104 glutSpecialFunc(Controller::specialPlay);
105 glutKeyboardUpFunc(Controller::keyboardUpPlay);
106 glutSpecialUpFunc(Controller::specialUpPlay);
107 glutDisplayFunc(Displayer::displayPlay);
108 glutIdleFunc(Game::idlePlay);
110 if (!(mode & CM_SOLO))
111 Communicator::barrier();
113 Game::go();
116 void MetaState::gameWon ( )
118 WinRecord::gameWon();
119 gameFinish();
122 void MetaState::gameLoss ( )
124 WinRecord::gameLoss();
125 gameFinish();
128 void MetaState::gameFinish ( )
130 final_time_step = Game::time_step;
132 if (state & MS_CONCESSION)
133 WinRecord::matchConceded();
135 if (!(mode & CM_SOLO))
136 Communicator::gameFinish();
137 Game::gameFinish();
138 Displayer::gameFinish();
140 if (WinRecord::isMatchFinished()) {
141 if (!(mode & CM_SOLO))
142 Communicator::cleanUp();
143 state = MS_GAME_OVER_KEY_WAIT;
145 } else
146 state = MS_CELEBRATION_WAIT | MS_BOTH_KEY_WAIT;
148 glutKeyboardFunc(Controller::keyboardMeta);
149 glutSpecialFunc(Controller::specialMeta);
150 glutDisplayFunc(Displayer::displayMeta);
151 glutIdleFunc(Game::idleMeta);
153 // GLUT 3.7 is seg faulting if glutKeyboardUpFunc is unset and
154 // glutSpecialUpFunc is set. This is the only use of
155 // Controller::keyboardUpMeta() .
156 // glutKeyboardUpFunc(null); // removed
157 if (mode & CM_SOLO) {
158 glutSpecialUpFunc(Controller::specialUpMeta);
159 glutKeyboardUpFunc(Controller::keyboardUpMeta); // workaround
160 } else {
161 glutSpecialUpFunc(null);
162 glutKeyboardUpFunc(null); // workaround
165 if (!(mode & CM_SOLO) && !WinRecord::isMatchFinished())
166 Communicator::barrier();
168 Game::go();
171 void MetaState::celebrationComplete ( )
173 // allow the player to end the celebration
174 state &= ~MS_CELEBRATION_WAIT;
177 void MetaState::localKeyPressed ( bool esc )
179 if (state & MS_CELEBRATION_WAIT) return;
181 switch (state) {
182 case MS_BOTH_KEY_WAIT:
183 if (WinRecord::current_game == -1)
184 MessageManager::freeMessage();
185 else
186 CelebrationManager::celebrationFinish();
187 MessageManager::readyMessage(MS_WAITING);
188 if (!(mode & CM_SOLO))
189 state = MS_REMOTE_KEY_WAIT;
190 else {
191 state = MS_READY_GAME_START;
192 remoteReady();
194 return;
196 case MS_LOCAL_KEY_WAIT:
197 if (WinRecord::current_game == -1)
198 MessageManager::freeMessage();
199 else
200 CelebrationManager::celebrationFinish();
201 MessageManager::readyMessage(MS_WAITING);
202 state = MS_READY_GAME_START;
203 return;
205 case MS_GAME_OVER_KEY_WAIT:
206 if (!esc) return;
207 exit(0);
208 return;
212 void MetaState::remoteKeyPressed ( )
214 switch (state) {
215 case MS_BOTH_KEY_WAIT:
216 state = MS_LOCAL_KEY_WAIT;
217 return;
219 case MS_REMOTE_KEY_WAIT:
220 state = MS_READY_GAME_START;
221 return;
225 void MetaState::remoteReady ( )
227 switch (state) {
228 case MS_READY_GAME_START:
229 gameStart();
230 return;
232 case MS_REMOTE_KEY_WAIT:
233 state = MS_READY_GAME_START;
234 return;
236 case MS_BOTH_KEY_WAIT:
237 state = MS_LOCAL_KEY_WAIT;
238 return;