1 /**************************************************************
2  *         _____    __                       _____            *
3  *        /  _  \  |  |    ____  ___  ___   /  |  |           *
4  *       /  /_\  \ |  |  _/ __ \ \  \/  /  /   |  |_          *
5  *      /    |    \|  |__\  ___/  >    <  /    ^   /          *
6  *      \____|__  /|____/ \___  >/__/\_ \ \____   |           *
7  *              \/            \/       \/      |__|           *
8  *                                                            *
9  **************************************************************
10  *    (c) Free Lunch Design 2003                              *
11  *    Written by Johan Peitz                                  *
12  *    http://www.freelunchdesign.com                          *
13  **************************************************************
14  *    This source code is released under the The GNU          *
15  *    General Public License (GPL). Please refer to the       *
16  *    document license.txt in the source directory or         *
17  *    http://www.gnu.org for license information.             *
18  **************************************************************/
19 
20 
21 
22 
23 
24 #include "allegro.h"
25 #include "control.h"
26 
27 // "constructor"
28 // sets default values
init_control(Tcontrol * c)29 void init_control(Tcontrol *c) {
30 	set_control(c,  KEY_UP,
31 					KEY_DOWN,
32 					KEY_LEFT,
33 					KEY_RIGHT,
34 					KEY_LCONTROL,
35 					KEY_ALT);
36 
37 	c->flags = 0;
38 	c->use_joy = 0;
39 }
40 
41 // sets the desired keys for the control unit
set_control(Tcontrol * c,int up,int down,int left,int right,int fire,int jump)42 void set_control(Tcontrol *c, int up, int down, int left, int right, int fire, int jump) {
43 	c->key_up     = up;
44 	c->key_down   = down;
45 	c->key_left   = left;
46 	c->key_right  = right;
47 	c->key_fire	  = fire;
48 	c->key_jump	  = jump;
49 }
50 
51 // updates the control unit
poll_control(Tcontrol * c)52 void poll_control(Tcontrol *c) {
53 	c->flags = 0;
54 
55 	if (c->use_joy) {
56 		poll_joystick();
57 
58 		if (joy[0].stick[0].axis[1].d1)	c->flags |= K_UP;
59 		if (joy[0].stick[0].axis[1].d2)	c->flags |= K_DOWN;
60 		if (joy[0].stick[0].axis[0].d1)	c->flags |= K_LEFT;
61 		if (joy[0].stick[0].axis[0].d2)	c->flags |= K_RIGHT;
62 		if (joy[0].button[1].b)	c->flags |= K_FIRE;
63 		if (joy[0].button[0].b)	c->flags |= K_JUMP;
64 	}
65 
66 	if (key[c->key_up]) c->flags |= K_UP;
67 	if (key[c->key_down]) c->flags |= K_DOWN;
68 	if (key[c->key_left]) c->flags |= K_LEFT;
69 	if (key[c->key_right]) c->flags |= K_RIGHT;
70 	if (key[c->key_fire]) c->flags |= K_FIRE;
71 	if (key[c->key_jump]) c->flags |= K_JUMP;
72 }
73 
74 // check if a key in the control is available
check_control_key(Tcontrol * c,int key)75 int check_control_key(Tcontrol *c, int key) {
76 	if (key == c->key_left) return TRUE;
77 	if (key == c->key_right) return TRUE;
78 	if (key == c->key_up) return TRUE;
79 	if (key == c->key_down) return TRUE;
80 	if (key == c->key_fire) return TRUE;
81 	if (key == c->key_jump) return TRUE;
82 	return FALSE;
83 }
84 
85 // returns true or false depending on if keys are pressed or not
is_up(Tcontrol * c)86 int is_up(Tcontrol *c)       { return (c->flags & K_UP       ? TRUE : FALSE); }
is_down(Tcontrol * c)87 int is_down(Tcontrol *c)     { return (c->flags & K_DOWN     ? TRUE : FALSE); }
is_left(Tcontrol * c)88 int is_left(Tcontrol *c)     { return (c->flags & K_LEFT     ? TRUE : FALSE); }
is_right(Tcontrol * c)89 int is_right(Tcontrol *c)    { return (c->flags & K_RIGHT    ? TRUE : FALSE); }
is_fire(Tcontrol * c)90 int is_fire(Tcontrol *c)     { return (c->flags & K_FIRE     ? TRUE : FALSE); }
is_jump(Tcontrol * c)91 int is_jump(Tcontrol *c)     { return (c->flags & K_JUMP     ? TRUE : FALSE); }
is_any(Tcontrol * c)92 int is_any(Tcontrol *c)      { return (c->flags              ? TRUE : FALSE); }
93 
94 // saves the control config to disk using FP
save_control(Tcontrol * c,PACKFILE * fp)95 void save_control(Tcontrol *c, PACKFILE *fp) {
96 	pack_fwrite(c, sizeof(Tcontrol), fp);
97 }
98 
99 // loads the control config from disk using FP
load_control(Tcontrol * c,PACKFILE * fp)100 void load_control(Tcontrol *c, PACKFILE *fp) {
101 	pack_fread(c, sizeof(Tcontrol), fp);
102 }
103