1 /*
2  * Controller.cxx
3  * Daniel Nelson - 8/25/0
4  *
5  * Copyright (C) 2000  Daniel Nelson
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
20  *
21  * Daniel Nelson - aluminumangel.org
22  * 174 W. 18th Ave.
23  * Columbus, OH  43210
24  *
25  * Keeps track of the button pressing.
26  */
27 
28 #include <GL/glut.h>
29 #include <cassert>
30 
31 #include "glext.h"
32 
33 #include "Game.h"
34 #include "Controller.h"
35 #include "ActionRecorder.h"
36 #include "MetaState.h"
37 #include "Grid.h"
38 
39 #ifdef DEVELOPMENT
40 #include "Displayer.h"
41 #endif
42 
43 using namespace std;
44 
45 int Controller::state;
46 
gameStart()47 void Controller::gameStart (   )
48 {
49   state = 0;
50 }
51 
keyboardPlay(unsigned char key,int x,int y)52 void Controller::keyboardPlay ( unsigned char key, int x, int y )
53 {
54   bool keypressed = true;
55   switch (key) {
56   case GC_LEFT_KEY: case (GC_LEFT_KEY - 32):
57     state |= CC_LEFT;
58     break;
59   case GC_RIGHT_KEY: case (GC_RIGHT_KEY - 32):
60     state |= CC_RIGHT;
61     break;
62   case GC_UP_KEY: case (GC_UP_KEY - 32):
63     state |= CC_UP;
64     break;
65   case GC_DOWN_KEY: case (GC_DOWN_KEY - 32):
66     state |= CC_DOWN;
67     break;
68   case GC_SWAP_KEY: case (GC_SWAP_KEY - 32): case ' ':
69     state |= CC_SWAP;
70     break;
71   case GC_ADVANCE_KEY: case (GC_ADVANCE_KEY - 32): case '\r':
72     state |= CC_ADVANCE;
73     break;
74   case GC_PAUSE_KEY: case (GC_PAUSE_KEY - 32):
75     state |= CC_PAUSE;
76     break;
77 #ifndef NDEBUG
78   case 'z':
79     Grid::dump();
80     break;
81 #endif
82 #ifdef DEVELOPMENT
83   case '.':
84     Displayer::screenShot();
85     break;
86 #endif
87   case 27:
88     Game::concession();
89     break;
90   default:
91     keypressed = false;
92   }
93   if(keypressed)
94     ActionRecorder::addAction(state);
95 }
96 
keyboardUpPlay(unsigned char key,int x,int y)97 void Controller::keyboardUpPlay ( unsigned char key, int x, int y )
98 {
99   bool keypressed = true;
100   switch (key) {
101   case GC_LEFT_KEY: case (GC_LEFT_KEY - 32):
102     state &= ~CC_LEFT;
103     break;
104   case GC_RIGHT_KEY: case (GC_RIGHT_KEY - 32):
105     state &= ~CC_RIGHT;
106     break;
107   case GC_UP_KEY: case (GC_UP_KEY - 32):
108     state &= ~CC_UP;
109     break;
110   case GC_DOWN_KEY: case (GC_DOWN_KEY - 32):
111     state &= ~CC_DOWN;
112     break;
113   case GC_SWAP_KEY: case (GC_SWAP_KEY - 32): case ' ':
114     state &= ~CC_SWAP;
115     break;
116   case GC_ADVANCE_KEY: case (GC_ADVANCE_KEY - 32): case '\r':
117     state &= ~CC_ADVANCE;
118     break;
119   case GC_PAUSE_KEY: case (GC_PAUSE_KEY - 32):
120     state &= ~CC_PAUSE;
121     break;
122   default:
123     keypressed = false;
124   }
125   if(keypressed)
126     ActionRecorder::addAction(state);
127 }
128 
specialPlay(int key,int x,int y)129 void Controller::specialPlay ( int key, int x, int y )
130 {
131   switch (key) {
132   case GLUT_KEY_LEFT:
133     state |= CC_LEFT;
134     break;
135   case GLUT_KEY_RIGHT:
136     state |= CC_RIGHT;
137     break;
138   case GLUT_KEY_UP:
139     state |= CC_UP;
140     break;
141   case GLUT_KEY_DOWN:
142     state |= CC_DOWN;
143     break;
144   }
145 }
146 
specialUpPlay(int key,int x,int y)147 void Controller::specialUpPlay ( int key, int x, int y )
148 {
149   switch (key) {
150   case GLUT_KEY_LEFT:
151     state &= ~CC_LEFT;
152     break;
153   case GLUT_KEY_RIGHT:
154     state &= ~CC_RIGHT;
155     break;
156   case GLUT_KEY_UP:
157     state &= ~CC_UP;
158     break;
159   case GLUT_KEY_DOWN:
160     state &= ~CC_DOWN;
161     break;
162   }
163 }
164 
keyboardMeta(unsigned char key,int x,int y)165 void Controller::keyboardMeta ( unsigned char key, int x, int y )
166 {
167 	switch (key) {
168 #ifdef DEVELOPMENT
169   case '.':
170     Displayer::screenShot(); break;
171 #endif
172   case GC_LEFT_KEY: case (GC_LEFT_KEY - 32):
173   case GC_RIGHT_KEY: case (GC_RIGHT_KEY - 32):
174   case GC_UP_KEY: case (GC_UP_KEY - 32):
175   case GC_DOWN_KEY: case (GC_DOWN_KEY - 32):
176   case GC_SWAP_KEY: case (GC_SWAP_KEY - 32): case ' ':
177   case GC_ADVANCE_KEY: case (GC_ADVANCE_KEY - 32): case '\r':
178   case GC_PAUSE_KEY: case (GC_PAUSE_KEY - 32):
179     MESSAGE("game key pressed");
180 		MetaState::localKeyPressed(false);
181     break;
182 	default:
183     MESSAGE("game key not-pressed");
184 		MetaState::localKeyPressed(true);
185 	}
186 }
187 
keyboardUpMeta(unsigned char key,int x,int y)188 void Controller::keyboardUpMeta ( unsigned char key, int x, int y )
189 {
190 }
191 
specialMeta(int key,int x,int y)192 void Controller::specialMeta ( int key, int x, int y )
193 {
194   if (MetaState::mode * CM_SOLO)
195     switch (key) {
196     case GLUT_KEY_UP:
197       state |= CC_UP;
198       break;
199     case GLUT_KEY_DOWN:
200       state |= CC_DOWN;
201       break;
202     default:
203       MetaState::localKeyPressed(false);
204       break;
205     }
206   else
207     MetaState::localKeyPressed(false);
208 }
209 
specialUpMeta(int key,int x,int y)210 void Controller::specialUpMeta ( int key, int x, int y )
211 {
212   assert(MetaState::mode & CM_SOLO);
213 
214   switch (key) {
215   case GLUT_KEY_UP:
216     state &= ~CC_UP;
217     break;
218   case GLUT_KEY_DOWN:
219     state &= ~CC_DOWN;
220     break;
221   }
222 }
223 
entry(int mouse_state)224 void Controller::entry ( int mouse_state )
225 {
226   if (mouse_state == GLUT_LEFT)
227     state = 0;
228 }
229