source: abuse/tags/pd/abuse/src/config.c @ 49

Last change on this file since 49 was 49, checked in by Sam Hocevar, 11 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 3.7 KB
Line 
1#include "keys.hpp"
2#include "lisp.hpp"
3#include <ctype.h>
4#include "joy.hpp"
5#include "jwindow.hpp"
6#include "config.hpp"
7#include "game.hpp"
8
9
10extern window_manager *eh;
11int key_players=0;
12int morph_detail=MEDIUM_DETAIL;
13
14struct player_keys
15{
16  int joy,left,right,up,down,b1,b2,b3,b4;
17} ;
18
19player_keys *key_map=NULL;
20
21int binding_for_player(int player)
22{
23  char tmp[40];
24  sprintf(tmp,"player%d",player);
25  Cell *f=find_symbol(tmp);
26  if (!NILP(f) && DEFINEDP(symbol_value(f)))
27  {
28    void *what=symbol_value(f);
29    if (what==make_find_symbol("keyboard"))
30      return 1;
31    else if (what==make_find_symbol("joystick"))
32      return 2;   
33  }
34  return 0;
35}
36
37
38int get_key_binding(char *dir, int i)
39{
40  char tmp[100],kn[50];
41  sprintf(tmp,"player%d-%s",i,dir);
42  Cell *f=find_symbol(tmp);
43  if (NILP(f) || !DEFINEDP(symbol_value(f))) return 0;
44  void *k=symbol_value(f);
45 
46  if (item_type(k)!=L_SYMBOL) return 0;
47
48
49#ifdef SCADALISP
50  strcpy(tmp,symbol_name(k));
51#else
52  strcpy(tmp,lstring_value(symbol_name(k)));
53#endif
54
55  for (char *c=tmp;*c;c++)
56  {
57    *c=tolower(*c);
58    if (*c=='_') *c=' ';   
59  }
60  for (int j=0;j<JK_MAX_KEY;j++) 
61  {
62    key_name(j,kn);
63    for (char *c=kn;*c;c++) *c=tolower(*c);
64    if (!strcmp(kn,tmp))
65      return j;
66  }
67  return 0;
68}
69
70void get_key_bindings()
71{
72  if (key_map)
73    jfree(key_map);
74  key_map=NULL;
75
76  for (key_players=0;binding_for_player(key_players+1);key_players++);
77  if (key_players)
78  {
79    key_map=(player_keys *)jmalloc(sizeof(player_keys)*key_players,"key bindings");
80    for (int i=0;i<key_players;i++)
81    {
82      key_map[i].joy=(binding_for_player(i+1)==2);
83      if (!key_map[i].joy)
84      {
85        key_map[i].left=get_key_binding("left",i+1);
86        key_map[i].right=get_key_binding("right",i+1);
87        key_map[i].up=get_key_binding("up",i+1);
88        key_map[i].down=get_key_binding("down",i+1);
89        key_map[i].b4=get_key_binding("b4",i+1);
90        key_map[i].b3=get_key_binding("b3",i+1);
91        key_map[i].b2=get_key_binding("b2",i+1);
92        key_map[i].b1=get_key_binding("b1",i+1);
93      }
94
95    }
96  }
97  else key_map=NULL;
98}
99
100#define is_pressed(x) the_game->key_down(x)
101
102void get_movement(int player, int &x, int &y, int &b1, int &b2, int &b3, int &b4)
103{
104  if (player<key_players)
105  {
106    if (key_map[player].joy)
107    {
108      joy_status(b1,b2,b3,x,y);
109      b3=(b1&&b2);
110      b4=0;
111    }
112    else if (!eh)
113    { x=y=b1=b2=b3=b4=0; }
114    else
115    {
116      if (is_pressed(key_map[player].left))
117        x=-1;
118      else if (is_pressed(key_map[player].right))
119        x=1;
120      else x=0;
121
122      if (is_pressed(key_map[player].up))
123        y=-1;
124      else if (is_pressed(key_map[player].down))
125        y=1;
126      else y=0;
127
128      if (is_pressed(key_map[player].b1))
129        b1=1;
130      else b1=0;
131
132      if (is_pressed(key_map[player].b2))
133        b2=1;
134      else b2=0;
135
136      if (is_pressed(key_map[player].b3))
137        b3=1;
138      else b3=0;
139
140      if (is_pressed(key_map[player].b4))
141        b4=1;
142      else b4=0;
143    }
144  } else
145  {
146    x=y=b1=b2=b3=0;
147  }
148}
149
150
151void key_bindings(int player, int &left, int &right, int &up, int &down, int &b1, int &b2, int &b3, int &b4)
152{
153
154  left=key_map[player].left;
155  right=key_map[player].right;
156  up=key_map[player].up;
157  down=key_map[player].down;
158  b1=key_map[player].b1;
159  b2=key_map[player].b2;
160  b3=key_map[player].b3;
161  b3=key_map[player].b4;
162}
163
164
165void config_cleanup()
166{
167  if (key_map)
168    jfree(key_map);
169}
170
171int get_keycode(char *str)  // -1 means not a valid key code
172{
173  if (!str[0]) return -1;
174  else if (!str[1]) return str[0];
175  else
176  {
177    int j;
178    char buf[20];
179    for (j=256;j<JK_MAX_KEY;j++) 
180    {
181      key_name(j,buf);
182      char *c=buf;
183      for (;*c;c++) if (*c==' ') *c='_'; else *c=tolower(*c);
184      if (strcmp(str,buf)==0) return j;
185    }
186  }
187  return -1;
188}
Note: See TracBrowser for help on using the repository browser.