File: control.c

package info (click to toggle)
alex4 1.1-8
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 2,112 kB
  • sloc: ansic: 6,312; makefile: 27
file content (116 lines) | stat: -rw-r--r-- 4,129 bytes parent folder | download | duplicates (6)
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
/**************************************************************
 *         _____    __                       _____            *
 *        /  _  \  |  |    ____  ___  ___   /  |  |           *
 *       /  /_\  \ |  |  _/ __ \ \  \/  /  /   |  |_          *
 *      /    |    \|  |__\  ___/  >    <  /    ^   /          *
 *      \____|__  /|____/ \___  >/__/\_ \ \____   |           *
 *              \/            \/       \/      |__|           *
 *                                                            *
 **************************************************************
 *    (c) Free Lunch Design 2003                              *
 *    Written by Johan Peitz                                  *
 *    http://www.freelunchdesign.com                          *
 **************************************************************
 *    This source code is released under the The GNU          *
 *    General Public License (GPL). Please refer to the       *
 *    document license.txt in the source directory or         *
 *    http://www.gnu.org for license information.             *
 **************************************************************/
 
 
 


#include "allegro.h"
#include "control.h"

// "constructor"
// sets default values
void init_control(Tcontrol *c) {
	set_control(c,  KEY_UP,
					KEY_DOWN,
					KEY_LEFT,
					KEY_RIGHT,
					KEY_LCONTROL,
					KEY_ALT);

	c->flags = 0;
	c->use_joy = 0;
}

// sets the desired keys for the control unit
void set_control(Tcontrol *c, int up, int down, int left, int right, int fire, int jump) {
	c->key_up     = up;
	c->key_down   = down;
	c->key_left   = left;
	c->key_right  = right;
	c->key_fire	  = fire;
	c->key_jump	  = jump;
}

// updates the control unit
void poll_control(Tcontrol *c) {
	c->flags = 0;

	if (c->use_joy) {
		poll_joystick();
		
		if (joy[0].stick[0].axis[1].d1)	c->flags |= K_UP;
		if (joy[0].stick[0].axis[1].d2)	c->flags |= K_DOWN;
		if (joy[0].stick[0].axis[0].d1)	c->flags |= K_LEFT;
		if (joy[0].stick[0].axis[0].d2)	c->flags |= K_RIGHT;
		if (joy[0].button[1].b)	c->flags |= K_FIRE;
		if (joy[0].button[0].b)	c->flags |= K_JUMP;
	}

	if (key[c->key_up]) c->flags |= K_UP;
	if (key[c->key_down]) c->flags |= K_DOWN;
	if (key[c->key_left]) c->flags |= K_LEFT;
	if (key[c->key_right]) c->flags |= K_RIGHT;
	if (key[c->key_fire]) c->flags |= K_FIRE;
	if (key[c->key_jump]) c->flags |= K_JUMP;
}

// check if a key in the control is available
int check_control_key(Tcontrol *c, int key) {
	if (key == c->key_left) return TRUE;
	if (key == c->key_right) return TRUE;
	if (key == c->key_up) return TRUE;
	if (key == c->key_down) return TRUE;
	if (key == c->key_fire) return TRUE;
	if (key == c->key_jump) return TRUE;
	return FALSE;
}

// returns true or false depending on if keys are pressed or not
int is_up(Tcontrol *c)       { return (c->flags & K_UP       ? TRUE : FALSE); }
int is_down(Tcontrol *c)     { return (c->flags & K_DOWN     ? TRUE : FALSE); }
int is_left(Tcontrol *c)     { return (c->flags & K_LEFT     ? TRUE : FALSE); }
int is_right(Tcontrol *c)    { return (c->flags & K_RIGHT    ? TRUE : FALSE); }
int is_fire(Tcontrol *c)     { return (c->flags & K_FIRE     ? TRUE : FALSE); }
int is_jump(Tcontrol *c)     { return (c->flags & K_JUMP     ? TRUE : FALSE); }
int is_any(Tcontrol *c)      { return (c->flags              ? TRUE : FALSE); }

// saves the control config to disk using FP
void save_control(Tcontrol *c, PACKFILE *fp) {
	pack_iputl(c->use_joy, fp);
	pack_iputl(c->key_left, fp);
	pack_iputl(c->key_right, fp);
	pack_iputl(c->key_up, fp);
	pack_iputl(c->key_down, fp);
	pack_iputl(c->key_fire, fp);
	pack_iputl(c->key_jump, fp);
	pack_putc(c->flags, fp);
}

// loads the control config from disk using FP
void load_control(Tcontrol *c, PACKFILE *fp) {
	c->use_joy = pack_igetl(fp);
	c->key_left = pack_igetl(fp);
	c->key_right = pack_igetl(fp);
	c->key_up = pack_igetl(fp);
	c->key_down = pack_igetl(fp);
	c->key_fire = pack_igetl(fp);
	c->key_jump = pack_igetl(fp);
	c->flags = pack_getc(fp);
}