[7f9531]: keyb.c  Maximize  Restore  History

Download this file

251 lines (234 with data), 9.2 kB

  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
/* Asteroids3D - a first person game of blowing up asteroids
* Copyright (C) 2000 Stuart Mark Pomerantz <smp [at] psc edu>
*
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*
* Stuart Pomerantz
* 3935 Stonecliffe drive
* Monroeville, PA 15146
*/
#include "asteroids3D.h"
// Maximum thrust that can be input
#define MAX_TINPUT 0.5
static inline void toggle_int_flag(int *);
// Define the keyboard map
KeyboardMap keymap = {
.asteroid_texture_toggle = 'm',
.auto_target_asteroid = 'b', // was '\t'
.axes_toggle = '$',
.crosshair_toggle = '+',
.draw_dust_toggle = '(',
.draw_shield_toggle = ')',
.draw_torp_lensflare_toggle = '*',
.draw_tracer_toggle = '_',
.end_game = 27 /* == '\e' */,
.fire_torp = ' ',
.headlight_toggle = '[',
.nebula_texture_toggle = 'k',
.pause = 'p',
.play_sound_toggle = '#',
.reverse_mouse_x_toggle = '!',
.reverse_mouse_y_toggle = '@',
.rot_aboutx_ccw = '2',
.rot_aboutx_cw = '8',
.rot_abouty_ccw = '4',
.rot_abouty_cw = '6',
.rot_aboutz_ccw = 'e',
.rot_aboutz_cw = 'q',
.slide_backward = 'w', // was 'v'
.slide_down = 'y', // was 't'
.slide_forward = 's', // was 'r'
.slide_left = 'a', // was 'd'
.slide_right = 'd', // was 'g'
.slide_up = 'c',
.stop = 'f',
.target_asteroid = '\t', // was 'b'
.torp_texture_toggle = 'o',
};
KeyboardMap keymap_old = {
.asteroid_texture_toggle = '&',
.auto_target_asteroid = '\t',
.axes_toggle = '$',
.crosshair_toggle = '+',
.draw_dust_toggle = '(',
.draw_shield_toggle = ')',
.draw_torp_lensflare_toggle = '*',
.draw_tracer_toggle = '_',
.end_game = 27 /* == '\e' */,
.fire_torp = ' ',
.headlight_toggle = '[',
.nebula_texture_toggle = '^',
.pause = 'p',
.play_sound_toggle = '#',
.reverse_mouse_x_toggle = '!',
.reverse_mouse_y_toggle = '@',
.rot_aboutx_ccw = '2',
.rot_aboutx_cw = '8',
.rot_abouty_ccw = '4',
.rot_abouty_cw = '6',
.rot_aboutz_ccw = '9',
.rot_aboutz_cw = '7',
.slide_backward = 'v',
.slide_down = 't',
.slide_forward = 'r',
.slide_left = 'd',
.slide_right = 'g',
.slide_up = 'c',
.stop = 'f',
.target_asteroid = 'b',
.torp_texture_toggle = '%',
};
// These are used for scaling keyboard rotation input
int keyb_rotx_scalar = 0, keyb_roty_scalar = 0, keyb_rotz_scalar = 0;
//-----------------------------------------------------------------------------
void keyboard_handler(unsigned char key, int x, int y) {
if(key == keymap.end_game)
end_game();
else if(key == keymap.pause)
start_pause();
else if(key == keymap.slide_forward)
slide_alongz(MAX_TINPUT);
else if(key == keymap.slide_backward)
slide_alongz(-MAX_TINPUT);
else if(key == keymap.slide_left)
slide_alongx(MAX_TINPUT);
else if(key == keymap.slide_right)
slide_alongx(-MAX_TINPUT);
else if(key == keymap.slide_up)
slide_alongy(MAX_TINPUT);
else if(key == keymap.slide_down)
slide_alongy(-MAX_TINPUT);
else if(key == keymap.stop)
stop();
else if(key == keymap.rot_aboutx_cw)
(keyb_rotx_scalar == 0) ? keyb_rotx_scalar = -4 : --keyb_rotx_scalar;
else if(key == keymap.rot_aboutx_ccw)
(keyb_rotx_scalar == 0) ? keyb_rotx_scalar = 4 : ++keyb_rotx_scalar;
else if(key == keymap.rot_abouty_cw)
(keyb_roty_scalar == 0) ? keyb_roty_scalar = 4 : ++keyb_roty_scalar;
else if(key == keymap.rot_abouty_ccw)
(keyb_roty_scalar == 0) ? keyb_roty_scalar = -4 : --keyb_roty_scalar;
else if(key == keymap.rot_aboutz_cw)
(keyb_rotz_scalar == 0) ? keyb_rotz_scalar = 4 : ++keyb_rotz_scalar;
else if(key == keymap.rot_aboutz_ccw)
(keyb_rotz_scalar == 0) ? keyb_rotz_scalar = -4 : --keyb_rotz_scalar;
else if(key == keymap.headlight_toggle)
toggle_headlight();
else if(key == keymap.target_asteroid)
target_asteroid();
else if(key == keymap.auto_target_asteroid)
auto_target_asteroid();
else if(key == keymap.fire_torp)
fire_torpedo();
else if(key == keymap.crosshair_toggle)
toggle_int_flag(&optmap.crosshair_flag);
else if(key == keymap.draw_tracer_toggle)
toggle_int_flag(&optmap.draw_tracer_flag);
else if(key == keymap.draw_shield_toggle)
toggle_int_flag(&optmap.draw_shield_flag);
else if(key == keymap.draw_dust_toggle)
toggle_int_flag(&optmap.draw_dust_flag);
else if(key == keymap.draw_torp_lensflare_toggle)
toggle_int_flag(&optmap.draw_torp_lensflare_flag);
else if(key == keymap.asteroid_texture_toggle)
toggle_int_flag(&optmap.texture_flag);
else if(key == keymap.nebula_texture_toggle)
toggle_int_flag(&optmap.nebula_texture_flag);
else if(key == keymap.torp_texture_toggle)
toggle_int_flag(&optmap.torp_texture_flag);
else if(key == keymap.axes_toggle)
toggle_int_flag(&optmap.axes_flag);
else if(key == keymap.play_sound_toggle)
toggle_int_flag(&optmap.play_sound_flag);
else if(key == keymap.reverse_mouse_x_toggle)
optmap.mouse_xdir *= -1;
else if(key == keymap.reverse_mouse_y_toggle)
optmap.mouse_ydir *= -1;
return;
}
static inline void toggle_int_flag(int *flag) {
*flag = !*flag;
return;
}
/*
* The purpose of these three decay_keyb_rotX_scalar() functions
* is a bit hard to explain. First, I determined that more than
* one frame of animation may be drawn between the time the keyboard
* is polled for successive keypresses. The consequence of this is that if one
* simply turns the ship X degrees each time a key is determined to be
* pressed, then the motion will be choppy on "slow" machines.
*
* Secondly, the speed of rotation when using the mouse is proportional
* to the distance the mouse is from the center of the screen. I thought
* it would be good ( for people who want to play using only the keyboard )
* to have an analogous quality for keyboard rotation inputs.
*
* So when a rotation key is hit keyb_rotX_scalar is incremented from zero.
* when the rotation is actualy applied in capture_passive_mouse_input()
* the decay functions are called to reduce ( but not immediately zero )
* keyb_rotX_scalar so that the next frames amount of rotation is
* less but not completely zero.
*
* I hope this makes some sense to me when I read it again...
*/
void decay_keyb_rotx_scalar( void ) {
static int count = 0 ;
count++ ;
count %= KEYB_SCALE_DECAY;
if( keyb_rotx_scalar > KEYB_MAX_ROT_SCALE )
keyb_rotx_scalar = KEYB_MAX_ROT_SCALE ;
if( keyb_rotx_scalar < -KEYB_MAX_ROT_SCALE )
keyb_rotx_scalar = -KEYB_MAX_ROT_SCALE ;
if( count != 0 )
return ;
if( keyb_rotx_scalar > 0 )
keyb_rotx_scalar-- ;
if( keyb_rotx_scalar < 0 )
keyb_rotx_scalar++ ;
return ;
}
void decay_keyb_roty_scalar( void ) {
static int count = 0 ;
count++ ;
count %= KEYB_SCALE_DECAY;
if( keyb_roty_scalar > KEYB_MAX_ROT_SCALE )
keyb_roty_scalar = KEYB_MAX_ROT_SCALE ;
if( keyb_roty_scalar < -KEYB_MAX_ROT_SCALE )
keyb_roty_scalar = -KEYB_MAX_ROT_SCALE ;
if( count != 0 )
return ;
if( keyb_roty_scalar > 0 )
keyb_roty_scalar-- ;
if( keyb_roty_scalar < 0 )
keyb_roty_scalar++ ;
return ;
}
void decay_keyb_rotz_scalar( void ) {
static int count = 0 ;
count++ ;
count %= KEYB_SCALE_DECAY;
if( keyb_rotz_scalar > KEYB_MAX_ROT_SCALE )
keyb_rotz_scalar = KEYB_MAX_ROT_SCALE ;
if( keyb_rotz_scalar < -KEYB_MAX_ROT_SCALE )
keyb_rotz_scalar = -KEYB_MAX_ROT_SCALE ;
if( count != 0 )
return ;
if( keyb_rotz_scalar > 0 )
keyb_rotz_scalar-- ;
if( keyb_rotz_scalar < 0 )
keyb_rotz_scalar++ ;
return ;
}

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks